Skip to content

Commit ee27656

Browse files
committed
fix: decode Dependency fixes for secret value
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
1 parent 9f3dd7e commit ee27656

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

src/labs/routers/utils.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"""
88

99
from sqlalchemy.ext.asyncio import AsyncSession
10-
from fastapi import Depends, HTTPException
10+
from fastapi import Depends, HTTPException, status
1111
from fastapi.security import OAuth2PasswordBearer
1212
import jwt
1313

@@ -34,8 +34,8 @@ async def get_current_user(
3434
try:
3535
payload = jwt.decode(
3636
token,
37-
config.JWT_SECRET_KEY,
38-
algorithm=config.JWT_ALGORITHM
37+
config.JWT_SECRET_KEY.get_secret_value(),
38+
algorithms=[config.JWT_ALGORITHM]
3939
)
4040

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

4848
except:
49-
raise credentials_exception
49+
raise credentials_exception
5050

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

@@ -59,11 +59,17 @@ async def get_current_user(
5959
async def get_current_active_user(
6060
current_user: User = Depends(get_current_user)
6161
):
62-
"""
62+
""" Demonstrates wrapping the base Dependency to a more specific one
63+
64+
You would use the same pattern to make sure that the user is
65+
an administrator or other specific roles.
66+
67+
Note: see the use of OAuth2 scopes for this purpose.
68+
6369
"""
6470
if current_user.verified:
65-
raise HTTPException(
66-
status_code=status.HTTP_400_BAD_REQUEST,
67-
detail="Inactive user"
68-
)
71+
raise HTTPException(
72+
status_code=status.HTTP_400_BAD_REQUEST,
73+
detail="Inactive user"
74+
)
6975
return current_user

0 commit comments

Comments
 (0)