Skip to content

Commit

Permalink
feat: add parameter guards
Browse files Browse the repository at this point in the history
  • Loading branch information
ctran88 committed Dec 9, 2024
1 parent 34df698 commit 3f6fb30
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 56 deletions.
10 changes: 1 addition & 9 deletions lib/passageidentity/auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,7 @@ def authenticate_request(request)
end

def validate_jwt(token)
if token.nil?
raise PassageError.new(
status_code: 400,
body: {
error: 'no authentication token',
code: 'missing_auth_token'
}
)
end
raise ArgumentError, 'jwt is required.' unless token && !token.empty?

unless get_cache(@app_id)
raise PassageError.new(
Expand Down
61 changes: 14 additions & 47 deletions lib/passageidentity/user_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def initialize(app_id, api_key)
end

def get(user_id:)
user_exists?(user_id)
raise ArgumentError, 'user_id is required.' unless user_id && !user_id.empty?

begin
response = @user_client.get_user(@app_id, user_id, @req_opts)
Expand All @@ -39,7 +39,7 @@ def get(user_id:)
end

def get_by_identifier(user_identifier:)
identifier_exists?(user_identifier)
raise ArgumentError, 'identifier is required.' unless user_identifier && !user_identifier.empty?

begin
@req_opts[:limit] = 1
Expand All @@ -66,7 +66,7 @@ def get_by_identifier(user_identifier:)
end

def activate(user_id:)
user_exists?(user_id)
raise ArgumentError, 'user_id is required.' unless user_id && !user_id.empty?

begin
response = @user_client.activate_user(@app_id, user_id, @req_opts)
Expand All @@ -80,7 +80,7 @@ def activate(user_id:)
end

def deactivate(user_id:)
user_exists?(user_id)
raise ArgumentError, 'user_id is required.' unless user_id && !user_id.empty?

begin
response = @user_client.deactivate_user(@app_id, user_id, @req_opts)
Expand All @@ -97,8 +97,6 @@ def update(user_id:, email: '', phone: '', user_metadata: {})
warn '[DEPRECATED] the `update` method parameters will change to `user_id: string, ' \
'options: UpdateUserArgs`. Parameters will change on or after 2025-1.'

user_exists?(user_id)

updates = {}
updates['email'] = email unless email.empty?
updates['phone'] = phone unless phone.empty?
Expand All @@ -120,7 +118,7 @@ def create(email: '', phone: '', user_metadata: {})
end

def delete(user_id:)
user_exists?(user_id)
raise ArgumentError, 'user_id is required.' unless user_id && !user_id.empty?

begin
@user_client.delete_user(@app_id, user_id, @req_opts)
Expand All @@ -135,8 +133,8 @@ def delete(user_id:)
end

def revoke_device(user_id:, device_id:)
user_exists?(user_id)
device_exists?(device_id)
raise ArgumentError, 'user_id is required.' unless user_id && !user_id.empty?
raise ArgumentError, 'device_id is required.' unless device_id && !device_id.empty?

begin
@user_device_client.delete_user_devices(@app_id, user_id, device_id, @req_opts)
Expand All @@ -154,7 +152,7 @@ def delete_device(user_id:, device_id:)
end

def list_devices(user_id:)
user_exists?(user_id)
raise ArgumentError, 'user_id is required.' unless user_id && !user_id.empty?

begin
response = @user_device_client.list_user_devices(@app_id, user_id, @req_opts)
Expand All @@ -173,7 +171,7 @@ def signout(user_id:)
end

def revoke_refresh_tokens(user_id:)
user_exists?(user_id)
raise ArgumentError, 'user_id is required.' unless user_id && !user_id.empty?

begin
tokens_client = OpenapiClient::TokensApi.new
Expand All @@ -189,6 +187,8 @@ def revoke_refresh_tokens(user_id:)
private

def create_v2(args: {})
raise ArgumentError, 'At least one of args.email or args.phone is required.' unless args['phone'] || args['email']

response = @user_client.create_user(@app_id, args, @req_opts)
response.user
rescue Faraday::Error => e
Expand All @@ -199,6 +199,9 @@ def create_v2(args: {})
end

def update_v2(user_id:, options: {})
raise ArgumentError, 'user_id is required.' unless user_id && !user_id.empty?
raise ArgumentError, 'options are required.' if options.empty?

response = @user_client.update_user(@app_id, user_id, options, @req_opts)
response.user
rescue Faraday::Error => e
Expand All @@ -207,42 +210,6 @@ def update_v2(user_id:, options: {})
body: e.response[:body]
)
end

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

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

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

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

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

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

deprecate(:signout, :revoke_refresh_tokens, 2025, 1)
Expand Down

0 comments on commit 3f6fb30

Please sign in to comment.