gauthify 1.0.1 → 1.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/gauthify.rb +284 -179
  3. metadata +14 -13
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ae79c8f5170cbca7d5e8c62276f73e900dc2f24f
4
+ data.tar.gz: 00b262b7a861a172af456677e649837a08cced5d
5
+ SHA512:
6
+ metadata.gz: a003964107bc6c555a388a050da9cebae75b41bf7c57937fe751c82524b69ab1ebc4c343b41ca464c782f3d03afa839d662a8fce988e5b5da45b94b55146edcf
7
+ data.tar.gz: aff87a8239fb037aa932393db3f7d62944738af77fc465567cf9074a186ab5ff327efaa87350bc83321cfb7e1d9f1614a0c0d3e7b44dae405a70469ba8a4aa7b
data/lib/gauthify.rb CHANGED
@@ -4,245 +4,350 @@ require 'rest-client'
4
4
  require 'json'
5
5
 
6
6
  class GAuthifyError < Exception
7
- <<-DOC
7
+ <<-DOC
8
8
  All Errors
9
- DOC
9
+ DOC
10
10
 
11
- attr_reader :msg, :http_status, :error_code, :response_body
11
+ attr_reader :msg, :http_status, :error_code, :response_body
12
12
 
13
- def initialize(msg, http_status = '', error_code = '', response_body='')
14
- @msg = msg
15
- @http_status = http_status
16
- @error_code = error_code
17
- @response_body = response_body
18
- end
13
+ def initialize(msg, http_status = '', error_code = '', response_body='')
14
+ @msg = msg
15
+ @http_status = http_status
16
+ @error_code = error_code
17
+ @response_body = response_body
18
+ end
19
19
  end
20
20
 
21
21
  class ApiKeyError < GAuthifyError
22
- <<-DOC
22
+ <<-DOC
23
23
  Raised when API Key is incorrect
24
- DOC
24
+ DOC
25
25
  end
26
26
 
27
27
  class ParameterError < GAuthifyError
28
- <<-DOC
28
+ <<-DOC
29
29
  Raised when submitting bad parameters or missing parameters
30
- DOC
30
+ DOC
31
31
  end
32
32
 
33
33
 
34
34
  class NotFoundError < GAuthifyError
35
- <<-DOC
35
+ <<-DOC
36
36
  Raised when a result isn't found for the parameters provided.
37
- DOC
37
+ DOC
38
38
  end
39
39
 
40
40
 
41
41
  class ServerError < GAuthifyError
42
- <<-DOC
42
+ <<-DOC
43
43
  Raised for any other error that the server can give, mainly a 500
44
- DOC
44
+ DOC
45
45
  end
46
46
 
47
47
  class RateLimitError < GAuthifyError
48
- <<-DOC
48
+ <<-DOC
49
49
  Raised when API limit reached either by lack of payment or membership limit
50
- DOC
50
+ DOC
51
51
  end
52
52
 
53
53
 
54
54
  class GAuthify
55
55
 
