Skip to content

Commit

Permalink
fix: decode Dependency fixes for secret value
Browse files Browse the repository at this point in the history
pyjwt requires a list of algorithms it ought to use to decode the token, the JWT
secret key needs to be read as one, not a strong REFS #52
  • Loading branch information
devraj committed Nov 20, 2022
1 parent 9f3dd7e commit ee27656
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/labs/routers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"""

from sqlalchemy.ext.asyncio import AsyncSession
from fastapi import Depends, HTTPException
from fastapi import Depends, HTTPException, status
from fastapi.security import OAuth2PasswordBearer
import jwt

Expand All @@ -34,8 +34,8 @@ async def get_current_user(
try:
payload = jwt.decode(
token,
config.JWT_SECRET_KEY,
algorithm=config.JWT_ALGORITHM
config.JWT_SECRET_KEY.get_secret_value(),
algorithms=[config.JWT_ALGORITHM]
)

username: str = payload.get("sub")
Expand All @@ -46,7 +46,7 @@ async def get_current_user(
token_data = TokenData(username=username)

except:
raise credentials_exception
raise credentials_exception

user = await User.get_by_email(session, token_data.username)

Expand All @@ -59,11 +59,17 @@ async def get_current_user(
async def get_current_active_user(
current_user: User = Depends(get_current_user)
):
"""
""" Demonstrates wrapping the base Dependency to a more specific one
You would use the same pattern to make sure that the user is
an administrator or other specific roles.
Note: see the use of OAuth2 scopes for this purpose.
"""
if current_user.verified:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Inactive user"
)
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Inactive user"
)
return current_user

0 comments on commit ee27656

Please sign in to comment.