mirror of
https://github.com/devproje/balance-application.git
synced 2024-10-20 15:11:21 +00:00
30 lines
681 B
Python
30 lines
681 B
Python
from fastapi import APIRouter, Response
|
|
from util.auth_lib import hash, gen_token
|
|
from service.auth_service import Credential, AuthService
|
|
|
|
router = APIRouter()
|
|
|
|
@router.post("/auth/login")
|
|
def login(auth: Credential, resp: Response):
|
|
service = AuthService()
|
|
data = service.read(auth.username)
|
|
if data == None:
|
|
resp.status_code = 401
|
|
return {
|
|
"ok": 0,
|
|
"errno": "Unauthorized"
|
|
}
|
|
|
|
hashed = hash(auth.password, data.salt)
|
|
if data.username != auth.username or data.password != hashed:
|
|
resp.status_code = 401
|
|
return {
|
|
"ok": 0,
|
|
"errno": "Unauthorized"
|
|
}
|
|
|
|
token = gen_token(auth.username, hashed)
|
|
return {
|
|
"ok": 1,
|
|
"token": "Basic {}".format(token)
|
|
}
|