Module: MvxAuthClient

Defined in:
lib/mvx_auth_client.rb,
lib/mvx_auth_client/version.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

AUTH_URL =
(ENV['AUTH_URL'] || 'http://10.109.182.207:3030')
VERSION =
"0.1.28"
@@auth_pub_key =
nil

Class Method Summary collapse

Class Method Details

.decrypt_token_full(token, repeat_get_key = true) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/mvx_auth_client.rb', line 39

def decrypt_token_full(token, repeat_get_key = true)
  key = get_key(AUTH_URL, '/public_rsa')
  rsa_public  = OpenSSL::PKey::RSA.new(key)
  begin
    return JWT.decode(token, rsa_public, true, algorithms: 'RS256').first.symbolize_keys
  rescue JWT::VerificationError => e
    if repeat_get_key
      @@auth_pub_key = nil
      return decrypt_token_full(token, false)
    else
      raise e
    end
  end
  return decoded
end

.request_token(login, password) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/mvx_auth_client.rb', line 11

def request_token(, password)
  json = {:login => , :password => password }
  create_connection(AUTH_URL, '/login')
  res = send_request(json)
  token = JSON.parse(res.body)['login_token']
  if token.blank?
    return nil
  end
   = decrypt_token_full(token)
  if .blank?
    return nil
  end
  opcode = [:opcode]
  warehouseId = [:warehouses].keys.first.to_s

  new_token_request = { warehouseId: warehouseId }.to_json
  cookie = 'Bearer ' + token
  uri = URI.parse(AUTH_URL + "/get_tokens")
  http = Net::HTTP.new(uri.host,uri.port, nil)
  req = Net::HTTP::Post.new(uri.path, initheader = {'Content-Type' =>'application/json'})
  req['Authorization'] = cookie
  req.body = new_token_request
  res = http.request(req)
  json_reply_hash = JSON.parse(res.body)
  json_reply_hash[:opcode] = opcode
  return json_reply_hash
end