Skip to content

Commit 1a1b975

Browse files
authored
[SDK-4011] Add API2 Factor Management Endpoints (#476)
2 parents fd06a18 + 489cfc4 commit 1a1b975

File tree

2 files changed

+170
-0
lines changed

2 files changed

+170
-0
lines changed

auth0/management/users.py

+92
Original file line numberDiff line numberDiff line change
@@ -419,3 +419,95 @@ def invalidate_remembered_browsers(self, user_id):
419419

420420
url = self._url(f"{user_id}/multifactor/actions/invalidate-remember-browser")
421421
return self.client.post(url)
422+
423+
def get_authentication_methods(self, user_id):
424+
"""Gets a list of authentication methods
425+
426+
Args:
427+
user_id (str): The user_id to get a list of authentication methods for.
428+
429+
See: https://auth0.com/docs/api/management/v2#!/Users/get_authentication_methods
430+
"""
431+
432+
url = self._url(f"{user_id}/authentication-methods")
433+
return self.client.get(url)
434+
435+
def get_authentication_method_by_id(self, user_id, authentication_method_id):
436+
"""Gets an authentication method by ID.
437+
438+
Args:
439+
user_id (str): The user_id to get an authentication method by ID for.
440+
authentication_method_id (str): The authentication_method_id to get an authentication method by ID for.
441+
442+
See: https://auth0.com/docs/api/management/v2#!/Users/get_authentication_methods_by_authentication_method_id
443+
"""
444+
445+
url = self._url(f"{user_id}/authentication-methods/{authentication_method_id}")
446+
return self.client.get(url)
447+
448+
def create_authentication_method(self, user_id, body):
449+
"""Creates an authentication method for a given user.
450+
451+
Args:
452+
user_id (str): The user_id to create an authentication method for a given user.
453+
body (dict): the request body to create an authentication method for a given user.
454+
455+
See: https://auth0.com/docs/api/management/v2#!/Users/post_authentication_methods
456+
"""
457+
458+
url = self._url(f"{user_id}/authentication-methods")
459+
return self.client.post(url, data=body)
460+
461+
def update_authentication_methods(self, user_id, body):
462+
"""Updates all authentication methods for a user by replacing them with the given ones.
463+
464+
Args:
465+
user_id (str): The user_id to update all authentication methods for.
466+
body (dict): the request body to update all authentication methods with.
467+
468+
See: https://auth0.com/docs/api/management/v2#!/Users/put_authentication_methods
469+
"""
470+
471+
url = self._url(f"{user_id}/authentication-methods")
472+
return self.client.put(url, data=body)
473+
474+
def update_authentication_method_by_id(
475+
self, user_id, authentication_method_id, body
476+
):
477+
"""Updates an authentication method.
478+
479+
Args:
480+
user_id (str): The user_id to update an authentication method.
481+
authentication_method_id (str): The authentication_method_id to update an authentication method for.
482+
body (dict): the request body to update an authentication method.
483+
484+
See: https://auth0.com/docs/api/management/v2#!/Users/patch_authentication_methods_by_authentication_method_id
485+
"""
486+
487+
url = self._url(f"{user_id}/authentication-methods/{authentication_method_id}")
488+
return self.client.patch(url, data=body)
489+
490+
def delete_authentication_methods(self, user_id):
491+
"""Deletes all authentication methods for the given user.
492+
493+
Args:
494+
user_id (str): The user_id to delete all authentication methods for the given user for.
495+
496+
See: https://auth0.com/docs/api/management/v2#!/Users/delete_authentication_methods
497+
"""
498+
499+
url = self._url(f"{user_id}/authentication-methods")
500+
return self.client.delete(url)
501+
502+
def delete_authentication_method_by_id(self, user_id, authentication_method_id):
503+
"""Deletes an authentication method by ID.
504+
505+
Args:
506+
user_id (str): The user_id to delete an authentication method by ID for.
507+
authentication_method_id (str): The authentication_method_id to delete an authentication method by ID for.
508+
509+
See: https://auth0.com/docs/api/management/v2#!/Users/delete_authentication_methods_by_authentication_method_id
510+
"""
511+
512+
url = self._url(f"{user_id}/authentication-methods/{authentication_method_id}")
513+
return self.client.delete(url)

auth0/test/management/test_users.py

+78
Original file line numberDiff line numberDiff line change
@@ -325,3 +325,81 @@ def test_invalidate_remembered_browsers(self, mock_rc):
325325
"https://domain/api/v2/users/user-id/multifactor/actions/invalidate-remember-browser",
326326
args[0],
327327
)
328+
329+
@mock.patch("auth0.management.users.RestClient")
330+
def test_get_authentication_methods(self, mock_rc):
331+
mock_instance = mock_rc.return_value
332+
333+
u = Users(domain="domain", token="jwttoken")
334+
u.get_authentication_methods("user_id")
335+
336+
mock_instance.get.assert_called_with(
337+
"https://domain/api/v2/users/user_id/authentication-methods"
338+
)
339+
340+
@mock.patch("auth0.management.users.RestClient")
341+
def test_get_authentication_method_by_id(self, mock_rc):
342+
mock_instance = mock_rc.return_value
343+
344+
u = Users(domain="domain", token="jwttoken")
345+
u.get_authentication_method_by_id("user_id", "authentication_method_id")
346+
347+
mock_instance.get.assert_called_with(
348+
"https://domain/api/v2/users/user_id/authentication-methods/authentication_method_id"
349+
)
350+
351+
@mock.patch("auth0.management.users.RestClient")
352+
def test_create_authentication_method(self, mock_rc):
353+
mock_instance = mock_rc.return_value
354+
355+
u = Users(domain="domain", token="jwttoken")
356+
u.create_authentication_method("user_id", {})
357+
358+
mock_instance.post.assert_called_with(
359+
"https://domain/api/v2/users/user_id/authentication-methods", data={}
360+
)
361+
362+
@mock.patch("auth0.management.users.RestClient")
363+
def test_update_authentication_methods(self, mock_rc):
364+
mock_instance = mock_rc.return_value
365+
366+
u = Users(domain="domain", token="jwttoken")
367+
u.update_authentication_methods("user_id", {})
368+
369+
mock_instance.put.assert_called_with(
370+
"https://domain/api/v2/users/user_id/authentication-methods", data={}
371+
)
372+
373+
@mock.patch("auth0.management.users.RestClient")
374+
def test_update_authentication_method_by_id(self, mock_rc):
375+
mock_instance = mock_rc.return_value
376+
377+
u = Users(domain="domain", token="jwttoken")
378+
u.update_authentication_method_by_id("user_id", "authentication_method_id", {})
379+
380+
mock_instance.patch.assert_called_with(
381+
"https://domain/api/v2/users/user_id/authentication-methods/authentication_method_id",
382+
data={},
383+
)
384+
385+
@mock.patch("auth0.management.users.RestClient")
386+
def test_delete_authentication_methods(self, mock_rc):
387+
mock_instance = mock_rc.return_value
388+
389+
u = Users(domain="domain", token="jwttoken")
390+
u.delete_authentication_methods("user_id")
391+
392+
mock_instance.delete.assert_called_with(
393+
"https://domain/api/v2/users/user_id/authentication-methods"
394+
)
395+
396+
@mock.patch("auth0.management.users.RestClient")
397+
def test_delete_authentication_method_by_id(self, mock_rc):
398+
mock_instance = mock_rc.return_value
399+
400+
u = Users(domain="domain", token="jwttoken")
401+
u.delete_authentication_method_by_id("user_id", "authentication_method_id")
402+
403+
mock_instance.delete.assert_called_with(
404+
"https://domain/api/v2/users/user_id/authentication-methods/authentication_method_id"
405+
)

0 commit comments

Comments
 (0)