You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.4 KiB

#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@Date:2022/07/27 15:55:01
'''
from flask import g
from flask_httpauth import HTTPBasicAuth, HTTPTokenAuth
from db_logic.user import BllUser
from models.user_models import EntityUser
from Common.errors import error_response
basic_auth = HTTPBasicAuth()
token_auth = HTTPTokenAuth()
@basic_auth.verify_password
def verify_password(username, password):
'''用于检查用户提供的用户名和密码'''
user = EntityUser.query.filter_by(username=username).first()
print(username, password)
if user is None:
return False
g.current_user = user
return user.check_password(password)
@basic_auth.error_handler
def basic_auth_error():
'''用于在认证失败的情况下返回错误响应'''
return error_response(401)
@token_auth.verify_token
def verify_token(token):
'''用于检查用户请求是否有token并且token真实存在还在有效期内'''
# g.current_user = User.verify_jwt(token) if token else None
g.current_user = BllUser().verify_jwt(token)
if g.current_user:
# 每次认证通过后即将访问资源API更新 last_seen 时间
g.current_user.update_last_visit_date()
BllUser().update(g.current_user)
return g.current_user is not None
@token_auth.error_handler
def token_auth_error():
'''用于在 Token Auth 认证失败的情况下返回错误响应'''
return error_response(401)