Skip to content

Commit

Permalink
refactor: PassageError messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanessa Burroughs committed Dec 3, 2024
1 parent 7871844 commit 4cd352d
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 118 deletions.
62 changes: 42 additions & 20 deletions lib/passageidentity/auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,25 @@ def authenticate_request(request)
if @auth_strategy == Passage::COOKIE_STRATEGY
unless request.cookies.key?('psg_auth_token')
raise PassageError.new(
message:
'missing authentication token: expected "psg_auth_token" cookie'
status_code: 400,
body: {
error: 'missing authentication token: expected "psg_auth_token" cookie',
code: 403
}
)
end
@token = request.cookies['psg_auth_token']
else
headers = request.headers
raise PassageError.new(message: 'no authentication token in header') unless headers.key?('Authorization')
unless headers.key?('Authorization')
raise PassageError.new(
status_code: 400,
body: {
error: 'no authentication token in header',
code: 403
}
)
end

@token = headers['Authorization'].split(' ').last
end
Expand All @@ -52,7 +63,13 @@ def authenticate_request(request)
def validate_jwt(token)
return authenticate_token(token) if token

raise PassageError.new(message: 'no authentication token')
raise PassageError.new(
status_code: 400,
body: {
error: 'no authentication token',
code: 403
}
)
end

def revoke_user_refresh_tokens(user_id)
Expand All @@ -65,7 +82,6 @@ def revoke_user_refresh_tokens(user_id)
true
rescue Faraday::Error => e
raise PassageError.new(
message: "failed to revoke user's refresh tokens",
status_code: e.response[:status],
body: e.response[:body]
)
Expand All @@ -92,8 +108,11 @@ def create_magic_link(
# check to see if the channel specified is valid before sending it off to the server
unless [PHONE_CHANNEL, EMAIL_CHANNEL].include? channel
raise PassageError.new(
message:
'channel: must be either Passage::EMAIL_CHANNEL or Passage::PHONE_CHANNEL'
status_code: 400,
body: {
error: 'channel: must be either Passage::EMAIL_CHANNEL or Passage::PHONE_CHANNEL',
code: 400
}
)
end
magic_link_req['channel'] = channel unless channel.empty?
Expand All @@ -113,7 +132,6 @@ def create_magic_link(
client.create_magic_link(@app_id, magic_link_req, @req_opts).magic_link
rescue Faraday::Error => e
raise PassageError.new(
message: 'failed to create Passage Magic Link',
status_code: e.response[:status],
body: e.response[:body]
)
Expand All @@ -128,7 +146,6 @@ def fetch_app
response.app
rescue Faraday::Error => e
raise PassageError.new(
message: 'failed to fetch passage app',
status_code: e.response[:status],
body: e.response[:body]
)
Expand Down Expand Up @@ -183,24 +200,29 @@ def authenticate_token(token)
}
)
claims[0]['sub']
rescue JWT::InvalidIssuerError => e
raise PassageError.new(message: e.message)
rescue JWT::InvalidAudError => e
raise PassageError.new(message: e.message)
rescue JWT::ExpiredSignature => e
raise PassageError.new(message: e.message)
rescue JWT::IncorrectAlgorithm => e
raise PassageError.new(message: e.message)
rescue JWT::DecodeError => e
raise PassageError.new(message: e.message)
rescue JWT::InvalidIssuerError, JWT::InvalidAudError, JWT::ExpiredSignature, JWT::IncorrectAlgorithm,
JWT::DecodeError => e
raise PassageError.new(
status_code: 400,
body: {
error: e.message,
code: 400
}
)
end

private

def user_exists?(user_id)
return unless user_id.to_s.empty?

raise PassageError.new(message: 'must supply a valid user_id')
raise PassageError.new(
status_code: 400,
body: {
error: 'Must supply a valid user_id',
code: 400
}
)
end

def get_cache(key)
Expand Down
8 changes: 7 additions & 1 deletion lib/passageidentity/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ def initialize(app_id:, api_key: '', auth_strategy: COOKIE_STRATEGY)

# check for valid auth strategy
unless [COOKIE_STRATEGY, HEADER_STRATEGY].include? auth_strategy
raise PassageError.new(message: 'invalid auth strategy.')
raise PassageError.new(
status_code: 400,
body: {
error: 'Invalid auth strategy',
code: 400
}
)
end

@auth_strategy = auth_strategy
Expand Down
137 changes: 50 additions & 87 deletions lib/passageidentity/user_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,10 @@ def get(user_id:)
response = @user_client.get_user(@app_id, user_id, @req_opts)
response.user
rescue Faraday::Error => e
if e.is_a? Faraday::ResourceNotFound
raise PassageError.new(
message: "Passage User with ID \"#{user_id}\" does not exist",
status_code: e.response[:status],
body: e.response[:body]
)
else
raise PassageError.new(
message: 'failed to get Passage User.',
status_code: e.response[:status],
body: e.response[:body]
)
end
raise PassageError.new(
status_code: e.response[:status],
body: e.response[:body]
)
end
end

Expand All @@ -58,26 +49,19 @@ def get_by_identifier(user_identifier:)

