Skip to content

Commit

Permalink
Merge pull request #26 from /dev
Browse files Browse the repository at this point in the history
  • Loading branch information
tokusumi authored Feb 23, 2021
1 parent 36d947f commit a8db880
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
30 changes: 26 additions & 4 deletions fastapi_cloudauth/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,10 @@ def verify_scope(self, http_auth: HTTPAuthorizationCredentials) -> bool:
return True

async def __call__(
self, http_auth: HTTPAuthorizationCredentials = Depends(HTTPBearer())
self,
http_auth: Optional[HTTPAuthorizationCredentials] = Depends(
HTTPBearer(auto_error=False)
),
) -> Optional[bool]:
"""User access-token verification Shortcut to pass it into dependencies.
Use as (`auth` is this instanse and `app` is fastapi.FastAPI instanse):
Expand All @@ -162,6 +165,14 @@ def api():
return "hello"
```
"""
if http_auth is None:
if self.auto_error:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN, detail=NOT_AUTHENTICATED
)
else:
return None

is_verified = self.verify_token(http_auth)
if not is_verified:
return None
Expand All @@ -179,7 +190,7 @@ class TokenUserInfoGetter(BaseTokenVerifier):
Verify `ID token` and extract user information
"""

user_info: Type[BaseModel] = None
user_info: Optional[Type[BaseModel]] = None

def __init__(self, *args, **kwargs):
if not self.user_info:
Expand All @@ -189,8 +200,11 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

async def __call__(
self, http_auth: HTTPAuthorizationCredentials = Depends(HTTPBearer())
) -> Optional[Type[BaseModel]]:
self,
http_auth: Optional[HTTPAuthorizationCredentials] = Depends(
HTTPBearer(auto_error=False)
),
) -> Optional[BaseModel]:
"""Get current user and verification with ID-token Shortcut.
Use as (`Auth` is this subclass, `auth` is `Auth` instanse and `app` is fastapi.FastAPI instanse):
```
Expand All @@ -201,6 +215,14 @@ def api(current_user: Auth = Depends(auth)):
return current_user
```
"""
if http_auth is None:
if self.auto_error:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN, detail=NOT_AUTHENTICATED
)
else:
return None

is_verified = self.verify_token(http_auth)
if not is_verified:
return None
Expand Down
4 changes: 4 additions & 0 deletions tests/test_cloudauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ def test_valid_token(self):
def test_no_token(self):
# handle in fastapi.security.HTTPBearer
self.failure_case("/")
# not auto_error
self.success_case("no-error")

def test_incompatible_kid_token(self):
# manipulate header
Expand Down Expand Up @@ -124,6 +126,8 @@ def test_valid_id_token(self):
def test_no_id_token(self):
# handle in fastapi.security.HTTPBearer
self.failure_case("/user/")
# not auto_error
self.success_case("/user/no-error")

def test_incompatible_kid_id_token(self):
# manipulate header
Expand Down

0 comments on commit a8db880

Please sign in to comment.