Module: LoginRadius::RequestClient

Constant Summary collapse

API_V2_BASE_URL =

LoginRadius Client Module: Methods relating to building and sending requests are defined here.

'https://api.loginradius.com/'
API_V2_BASE_URL_CONFIG =
'https://config.lrcontent.com/'
INIT_VECTOR =
'tu89geji340t89u2'
KEY_SIZE =
256

Instance Method Summary collapse

Instance Method Details

#build_new_uri_obj(resource) ⇒ URI

Builds a URI instance given type and resource

Parameters:

  • resource (String)

    Target resource custom_api_domain is set

Returns:

  • (URI)

    uri instance



243
244
245
246
247
248
249
250
251
252
253
# File 'lib/login_radius/request_client.rb', line 243

def build_new_uri_obj(resource)
  if resource == 'ciam/appinfo'
    return URI.parse(API_V2_BASE_URL_CONFIG + resource)
  else
    if ENV['CUSTOM_API_DOMAIN'] == 'false' || ENV['CUSTOM_API_DOMAIN'] == nil
      return URI.parse(API_V2_BASE_URL + resource)
    else
      return URI.parse(ENV['CUSTOM_API_DOMAIN'] + resource)
    end
  end
end

#create_hash_secret(endpoint, secret_key, headers, body = {}) ⇒ URI, headers

Create a has digest in header

Parameters:

  • endpoint (String)

    endpoint

  • secret_key (String)

    secret key

  • headers (String)

    headers

  • body (String) (defaults to: {})

    body

Returns:

  • (URI)

    uri instance

  • (headers)

    header



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/login_radius/request_client.rb', line 264

def create_hash_secret(endpoint, secret_key, headers, body = {})
  endpoint_uri = 'https://api.loginradius.com' + endpoint
  expiry_time = (Time.now.getutc() + (1*60*60)).strftime('%Y/%m/%d %H:%M:%S')

  encoded_uri = CGI.escape(CGI.unescape(endpoint_uri))

  if body.blank?
    string_to_hash = expiry_time + ':' + encoded_uri.downcase
  else
    string_to_hash = expiry_time + ':' + encoded_uri.downcase + ':' + body.to_json
  end

  mac = Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), secret_key, string_to_hash)).strip()
  headers['X-Request-Expires'] = expiry_time
  headers['digest'] = 'SHA-256='+mac
  return headers
end

#delete_request(uri_endpoint, params, body = {}) ⇒ LoginRadius::Response

Sends a DELETE API request.

Parameters:

  • uri_endpoint (URI)

    Target uri instance

  • params (Hash)

    Parameters to send

  • body (Hash) (defaults to: {})

    POST body

Returns:



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/login_radius/request_client.rb', line 190