56
- attr_accessor :headers, :access_points
57
-
58
- def initialize(api_key)
59
- @access_points = [
60
- 'https://api.gauthify.com/v1/',
61
- 'https://backup.gauthify.com/v1/'
62
- ]
63
- @headers = {:authorization => api_key, :user_agent => 'GAuthify/v1.00 Ruby/1.01'}
64
-
65
- end
66
-
67
- def requests_handler(type, url_addon='', params={})
68
- type = type.downcase
69
- for each in @access_points
70
- begin
71
- req_url = each + url_addon
72
- req = RestClient::Request.execute(:method => type, :url => req_url, :timeout => 1.5, :headers => @headers, :payload => params)
73
- status_code = req.code
74
- begin
75
- json_resp = JSON.parse(req.to_str)
76
- rescue
77
- json_resp = false
78
- end
79
- if not json_resp.is_a? Hash or (status_code > 400 and not [401, 402, 406, 404].include?(status_code))
80
- raise RestClient::Exception
81
- end
82
- break
83
- rescue Exception => e
84
- if e.is_a? RestClient::Exception
85
- case e.http_code
86
- when 401
87
- json_resp = JSON.parse(e.http_body)
88
- raise ApiKeyError.new(json_resp['error_message'], status_code, json_resp['error_code'], e.http_body), json_resp['error_message']
89
- when 402
90
- json_resp = JSON.parse(e.http_body)
91
- raise RateLimitError.new(json_resp['error_message'], status_code, json_resp['error_code'], e.http_body), json_resp['error_message']
92
- when 406
93
- json_resp = JSON.parse(e.http_body)
94
- raise ParameterError.new(json_resp['error_message'], status_code, json_resp['error_code'], e.http_body), json_resp['error_message']
95
- when 404
96
- json_resp = JSON.parse(e.http_body)
97
- raise NotFoundError.new(json_resp['error_message'], status_code, json_resp['error_code'], e.http_body), json_resp['error_message']
98
- end
99
- end
100
- if each == @access_points[-1]
101
- e_msg = "#{e.to_s}. Please contact support@gauthify.com for help"
102
- raise ServerError.new(e_msg, 500, '500', ''), e_msg
103
- end
104
- next
105
- end
56
+ attr_accessor :headers, :access_points
57
+
58
+ def initialize(api_key)
59
+ @access_points = [
60
+ 'https://api.gauthify.com/v1/',
61
+ 'https://backup.gauthify.com/v1/'
62
+ ]
63
+ @headers = {:authorization => api_key, :user_agent => 'GAuthify/v1.25 Ruby/1.25'}
64
+
65
+ end
66
+
67
+ def requests_handler(type, url_addon='', params={})
68
+ type = type.downcase
69
+ for each in @access_points
70
+ begin
71
+ req_url = each + url_addon
72
+ req = RestClient::Request.execute(:method => type, :url => req_url, :timeout => 1.5, :headers => @headers, :payload => params)
73
+ status_code = req.code
74
+ begin
75
+ json_resp = JSON.parse(req.to_str)
76
+ rescue
77
+ json_resp = false
78
+ end
79
+ if not json_resp.is_a? Hash or (status_code > 400 and not [401, 402, 406, 404].include?(status_code))
80
+ raise RestClient::Exception
81
+ end
82
+ break
83
+ rescue Exception => e
84
+ if e.is_a? RestClient::Exception
85
+ case e.http_code
86
+ when 401
87
+ json_resp = JSON.parse(e.http_body)
88
+ raise ApiKeyError.new(json_resp['error_message'], status_code, json_resp['error_code'], e.http_body), json_resp['error_message']
89
+ when 402
90
+ json_resp = JSON.parse(e.http_body)
91
+ raise RateLimitError.new(json_resp['error_message'], status_code, json_resp['error_code'], e.http_body), json_resp['error_message']
92
+ when 406
93
+ json_resp = JSON.parse(e.http_body)
94
+ raise ParameterError.new(json_resp['error_message'], status_code, json_resp['error_code'], e.http_body), json_resp['error_message']
95
+ when 404
96
+ json_resp = JSON.parse(e.http_body)
97
+ raise NotFoundError.new(json_resp['error_message'], status_code, json_resp['error_code'], e.http_body), json_resp['error_message']
98
+ end
106
99
  end
107
- return json_resp['data']
100
+ if each == @access_points[-1]
101
+ e_msg = "#{e.to_s}. Please contact support@gauthify.com for help"
102
+ raise ServerError.new(e_msg, 500, '500', ''), e_msg
103
+ end
104
+ next
105
+ end
108
106
  end
107
+ return json_resp['data']
108
+ end
109
+
109
110
 
111
+ def create_user(unique_id, display_name, email=nil, phone_number=nil)
112
+ <<-DOC
113
+ Creates new user with a new secret key or resets if already exists
114
+ DOC
110
115
 
111
- def create_user(unique_id, display_name)
112
- <<-DOC
113
- Creates or upserts a new user with a new secret key
114
- DOC
116
+ params = {'display_name' => display_name}
117
+ if email
118
+ params['email'] = email
119
+ end
120
+ if phone_number
121
+ params['phone_number'] = phone_number
122
+ end
123
+ url_addon = "users/#{unique_id}/"
124
+ puts "HELLO", params
125
+ return requests_handler('post', url_addon, params=params)
126
+ end
115
127
 
116
- params = {'display_name' => display_name}
117
- url_addon = "users/#{unique_id}/"
118
- return requests_handler('post', url_addon, params=params)
128
+ def update_user(unique_id, email=nil, phone_number=nil, meta=nil, reset_key = false)
129
+ <<-DOC
130
+ Creates new user with a new secret key or resets if already exists
131
+ DOC
132
+
133
+ params = Hash.new
134
+ if email
135
+ params['email'] = email
136
+ end
137
+ if phone_number
138
+ params['phone_number'] = phone_number
139
+ end
140
+ if meta
141
+ params['meta'] = meta.to_json
119
142
  end
143
+ if reset_key
144
+ params['reset_key'] = 'true'
145
+ end
146
+ puts params
147
+ url_addon = "users/#{unique_id}/"
148
+ return requests_handler('put', url_addon, params=params)
149
+ end
150
+
120
151
 
121
- def delete_user(unique_id)
122
- <<-DOC
152
+ def delete_user(unique_id)
153
+ <<-DOC
123
154
  Deletes user given by unique_id
124
- DOC
125
- url_addon = "users/#{unique_id}/"
126
- return requests_handler('delete', url_addon)
155
+ DOC
156
+ url_addon = "users/#{unique_id}/"
157
+ return requests_handler('delete', url_addon)
127
158
 
128
- end
159
+ end
129
160
 
130
- def get_all_users()
131
- <<-DOC
161
+ def get_all_users()
162
+ <<-DOC
132
163
  Retrieves a list of all users
133
- DOC
134
- return requests_handler('get', 'users/')
135
- end
164
+ DOC
165
+ return requests_handler('get', 'users/')
166
+ end
136
167
 
137
168
 
138
- def get_user(unique_id, auth_code=nil)
139
- <<-DOC
169
+ def get_user(unique_id, auth_code=nil)
170
+ <<-DOC
140
171
  Returns a single user, checks the otp if provided
141
- DOC
142
- url_addon = "users/#{unique_id}/"
143
- url_addon << "check/#{auth_code}" if auth_code
144
- return requests_handler('get', url_addon)
145
- end
172
+ DOC
173
+ url_addon = "users/#{unique_id}/"
174
+ url_addon << "check/#{auth_code}" if auth_code
175
+ return requests_handler('get', url_addon)
176
+ end
146
177
 
147
- def check_auth(unique_id, auth_code, safe_mode = false)
148
- <<-DOC
149
- Checks OTP returns True/False depending on OTP correctness.
150
- DOC
151
- begin
152
- response = get_user(unique_id, auth_code)
153
- if not response['provided_auth']
154
- raise ParameterError('auth_code not detected. Check if params sent via get request.')
155
- end
156
- return response['authenticated']
157
- rescue GAuthifyError => e
158
- if safe_mode
159
- return True
160
- else
161
- raise e
162
- end
163
- end
178
+ def get_user_by_token(unique_id)
179
+ <<-DOC
180
+ Returns a single user by ezGAuth token
181
+ DOC
182
+ url_addon = "token/#{unique_id}/"
183
+ return requests_handler('get', url_addon)
184
+ end
164
185
 
186
+ def check_auth(unique_id, auth_code, safe_mode = false)
187
+ <<-DOC
188
+ Checks OTP returns True/False depending on OTP correctness.
189
+ DOC
190
+ begin
191
+ response = get_user(unique_id, auth_code)
192
+ if not response['provided_auth']
193
+ raise ParameterError('auth_code not detected. Check if params sent via get request.')
194
+ end
195
+ return response['authenticated']
196
+ rescue GAuthifyError => e
197
+ if safe_mode
198
+ return True
199
+ else
200
+ raise e
201
+ end
165
202
  end
166
203
 
204
+ end
205
+
167
206
 
168
- def send_sms(unique_id, phone_number)
169
- <<-DOC
207
+ def send_sms(unique_id, phone_number = nil)
208
+ <<-DOC
170
209
  Sends text message to phone number with the one time auth_code
171
- DOC
172
- url_addon = "users/#{unique_id}/sms/#{phone_number}"
173
- return requests_handler('get', url_addon)
210
+ DOC
211
+ url_addon = "users/#{unique_id}/sms/"
212
+ if phone_number
213
+ url_addon << "#{phone_number}/"
174
214
  end
