balance-application/routes/auth.py

31 lines
681 B
Python
Raw Normal View History

2024-09-10 17:59:43 +09:00
from fastapi import APIRouter, Response
from util.auth_lib import hash, gen_token
from service.auth_service import Credential, AuthService
2024-09-10 14:20:11 +09:00
router = APIRouter()
@router.post("/auth/login")
2024-09-10 17:59:43 +09:00
def login(auth: Credential, resp: Response):
service = AuthService()
data = service.read(auth.username)
2024-09-11 12:35:54 +09:00
if data == None:
resp.status_code = 401
return {
"ok": 0,
"errno": "Unauthorized"
}
2024-09-10 16:36:03 +09:00
2024-09-10 17:59:43 +09:00
hashed = hash(auth.password, data.salt)
2024-09-11 00:36:03 +09:00
if data.username != auth.username or data.password != hashed:
2024-09-10 17:59:43 +09:00
resp.status_code = 401
return {
"ok": 0,
"errno": "Unauthorized"
}
token = gen_token(auth.username, hashed)
return {
"ok": 1,
"token": "Basic {}".format(token)
}