login_radius 11.0.0 → 11.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (29) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +21 -21
  3. data/README.md +58 -58
  4. data/lib/login_radius/api/account/account_api.rb +581 -581
  5. data/lib/login_radius/api/account/role_api.rb +330 -330
  6. data/lib/login_radius/api/account/sott_api.rb +47 -47
  7. data/lib/login_radius/api/advanced/configuration_api.rb +57 -57
  8. data/lib/login_radius/api/advanced/consent_management_api.rb +161 -161
  9. data/lib/login_radius/api/advanced/custom_object_api.rb +316 -316
  10. data/lib/login_radius/api/advanced/custom_registration_data_api.rb +195 -195
  11. data/lib/login_radius/api/advanced/multi_factor_authentication_api.rb +942 -606
  12. data/lib/login_radius/api/advanced/re_authentication_api.rb +317 -243
  13. data/lib/login_radius/api/advanced/web_hook_api.rb +101 -101
  14. data/lib/login_radius/api/authentication/authentication_api.rb +1036 -989
  15. data/lib/login_radius/api/authentication/one_touch_login_api.rb +160 -160
  16. data/lib/login_radius/api/authentication/password_less_login_api.rb +202 -158
  17. data/lib/login_radius/api/authentication/phone_authentication_api.rb +329 -329
  18. data/lib/login_radius/api/authentication/pin_authentication_api.rb +316 -316
  19. data/lib/login_radius/api/authentication/risk_based_authentication_api.rb +286 -286
  20. data/lib/login_radius/api/authentication/smart_login_api.rb +146 -146
  21. data/lib/login_radius/api/social/native_social_api.rb +255 -255
  22. data/lib/login_radius/api/social/social_api.rb +784 -806
  23. data/lib/login_radius/error.rb +7 -7
  24. data/lib/login_radius/request_client.rb +311 -295
  25. data/lib/login_radius/response.rb +18 -12
  26. data/lib/login_radius/version.rb +2 -2
  27. data/lib/login_radius.rb +30 -30
  28. data/login_radius.gemspec +25 -28
  29. metadata +7 -7