if users.empty?
raise PassageError.new(
message: "Passage User with identifer \"#{user_identifier}\" does not exist",
status_code: 404,
body: 'user_not_found'
body: {
error: "Passage User with identifer \"#{user_identifier}\" does not exist",
code: 404
}
)
end
get(user_id: users.first.id)
rescue Faraday::Error => e
if e.is_a? Faraday::ResourceNotFound
raise PassageError.new(
message: "Passage User with identifer \"#{user_identifier}\" does not exist",
status_code: e.response[:status],
body: e.response[:body]
)
else
raise PassageError.new(
message: 'failed to get Passage User.',
status_code: e.response[:status],
body: e.response[:body]
)
end
raise PassageError.new(
status_code: e.response[:status],
body: e.response[:body]
)
end
end

Expand All @@ -88,19 +72,10 @@ def activate(user_id:)
response = @user_client.activate_user(@app_id, user_id, @req_opts)
response.user
rescue Faraday::Error => e
if e.is_a? Faraday::ResourceNotFound
raise PassageError.new(
message: "Passage User with ID \"#{user_id}\" does not exist",
status_code: e.response[:status],
body: e.response[:body]
)
else
raise PassageError.new(
message: 'failed to activate Passage User.',
status_code: e.response[:status],
body: e.response[:body]
)
end
raise PassageError.new(
status_code: e.response[:status],
body: e.response[:body]
)
end
end

Expand All @@ -111,19 +86,10 @@ def deactivate(user_id:)
response = @user_client.deactivate_user(@app_id, user_id, @req_opts)
response.user
rescue Faraday::Error => e
if e.is_a? Faraday::ResourceNotFound
raise PassageError.new(
message: "Passage User with ID \"#{user_id}\" does not exist",
status_code: e.response[:status],
body: e.response[:body]
)
else
raise PassageError.new(
message: 'failed to deactivate Passage User.',
status_code: e.response[:status],
body: e.response[:body]
)
end
raise PassageError.new(
status_code: e.response[:status],
body: e.response[:body]
)
end
end

Expand Down Expand Up @@ -160,19 +126,11 @@ def delete(user_id:)
@user_client.delete_user(@app_id, user_id, @req_opts)
true
rescue Faraday::Error => e
if e.is_a? Faraday::ResourceNotFound
raise PassageError.new(
"passage User with ID \"#{user_id}\" does not exist",
status_code: e.response[:status],
body: e.response[:body]
)
else
raise PassageError.new(
'failed to delete Passage User',
status_code: e.response[:status],
body: e.response[:body]
)
end
raise PassageError.new(
'failed to delete Passage User',
status_code: e.response[:status],
body: e.response[:body]
)
end
end

Expand All @@ -185,7 +143,6 @@ def revoke_device(user_id:, device_id:)
true
rescue Faraday::Error => e
raise PassageError.new(
'failed to delete Passage User Device',
status_code: e.response[:status],
body: e.response[:body]
)
Expand All @@ -204,7 +161,6 @@ def list_devices(user_id:)
response.devices
rescue Faraday::Error => e
raise PassageError.new(
'failed to delete Passage User Device',
status_code: e.response[:status],
body: e.response[:body]
)
Expand All @@ -224,7 +180,6 @@ def revoke_refresh_tokens(user_id:)
true
rescue Faraday::Error => e
raise PassageError.new(
"failed to revoke user's refresh tokens",
status_code: e.response[:status],
body: e.response[:body]
)
Expand All @@ -242,7 +197,6 @@ def create_v2(args: {})
response.user
rescue Faraday::Error => e
raise PassageError.new(
'failed to create Passage User',
status_code: e.response[:status],
body: e.response[:body]
)
Expand All @@ -259,38 +213,47 @@ def update_v2(user_id:, options: {})
response = @user_client.update_user(@app_id, user_id, options, @req_opts)
response.user
rescue Faraday::Error => e
if e.is_a? Faraday::ResourceNotFound
raise PassageError.new(
message: "Passage User with ID \"#{user_id}\" does not exist",
status_code: e.response[:status],
body: e.response[:body]
)
else
raise PassageError.new(
'failed to update Passage User',
status_code: e.response[:status],
body: e.response[:body]
)
end
raise PassageError.new(
status_code: e.response[:status],
body: e.response[:body]
)
end
end

def user_exists?(user_id)
return unless user_id.to_s.empty?

raise PassageError.new(message: 'must supply a valid user_id')
raise PassageError.new(
status_code: 404,
body: {
error: 'must supply a valid user_id',
code: 404
}
)
end

def identifier_exists?(identifier)
return unless identifier.to_s.empty?

raise PassageError.new(message: 'must supply a valid identifier')
raise PassageError.new(
status_code: 400,
body: {
error: 'must supply a valid identifier',
code: 400
}
)
end

def device_exists?(device_id)
return unless device_id.to_s.empty?

raise PassageError.new(message: 'must supply a valid device_id')
raise PassageError.new(
status_code: 400,
body: {
error: 'must supply a valid device_id',
code: 400
}
)
end
# rubocop:enable Metrics/AbcSize

Expand Down
4 changes: 2 additions & 2 deletions tests/app_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ def test_get_app
def test_create_magic_link
magic_link =
PassageClient.create_magic_link(
email: 'chris@passage.id',
email: 'passage@passage.id',
channel: Passage::EMAIL_CHANNEL,
ttl: 122
)

assert_equal 122, magic_link.ttl
assert_equal 'chris@passage.id', magic_link.identifier
assert_equal 'passage@passage.id', magic_link.identifier
end
end
Loading

0 comments on commit 4cd352d

Please sign in to comment.