-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support to receive a Bearer token in the API requests and decode it.
- Loading branch information
Showing
5 changed files
with
47 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from fastapi import Request, HTTPException | ||
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials | ||
|
||
from .auth_handler import decode_token | ||
|
||
|
||
class BearerToken(HTTPBearer): | ||
def __init__(self, auto_error: bool = True): | ||
super(BearerToken, self).__init__(auto_error=auto_error) | ||
|
||
async def __call__(self, request: Request): | ||
credentials: HTTPAuthorizationCredentials = await super(BearerToken, self).__call__(request) | ||
if credentials: | ||
if not credentials.scheme == "Bearer": | ||
raise HTTPException(status_code=401, detail="Invalid authentication scheme.") | ||
if not self.verify_token(credentials.credentials): | ||
raise HTTPException(status_code=401, detail="Invalid or expired token.") | ||
return credentials.credentials | ||
else: | ||
raise HTTPException(status_code=401, detail="Invalid authorization code.") | ||
|
||
def verify_token(self, token: str) -> bool: | ||
payload = decode_token(token) | ||
return True if payload else False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import time | ||
import jwt | ||
from decouple import config | ||
|
||
|
||
JWT_SECRET = config("JWT_SECRET") | ||
JWT_ALGORITHM = config("JWT_ALGORITHM") | ||
|
||
|
||
def token_response(token: str): | ||
return {"access_token": token} | ||
|
||
|
||
def decode_token(token: str) -> dict: | ||
decoded_token = jwt.decode(token, JWT_SECRET, algorithms=[JWT_ALGORITHM]) | ||
return decoded_token if decoded_token["expires"] >= time.time() else None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters