passageidentity 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: aa261c90ca10b6232e6b70dc1e76ec98742f203852f2288db411390aaf8cd824
4
- data.tar.gz: 195611e6726801ac07538472c70f438b71d43c667f56bfcf6bc9cb7dd1a8772b
3
+ metadata.gz: 10dbb3b156d4428c588e7aae5bb1d10f0430c5d7275417ac76b825207a51ea3f
4
+ data.tar.gz: 9b11bbdc0e13d5369829cf74b51e73e8d220587b19b45e18f454c7e69837cff9
5
5
  SHA512:
6
- metadata.gz: aab149a0b1256d1dfe525d31915098a4a1c6b9b362ddb6c7be039ed9eec960c4b174ec42ffd65fe0f87117e472ec0359690752de371f60bf025ad01aac8c408e
7
- data.tar.gz: 4c510ab7268ad4fd4cdef05c1769a5242a72e8238b3f807b6adf1592ab0118a5b0506c0aa6012af72b7d3c914c542f021395298082036a817ec60eb410ec41d7
6
+ metadata.gz: 9a7ef97b70cc9da490c8d8e00c3befb8d222ec101819e9277be0b53da30181904eda00f1e9c73c4368d6cd5447a8c42918c919d9078cfd70a3933cfe7959969d
7
+ data.tar.gz: b38620497b8b0adbf2c8fc8409cfeab86694721dde532e05b34246d4c1050774d0d038ecd661fd1aacfa65890ad5b3839224be59359b47e627eb1f9c14b507b3
@@ -19,8 +19,11 @@ module Passage
19
19
  response = @connection.get("/v1/apps/#{@app_id}")
20
20
  return response.body["app"]
21
21
  rescue Faraday::Error => e
22
- raise PassageError,
23
- "failed to get Passage App. Http Status: #{e.response[:status]}. Response: #{e.response[:body]["error"]}"
22
+ raise PassageError.new(
23
+ message: "failed to fetch passage app",
24
+ status_code: e.response[:status],
25
+ body: e.response[:body]
26
+ )
24
27
  end
25
28
  end
26
29
 
@@ -52,14 +55,16 @@ module Passage
52
55
  # Get the token based on the strategy
53
56
  if @auth_strategy === Passage::COOKIE_STRATEGY
54
57
  unless request.cookies["psg_auth_token"].present?
55
- raise PassageError,
56
- `missing authentication token: expected "psg_auth_token" cookie`
58
+ raise PassageError.new(
59
+ message:
60
+ `missing authentication token: expected "psg_auth_token" cookie`
61
+ )
57
62
  end
58
63
  @token = request.cookies["psg_auth_token"]
59
64
  else
60
65
  headers = request.headers
61
66
  unless headers["Authorization"].present?
62
- raise PassageError, "no authentication token in header"
67
+ raise PassageError.new(message: "no authentication token in header")
63
68
  end
64
69
  @token = headers["Authorization"].split(" ").last
65
70
  end
@@ -68,7 +73,7 @@ module Passage
68
73
  if @token
69
74
  return authenticate_token(@token)
70
75
  else
71
- raise PassageError, "no authentication token"
76
+ raise PassageError.new(message: "no authentication token")
72
77
  end
73
78
  nil
74
79
  end
@@ -98,15 +103,15 @@ module Passage
98
103
  )
99
104
  return claims[0]["sub"]
100
105
  rescue JWT::InvalidIssuerError => e
101
- raise Passage::PassageError, e.message
106
+ raise PassageError.new(message: e.message)
102
107
  rescue JWT::InvalidAudError => e
103
- raise Passage::PassageError, e.message
108
+ raise PassageError.new(e.message)
104
109
  rescue JWT::ExpiredSignature => e
105
- raise Passage::PassageError, e.message
110
+ raise PassageError.new(e.message)
106
111
  rescue JWT::IncorrectAlgorithm => e
107
- raise Passage::PassageError, e.message
112
+ raise PassageError.new(e.message)
108
113
  rescue JWT::DecodeError => e
109
- raise Passage::PassageError, e.message
114
+ raise PassageError.new(e.message)
110
115
  end
111
116
  end
112
117
  end
@@ -77,7 +77,7 @@ module Passage
77
77
 
78
78
  # check for valid auth strategy
79
79
  unless [COOKIE_STRATEGY, HEADER_STRATEGY].include? auth_strategy
