|
18 | 18 | import random
|
19 | 19 | import datetime
|
20 | 20 | import python_jwt as jwt
|
21 |
| -import jwcrypto.jwk as jwk |
22 | 21 | from hashlib import sha256
|
| 22 | +from jwcrypto.jwk import JWK |
23 | 23 | from urllib.parse import parse_qs
|
24 | 24 | from google.auth.transport.requests import Request
|
25 | 25 | from cryptography.hazmat.primitives.serialization import Encoding, NoEncryption, PrivateFormat
|
@@ -209,7 +209,7 @@ def create_custom_token(self, uid, additional_claims=None, expiry_minutes=60):
|
209 | 209 | """
|
210 | 210 |
|
211 | 211 | service_account_email = self.credentials.service_account_email
|
212 |
| - private_key = jwk.JWK.from_pem(self.credentials.signer._key.private_bytes(encoding=Encoding.PEM, format=PrivateFormat.PKCS8, encryption_algorithm=NoEncryption())) |
| 212 | + private_key = JWK.from_pem(self.credentials.signer._key.private_bytes(encoding=Encoding.PEM, format=PrivateFormat.PKCS8, encryption_algorithm=NoEncryption())) |
213 | 213 |
|
214 | 214 | payload = {
|
215 | 215 | "iss": service_account_email,
|
@@ -587,6 +587,32 @@ def set_custom_user_claims(self, user_id, custom_claims):
|
587 | 587 |
|
588 | 588 | raise_detailed_error(request_object)
|
589 | 589 |
|
| 590 | + def verify_id_token(self, id_token): |
| 591 | + """ Decode Firebase Auth ID token. |
| 592 | +
|
| 593 | + | For more details: |
| 594 | + | `Firebase Authentication | Verify ID tokens using a third-party JWT library`_ |
| 595 | +
|
| 596 | + .. _Firebase Authentication | Verify ID tokens using a third-party JWT library: https://firebase.google.com/docs/auth/admin/verify-id-tokens#verify_id_tokens_using_a_third-party_jwt_library |
| 597 | +
|
| 598 | + :type id_token: str |
| 599 | + :param id_token: A Firebase Auth ID token for the user. |
| 600 | +
|
| 601 | + :return: Decoded claims of Firebase Auth ID token. |
| 602 | + :rtype: dict |
| 603 | + """ |
| 604 | + |
| 605 | + header, _ = jwt.process_jwt(id_token) |
| 606 | + |
| 607 | + response = self. requests. get( 'https://www.googleapis.com/robot/v1/metadata/x509/[email protected]') |
| 608 | + |
| 609 | + pub_pem = response.json()[header['kid']] |
| 610 | + |
| 611 | + pub_key = JWK.from_pem(bytes(pub_pem.encode('utf-8'))) |
| 612 | + _, claims = jwt.verify_jwt(id_token, pub_key, [header['alg']], checks_optional=True) |
| 613 | + |
| 614 | + return claims |
| 615 | + |
590 | 616 |
|
591 | 617 | def _load_client_secret(secret):
|
592 | 618 | """ Load social providers' client secret from file if file path
|
|
0 commit comments