Skip to content

Commit

Permalink
Merge branch 'release/0.3.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
francipvb committed May 18, 2022
2 parents f2f20b5 + 8b2af4d commit d7fab96
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
2 changes: 1 addition & 1 deletion fastapi_firebase/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
"""
from .app import firebase_app, setup_firebase

__version__ = "0.2.2"
__version__ = "0.2.3"
35 changes: 27 additions & 8 deletions fastapi_firebase/auth.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import typing
import fastapi

import fastapi
import firebase_admin
import pydantic
from fastapi import Depends, Security
Expand All @@ -12,25 +12,44 @@

token = HTTPBearer(
scheme_name="firebaseIdToken",
bearerFormat="JWT",
description="The firebase Id token, provided by client SDK.",
)
_failed_auth_headers = {"WWW-Authenticate": "Bearer"}


def validate_token(
credential: HTTPAuthorizationCredentials = Security(token),
credential: typing.Optional[HTTPAuthorizationCredentials] = Security(token),
app: firebase_admin.App = Depends(firebase_app),
) -> typing.Dict[str, typing.Any]:
) -> typing.Optional[typing.Dict[str, typing.Any]]:
if credential is None:
return None

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.")
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.")


def token_info(token: typing.Optional[typing.Dict[str, typing.Any]] = Depends(validate_token)):
if token is None:
return None

def token_info(token: typing.Dict[str, typing.Any] = Depends(validate_token)):
return pydantic.parse_obj_as(TokenData, token)


def required_token_info(info: TokenData = fastapi.Depends(token_info)):
if info is None:
raise fastapi.HTTPException(
status_code=fastapi.status.HTTP_401_UNAUTHORIZED,
detail="Not authenticated",
headers={"www-authenticate": "Bearer"},
)

return info
3 changes: 2 additions & 1 deletion fastapi_firebase/schemes.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import datetime
from typing import Optional
import pydantic


class TokenData(pydantic.BaseModel):
provider_id: str
provider_id: Optional[str] = None
issuer: pydantic.HttpUrl = pydantic.Field(..., alias="iss")
audience: str = pydantic.Field(..., alias="aud")
auth_time: datetime.datetime
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ exclude = [
]
name = "fastapi-firebase"
readme = "README.md"
version = "0.2.2"
version = "0.3.0"

[tool.poetry.dependencies]
fastapi = ">0.60.0<1.0.0"
Expand Down

0 comments on commit d7fab96

Please sign in to comment.