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