-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthentication.py
65 lines (48 loc) · 1.96 KB
/
authentication.py
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
from passlib.context import CryptContext
import jwt
from dotenv import dotenv_values
from fastapi import HTTPException, status
from models import User
config_credential = dotenv_values(".env")
password_context = CryptContext(schemes=['bcrypt'], deprecated='auto')
def get_hashed_password(password):
"""'хэшироване пароля пользователя"""
return password_context.hash(password)
def verify_password(plain_password, hashed_password):
return password_context.verify(plain_password, hashed_password)
async def verify_token(token:str):
"""создание подтверждающего токена"""
try:
payload = jwt.decode(token, config_credential['SECRET'], algorithms=['HS256'])
user = await User.get(id = payload.get('id'))
except:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid username or password",
headers={"WWW-Authenticate": "Bearer"},
)
return user
async def verify_password(plain_password, hashed_password):
"""проверка пароля с паролем в ДБ"""
return password_context.verify(plain_password, hashed_password)
async def authenticate_user(username, password):
"""аутентификация пользователя"""
user = await User.get(username=username)
if user and verify_password(password, user.password):
return user
return False
async def token_generator(username: str, password: str):
"""генератор токена"""
user = await authenticate_user(username, password)
if not user:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid username or password",
headers={"WWW-Authenticate": "Bearer"},
)
token_data = {
"id": user.id,
"username": user.username
}
token = jwt.encode(token_data, config_credential["SECRET"])
return token