7
7
"""
8
8
9
9
from sqlalchemy .ext .asyncio import AsyncSession
10
- from fastapi import Depends , HTTPException
10
+ from fastapi import Depends , HTTPException , status
11
11
from fastapi .security import OAuth2PasswordBearer
12
12
import jwt
13
13
@@ -34,8 +34,8 @@ async def get_current_user(
34
34
try :
35
35
payload = jwt .decode (
36
36
token ,
37
- config .JWT_SECRET_KEY ,
38
- algorithm = config .JWT_ALGORITHM
37
+ config .JWT_SECRET_KEY . get_secret_value () ,
38
+ algorithms = [ config .JWT_ALGORITHM ]
39
39
)
40
40
41
41
username : str = payload .get ("sub" )
@@ -46,7 +46,7 @@ async def get_current_user(
46
46
token_data = TokenData (username = username )
47
47
48
48
except :
49
- raise credentials_exception
49
+ raise credentials_exception
50
50
51
51
user = await User .get_by_email (session , token_data .username )
52
52
@@ -59,11 +59,17 @@ async def get_current_user(
59
59
async def get_current_active_user (
60
60
current_user : User = Depends (get_current_user )
61
61
):
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
+
63
69
"""
64
70
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
+ )
69
75
return current_user
0 commit comments