From 4585cb187eb33236cbb3ab1e2ba0eb8a3c6a1884 Mon Sep 17 00:00:00 2001 From: Francisco Del Roio Date: Tue, 15 Feb 2022 18:23:26 -0300 Subject: [PATCH 1/2] Fixed token validation process. --- fastapi_firebase/auth.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/fastapi_firebase/auth.py b/fastapi_firebase/auth.py index 492aeb3..5b2a13b 100644 --- a/fastapi_firebase/auth.py +++ b/fastapi_firebase/auth.py @@ -1,4 +1,5 @@ import typing +import fastapi import firebase_admin import pydantic @@ -12,13 +13,23 @@ token = HTTPBearer( scheme_name="firebaseIdToken", ) +_failed_auth_headers = {"WWW-Authenticate": "Bearer"} def validate_token( - token: HTTPAuthorizationCredentials = Security(token), + credential: HTTPAuthorizationCredentials = Security(token), app: firebase_admin.App = Depends(firebase_app), ) -> typing.Dict[str, typing.Any]: - return auth.verify_id_token(token.credentials, app) + try: + return auth.verify_id_token(credential.credentials, app) + except auth.InvalidIdTokenError: + raise fastapi.HTTPException(401, "Invalid token received.", _failed_auth_headers) + except auth.UserDisabledError: + raise fastapi.HTTPException(403, "The user has been disabled.") + except auth.RevokedIdTokenError: + raise fastapi.HTTPException(403, "The token has been revoked.") + except auth.ExpiredIdTokenError: + raise fastapi.HTTPException(403, "The token has expired.") def token_info(token: typing.Dict[str, typing.Any] = Depends(validate_token)): From b93310601ddc496d837ac012b67c116d74eb4c64 Mon Sep 17 00:00:00 2001 From: Francisco Del Roio Date: Tue, 15 Feb 2022 18:24:17 -0300 Subject: [PATCH 2/2] Bumped version. --- fastapi_firebase/__init__.py | 2 +- pyproject.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/fastapi_firebase/__init__.py b/fastapi_firebase/__init__.py index 3061523..a310a19 100644 --- a/fastapi_firebase/__init__.py +++ b/fastapi_firebase/__init__.py @@ -6,4 +6,4 @@ """ from .app import firebase_app, setup_firebase -__version__ = "0.2.1" +__version__ = "0.2.2" diff --git a/pyproject.toml b/pyproject.toml index 7b1dc5e..d54da26 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -9,7 +9,7 @@ exclude = [ ] name = "fastapi-firebase" readme = "README.md" -version = "0.2.1" +version = "0.2.2" [tool.poetry.dependencies] fastapi = ">0.60.0<1.0.0"