def delete_request(uri_endpoint, params, body = {})
  uri_obj = build_new_uri_obj(uri_endpoint)

  headers = { 'Content-Type' => 'application/json' }
  if params.key?('access_token') # has_key
    if uri_endpoint.include? 'auth'
      access_token = params['access_token']
      params.delete('access_token')
      headers['Authorization'] = 'Bearer ' + access_token
    end
  end

  if params.key?('apiSecret') # has_key
    secret_key = params['apiSecret']
    params.delete('apiSecret')

    if ENV['API_REQUEST_SIGNING'] == 'false' || ENV['API_REQUEST_SIGNING'] == nil
      headers['X-LoginRadius-ApiSecret'] = secret_key
    else
      uri_obj = build_new_uri_obj(uri_endpoint)
      uri_obj.query = URI.encode_www_form(params)
      headers = create_hash_secret(uri_obj.request_uri, secret_key, headers, body)
    end
  end
  if params.key?('sott') # has_key
    headers['X-LoginRadius-Sott'] = params['sott']
    params.delete('sott')
  end

  unless ENV['Origin_IP'] == "" || ENV['Origin_IP'] == nil
    headers['X-Origin-IP'] = ENV['Origin_IP']       
  end

  uri_obj.query = URI.encode_www_form(params)
  http = Net::HTTP.new(uri_obj.host, uri_obj.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  req = Net::HTTP::Delete.new(uri_obj.request_uri, headers)
  req.body = body.to_json
  response = http.request(req)

  begin
    return LoginRadius::Response.new(response)
  rescue JSON::ParserError => e
    raise LoginRadius::Error.new("JSON parsing error has occurred. More info: #{e.message}")
  end
end

#get_request(uri_endpoint, params, body = {}) ⇒ LoginRadius::Response

Sends a GET API request.

Parameters:

  • uri_endpoint (URI)

    Target uri instance

  • params (Hash)

    Parameters to send

  • body (Hash) (defaults to: {})

    Request body

Returns:



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/login_radius/request_client.rb', line 84

def get_request(uri_endpoint, params, body = {})      
  uri_obj = build_new_uri_obj(uri_endpoint)

  headers = {'Content-Type' => 'application/json'}
  if params.key?('access_token') # has_key
    if uri_endpoint.include? 'auth'
      access_token = params['access_token']
      params.delete('access_token')
      headers['Authorization'] = 'Bearer ' + access_token
    end
  end

  if params.key?('apiSecret') # has_key
    secret_key = params['apiSecret']
    params.delete('apiSecret')

    if ENV['API_REQUEST_SIGNING'] == 'false' || ENV['API_REQUEST_SIGNING'] == nil
      headers['X-LoginRadius-ApiSecret'] = secret_key
    else
      uri_obj = build_new_uri_obj(uri_endpoint)
      uri_obj.query = URI.encode_www_form(params)
      headers = create_hash_secret(uri_obj.request_uri, secret_key, headers, body)
    end
  end
  if params.key?('sott') # has_key
    headers['X-LoginRadius-Sott'] = params['sott']
    params.delete('sott')
  end

  unless ENV['Origin_IP'] == "" || ENV['Origin_IP'] == nil
    headers['X-Origin-IP'] = ENV['Origin_IP']       
  end

  uri_obj.query = URI.encode_www_form(params)
  http = Net::HTTP.new(uri_obj.host, uri_obj.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE   
  response = http.get(uri_obj.request_uri, headers)

  begin
    return LoginRadius::Response.new(response)
  rescue JSON::ParserError => e
    raise LoginRadius::Error.new("JSON parsing error has occurred. More info: #{e.message}")
  end
end

#get_sott(time_difference = "", api_key = "", api_secret = "", start_time = "", end_time = "") ⇒ Object

Local - Generate SOTT: Generates a Secured One Time Token manually.

Do not pass the time difference if you are passing start_time & end_time. You can pass the start_time , end_time interval and the SOTT will be valid for this time duration.



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/login_radius/request_client.rb', line 293

def get_sott(time_difference="", api_key="", api_secret="",start_time="",end_time="")

  key= !isNullOrWhiteSpace(api_key) ? api_key:ENV['API_KEY']
  time_difference= !isNullOrWhiteSpace(time_difference) ? time_difference.to_i : 10
  secret=!isNullOrWhiteSpace(api_secret) ? api_secret:ENV['API_SECRET']
  start_date_time=!isNullOrWhiteSpace(start_time)&&!isNullOrWhiteSpace(end_time)? start_time:Time.now.getutc().strftime('%Y/%m/%d %H:%M:%S')
  end_date_time =!isNullOrWhiteSpace(start_time)&&!isNullOrWhiteSpace(end_time)? end_time:(Time.now.getutc() + (time_difference*60)).strftime('%Y/%m/%d %H:%M:%S')

  
  plain_text = start_date_time + '#' + key + '#' + end_date_time
  iter = 10000
  salt = "\x00\x00\x00\x00\x00\x00\x00\x00"
  key_len = KEY_SIZE / 8
  cipher_key = OpenSSL::PKCS5.pbkdf2_hmac_sha1(secret, salt, iter, key_len)

  cipher = OpenSSL::Cipher.new('aes-' + KEY_SIZE.to_s + '-cbc')
  cipher.encrypt
  cipher.key = cipher_key
  cipher.iv = INIT_VECTOR

  encrypted = cipher.update(plain_text) + cipher.final
  encrypted_b64 = Base64.strict_encode64(encrypted)

  hash = Digest::MD5.hexdigest(encrypted_b64)
  sott = encrypted_b64 + '*' + hash
  return sott
end

#getValidationMessage(params) ⇒ Object



73
74
75
# File 'lib/login_radius/request_client.rb', line 73

def getValidationMessage(params)
  return params + " is a required parameter."
end

#isNullOrWhiteSpace(params) ⇒ Object



69
70
71
# File 'lib/login_radius/request_client.rb', line 69

def isNullOrWhiteSpace(params)
  return params.blank? ? true : false
end

#local_generate_sott(time_difference = 10, api_key = "", api_secret = "") ⇒ Object

Local - Generate SOTT: Generates a Secured One Time Token manually.



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/login_radius/request_client.rb', line 329

def local_generate_sott(time_difference = 10, api_key="", api_secret="")

  key=!isNullOrWhiteSpace(api_key) ? api_key:ENV['API_KEY']

  secret=!isNullOrWhiteSpace(api_secret) ? api_secret:ENV['API_SECRET']

  start_time = Time.now.getutc().strftime('%Y/%m/%d %H:%M:%S')
  end_time = (Time.now.getutc() + (time_difference*60)).strftime('%Y/%m/%d %H:%M:%S')
  plain_text = start_time + '#' + key + '#' + end_time
  iter = 10000
  salt = "\x00\x00\x00\x00\x00\x00\x00\x00"
  key_len = KEY_SIZE / 8
  cipher_key = OpenSSL::PKCS5.pbkdf2_hmac_sha1(secret, salt, iter, key_len)

  cipher = OpenSSL::Cipher.new('aes-' + KEY_SIZE.to_s + '-cbc')
  cipher.encrypt
  cipher.key = cipher_key
  cipher.iv = INIT_VECTOR

  encrypted = cipher.update(plain_text) + cipher.final
  encrypted_b64 = Base64.strict_encode64(encrypted)

  hash = Digest::MD5.hexdigest(encrypted_b64)
  sott = encrypted_b64 + '*' + hash
  return sott
end

#post_request(uri_endpoint, params, body = {}) ⇒ LoginRadius::Response

Sends a POST API request.

Parameters:

  • uri_endpoint (URI)

    Target uri instance

  • params (Hash)

    Parameters to send

  • body (Hash) (defaults to: {})

    POST body

Returns:



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/login_radius/request_client.rb', line 22

def post_request(uri_endpoint, params, body = {})
  uri_obj = build_new_uri_obj(uri_endpoint)

  headers = { 'Content-Type' => 'application/json' }
  if params.key?('access_token') # has_key
    if uri_endpoint.include? 'auth'
      access_token = params['access_token']
      params.delete('access_token')
      headers['Authorization'] = 'Bearer ' + access_token
    end
  end

  if params.key?('apiSecret') # has_key
    secret_key = params['apiSecret']
    params.delete('apiSecret')

    if ENV['API_REQUEST_SIGNING'] == 'false' || ENV['API_REQUEST_SIGNING'] == nil
      headers['X-LoginRadius-ApiSecret'] = secret_key
    else
      uri_obj = build_new_uri_obj(uri_endpoint)
      uri_obj.query = URI.encode_www_form(params)
      headers = create_hash_secret(uri_obj.request_uri, secret_key, headers, body)
    end
  end
  if params.key?('sott') # has_key
    headers['X-LoginRadius-Sott'] = params['sott']
    params.delete('sott')
  end

  unless ENV['Origin_IP'] == "" || ENV['Origin_IP'] == nil
    headers['X-Origin-IP'] = ENV['Origin_IP']       
  end
  
  uri_obj.query = URI.encode_www_form(params)
  http = Net::HTTP.new(uri_obj.host, uri_obj.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  response = http.post(uri_obj.request_uri, body.to_json, headers)

  begin
    return LoginRadius::Response.new(response)
  rescue JSON::ParserError => e
    raise LoginRadius::Error.new("JSON parsing error has occurred. More info: #{e.message}")        
  end
end

#put_request(uri_endpoint, params, body = {}) ⇒ LoginRadius::Response

Sends a PUT API request.

Parameters:

  • uri_endpoint (URI)

    Target uri instance

  • params (Hash)

    Parameters to send

  • body (Hash) (defaults to: {})

    PUT body

Returns:



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/login_radius/request_client.rb', line 137

def put_request(uri_endpoint, params, body = {})
  uri_obj = build_new_uri_obj(uri_endpoint)

  headers = { 'Content-Type' => 'application/json' }
  if params.key?('access_token') # has_key
    if uri_endpoint.include? 'auth'
      access_token = params['access_token']
      params.delete('access_token')
      headers['Authorization'] = 'Bearer ' + access_token
    end
  end

  if params.key?('apiSecret') # has_key
    secret_key = params['apiSecret']
    params.delete('apiSecret')

    if ENV['API_REQUEST_SIGNING'] == 'false' || ENV['API_REQUEST_SIGNING'] == nil
      headers['X-LoginRadius-ApiSecret'] = secret_key
    else
      uri_obj = build_new_uri_obj(uri_endpoint)
      uri_obj.query = URI.encode_www_form(params)

      headers = create_hash_secret(uri_obj.request_uri, secret_key, headers, body)
    end
  end
  if params.key?('sott') # has_key
    headers['X-LoginRadius-Sott'] = params['sott']
    params.delete('sott')
  end

  unless ENV['Origin_IP'] == "" || ENV['Origin_IP'] == nil
    headers['X-Origin-IP'] = ENV['Origin_IP']       
  end

  uri_obj.query = URI.encode_www_form(params)
  http = Net::HTTP.new(uri_obj.host, uri_obj.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  response = http.put(uri_obj.request_uri, body.to_json, headers)
  begin
    return LoginRadius::Response.new(response)
  rescue JSON::ParserError => e
    raise LoginRadius::Error.new("JSON parsing error has occurred. More info: #{e.message}")
  end
end