-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add authentication * Minor tweaks
- Loading branch information
Showing
7 changed files
with
59 additions
and
10 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
API_BASE_URL=https://rag.superagent.sh | ||
COHERE_API_KEY= | ||
HUGGINGFACE_API_KEY= | ||
HUGGINGFACE_API_KEY= | ||
JWT_SECRET= |
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
Empty file.
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,33 @@ | ||
import logging | ||
import jwt | ||
|
||
from decouple import config | ||
from fastapi import HTTPException, Security | ||
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer | ||
from superagent.client import AsyncSuperagent | ||
|
||
logger = logging.getLogger(__name__) | ||
security = HTTPBearer() | ||
|
||
|
||
def generate_jwt(data: dict): | ||
token = jwt.encode({**data}, config("JWT_SECRET"), algorithm="HS256") | ||
return token | ||
|
||
|
||
def decode_jwt(token: str): | ||
return jwt.decode(token, config("JWT_SECRET"), algorithms=["HS256"]) | ||
|
||
|
||
async def get_current_api_user( | ||
authorization: HTTPAuthorizationCredentials = Security(security), | ||
): | ||
token = authorization.credentials | ||
decoded_token = decode_jwt(token) | ||
superagent = AsyncSuperagent( | ||
base_url="https://api.beta.superagent.sh", token=decoded_token | ||
) | ||
api_user = superagent.api_user.get() | ||
if not api_user: | ||
raise HTTPException(status_code=401, detail="Invalid token or expired token") | ||
return api_user |
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