|
21 | 21 | import jwcrypto.jwk as jwk
|
22 | 22 | from hashlib import sha256
|
23 | 23 | from urllib.parse import parse_qs
|
| 24 | +from google.auth.transport.requests import Request |
24 | 25 | from cryptography.hazmat.primitives.serialization import Encoding, NoEncryption, PrivateFormat
|
25 | 26 |
|
26 | 27 | from firebase._exception import raise_detailed_error
|
@@ -557,6 +558,35 @@ def update_profile(self, id_token, display_name=None, photo_url=None, delete_att
|
557 | 558 |
|
558 | 559 | return request_object.json()
|
559 | 560 |
|
| 561 | + def set_custom_user_claims(self, user_id, custom_claims): |
| 562 | + """ Add or remove custom claims from/to an existing user. |
| 563 | +
|
| 564 | + | For more details: |
| 565 | + | `Firebase Auth REST API | Set and validate custom user claims`_ |
| 566 | +
|
| 567 | + .. _Firebase Auth REST API | Set and validate custom user claims: https://firebase.google.com/docs/auth/admin/custom-claims#set_and_validate_custom_user_claims_via_the_admin_sdk |
| 568 | +
|
| 569 | + :type user_id: str |
| 570 | + :param user_id: Firebase User UID. |
| 571 | +
|
| 572 | + :type custom_claims: dict |
| 573 | + :param custom_claims: Claims to add to that user's token. |
| 574 | + """ |
| 575 | + |
| 576 | + request_ref = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/setAccountInfo?key={0}".format(self.api_key) |
| 577 | + |
| 578 | + if not self.credentials.valid: |
| 579 | + self.credentials.refresh(Request()) |
| 580 | + |
| 581 | + access_token = self.credentials.token |
| 582 | + |
| 583 | + headers = {"Authorization": "Bearer " + access_token, "content-type": "application/json; charset=UTF-8"} |
| 584 | + |
| 585 | + data = json.dumps({"localId": user_id, "customAttributes":json.dumps(custom_claims), "returnSecureToken": False}) |
| 586 | + request_object = self.requests.post(request_ref, headers=headers, data=data) |
| 587 | + |
| 588 | + raise_detailed_error(request_object) |
| 589 | + |
560 | 590 |
|
561 | 591 | def _load_client_secret(secret):
|
562 | 592 | """ Load social providers' client secret from file if file path
|
|
0 commit comments