From 3f6fb304d8a8deffe27836e71822080e7e507870 Mon Sep 17 00:00:00 2001 From: Chris Tran Date: Mon, 9 Dec 2024 21:25:35 +0000 Subject: [PATCH] feat: add parameter guards --- lib/passageidentity/auth.rb | 10 +----- lib/passageidentity/user_api.rb | 61 ++++++++------------------------- 2 files changed, 15 insertions(+), 56 deletions(-) diff --git a/lib/passageidentity/auth.rb b/lib/passageidentity/auth.rb index d8657ab..8cf4b6d 100644 --- a/lib/passageidentity/auth.rb +++ b/lib/passageidentity/auth.rb @@ -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( diff --git a/lib/passageidentity/user_api.rb b/lib/passageidentity/user_api.rb index fb92a97..22cd395 100644 --- a/lib/passageidentity/user_api.rb +++ b/lib/passageidentity/user_api.rb @@ -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) @@ -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 @@ -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) @@ -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) @@ -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? @@ -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) @@ -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) @@ -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) @@ -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 @@ -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 @@ -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 @@ -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)