-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthentication.py
64 lines (56 loc) · 2.35 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
from fastapi import APIRouter, HTTPException, status, Depends
from fastapi.security.oauth2 import OAuth2PasswordRequestForm
from sqlalchemy.orm.session import Session
from db.database import get_db
from db.model import DbUser
from db.hash import Hash
from auth import oauth2
router = APIRouter(
prefix="/auth",
tags=["authentication"]
)
# Địa chỉ này phải đúng với tokenURL trong: oauth2_scheme = OAuth2PasswordBearer(tokenUrl="login")
@router.post("/login")
def login(request: OAuth2PasswordRequestForm = Depends(), db: Session = Depends(get_db)):
"""
Tạo ra một token có thời gian tồn tại để có thể truy vấn đến các API được khóa
- **request** sẽ là dữ liệu theo dạng biểu mẫu của `OAuth2PasswordRequestForm` gồm `username` và `password`
Để sử dụng hàm này ta cần cung cấp `username` và `password` đã được đăng ký trong CSDL
### Ví dụ
```python
import requests
url_get_token = "http://172.31.99.42:8000/auth/login"
data = {
"username": "admin",
"password": "123456789"
}
response = requests.post(url=url_get_token, data= data)
token = response.json().get("access_token")
print(token)
```
[FastAPI docs for Simple OAuth2 with Password and Bearer](https://fastapi.tiangolo.com/tutorial/security/simple-oauth2/)
"""
user = db.query(DbUser).filter(DbUser.username == request.username).first()
if not user:
raise HTTPException(
status_code= status.HTTP_404_NOT_FOUND,
detail=f"Not Found User with username: {request.username}"
)
# Lấy mật khẩu người dùng từ CSDL
hashed_password = user.password
# so sánh mật khẩu người dùng vừa cung cấp với mật khẩu trong CSDL
if not Hash.verify(plain_password= request.password, hashed_password= hashed_password):
raise HTTPException(
status_code= status.HTTP_404_NOT_FOUND,
detail=f"Incorrect Password: {request.username}"
)
# Tạo token với thông tin đi kèm là email
access_token = oauth2.create_access_token(data= {
"email": user.email
})
return {
"access_token": access_token,
"token_type": "bearer", # token tiêu chuẩn: bearer
"user_id": user.id,
"username": user.username
}