80
- raise PassageError, "invalid auth strategy."
80
+ raise PassageError.new(message: "invalid auth strategy.")
81
81
  end
82
82
  @auth_strategy = auth_strategy
83
83
 
@@ -165,8 +165,10 @@ module Passage
165
165
 
166
166
  # check to see if the channel specified is valid before sending it off to the server
167
167
  unless [PHONE_CHANNEL, EMAIL_CHANNEL].include? channel
168
- raise PassageError,
169
- "channel: must be either Passage::EMAIL_CHANNEL or Passage::PHONE_CHANNEL"
168
+ raise PassageError.new(
169
+ message:
170
+ "channel: must be either Passage::EMAIL_CHANNEL or Passage::PHONE_CHANNEL"
171
+ )
170
172
  end
171
173
  magic_link_req["channel"] = channel unless channel.empty?
172
174
  magic_link_req["send"] = send
@@ -195,8 +197,11 @@ module Passage
195
197
  )
196
198
  )
197
199
  rescue Faraday::Error => e
198
- raise PassageError,
199
- "failed to create Passage Magic Link. Http Status: #{e.response[:status]}. Response: #{e.response[:body]["error"]}"
200
+ raise PassageError.new(
201
+ message: "failed to create Passage Magic Link",
202
+ status_code: e.response[:status],
203
+ body: e.response[:body]
204
+ )
200
205
  end
201
206
  end
202
207
  end
@@ -1,4 +1,24 @@
1
+ require "net/http"
2
+
1
3
  module Passage
2
4
  class PassageError < StandardError
5
+ attr_reader :status_code
6
+ attr_reader :status_text
7
+ attr_reader :message
8
+ attr_reader :error
9
+
10
+ def initialize(message:, status_code: nil, body: nil)
11
+ @message = message
12
+ @status_code = status_code
13
+ @status_text =
14
+ (
15
+ if status_code.nil?
16
+ nil
17
+ else
18
+ Net::HTTPResponse::CODE_TO_OBJ[status_code.to_s]
19
+ end
20
+ )
21
+ @error = body.nil? ? nil : body["error"]
22
+ end
3
23
  end
4
24
  end
@@ -10,7 +10,8 @@ module Passage
10
10
  end
11
11
 
12
12
  def get(user_id:)
13
- raise PassageError, "must supply a valid user_id" if user_id.to_s.empty?
13
+ user_exists?(user_id)
14
+
14
15
  begin
15
16
  response = @connection.get("/v1/apps/#{@app_id}/users/#{user_id}")
16
17
  user = response.body["user"]
@@ -35,17 +36,24 @@ module Passage
35
36
  )
36
37
  rescue Faraday::Error => e
37
38
  if e.is_a? Faraday::ResourceNotFound
38
- raise PassageError,
39
- "passage User with ID \"#{user_id}\" does not exist"
39
+ raise PassageError.new(
40
+ message: "Passage User with ID \"#{user_id}\" does not exist",
41
+ status_code: e.response[:status],
42
+ body: e.response[:body]
43
+ )
40
44
  else
41
- raise PassageError,
42
- "failed to get Passage User. Http Status: #{e.response[:status]}. Response: #{e.response[:body]["error"]}"
45
+ raise PassageError.new(
46
+ message: "failed to get Passage User.",
47
+ status_code: e.response[:status],
48
+ body: e.response[:body]
49
+ )
43
50
  end
44
51
  end
45
52
  end
46
53
 
47
54
  def activate(user_id:)
48
- raise PassageError, "must supply a valid user_id" if user_id.to_s.empty?
55
+ user_exists?(user_id)
56
+
49
57
  begin
50
58
  response =
51
59
  @connection.patch("/v1/apps/#{@app_id}/users/#{user_id}/activate")
@@ -70,17 +78,24 @@ module Passage
70
78
  )
71
79
  rescue Faraday::Error => e
72
80
  if e.is_a? Faraday::ResourceNotFound
73
- raise PassageError,
74
- "passage User with ID \"#{user_id}\" does not exist"
81
+ raise PassageError.new(
82
+ message: "Passage User with ID \"#{user_id}\" does not exist",
83
+ status_code: e.response[:status],
84
+ body: e.response[:body]
85
+ )
75
86
  else
76
- raise PassageError,
77
- "failed to activate Passage User. Http Status: #{e.response[:status]}. Response: #{e.response[:body]["error"]}"
87
+ raise PassageError.new(
88
+ message: "failed to activate Passage User.",
89
+ status_code: e.response[:status],
90
+ body: e.response[:body]
91
+ )
78
92
  end
