-
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.
- Loading branch information
Showing
2 changed files
with
16 additions
and
3 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,34 +1,45 @@ | ||
import logging | ||
import os | ||
import typing | ||
from datetime import timedelta, datetime | ||
|
||
import jwt | ||
from fastapi import HTTPException, status | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def create_access_token(data: dict, expires_delta: typing.Optional[timedelta] = None): | ||
logger.info(f"Creating access token: {data}") | ||
to_encode = data.copy() | ||
if expires_delta: | ||
expire = datetime.utcnow() + expires_delta | ||
else: | ||
expire = datetime.utcnow() + timedelta(minutes=15) | ||
to_encode.update({"exp": expire}) | ||
secret_key = os.environ["JWT_SECRET_KEY"] | ||
logger.info(f"JWT secret key: {secret_key}") | ||
encoded_jwt = jwt.encode(to_encode, secret_key, algorithm="HS256") | ||
return encoded_jwt | ||
|
||
|
||
def get_jhub_token_from_jwt_token(token): | ||
logger.info(f"Trying to get JHUB Apps token from JWT Token: {token}") | ||
credentials_exception = HTTPException( | ||
status_code=status.HTTP_401_UNAUTHORIZED, | ||
detail="Could not validate credentials", | ||
detail={ | ||
"msg": "Could not validate credentials" | ||
}, | ||
headers={"WWW-Authenticate": "Bearer"}, | ||
) | ||
try: | ||
payload = jwt.decode(token, os.environ["JWT_SECRET_KEY"], algorithms=["HS256"]) | ||
access_token_data: dict = payload.get("sub") | ||
if access_token_data is None: | ||
raise credentials_exception | ||
except jwt.PyJWTError: | ||
except jwt.PyJWTError as e: | ||
logger.warning(f"Authentication failed for token: {token}, JWT_SECRET_KEY: {os.environ['JWT_SECRET_KEY']}") | ||
logger.exception(e) | ||
raise credentials_exception | ||
logger.info("Fetched access token from JWT Token") | ||
return access_token_data["access_token"] |
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