Skip to content

Commit 92e11ae

Browse files
[SDK-4014] User Authentication Method management API support (#450)
1 parent da8be6a commit 92e11ae

File tree

3 files changed

+335
-1
lines changed

3 files changed

+335
-1
lines changed

.circleci/config.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
version: 2.1
22
orbs:
3-
ship: auth0/ship@dev:alpha
3+
ship: auth0/ship@0
44
codecov: codecov/codecov@3
55

66
matrix_ruby_versions: &matrix_ruby_versions

lib/auth0/api/v2/users.rb

+116
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,122 @@ def get_user_organizations(user_id)
329329
get "#{users_path}/#{user_id}/organizations"
330330
end
331331

332+
# Get the available authentication methods for a user.
333+
#
334+
# @param user_id [string] The user ID of the authentication methods to get
335+
# @param options [hash] A hash of options for getting permissions
336+
# * :per_page [integer] The amount of permissions per page. (optional)
337+
# * :page [integer] The page number. Zero based. (optional)
338+
# * :include_totals [boolean] True if a query summary must be included in the result. (optional)
339+
# @return [json] The user's authentication methods
340+
# @see https://auth0.com/docs/api/management/v2#!/Users/get_authentication_methods
341+
def user_authentication_methods(user_id, options = {})
342+
raise Auth0::MissingUserId, 'Must supply a valid user_id' if user_id.to_s.empty?
343+
344+
request_params = {
345+
per_page: options.fetch(:per_page, nil),
346+
page: options.fetch(:page, nil),
347+
include_totals: options.fetch(:include_totals, nil)
348+
}
349+
350+
get "#{users_path}/#{user_id}/authentication-methods", request_params
351+
end
352+
alias get_user_authentication_methods user_authentication_methods
353+
354+
# Get a specific authentication method for a user.
355+
#
356+
# @param user_id [string] The user ID of the authentication methods to get
357+
# @param authentication_method_id [string] The ID of the authentication method
358+
# @return [json] The user authentication method
359+
# @see https://auth0.com/docs/api/management/v2#!/Users/get_authentication_methods_by_authentication_method_id
360+
def user_authentication_method(user_id, authentication_method_id)
361+
raise Auth0::MissingUserId, 'Must supply a valid user_id' if user_id.to_s.empty?
362+
raise Auth0::MissingParameter, 'Must supply a valid authentication_method_id' if authentication_method_id.to_s.empty?
363+
364+
get "#{users_path}/#{user_id}/authentication-methods/#{authentication_method_id}"
365+
end
366+
alias get_user_authentication_method user_authentication_method
367+
368+
# Create an authentication method for a user
369+
#
370+
# @param user_id [string] The user ID of the authentication methods to get
371+
# @param body [hash] The post body content
372+
# * :type [string] "phone" or "email" or "totp" or "webauthn-roaming"
373+
# * :name [string] A human-readable label to identify the authentication method (optional)
374+
# * :totp_secret [string] Base32 encoded secret for TOTP generation (optional)
375+
# * :phone_number [string] Applies to phone authentication methods only. The destination phone number used to send verification codes via text and voice (optional)
376+
# * :email [string] Applies to email authentication methods only. The email address used to send verification messages (optional)
377+
# * :preferred_authentication_method [string] Preferred phone authentication method (optional)
378+
# * :key_id [string] Applies to email webauthn authenticators only. The id of the credential (optional)
379+
# * :public_key [string] Applies to email webauthn authenticators only. The public key (optional)
380+
# * :relying_party_identifier [string] Applies to email webauthn authenticators only. The relying party identifier (optional)
381+
# @see https://auth0.com/docs/api/management/v2#!/Users/post_authentication_methods
382+
def post_user_authentication_method(user_id, body)
383+
raise Auth0::MissingUserId, 'Must supply a valid user_id' if user_id.to_s.empty?
384+
raise Auth0::MissingParameter, 'Must supply a body' if body.to_s.empty?
385+
386+
post "#{users_path}/#{user_id}/authentication-methods", body
387+
end
388+
alias create_user_authentication_method post_user_authentication_method
389+
390+
# Updates all authentication methods by replacing them with the given ones
391+
#
392+
# @param user_id [string] The user ID of the authentication methods to get
393+
# @param body [hash array] The mehods to update
394+
# * :type [string] "phone" or "email" or "totp" or "webauthn-roaming"
395+
# * :name [string] A human-readable label to identify the authentication method (optional)
396+
# * :totp_secret [string] Base32 encoded secret for TOTP generation (optional)
397+
# * :phone_number [string] Applies to phone authentication methods only. The destination phone number used to send verification codes via text and voice (optional)
398+
# * :email [string] Applies to email authentication methods only. The email address used to send verification messages (optional)
399+
# * :preferred_authentication_method [string] Preferred phone authentication method (optional)
400+
# @see https://auth0.com/docs/api/management/v2#!/Users/put_authentication_methods
401+
def put_all_user_authentication_methods(user_id, body)
402+
raise Auth0::MissingUserId, 'Must supply a valid user_id' if user_id.to_s.empty?
403+
raise Auth0::MissingParameter, 'Must supply a body' if body.to_s.empty?
404+
405+
put "#{users_path}/#{user_id}/authentication-methods", body
406+
end
407+
alias update_all_user_authentication_methods put_all_user_authentication_methods
408+
409+
# Updates a user authentication method
410+
#
411+
# @param user_id [string] The user ID of the authentication methods to get
412+
# @param body [hash array] The mehods to update
413+
# * :name [string] A human-readable label to identify the authentication method (optional)
414+
# * :preferred_authentication_method [string] Preferred phone authentication method (optional)
415+
# @see https://auth0.com/docs/api/management/v2#!/Users/put_authentication_methods
416+
def patch_user_authentication_method(user_id, authentication_method_id, body)
417+
raise Auth0::MissingUserId, 'Must supply a valid user_id' if user_id.to_s.empty?
418+
raise Auth0::MissingParameter, 'Must supply an authentication_method_id' if authentication_method_id.to_s.empty?
419+
raise Auth0::MissingParameter, 'Must supply a body' if body.to_s.empty?
420+
421+
patch "#{users_path}/#{user_id}/authentication-methods/#{authentication_method_id}", body
422+
end
423+
alias update_user_authentication_method patch_user_authentication_method
424+
425+
# Deletes all of the user's authentication methods
426+
#
427+
# @param user_id [string] The user ID
428+
# @see https://auth0.com/docs/api/management/v2#!/Users/delete_authentication_methods
429+
def delete_user_authentication_methods(user_id)
430+
raise Auth0::MissingUserId, 'Must supply a valid user_id' if user_id.to_s.empty?
431+
432+
delete "#{users_path}/#{user_id}/authentication-methods"
433+
end
434+
435+
436+
# Deletes the user's authentication method specified by authentication_method_id
437+
#
438+
# @param user_id [string] The user ID
439+
# @param authentication_method_id [string] The ID of the authentication method
440+
# @see https://auth0.com/docs/api/management/v2#!/Users/delete_authentication_methods_by_authentication_method_id
441+
def delete_user_authentication_method(user_id, authentication_method_id)
442+
raise Auth0::MissingUserId, 'Must supply a valid user_id' if user_id.to_s.empty?
443+
raise Auth0::MissingParameter, 'Must supply an authentication_method_id' if authentication_method_id.to_s.empty?
444+
445+
delete "#{users_path}/#{user_id}/authentication-methods/#{authentication_method_id}"
446+
end
447+
332448
private
333449

334450
# Users API path

spec/lib/auth0/api/v2/users_spec.rb

+218
Original file line numberDiff line numberDiff line change
@@ -583,4 +583,222 @@
583583
end.not_to raise_error
584584
end
585585
end
586+
587+
context '.get_user_authentication_methods' do
588+
it 'is expected to respond to user_authentication_methods method' do
589+
expect(@instance).to respond_to(:user_authentication_methods)
590+
end
591+
592+
it 'is expected to respond to get_user_authentication_methods method' do
593+
expect(@instance).to respond_to(:get_user_authentication_methods)
594+
end
595+
596+
it 'is expected to raise an exception when the user ID is empty' do
597+
expect { @instance.user_authentication_methods(nil) }.to raise_exception(Auth0::MissingUserId)
598+
end
599+
600+
it 'is expected to get user authentication methods' do
601+
expect(@instance).to receive(:get).with(
602+
'/api/v2/users/USER_ID/authentication-methods', {
603+
per_page: nil,
604+
page: nil,
605+
include_totals: nil
606+
}
607+
)
608+
609+
expect do
610+
@instance.user_authentication_methods('USER_ID')
611+
end.not_to raise_error
612+
end
613+
614+
it 'is expected to get user authentication methods with paging' do
615+
expect(@instance).to receive(:get).with(
616+
'/api/v2/users/USER_ID/authentication-methods', {
617+
per_page: 1,
618+
page: 2,
619+
include_totals: true
620+
}
621+
)
622+
623+
expect do
624+
@instance.user_authentication_methods('USER_ID', per_page: 1, page: 2, include_totals: true)
625+
end.not_to raise_error
626+
end
627+
end
628+
629+
context '.get_user_authentication_method' do
630+
it 'is expected to respond to get_user_authentication_method' do
631+
expect(@instance).to respond_to :user_authentication_method
632+
end
633+
634+
it 'is expected to respond to get_user_authentication_method' do
635+
expect(@instance).to respond_to :get_user_authentication_method
636+
end
637+
638+
it 'is expected to raise an exception for a missing user ID' do
639+
expect { @instance.user_authentication_method(nil, nil) }.to raise_exception(Auth0::MissingUserId)
640+
end
641+
642+
it 'is expected to raise an exception for a missing authentication method ID' do
643+
expect { @instance.user_authentication_method('USER_ID', nil) }.to raise_exception(Auth0::MissingParameter)
644+
end
645+
646+
it 'is expected to GET a user authentication method' do
647+
expect(@instance).to receive(:get).with(
648+
'/api/v2/users/USER_ID/authentication-methods/AUTH_METHOD_ID'
649+
)
650+
651+
expect do
652+
@instance.user_authentication_method('USER_ID', 'AUTH_METHOD_ID')
653+
end.not_to raise_error
654+
655+
end
656+
end
657+
658+
context '.create_user_authentication_method' do
659+
it 'is expected to respond to create_user_authentication_method' do
660+
expect(@instance).to respond_to :create_user_authentication_method
661+
end
662+
663+
it 'is expected to respond to post_user_authentication_method' do
664+
expect(@instance).to respond_to :post_user_authentication_method
665+
end
666+
667+
it 'is expected to raise an exception for a missing user ID' do
668+
expect { @instance.create_user_authentication_method(nil, nil) }.to raise_exception(Auth0::MissingUserId)
669+
end
670+
671+
it 'is expected to raise an exception for a missing body' do
672+
expect { @instance.create_user_authentication_method('USER_ID', nil) }.to raise_exception(Auth0::MissingParameter)
673+
end
674+
675+
it 'is expected to send the body to the endpoint' do
676+
body = {
677+
type: 'phone'
678+
}
679+
680+
expect(@instance).to receive(:post).with(
681+
'/api/v2/users/USER_ID/authentication-methods',
682+
body
683+
)
684+
685+
expect do
686+
@instance.create_user_authentication_method 'USER_ID', body
687+
end.not_to raise_error
688+
end
689+
end
690+
691+
context '.put_all_user_authentication_methods' do
692+
it 'is expected to respond to put_all_user_authentication_methods' do
693+
expect(@instance).to respond_to(:put_all_user_authentication_methods)
694+
end
695+
696+
it 'is expected to respond to update_all_user_authentication_methods' do
697+
expect(@instance).to respond_to(:update_all_user_authentication_methods)
698+
end
699+
700+
it 'is expected to raise an exception for a missing user ID' do
701+
expect { @instance.put_all_user_authentication_methods(nil, nil) }.to raise_exception(Auth0::MissingUserId)
702+
end
703+
704+
it 'is expected to raise an exception for a missing body' do
705+
expect { @instance.put_all_user_authentication_methods('USER_ID', nil) }.to raise_exception(Auth0::MissingParameter)
706+
end
707+
708+
it 'is expected to send the body to the endpoint' do
709+
body = {
710+
type: 'phone'
711+
}
712+
713+
expect(@instance).to receive(:put).with(
714+
'/api/v2/users/USER_ID/authentication-methods',
715+
[body]
716+
)
717+
718+
expect do
719+
@instance.put_all_user_authentication_methods 'USER_ID', [body]
720+
end.to_not raise_error
721+
end
722+
end
723+
724+
context '.patch_user_authentication_method' do
725+
it 'is expected to respond to patch_user_authentication_method' do
726+
expect(@instance).to respond_to(:patch_user_authentication_method)
727+
end
728+
729+
it 'is expected to respond to update_user_authentication_method' do
730+
expect(@instance).to respond_to(:update_user_authentication_method)
731+
end
732+
733+
it 'is expected to raise an exception for a missing user ID' do
734+
expect { @instance.patch_user_authentication_method(nil, nil, nil) }.to raise_exception(Auth0::MissingUserId)
735+
end
736+
737+
it 'is expected to raise an exception for a missing authentication_method_id' do
738+
expect { @instance.patch_user_authentication_method('USER_ID', nil, nil) }.to raise_exception(Auth0::MissingParameter)
739+
end
740+
741+
it 'is expected to raise an exception for a missing body' do
742+
expect { @instance.patch_user_authentication_method('USER_ID', 'AUTH_METHOD_ID', nil) }.to raise_exception(Auth0::MissingParameter)
743+
end
744+
745+
it 'is expected to send the body to the endpoint' do
746+
body = {
747+
name: 'auth method name'
748+
}
749+
750+
expect(@instance).to receive(:patch).with(
751+
'/api/v2/users/USER_ID/authentication-methods/AUTH_METHOD_ID',
752+
body
753+
)
754+
755+
expect do
756+
@instance.patch_user_authentication_method 'USER_ID', 'AUTH_METHOD_ID', body
757+
end.to_not raise_error
758+
end
759+
end
760+
761+
context '.delete_user_authentication_methods' do
762+
it 'is expected to respond to delete_user_authentication_methods' do
763+
expect(@instance).to respond_to(:delete_user_authentication_methods)
764+
end
765+
766+
it 'is expected to raise an exception for a missing user ID' do
767+
expect { @instance.delete_user_authentication_methods(nil) }.to raise_exception(Auth0::MissingUserId)
768+
end
769+
770+
it 'is expected to call the endpoint' do
771+
expect(@instance).to receive(:delete).with(
772+
'/api/v2/users/USER_ID/authentication-methods'
773+
)
774+
775+
expect do
776+
@instance.delete_user_authentication_methods 'USER_ID'
777+
end.to_not raise_error
778+
end
779+
end
780+
781+
context '.delete_user_authentication_method' do
782+
it 'is expected to respond to delete_user_authentication_method' do
783+
expect(@instance).to respond_to(:delete_user_authentication_method)
784+
end
785+
786+
it 'is expected to raise an exception for a missing user ID' do
787+
expect { @instance.delete_user_authentication_method(nil, nil) }.to raise_exception(Auth0::MissingUserId)
788+
end
789+
790+
it 'is expected to raise an exception for a missing authentication_method_id' do
791+
expect { @instance.delete_user_authentication_method('USER_ID', nil) }.to raise_exception(Auth0::MissingParameter)
792+
end
793+
794+
it 'is expected to call the endpoint' do
795+
expect(@instance).to receive(:delete).with(
796+
'/api/v2/users/USER_ID/authentication-methods/AUTH_METHOD_ID'
797+
)
798+
799+
expect do
800+
@instance.delete_user_authentication_method 'USER_ID', 'AUTH_METHOD_ID'
801+
end.to_not raise_error
802+
end
803+
end
586804
end

0 commit comments

Comments
 (0)