79
93
  end
80
94
  end
81
95
 
82
96
  def deactivate(user_id:)
83
- raise PassageError, "must supply a valid user_id" if user_id.to_s.empty?
97
+ user_exists?(user_id)
98
+
84
99
  begin
85
100
  response =
86
101
  @connection.patch("/v1/apps/#{@app_id}/users/#{user_id}/deactivate")
@@ -105,17 +120,24 @@ module Passage
105
120
  )
106
121
  rescue Faraday::Error => e
107
122
  if e.is_a? Faraday::ResourceNotFound
108
- raise PassageError,
109
- "passage User with ID \"#{user_id}\" does not exist"
123
+ raise PassageError.new(
124
+ message: "Passage User with ID \"#{user_id}\" does not exist",
125
+ status_code: e.response[:status],
126
+ body: e.response[:body]
127
+ )
110
128
  else
111
- raise PassageError,
112
- "failed to deactivate Passage User. Http Status: #{e.response[:status]}. Response: #{e.response[:body]["error"]}"
129
+ raise PassageError.new(
130
+ message: "failed to deactivate Passage User.",
131
+ status_code: e.response[:status],
132
+ body: e.response[:body]
133
+ )
113
134
  end
114
135
  end
115
136
  end
116
137
 
117
138
  def update(user_id:, email: "", phone: "", user_metadata: {})
118
- raise PassageError, "must supply a valid user_id" if user_id.to_s.empty?
139
+ user_exists?(user_id)
140
+
119
141
  updates = {}
120
142
  updates["email"] = email unless email.empty?
121
143
  updates["phone"] = phone unless phone.empty?
@@ -144,11 +166,17 @@ module Passage
144
166
  )
145
167
  rescue Faraday::Error => e
146
168
  if e.is_a? Faraday::ResourceNotFound
147
- raise PassageError,
148
- "passage User with ID \"#{user_id}\" does not exist"
169
+ raise PassageError.new(
170
+ message: "Passage User with ID \"#{user_id}\" does not exist",
171
+ status_code: e.response[:status],
172
+ body: e.response[:body]
173
+ )
149
174
  else
150
- raise PassageError,
151
- "failed to update Passage User. Http Status: #{e.response[:status]}. Response: #{e.response[:body]["error"]}"
175
+ raise PassageError.new(
176
+ "failed to update Passage User",
177
+ status_code: e.response[:status],
178
+ body: e.response[:body]
179
+ )
152
180
  end
153
181
  end
154
182
  end
@@ -180,32 +208,41 @@ module Passage
180
208
  )
181
209
  )
182
210
  rescue Faraday::Error => e
183
- raise PassageError,
184
- "failed to create Passage User. Http Status: #{e.response[:status]}. Response: #{e.response[:body]["error"]}"
211
+ raise PassageError.new(
212
+ "failed to create Passage User",
213
+ status_code: e.response[:status],
214
+ body: e.response[:body]
215
+ )
185
216
  end
186
217
  end
187
218
 
188
219
  def delete(user_id:)
189
- raise PassageError, "must supply a valid user_id" if user_id.to_s.empty?
220
+ user_exists?(user_id)
221
+
190
222
  begin
191
223
  response = @connection.delete("/v1/apps/#{@app_id}/users/#{user_id}")
192
224
  return true
193
225
  rescue Faraday::Error => e
194
226
  if e.is_a? Faraday::ResourceNotFound
195
- raise PassageError,
196
- "passage User with ID \"#{user_id}\" does not exist"
227
+ raise PassageError.new(
228
+ "passage User with ID \"#{user_id}\" does not exist",
229
+ status_code: e.response[:status],
230
+ body: e.response[:body]
231
+ )
197
232
  else
198
- raise PassageError,
199
- "failed to delete Passage User. Http Status: #{e.response[:status]}. Response: #{e.response[:body]["error"]}"
233
+ raise PassageError.new(
234
+ "failed to delete Passage User",
235
+ status_code: e.response[:status],
236
+ body: e.response[:body]
237
+ )
200
238
  end
201
239
  end
202
240
  end
203
241
 
204
242
  def delete_device(user_id:, device_id:)
205
- raise PassageError, "must supply a valid user_id" if user_id.to_s.empty?
206
- if device_id.to_s.empty?
207
- raise PassageError, "must supply a valid device_id"
208
- end
243
+ user_exists?(user_id)
244
+ device_exists?(device_id)
245
+
209
246
  begin
