-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from avantifellows/auth_for_delhi_api
[PPS] Added auth for Delhi API
- Loading branch information
Showing
4 changed files
with
59 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import httpx | ||
from fastapi import Request, HTTPException, status | ||
|
||
# Portal Backend URL | ||
VERIFICATION_URL = "https://9nqmv8p8k2.execute-api.ap-south-1.amazonaws.com/auth/verify" | ||
|
||
|
||
async def verify_token(request: Request) -> bool: | ||
# This function will be used to verify the bearer token | ||
auth_header = request.headers.get("Authorization") | ||
bearer_prefix = "Bearer " | ||
|
||
if not auth_header or not auth_header.startswith(bearer_prefix): | ||
raise HTTPException( | ||
status_code=status.HTTP_401_UNAUTHORIZED, | ||
detail="Invalid or missing token", | ||
headers={"WWW-Authenticate": "Bearer"}, | ||
) | ||
|
||
token = auth_header[len(bearer_prefix) :] | ||
headers = {"Authorization": f"Bearer {token}"} | ||
async with httpx.AsyncClient() as client: | ||
response = await client.get(VERIFICATION_URL, headers=headers) | ||
|
||
if response.status_code == 200: | ||
json_response = response.json() | ||
if "id" in json_response: | ||
return token | ||
|
||
raise HTTPException( | ||
status_code=status.HTTP_401_UNAUTHORIZED, | ||
detail="Invalid token", | ||
headers={"WWW-Authenticate": "Bearer"}, | ||
) |
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
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