gauthify 1.0.0
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.
- data/lib/gauthify.rb +257 -0
- metadata +57 -0
data/lib/gauthify.rb
ADDED
@@ -0,0 +1,257 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'uri'
|
3
|
+
require 'rest-client'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
class GAuthifyError < Exception
|
7
|
+
<<-DOC
|
8
|
+
All Errors
|
9
|
+
DOC
|
10
|
+
|
11
|
+
attr_reader :msg, :http_status, :error_code, :response_body
|
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
|
19
|
+
end
|
20
|
+
|
21
|
+
class ApiKeyError < GAuthifyError
|
22
|
+
<<-DOC
|
23
|
+
Raised when API Key is incorrect
|
24
|
+
DOC
|
25
|
+
end
|
26
|
+
|
27
|
+
class ParameterError < GAuthifyError
|
28
|
+
<<-DOC
|
29
|
+
Raised when submitting bad parameters or missing parameters
|
30
|
+
DOC
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
class NotFoundError < GAuthifyError
|
35
|
+
<<-DOC
|
36
|
+
Raised when a result isn't found for the parameters provided.
|
37
|
+
DOC
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
class ConnectionError < GAuthifyError
|
42
|
+
<<-DOC
|
43
|
+
Raised when couldn't connect to GAuthify.com. Check firewalls and other
|
44
|
+
things that could effect your network connection. Its a good idea to
|
45
|
+
accept 2nd factor authentication during times when this happens.
|
46
|
+
DOC
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
class ServerError < GAuthifyError
|
51
|
+
<<-DOC
|
52
|
+
Raised for any other error that the server can give, mainly a 500
|
53
|
+
DOC
|
54
|
+
end
|
55
|
+
|
56
|
+
class RateLimitError < GAuthifyError
|
57
|
+
<<-DOC
|
58
|
+
Raised when API limit reached either by lack of payment or membership limit
|
59
|
+
DOC
|
60
|
+
end
|
61
|
+
|
62
|
+
|
63
|
+
class GAuthify
|
64
|
+
|
65
|
+
attr_accessor :headers, :access_points
|
66
|
+
|
67
|
+
def initialize(api_key)
|
68
|
+
@access_points = [
|
69
|
+
'https://api.gauthify.com/v1/',
|
70
|
+
'https://backup.gauthify.com/v1/'
|
71
|
+
]
|
72
|
+
@headers = {:authorization => api_key, :user_agent => 'GAuthify/v1.00 Ruby/1.00'}
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
def requests_handler(type, url_addon='', params={})
|
77
|
+
type = type.downcase
|
78
|
+
for each in @access_points
|
79
|
+
begin
|
80
|
+
req_url = each + url_addon
|
81
|
+
req = RestClient::Request.execute(:method => type, :url => req_url, :timeout => 1.5, :headers => @headers, :payload => params)
|
82
|
+
status_code = req.code
|
83
|
+
begin
|
84
|
+
json_resp = JSON.parse(req.to_str)
|
85
|
+
rescue
|
86
|
+
json_resp = false
|
87
|
+
end
|
88
|
+
if not json_resp.is_a? Hash or (status_code > 400 and not [401, 402, 406, 404].include?(status_code))
|
89
|
+
raise RestClient::Exception
|
90
|
+
end
|
91
|
+
break
|
92
|
+
rescue Exception => e
|
93
|
+
if e.is_a? RestClient::Exception
|
94
|
+
case e.http_code
|
95
|
+
when 401
|
96
|
+
json_resp = JSON.parse(e.http_body)
|
97
|
+
raise ApiKeyError.new(json_resp['error_message'], status_code, json_resp['error_code'], e.http_body), json_resp['error_message']
|
98
|
+
when 402
|
99
|
+
json_resp = JSON.parse(e.http_body)
|
100
|
+
raise RateLimitError.new(json_resp['error_message'], status_code, json_resp['error_code'], e.http_body), json_resp['error_message']
|
101
|
+
when 406
|
102
|
+
json_resp = JSON.parse(e.http_body)
|
103
|
+
raise ParameterError.new(json_resp['error_message'], status_code, json_resp['error_code'], e.http_body), json_resp['error_message']
|
104
|
+
when 404
|
105
|
+
json_resp = JSON.parse(e.http_body)
|
106
|
+
raise NotFoundError.new(json_resp['error_message'], status_code, json_resp['error_code'], e.http_body), json_resp['error_message']
|
107
|
+
end
|
108
|
+
end
|
109
|
+
if each == @access_points[-1]
|
110
|
+
e_msg = "#{e.to_s}. Please contact support@gauthify.com for help"
|
111
|
+
raise ServerError.new(e_msg, 500, '500', ''), e_msg
|
112
|
+
end
|
113
|
+
next
|
114
|
+
end
|
115
|
+
end
|
116
|
+
return json_resp['data']
|
117
|
+
end
|
118
|
+
|
119
|
+
|
120
|
+
def create_user(unique_id, display_name)
|
121
|
+
<<-DOC
|
122
|
+
Creates or upserts a new user with a new secret key
|
123
|
+
DOC
|
124
|
+
|
125
|
+
params = {'display_name' => display_name}
|
126
|
+
url_addon = "users/#{unique_id}/"
|
127
|
+
return requests_handler('post', url_addon, params=params)
|
128
|
+
end
|
129
|
+
|
130
|
+
def delete_user(unique_id)
|
131
|
+
<<-DOC
|
132
|
+
Deletes user given by unique_id
|
133
|
+
DOC
|
134
|
+
url_addon = "users/#{unique_id}/"
|
135
|
+
return requests_handler('delete', url_addon)
|
136
|
+
|
137
|
+
end
|
138
|
+
|
139
|
+
def get_all_users()
|
140
|
+
<<-DOC
|
141
|
+
Retrieves a list of all users
|
142
|
+
DOC
|
143
|
+
return requests_handler('get', 'users/')
|
144
|
+
end
|
145
|
+
|
146
|
+
|
147
|
+
def get_user(unique_id, auth_code=nil)
|
148
|
+
<<-DOC
|
149
|
+
Returns a single user, checks the otp if provided
|
150
|
+
DOC
|
151
|
+
url_addon = "users/#{unique_id}/"
|
152
|
+
url_addon << "check/#{auth_code}" if auth_code
|
153
|
+
return requests_handler('get', url_addon)
|
154
|
+
end
|
155
|
+
|
156
|
+
def check_auth(unique_id, auth_code, safe_mode = false)
|
157
|
+
<<-DOC
|
158
|
+
Checks OTP returns True/False depending on OTP correctness.
|
159
|
+
DOC
|
160
|
+
begin
|
161
|
+
response = get_user(unique_id, auth_code)
|
162
|
+
if not response['provided_auth']
|
163
|
+
raise ParameterError('auth_code not detected. Check if params sent via get request.')
|
164
|
+
end
|
165
|
+
return response['authenticated']
|
166
|
+
rescue GAuthifyError => e
|
167
|
+
if safe_mode
|
168
|
+
return True
|
169
|
+
else
|
170
|
+
raise e
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
end
|
175
|
+
|
176
|
+
|
177
|
+
def send_sms(unique_id, phone_number)
|
178
|
+
<<-DOC
|
179
|
+
Sends text message to phone number with the one time auth_code
|
180
|
+
DOC
|
181
|
+
url_addon = "users/#{unique_id}/sms/#{phone_number}"
|
182
|
+
return requests_handler('get', url_addon)
|
183
|
+
end
|
184
|
+
|
185
|
+
def send_email(unique_id, email)
|
186
|
+
<<-DOC
|
187
|
+
Sends email message to phone number with the one time auth_code
|
188
|
+
DOC
|
189
|
+
url_addon = "users/#{unique_id}/email/#{email}"
|
190
|
+
return requests_handler('get', url_addon)
|
191
|
+
end
|
192
|
+
|
193
|
+
|
194
|
+
def quick_test(test_email = nil, test_number = nil)
|
195
|
+
<<-DOC
|
196
|
+
Runs initial tests to make sure everything is working fine
|
197
|
+
DOC
|
198
|
+
account_name = 'testuser@gauthify.com'
|
199
|
+
puts("1) Testing Creating a User...")
|
200
|
+
result = create_user(account_name,
|
201
|
+
account_name)
|
202
|
+
puts result
|
203
|
+
puts("Success ")
|
204
|
+
puts("2) Retrieving Created User...")
|
205
|
+
user = get_user(account_name)
|
206
|
+
puts user
|
207
|
+
puts("Success ")
|
208
|
+
puts("3) Retrieving All Users...")
|
209
|
+
result = get_all_users()
|
210
|
+
puts result
|
211
|
+
puts("Success ")
|
212
|
+
puts("4) Bad Auth Code...")
|
213
|
+
result = check_auth(account_name, '112345')
|
214
|
+
puts(result)
|
215
|
+
if result
|
216
|
+
raise Exception
|
217
|
+
end
|
218
|
+
puts("Success ")
|
219
|
+
puts("5) Testing one time pass (OTP)....")
|
220
|
+
result = check_auth(account_name, user['otp'])
|
221
|
+
puts(result)
|
222
|
+
if not result
|
223
|
+
raise Exception
|
224
|
+
end
|
225
|
+
if test_email
|
226
|
+
puts("5A) Testing email to #{test_email}....")
|
227
|
+
result = send_email(account_name, test_email)
|
228
|
+
puts(result)
|
229
|
+
end
|
230
|
+
if test_number
|
231
|
+
puts("5B) Testing SMS to #{test_number}....")
|
232
|
+
result = send_sms(account_name, test_number)
|
233
|
+
puts(result)
|
234
|
+
end
|
235
|
+
puts("Success ")
|
236
|
+
puts("6) Detection of provided auth...")
|
237
|
+
result = get_user(account_name, 'test12')
|
238
|
+
if not result
|
239
|
+
raise Exception
|
240
|
+
end
|
241
|
+
puts("7) Deleting Created User...")
|
242
|
+
result = delete_user(account_name)
|
243
|
+
puts(result)
|
244
|
+
puts("Success ")
|
245
|
+
|
246
|
+
|
247
|
+
puts("8) Testing backup server...")
|
248
|
+
@access_points[0] = 'https://blah.gauthify.com/v1/'
|
249
|
+
results = get_all_users()
|
250
|
+
@access_points[0] = 'https://api.gauthify.com/v1/'
|
251
|
+
puts(result)
|
252
|
+
puts("Tests Look Good.")
|
253
|
+
end
|
254
|
+
|
255
|
+
|
256
|
+
end
|
257
|
+
|
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gauthify
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- GAuthify
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-11 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rest-client
|
16
|
+
requirement: &70292122966280 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - =
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.6.7
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70292122966280
|
25
|
+
description: API library for GAuthify.com (Google Authenticator, SMS, email multi
|
26
|
+
factor authentication).
|
27
|
+
email: support@gauthify.com
|
28
|
+
executables: []
|
29
|
+
extensions: []
|
30
|
+
extra_rdoc_files: []
|
31
|
+
files:
|
32
|
+
- lib/gauthify.rb
|
33
|
+
homepage: https://www.gauthify.com
|
34
|
+
licenses: []
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options: []
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
+
none: false
|
47
|
+
requirements:
|
48
|
+
- - ! '>='
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
requirements: []
|
52
|
+
rubyforge_project:
|
53
|
+
rubygems_version: 1.8.11
|
54
|
+
signing_key:
|
55
|
+
specification_version: 3
|
56
|
+
summary: ''
|
57
|
+
test_files: []
|