210
247
  response =
211
248
  @connection.delete(
@@ -213,13 +250,17 @@ module Passage
213
250
  )
214
251
  return true
215
252
  rescue Faraday::Error => e
216
- raise PassageError,
217
- "failed to delete Passage User Device. Http Status: #{e.response[:status]}. Response: #{e.response[:body]["error"]}"
253
+ raise PassageError.new(
254
+ "failed to delete Passage User Device",
255
+ status_code: e.response[:status],
256
+ body: e.response[:body]
257
+ )
218
258
  end
219
259
  end
220
260
 
221
261
  def list_devices(user_id:)
222
- raise PassageError, "must supply a valid user_id" if user_id.to_s.empty?
262
+ user_exists?(user_id)
263
+
223
264
  begin
224
265
  response =
225
266
  @connection.get("/v1/apps/#{@app_id}/users/#{user_id}/devices")
@@ -240,20 +281,40 @@ module Passage
240
281
  end
241
282
  return devices
242
283
  rescue Faraday::Error => e
243
- raise PassageError,
244
- "failed to delete Passage User Device. Http Status: #{e.response[:status]}. Response: #{e.response[:body]["error"]}"
284
+ raise PassageError.new(
285
+ "failed to delete Passage User Device",
286
+ status_code: e.response[:status],
287
+ body: e.response[:body]
288
+ )
245
289
  end
246
290
  end
247
291
 
248
292
  def signout(user_id:)
249
- raise PassageError, "must supply a valid user_id" if user_id.to_s.empty?
293
+ user_exists?(user_id)
250
294
  begin
251
295
  response =
252
296
  @connection.delete("/v1/apps/#{@app_id}/users/#{user_id}/tokens/")
253
297
  return true
254
298
  rescue Faraday::Error => e
255
- raise PassageError,
256
- "failed to revoke user's refresh tokens. Http Status: #{e.response[:status]}. Response: #{e.response[:body]["error"]}"
299
+ raise PassageError.new(
300
+ "failed to revoke user's refresh tokens",
301
+ status_code: e.response[:status],
302
+ body: e.response[:body]
303
+ )
304
+ end
305
+ end
306
+
307
+ private
308
+
309
+ def user_exists?(user_id)
310
+ if user_id.to_s.empty?
311
+ raise PassageError.new(message: "must supply a valid user_id")
312
+ end
313
+ end
314
+
315
+ def device_exists?(device_id)
316
+ if device_id.to_s.empty?
317
+ raise PassageError.new(message: "must supply a valid device_id")
257
318
  end
258
319
  end
259
320
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'passageidentity'
3
- s.version = '0.1.2'
3
+ s.version = '0.2.0'
4
4
  s.summary = 'Passage SDK for biometric authentication'
5
5
  s.description =
6
6
  'Enables verification of server-side authentication and user management for applications using Passage'
@@ -0,0 +1,30 @@
1
+ require_relative "../lib/passageidentity/error"
2
+ require "test/unit"
3
+ require "net/http"
4
+
5
+ class ErrorTest < Test::Unit::TestCase
6
+ def test_initialize
7
+ body = { "error" => "some error" }
8
+
9
+ error =
10
+ Passage::PassageError.new(
11
+ message: "some message",
12
+ status_code: 400,
13
+ body: body
14
+ )
15
+
16
+ assert_equal error.message, "some message"
17
+ assert_equal error.error, "some error"
18
+ assert_equal error.status_code, 400
19
+ assert_equal error.status_text, Net::HTTPBadRequest
20
+ end
21
+
22
+ def test_initialize_message_only
23
+ error = Passage::PassageError.new(message: "some message")
24
+
25
+ assert_equal error.message, "some message"
26
+ assert_equal error.error, nil
27
+ assert_equal error.status_code, nil
28
+ assert_equal error.status_text, nil
29
+ end
30
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: passageidentity
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Passage Identity
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-09-14 00:00:00.000000000 Z
11
+ date: 2022-09-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -94,6 +94,7 @@ files:
94
94
  - tests/all.rb
95
95
  - tests/app_test.rb
96
96
  - tests/auth_test.rb
97
+ - tests/errors_test.rb
97
98
  - tests/magic_link_test.rb
98
99
  - tests/user_api_test.rb
99
100
  homepage: https://rubygems.org/gems/passageidentity