@@ -454,8 +454,13 @@ def encode_action_code_settings(settings):
454
454
class UserManager :
455
455
"""Provides methods for interacting with the Google Identity Toolkit."""
456
456
457
- def __init__ (self , client ):
458
- self ._client = client
457
+ ID_TOOLKIT_URL = 'https://identitytoolkit.googleapis.com/v1'
458
+
459
+ def __init__ (self , http_client , project_id , tenant_id = None ):
460
+ self .http_client = http_client
461
+ self .base_url = '{0}/projects/{1}' .format (self .ID_TOOLKIT_URL , project_id )
462
+ if tenant_id :
463
+ self .base_url += '/tenants/{0}' .format (tenant_id )
459
464
460
465
def get_user (self , ** kwargs ):
461
466
"""Gets the user data corresponding to the provided key."""
@@ -471,17 +476,12 @@ def get_user(self, **kwargs):
471
476
else :
472
477
raise TypeError ('Unsupported keyword arguments: {0}.' .format (kwargs ))
473
478
474
- try :
475
- body , http_resp = self ._client .body_and_response (
476
- 'post' , '/accounts:lookup' , json = payload )
477
- except requests .exceptions .RequestException as error :
478
- raise _auth_utils .handle_auth_backend_error (error )
479
- else :
480
- if not body or not body .get ('users' ):
481
- raise _auth_utils .UserNotFoundError (
482
- 'No user record found for the provided {0}: {1}.' .format (key_type , key ),
483
- http_response = http_resp )
484
- return body ['users' ][0 ]
479
+ body , http_resp = self ._make_request ('post' , '/accounts:lookup' , json = payload )
480
+ if not body or not body .get ('users' ):
481
+ raise _auth_utils .UserNotFoundError (
482
+ 'No user record found for the provided {0}: {1}.' .format (key_type , key ),
483
+ http_response = http_resp )
484
+ return body ['users' ][0 ]
485
485
486
486
def list_users (self , page_token = None , max_results = MAX_LIST_USERS_RESULTS ):
487
487
"""Retrieves a batch of users."""
@@ -498,10 +498,8 @@ def list_users(self, page_token=None, max_results=MAX_LIST_USERS_RESULTS):
498
498
payload = {'maxResults' : max_results }
499
499
if page_token :
500
500
payload ['nextPageToken' ] = page_token
501
- try :
502
- return self ._client .body ('get' , '/accounts:batchGet' , params = payload )
503
- except requests .exceptions .RequestException as error :
504
- raise _auth_utils .handle_auth_backend_error (error )
501
+ body , _ = self ._make_request ('get' , '/accounts:batchGet' , params = payload )
502
+ return body
505
503
506
504
def create_user (self , uid = None , display_name = None , email = None , phone_number = None ,
507
505
photo_url = None , password = None , disabled = None , email_verified = None ):
@@ -517,15 +515,11 @@ def create_user(self, uid=None, display_name=None, email=None, phone_number=None
517
515
'disabled' : bool (disabled ) if disabled is not None else None ,
518
516
}
519
517
payload = {k : v for k , v in payload .items () if v is not None }
520
- try :
521
- body , http_resp = self ._client .body_and_response ('post' , '/accounts' , json = payload )
522
- except requests .exceptions .RequestException as error :
523
- raise _auth_utils .handle_auth_backend_error (error )
524
- else :
525
- if not body or not body .get ('localId' ):
526
- raise _auth_utils .UnexpectedResponseError (
527
- 'Failed to create new user.' , http_response = http_resp )
528
- return body .get ('localId' )
518
+ body , http_resp = self ._make_request ('post' , '/accounts' , json = payload )
519
+ if not body or not body .get ('localId' ):
520
+ raise _auth_utils .UnexpectedResponseError (
521
+ 'Failed to create new user.' , http_response = http_resp )
522
+ return body .get ('localId' )
529
523
530
524
def update_user (self , uid , display_name = None , email = None , phone_number = None ,
531
525
photo_url = None , password = None , disabled = None , email_verified = None ,
@@ -568,29 +562,19 @@ def update_user(self, uid, display_name=None, email=None, phone_number=None,
568
562
payload ['customAttributes' ] = _auth_utils .validate_custom_claims (json_claims )
569
563
570
564
payload = {k : v for k , v in payload .items () if v is not None }
571
- try :
572
- body , http_resp = self ._client .body_and_response (
573
- 'post' , '/accounts:update' , json = payload )
574
- except requests .exceptions .RequestException as error :
575
- raise _auth_utils .handle_auth_backend_error (error )
576
- else :
577
- if not body or not body .get ('localId' ):
578
- raise _auth_utils .UnexpectedResponseError (
579
- 'Failed to update user: {0}.' .format (uid ), http_response = http_resp )
580
- return body .get ('localId' )
565
+ body , http_resp = self ._make_request ('post' , '/accounts:update' , json = payload )
566
+ if not body or not body .get ('localId' ):
567
+ raise _auth_utils .UnexpectedResponseError (
568
+ 'Failed to update user: {0}.' .format (uid ), http_response = http_resp )
569
+ return body .get ('localId' )
581
570
582
571
def delete_user (self , uid ):
583
572
"""Deletes the user identified by the specified user ID."""
584
573
_auth_utils .validate_uid (uid , required = True )
585
- try :
586
- body , http_resp = self ._client .body_and_response (
587
- 'post' , '/accounts:delete' , json = {'localId' : uid })
588
- except requests .exceptions .RequestException as error :
589
- raise _auth_utils .handle_auth_backend_error (error )
590
- else :
591
- if not body or not body .get ('kind' ):
592
- raise _auth_utils .UnexpectedResponseError (
593
- 'Failed to delete user: {0}.' .format (uid ), http_response = http_resp )
574
+ body , http_resp = self ._make_request ('post' , '/accounts:delete' , json = {'localId' : uid })
575
+ if not body or not body .get ('kind' ):
576
+ raise _auth_utils .UnexpectedResponseError (
577
+ 'Failed to delete user: {0}.' .format (uid ), http_response = http_resp )
594
578
595
579
def import_users (self , users , hash_alg = None ):
596
580
"""Imports the given list of users to Firebase Auth."""
@@ -609,16 +593,11 @@ def import_users(self, users, hash_alg=None):
609
593
if not isinstance (hash_alg , _user_import .UserImportHash):
610
594
raise ValueError ('A UserImportHash is required to import users with passwords.' )
611
595
payload .update (hash_alg .to_dict ())
612
- try :
613
- body , http_resp = self ._client .body_and_response (
614
- 'post' , '/accounts:batchCreate' , json = payload )
615
- except requests .exceptions .RequestException as error :
616
- raise _auth_utils .handle_auth_backend_error (error )
617
- else :
618
- if not isinstance (body , dict ):
619
- raise _auth_utils .UnexpectedResponseError (
620
- 'Failed to import users.' , http_response = http_resp )
621
- return body
596
+ body , http_resp = self ._make_request ('post' , '/accounts:batchCreate' , json = payload )
597
+ if not isinstance (body , dict ):
598
+ raise _auth_utils .UnexpectedResponseError (
599
+ 'Failed to import users.' , http_response = http_resp )
600
+ return body
622
601
623
602
def generate_email_action_link (self , action_type , email , action_code_settings = None ):
624
603
"""Fetches the email action links for types
@@ -646,16 +625,18 @@ def generate_email_action_link(self, action_type, email, action_code_settings=No
646
625
if action_code_settings :
647
626
payload .update (encode_action_code_settings (action_code_settings ))
648
627
628
+ body , http_resp = self ._make_request ('post' , '/accounts:sendOobCode' , json = payload )
629
+ if not body or not body .get ('oobLink' ):
630
+ raise _auth_utils .UnexpectedResponseError (
631
+ 'Failed to generate email action link.' , http_response = http_resp )
632
+ return body .get ('oobLink' )
633
+
634
+ def _make_request (self , method , path , ** kwargs ):
635
+ url = '{0}{1}' .format (self .base_url , path )
649
636
try :
650
- body , http_resp = self ._client .body_and_response (
651
- 'post' , '/accounts:sendOobCode' , json = payload )
637
+ return self .http_client .body_and_response (method , url , ** kwargs )
652
638
except requests .exceptions .RequestException as error :
653
639
raise _auth_utils .handle_auth_backend_error (error )
654
- else :
655
- if not body or not body .get ('oobLink' ):
656
- raise _auth_utils .UnexpectedResponseError (
657
- 'Failed to generate email action link.' , http_response = http_resp )
658
- return body .get ('oobLink' )
659
640
660
641
661
642
class _UserIterator :
0 commit comments