-
Notifications
You must be signed in to change notification settings - Fork 372
Expand file tree
/
Copy pathauth.py
More file actions
73 lines (62 loc) · 2.13 KB
/
auth.py
File metadata and controls
73 lines (62 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import jwt
from fastapi import Depends, HTTPException, status
from jwt import PyJWTError
from app.db import session
from app.db.users.crud import get_user_by_email, create_user
from app.db.users.models import User
from app.db.users.schemas import TokenData, UserCreate
from app.core import security
async def get_current_user(
db=Depends(session.get_db), token: str = Depends(security.oauth2_scheme)
):
credentials_exception = HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Could not validate credentials",
headers={"WWW-Authenticate": "Bearer"},
)
try:
payload = jwt.decode(
token, security.SECRET_KEY, algorithms=[security.ALGORITHM]
)
email: str = payload.get("sub")
if email is None:
raise credentials_exception
permissions: str = payload.get("permissions")
token_data = TokenData(email=email, permissions=permissions)
except PyJWTError:
raise credentials_exception
user = get_user_by_email(db, token_data.email)
if user is None:
raise credentials_exception
return user
async def get_current_active_user(
current_user: User = Depends(get_current_user),
):
if not current_user.is_active:
raise HTTPException(status_code=400, detail="Inactive user")
return current_user
async def get_current_active_superuser(
current_user: User = Depends(get_current_user),
) -> User:
if not current_user.is_superuser:
raise HTTPException(
status_code=403, detail="The user doesn't have enough privileges"
)
return current_user
def authenticate_user(db, email: str, password: str):
user = get_user_by_email(db, email)
if not user:
return False
if not security.verify_password(password, user.hashed_password):
return False
return user
def sign_up_new_user(db, email: str, password: str):
user = get_user_by_email(db, email)
if user:
return False # User already exists
return create_user(db, UserCreate(
email=email,
password=password,
is_active=True,
is_superuser=False,
))