215
+ return requests_handler('get', url_addon)
216
+ end
175
217
 
176
- def send_email(unique_id, email)
177
- <<-DOC
218
+ def send_email(unique_id, email=nil)
219
+ <<-DOC
178
220
  Sends email message to phone number with the one time auth_code
179
- DOC
180
- url_addon = "users/#{unique_id}/email/#{email}"
181
- return requests_handler('get', url_addon)
221
+ DOC
222
+ url_addon = "users/#{unique_id}/email/"
223
+ if email
224
+ url_addon << "#{email}/"
182
225
  end
226
+ return requests_handler('get', url_addon)
227
+ end
183
228
 
229
+ def api_errors()
230
+ <<-DOC
231
+ Returns hash containing api errors.
232
+ DOC
233
+ url_addon = "errors/"
234
+ return requests_handler('get', url_addon)
235
+ end
184
236
 
185
- def quick_test(test_email = nil, test_number = nil)
186
- <<-DOC
237
+
238
+ def quick_test(test_email = nil, test_number = nil)
239
+ <<-DOC
187
240
  Runs initial tests to make sure everything is working fine
188
- DOC
189
- account_name = 'testuser@gauthify.com'
190
- puts("1) Testing Creating a User...")
191
- result = create_user(account_name,
192
- account_name)
193
- puts result
194
- puts("Success ")
195
- puts("2) Retrieving Created User...")
196
- user = get_user(account_name)
197
- puts user
198
- puts("Success ")
199
- puts("3) Retrieving All Users...")
200
- result = get_all_users()
201
- puts result
202
- puts("Success ")
203
- puts("4) Bad Auth Code...")
204
- result = check_auth(account_name, '112345')
205
- puts(result)
206
- if result
207
- raise Exception
208
- end
209
- puts("Success ")
210
- puts("5) Testing one time pass (OTP)....")
211
- result = check_auth(account_name, user['otp'])
212
- puts(result)
213
- if not result
214
- raise Exception
215
- end
216
- if test_email
217
- puts("5A) Testing email to #{test_email}....")
218
- result = send_email(account_name, test_email)
219
- puts(result)
220
- end
221
- if test_number
222
- puts("5B) Testing SMS to #{test_number}....")
223
- result = send_sms(account_name, test_number)
224
- puts(result)
225
- end
226
- puts("Success ")
227
- puts("6) Detection of provided auth...")
228
- result = get_user(account_name, 'test12')['provided_auth']
229
- if not result
230
- raise Exception
231
- end
232
- puts("7) Deleting Created User...")
233
- result = delete_user(account_name)
234
- puts(result)
235
- puts("Success ")
241
+ DOC
242
+ account_name = 'testuser@gauthify.com'
236
243
 
244
+ def success()
245
+ print("Success \n")
246
+ end
237
247
 
238
- puts("8) Testing backup server...")
239
- @access_points[0] = 'https://blah.gauthify.com/v1/'
240
- results = get_all_users()
241
- @access_points[0] = 'https://api.gauthify.com/v1/'
242
- puts(result)
243
- puts("Tests Look Good.")
248
+ puts("1) Testing Creating a User...")
249
+ result = create_user(account_name,
250
+ account_name, email='firsttest@gauthify.com',
251
+ phone_number='0123456789')
252
+ if not result['unique_id'] == account_name
253
+ raise Exception
254
+ end
255
+ if not result['display_name'] == account_name
256
+ raise Exception
244
257
  end
258
+ if not result['email'] == 'firsttest@gauthify.com'
259
+ raise Exception
260
+ end
261
+ if not result['phone_number'] == '0123456789'
262
+ raise Exception
263
+ end
264
+ puts(result)
265
+ success()
245
266
 
267
+ puts("2) Retrieving Created User...")
268
+ user = get_user(account_name)
269
+ if not user.class == Hash
270
+ raise Exception
271
+ end
272
+ puts(result)
273
+ success()
246
274
 
247
- end
275
+ puts("3) Retrieving All Users...")
276
+ result = get_all_users()
277
+ if not result.class == Array
278
+ raise Exception
279
+ end
280
+ puts(result)
281
+ success()
248
282
 