@@ -1,7 +1,7 @@
1
- module LoginRadius
2
- class Error < StandardError
3
- def initialize(msg = 'An error occurred.')
4
- super(msg)
5
- end
6
- end
7
- end
1
+ module LoginRadius
2
+ class Error < StandardError
3
+ def initialize(msg = 'An error occurred.')
4
+ super(msg)
5
+ end
6
+ end
7
+ end
@@ -1,295 +1,311 @@
1
- require 'net/http'
2
- require 'openssl'
3
- require 'base64'
4
- require 'cgi'
5
-
6
- module LoginRadius
7
- module RequestClient
8
- # LoginRadius Client Module: Methods relating to building and sending requests are defined here.
9
-
10
- API_V2_BASE_URL = 'https://api.loginradius.com/'
11
- API_V2_BASE_URL_CONFIG = 'https://config.lrcontent.com/'
12
- INIT_VECTOR = 'tu89geji340t89u2'
13
- KEY_SIZE = 256
14
-
15
- # Sends a POST API request.
16
- #
17
- # @param uri_endpoint [URI] Target uri instance
18
- # @param params [Hash] Parameters to send
19
- # @param body [Hash] POST body
20
- #
21
- # @return [LoginRadius::Response] LoginRadius response instance
22
- def post_request(uri_endpoint, params, body = {})
23
- uri_obj = build_new_uri_obj(uri_endpoint)
24
-
25
- headers = { 'Content-Type' => 'application/json' }
26
- if params.key?('access_token') # has_key
27
- if uri_endpoint.include? 'auth'
28
- access_token = params['access_token']
29
- params.delete('access_token')
30
- headers['Authorization'] = 'Bearer ' + access_token
31
- end
32
- end
33
-
34
- if params.key?('apiSecret') # has_key
35
- secret_key = params['apiSecret']
36
- params.delete('apiSecret')
37
-
38
- if ENV['API_REQUEST_SIGNING'] == 'false' || ENV['API_REQUEST_SIGNING'] == nil
39
- headers['X-LoginRadius-ApiSecret'] = secret_key
40
- else
41
- uri_obj = build_new_uri_obj(uri_endpoint)
42
- uri_obj.query = URI.encode_www_form(params)
43
- headers = create_hash_secret(uri_obj.request_uri, secret_key, headers, body)
44
- end
45
- end
46
- if params.key?('sott') # has_key
47
- headers['X-LoginRadius-Sott'] = params['sott']
48
- params.delete('sott')
49
- end
50
-
51
- uri_obj.query = URI.encode_www_form(params)
52
- http = Net::HTTP.new(uri_obj.host, uri_obj.port)
53
- http.use_ssl = true
54
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
55
-
56
- response = http.post(uri_obj.request_uri, body.to_json, headers)
57
-
58
- begin
59
- return LoginRadius::Response.new(response)
60
- rescue JSON::ParserError => e
61
- raise LoginRadius::Error.new("JSON parsing error has occurred. More info: #{e.message}")
62
- end
63
- end
64
-
65
- def isNullOrWhiteSpace(params)
66
- return params.blank? ? true : false
67
- end
68
-
69
- def getValidationMessage(params)
70
- return params + " is a required parameter."
71
- end
72
-
73
- # Sends a GET API request.
74
- #
75
- # @param uri_endpoint [URI] Target uri instance
76
- # @param params [Hash] Parameters to send
77
- # @param body [Hash] Request body
78
- #
79
- # @return [LoginRadius::Response] LoginRadius response instance
80
- def get_request(uri_endpoint, params, body = {})
81
- uri_obj = build_new_uri_obj(uri_endpoint)
82
-
83
- headers = {'Content-Type' => 'application/json'}
84
- if params.key?('access_token') # has_key
85
- if uri_endpoint.include? 'auth'
86
- access_token = params['access_token']
87
- params.delete('access_token')
88
- headers['Authorization'] = 'Bearer ' + access_token
89
- end
90
- end
91
-
92
- if params.key?('apiSecret') # has_key
93
- secret_key = params['apiSecret']
94
- params.delete('apiSecret')
95
-
96
- if ENV['API_REQUEST_SIGNING'] == 'false' || ENV['API_REQUEST_SIGNING'] == nil
97
- headers['X-LoginRadius-ApiSecret'] = secret_key
98
- else
99
- uri_obj = build_new_uri_obj(uri_endpoint)
100
- uri_obj.query = URI.encode_www_form(params)
101
- headers = create_hash_secret(uri_obj.request_uri, secret_key, headers, body)
102
- end
103
- end
104
- if params.key?('sott') # has_key
105
- headers['X-LoginRadius-Sott'] = params['sott']
106
- params.delete('sott')
107
- end
108
-
109
- uri_obj.query = URI.encode_www_form(params)
110
- http = Net::HTTP.new(uri_obj.host, uri_obj.port)
111
- http.use_ssl = true
112
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
113
- response = http.get(uri_obj.request_uri, headers)
114
-
115
- begin
116
- return LoginRadius::Response.new(response)
117
- rescue JSON::ParserError => e
118
- raise LoginRadius::Error.new("JSON parsing error has occurred. More info: #{e.message}")
119
- end
120
- end
121
-
122
- # Sends a PUT API request.
123
- #
124
- # @param uri_endpoint [URI] Target uri instance
125
- # @param params [Hash] Parameters to send
126
- # @param body [Hash] PUT body
127
- #
128
- # @return [LoginRadius::Response] LoginRadius response instance
129
- def put_request(uri_endpoint, params, body = {})
130
- uri_obj = build_new_uri_obj(uri_endpoint)
131
-
132
- headers = { 'Content-Type' => 'application/json' }
133
- if params.key?('access_token') # has_key
134
- if uri_endpoint.include? 'auth'
135
- access_token = params['access_token']
136
- params.delete('access_token')
137
- headers['Authorization'] = 'Bearer ' + access_token
138
- end
139
- end
140
-
141
- if params.key?('apiSecret') # has_key
142
- secret_key = params['apiSecret']
143
- params.delete('apiSecret')
144
-
145
- if ENV['API_REQUEST_SIGNING'] == 'false' || ENV['API_REQUEST_SIGNING'] == nil
146
- headers['X-LoginRadius-ApiSecret'] = secret_key
147
- else
148
- uri_obj = build_new_uri_obj(uri_endpoint)
149
- uri_obj.query = URI.encode_www_form(params)
150
-
151
- headers = create_hash_secret(uri_obj.request_uri, secret_key, headers, body)
152
- end
153
- end
154
- if params.key?('sott') # has_key
155
- headers['X-LoginRadius-Sott'] = params['sott']
156
- params.delete('sott')
157
- end
158
-
159
- uri_obj.query = URI.encode_www_form(params)
160
- http = Net::HTTP.new(uri_obj.host, uri_obj.port)
161
- http.use_ssl = true
162
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
163
- response = http.put(uri_obj.request_uri, body.to_json, headers)
164
- begin
165
- return LoginRadius::Response.new(response)
166
- rescue JSON::ParserError => e
167
- raise LoginRadius::Error.new("JSON parsing error has occurred. More info: #{e.message}")
168
- end
169
- end
170
-
171
- # Sends a DELETE API request.
172
- #
173
- # @param uri_endpoint [URI] Target uri instance
174
- # @param params [Hash] Parameters to send
175
- # @param body [Hash] POST body
176
- #
177
- # @return [LoginRadius::Response] LoginRadius response instance
178
- def delete_request(uri_endpoint, params, body = {})
179
- uri_obj = build_new_uri_obj(uri_endpoint)
180
-
181
- headers = { 'Content-Type' => 'application/json' }
182
- if params.key?('access_token') # has_key
183
- if uri_endpoint.include? 'auth'
184
- access_token = params['access_token']
185
- params.delete('access_token')
186
- headers['Authorization'] = 'Bearer ' + access_token
187
- end
188
- end
189
-
190
- if params.key?('apiSecret') # has_key
191
- secret_key = params['apiSecret']
192
- params.delete('apiSecret')
193
-
194
- if ENV['API_REQUEST_SIGNING'] == 'false' || ENV['API_REQUEST_SIGNING'] == nil
195
- headers['X-LoginRadius-ApiSecret'] = secret_key
196
- else
197
- uri_obj = build_new_uri_obj(uri_endpoint)
198
- uri_obj.query = URI.encode_www_form(params)
199
- headers = create_hash_secret(uri_obj.request_uri, secret_key, headers, body)
200
- end
201
- end
202
- if params.key?('sott') # has_key
203
- headers['X-LoginRadius-Sott'] = params['sott']
204
- params.delete('sott')
205
- end
206
-
207
- uri_obj.query = URI.encode_www_form(params)
208
- http = Net::HTTP.new(uri_obj.host, uri_obj.port)
209
- http.use_ssl = true
210
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
211
- req = Net::HTTP::Delete.new(uri_obj.request_uri, headers)
212
- req.body = body.to_json
213
- response = http.request(req)
214
-
215
- begin
216
- return LoginRadius::Response.new(response)
217
- rescue JSON::ParserError => e
218
- raise LoginRadius::Error.new("JSON parsing error has occurred. More info: #{e.message}")
219
- end
220
- end
221
-
222
- # Builds a URI instance given type and resource
223
- #
224
- # @param resource [String] Target resource
225
- # custom_api_domain is set
226
- # @return [URI] uri instance
227
- def build_new_uri_obj(resource)
228
- if resource == 'ciam/appinfo'
229
- return URI.parse(API_V2_BASE_URL_CONFIG + resource)
230
- else
231
- if ENV['CUSTOM_API_DOMAIN'] == 'false' || ENV['CUSTOM_API_DOMAIN'] == nil
232
- return URI.parse(API_V2_BASE_URL + resource)
233
- else
234
- return URI.parse(ENV['CUSTOM_API_DOMAIN'] + resource)
235
- end
236
- end
237
- end
238
-
239
- # Create a has digest in header
240
- #
241
- # @param endpoint [String] endpoint
242
- # @param secret_key [String] secret key
243
- # @param headers [String] headers
244
- # @param body [String] body
245
- # @return [URI] uri instance
246
- #
247
- # @return [headers] header
248
- def create_hash_secret(endpoint, secret_key, headers, body = {})
249
- endpoint_uri = 'https://api.loginradius.com' + endpoint
250
- expiry_time = (Time.now.getutc() + (1*60*60)).strftime('%Y/%m/%d %H:%M:%S')
251
-
252
- encoded_uri = CGI.escape(CGI.unescape(endpoint_uri))
253
-
254
- if body.blank?
255
- string_to_hash = expiry_time + ':' + encoded_uri.downcase
256
- else
257
- string_to_hash = expiry_time + ':' + encoded_uri.downcase + ':' + body.to_json
258
- end
259
-
260
- mac = Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), secret_key, string_to_hash)).strip()
261
- headers['X-Request-Expires'] = expiry_time
262
- headers['digest'] = 'SHA-256='+mac
263
- return headers
264
- end
265
-
266
- # Local - Generate SOTT:
267
- # Generates a Secured One Time Token locally.
268
- #
269
- # @params validity_length [Integer] Length of time the SOTT is valid for in minutes
270
- #
271
- # @returns sott [String] LoginRadius Secured One Time Token
272
- def local_generate_sott(validity_length = 10)
273
- start_time = Time.now.getutc().strftime('%Y/%m/%d %H:%M:%S')
274
- end_time = (Time.now.getutc() + (validity_length*60)).strftime('%Y/%m/%d %H:%M:%S')
275
-
276
- plain_text = start_time + '#' + ENV['API_KEY'] + '#' + end_time
277
- iter = 10000
278
- salt = "\x00\x00\x00\x00\x00\x00\x00\x00"
279
- key_len = KEY_SIZE / 8
280
- cipher_key = OpenSSL::PKCS5.pbkdf2_hmac_sha1(ENV['API_SECRET'], salt, iter, key_len)
281
-
282
- cipher = OpenSSL::Cipher.new('aes-' + KEY_SIZE.to_s + '-cbc')
283
- cipher.encrypt
284
- cipher.key = cipher_key
285
- cipher.iv = INIT_VECTOR
286
-
287
- encrypted = cipher.update(plain_text) + cipher.final
288
- encrypted_b64 = Base64.strict_encode64(encrypted)
289
-
290
- hash = Digest::MD5.hexdigest(encrypted_b64)
291
- sott = encrypted_b64 + '*' + hash
292
- return sott
293
- end
294
- end
295
- end
1
+ require 'net/http'
2
+ require 'openssl'
3
+ require 'base64'
4
+ require 'cgi'
5
+
6
+ module LoginRadius
7
+ module RequestClient
8
+ # LoginRadius Client Module: Methods relating to building and sending requests are defined here.
9
+
10
+ API_V2_BASE_URL = 'https://api.loginradius.com/'
11
+ API_V2_BASE_URL_CONFIG = 'https://config.lrcontent.com/'
12
+ INIT_VECTOR = 'tu89geji340t89u2'
13
+ KEY_SIZE = 256
14
+
15
+ # Sends a POST API request.
16
+ #
17
+ # @param uri_endpoint [URI] Target uri instance
18
+ # @param params [Hash] Parameters to send
19
+ # @param body [Hash] POST body
20
+ #
21
+ # @return [LoginRadius::Response] LoginRadius response instance
22
+ def post_request(uri_endpoint, params, body = {})
23
+ uri_obj = build_new_uri_obj(uri_endpoint)
24
+
25
+ headers = { 'Content-Type' => 'application/json' }
26
+ if params.key?('access_token') # has_key
27
+ if uri_endpoint.include? 'auth'
28
+ access_token = params['access_token']
29
+ params.delete('access_token')
30
+ headers['Authorization'] = 'Bearer ' + access_token
31
+ end
32
+ end
33
+
34
+ if params.key?('apiSecret') # has_key
35
+ secret_key = params['apiSecret']
36
+ params.delete('apiSecret')
37
+
38
+ if ENV['API_REQUEST_SIGNING'] == 'false' || ENV['API_REQUEST_SIGNING'] == nil
39
+ headers['X-LoginRadius-ApiSecret'] = secret_key
40
+ else
41
+ uri_obj = build_new_uri_obj(uri_endpoint)
42
+ uri_obj.query = URI.encode_www_form(params)
43
+ headers = create_hash_secret(uri_obj.request_uri, secret_key, headers, body)
44
+ end
45
+ end
46
+ if params.key?('sott') # has_key
47
+ headers['X-LoginRadius-Sott'] = params['sott']
48
+ params.delete('sott')
49
+ end
50
+
51
+ unless ENV['Origin_IP'] == "" || ENV['Origin_IP'] == nil
52
+ headers['X-Origin-IP'] = ENV['Origin_IP']
53
+ end
54
+
55
+ uri_obj.query = URI.encode_www_form(params)
56
+ http = Net::HTTP.new(uri_obj.host, uri_obj.port)
57
+ http.use_ssl = true
58
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
59
+
60
+ response = http.post(uri_obj.request_uri, body.to_json, headers)
61
+
62
+ begin
63
+ return LoginRadius::Response.new(response)
64
+ rescue JSON::ParserError => e
65
+ raise LoginRadius::Error.new("JSON parsing error has occurred. More info: #{e.message}")
66
+ end
67
+ end
68
+
69
+ def isNullOrWhiteSpace(params)
70
+ return params.blank? ? true : false
71
+ end
72
+
73
+ def getValidationMessage(params)
74
+ return params + " is a required parameter."
75
+ end
76
+
77
+ # Sends a GET API request.
78
+ #
79
+ # @param uri_endpoint [URI] Target uri instance
80
+ # @param params [Hash] Parameters to send
81
+ # @param body [Hash] Request body
82
+ #
83
+ # @return [LoginRadius::Response] LoginRadius response instance
84
+ def get_request(uri_endpoint, params, body = {})
85
+ uri_obj = build_new_uri_obj(uri_endpoint)
86
+
87
+ headers = {'Content-Type' => 'application/json'}
88
+ if params.key?('access_token') # has_key
89
+ if uri_endpoint.include? 'auth'
90
+ access_token = params['access_token']
91
+ params.delete('access_token')
92
+ headers['Authorization'] = 'Bearer ' + access_token
93
+ end
94
+ end
95
+
96
+ if params.key?('apiSecret') # has_key
97
+ secret_key = params['apiSecret']
98
+ params.delete('apiSecret')
99
+
100
+ if ENV['API_REQUEST_SIGNING'] == 'false' || ENV['API_REQUEST_SIGNING'] == nil
101
+ headers['X-LoginRadius-ApiSecret'] = secret_key
102
+ else
103
+ uri_obj = build_new_uri_obj(uri_endpoint)
104
+ uri_obj.query = URI.encode_www_form(params)
105
+ headers = create_hash_secret(uri_obj.request_uri, secret_key, headers, body)
106
+ end
107
+ end
108
+ if params.key?('sott') # has_key
109
+ headers['X-LoginRadius-Sott'] = params['sott']
110
+ params.delete('sott')
111
+ end
112
+
113
+ unless ENV['Origin_IP'] == "" || ENV['Origin_IP'] == nil
114
+ headers['X-Origin-IP'] = ENV['Origin_IP']
115
+ end
116
+
117
+ uri_obj.query = URI.encode_www_form(params)
118
+ http = Net::HTTP.new(uri_obj.host, uri_obj.port)
119
+ http.use_ssl = true
120
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
121
+ response = http.get(uri_obj.request_uri, headers)
122
+
123
+ begin
124
+ return LoginRadius::Response.new(response)
125
+ rescue JSON::ParserError => e
126
+ raise LoginRadius::Error.new("JSON parsing error has occurred. More info: #{e.message}")
127
+ end
128
+ end
129
+
130
+ # Sends a PUT API request.
131
+ #
132
+ # @param uri_endpoint [URI] Target uri instance
133
+ # @param params [Hash] Parameters to send
134
+ # @param body [Hash] PUT body
135
+ #
136
+ # @return [LoginRadius::Response] LoginRadius response instance
137
+ def put_request(uri_endpoint, params, body = {})
138
+ uri_obj = build_new_uri_obj(uri_endpoint)
139
+
140
+ headers = { 'Content-Type' => 'application/json' }
141
+ if params.key?('access_token') # has_key
142
+ if uri_endpoint.include? 'auth'
143
+ access_token = params['access_token']
144
+ params.delete('access_token')
145
+ headers['Authorization'] = 'Bearer ' + access_token
146
+ end
147
+ end
148
+
149
+ if params.key?('apiSecret') # has_key
150
+ secret_key = params['apiSecret']
151
+ params.delete('apiSecret')
152
+
153
+ if ENV['API_REQUEST_SIGNING'] == 'false' || ENV['API_REQUEST_SIGNING'] == nil
154
+ headers['X-LoginRadius-ApiSecret'] = secret_key
155
+ else
156
+ uri_obj = build_new_uri_obj(uri_endpoint)
157
+ uri_obj.query = URI.encode_www_form(params)
158
+
159
+ headers = create_hash_secret(uri_obj.request_uri, secret_key, headers, body)
160
+ end
161
+ end
162
+ if params.key?('sott') # has_key
163
+ headers['X-LoginRadius-Sott'] = params['sott']
164
+ params.delete('sott')
165
+ end
166
+
167
+ unless ENV['Origin_IP'] == "" || ENV['Origin_IP'] == nil
168
+ headers['X-Origin-IP'] = ENV['Origin_IP']
169
+ end
170
+
171
+ uri_obj.query = URI.encode_www_form(params)
172
+ http = Net::HTTP.new(uri_obj.host, uri_obj.port)
173
+ http.use_ssl = true
174
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
175
+ response = http.put(uri_obj.request_uri, body.to_json, headers)
176
+ begin
177
+ return LoginRadius::Response.new(response)
178
+ rescue JSON::ParserError => e
179
+ raise LoginRadius::Error.new("JSON parsing error has occurred. More info: #{e.message}")
180
+ end
181
+ end
182
+
183
+ # Sends a DELETE API request.
184
+ #
185
+ # @param uri_endpoint [URI] Target uri instance
186
+ # @param params [Hash] Parameters to send
187
+ # @param body [Hash] POST body
188
+ #
189
+ # @return [LoginRadius::Response] LoginRadius response instance
190
+ def delete_request(uri_endpoint, params, body = {})
191
+ uri_obj = build_new_uri_obj(uri_endpoint)
192
+
193
+ headers = { 'Content-Type' => 'application/json' }
194
+ if params.key?('access_token') # has_key
195
+ if uri_endpoint.include? 'auth'
196
+ access_token = params['access_token']
197
+ params.delete('access_token')
198
+ headers['Authorization'] = 'Bearer ' + access_token
199
+ end
200
+ end
201
+
202
+ if params.key?('apiSecret') # has_key
203
+ secret_key = params['apiSecret']
204
+ params.delete('apiSecret')
205
+
206
+ if ENV['API_REQUEST_SIGNING'] == 'false' || ENV['API_REQUEST_SIGNING'] == nil
207
+ headers['X-LoginRadius-ApiSecret'] = secret_key
208
+ else
209
+ uri_obj = build_new_uri_obj(uri_endpoint)
210
+ uri_obj.query = URI.encode_www_form(params)
211
+ headers = create_hash_secret(uri_obj.request_uri, secret_key, headers, body)
212
+ end
213
+ end
214
+ if params.key?('sott') # has_key
215
+ headers['X-LoginRadius-Sott'] = params['sott']
216
+ params.delete('sott')
217
+ end
218
+
219
+ unless ENV['Origin_IP'] == "" || ENV['Origin_IP'] == nil
220
+ headers['X-Origin-IP'] = ENV['Origin_IP']
221
+ end
222
+
223
+ uri_obj.query = URI.encode_www_form(params)
224
+ http = Net::HTTP.new(uri_obj.host, uri_obj.port)
225
+ http.use_ssl = true
226
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
227
+ req = Net::HTTP::Delete.new(uri_obj.request_uri, headers)
228
+ req.body = body.to_json
229
+ response = http.request(req)
230
+
231
+ begin
232
+ return LoginRadius::Response.new(response)
233
+ rescue JSON::ParserError => e
234
+ raise LoginRadius::Error.new("JSON parsing error has occurred. More info: #{e.message}")
235
+ end
236
+ end
237
+
238
+ # Builds a URI instance given type and resource
239
+ #
240
+ # @param resource [String] Target resource
241
+ # custom_api_domain is set
242
+ # @return [URI] uri instance
243
+ def build_new_uri_obj(resource)
244
+ if resource == 'ciam/appinfo'
245
+ return URI.parse(API_V2_BASE_URL_CONFIG + resource)
246
+ else
247
+ if ENV['CUSTOM_API_DOMAIN'] == 'false' || ENV['CUSTOM_API_DOMAIN'] == nil
248
+ return URI.parse(API_V2_BASE_URL + resource)
249
+ else
250
+ return URI.parse(ENV['CUSTOM_API_DOMAIN'] + resource)
251
+ end
252
+ end
253
+ end
254
+
255
+ # Create a has digest in header
256
+ #
257
+ # @param endpoint [String] endpoint
258
+ # @param secret_key [String] secret key
259
+ # @param headers [String] headers
260
+ # @param body [String] body
261
+ # @return [URI] uri instance
262
+ #
263
+ # @return [headers] header
264
+ def create_hash_secret(endpoint, secret_key, headers, body = {})
265
+ endpoint_uri = 'https://api.loginradius.com' + endpoint
266
+ expiry_time = (Time.now.getutc() + (1*60*60)).strftime('%Y/%m/%d %H:%M:%S')
267
+
268
+ encoded_uri = CGI.escape(CGI.unescape(endpoint_uri))
269
+
270
+ if body.blank?
271
+ string_to_hash = expiry_time + ':' + encoded_uri.downcase
272
+ else
273
+ string_to_hash = expiry_time + ':' + encoded_uri.downcase + ':' + body.to_json
274
+ end
275
+
276
+ mac = Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), secret_key, string_to_hash)).strip()
277
+ headers['X-Request-Expires'] = expiry_time
278
+ headers['digest'] = 'SHA-256='+mac
279
+ return headers
280
+ end
281
+
282
+ # Local - Generate SOTT:
283
+ # Generates a Secured One Time Token locally.
284
+ #
285
+ # @params validity_length [Integer] Length of time the SOTT is valid for in minutes
286
+ #
287
+ # @returns sott [String] LoginRadius Secured One Time Token
288
+ def local_generate_sott(validity_length = 10)
289
+ start_time = Time.now.getutc().strftime('%Y/%m/%d %H:%M:%S')
290
+ end_time = (Time.now.getutc() + (validity_length*60)).strftime('%Y/%m/%d %H:%M:%S')
291
+
292
+ plain_text = start_time + '#' + ENV['API_KEY'] + '#' + end_time
293
+ iter = 10000
294
+ salt = "\x00\x00\x00\x00\x00\x00\x00\x00"
295
+ key_len = KEY_SIZE / 8
296
+ cipher_key = OpenSSL::PKCS5.pbkdf2_hmac_sha1(ENV['API_SECRET'], salt, iter, key_len)
297
+
298
+ cipher = OpenSSL::Cipher.new('aes-' + KEY_SIZE.to_s + '-cbc')
299
+ cipher.encrypt
300
+ cipher.key = cipher_key
301
+ cipher.iv = INIT_VECTOR
302
+
303
+ encrypted = cipher.update(plain_text) + cipher.final
304
+ encrypted_b64 = Base64.strict_encode64(encrypted)
305
+
306
+ hash = Digest::MD5.hexdigest(encrypted_b64)
307
+ sott = encrypted_b64 + '*' + hash
308
+ return sott
309
+ end
310
+ end
311
+ end