283
+ puts("4) Bad Auth Code...")
284
+ result = check_auth(account_name, '112345')
285
+ if result
286
+ raise Exception
287
+ end
288
+ puts(result)
289
+ success()
290
+
291
+ puts("5) Testing one time pass (OTP)....")
292
+ result = check_auth(account_name, user['otp'])
293
+ puts(result)
294
+ if not result
295
+ raise ParameterError('Server error. OTP not working. Contact ', 'support@gauthify.com for help.', 500, '500', '')
296
+ end
297
+ success()
298
+ if test_email
299
+ puts("5A) Testing email to #{test_email}")
300
+ result = send_email(account_name, test_email)
301
+ puts(result)
302
+ success()
303
+ end
304
+ if test_number
305
+ puts("5B) Testing SMS to #{test_number}")
306
+ send_sms(account_name, test_number)
307
+ success()
308
+ end
309
+ puts("6) Detection of provided auth...")
310
+ result = get_user(account_name, 'test12')
311
+ if not result['provided_auth']
312
+ raise Exception
313
+ end
314
+ puts(result)
315
+ success()
316
+
317
+ puts("7) Testing updating email, phone, and meta")
318
+ result = update_user(account_name, email='test@gauthify.com',
319
+ phone_number='1234567890', meta={'a' => 'b'})
320
+ if not result['email'] == 'test@gauthify.com'
321
+ raise Exception
322
+ end
323
+ if not result['phone_number'] == '1234567890'
324
+ raise Exception
325
+ end
326
+ if not result['meta']['a'] == 'b'
327
+ raise Exception
328
+ end
329
+ current_key = result['key']
330
+ success()
331
+
332
+ puts("8) Testing key/secret")
333
+ result = update_user(account_name, nil, nil, nil, true)
334
+ puts(current_key, result['key'])
335
+ if not result['key'] != current_key
336
+ raise Exception
337
+ end
338
+ success()
339
+
340
+ puts("9) Deleting Created User...")
341
+ result = delete_user(account_name)
342
+ success()
343
+
344
+ puts("10) Testing backup server...")
345
+ current = @access_points[0]
346
+ @access_points[0] = 'https://blah.gauthify.com/v1/'
347
+ result = get_all_users()
348
+ @access_points[0] = current
349
+ puts(result)
350
+ success()
351
+
352
+ end
353
+ end
metadata CHANGED
@@ -1,27 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gauthify
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
5
- prerelease:
4
+ version: 1.2.6
6
5
  platform: ruby
7
6
  authors:
8
7
  - GAuthify
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-01-11 00:00:00.000000000 Z
11
+ date: 2013-05-08 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rest-client
16
- requirement: &70307964850660 !ruby/object:Gem::Requirement
17
- none: false
15
+ requirement: !ruby/object:Gem::Requirement
18
16
  requirements:
19
- - - =
17
+ - - '='
20
18
  - !ruby/object:Gem::Version
21
19
  version: 1.6.7
22
20
  type: :runtime
23
21
  prerelease: false
24
- version_requirements: *70307964850660
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 1.6.7
25
27
  description: API library for GAuthify.com (Google Authenticator, SMS, email multi
26
28
  factor authentication).
27
29
  email: support@gauthify.com
@@ -32,26 +34,25 @@ files:
32
34
  - lib/gauthify.rb
33
35
  homepage: https://www.gauthify.com
34
36
  licenses: []
37
+ metadata: {}
35
38
  post_install_message:
36
39
  rdoc_options: []
37
40
  require_paths:
38
41
  - lib
39
42
  required_ruby_version: !ruby/object:Gem::Requirement
40
- none: false
41
43
  requirements:
42
- - - ! '>='
44
+ - - '>='
43
45
  - !ruby/object:Gem::Version
44
46
  version: '0'
45
47
  required_rubygems_version: !ruby/object:Gem::Requirement
46
- none: false
47
48
  requirements:
48
- - - ! '>='
49
+ - - '>='
49
50
  - !ruby/object:Gem::Version
50
51
  version: '0'
51
52
  requirements: []
52
53
  rubyforge_project:
53
- rubygems_version: 1.8.11
54
+ rubygems_version: 2.0.3
54
55
  signing_key:
55
- specification_version: 3
56
+ specification_version: 4
56
57
  summary: ''
57
58
  test_files: []