def identify(self, request):
     """
     用户鉴权
     :return: list
     """
     auth_header = request.headers.get('Authorization')
     if (auth_header):
         auth_tokenArr = auth_header.split(" ")
         if (not auth_tokenArr or auth_tokenArr[0] != 'JWT'
                 or len(auth_tokenArr) != 2):
             return '请传递正确的验证头信息'
         else:
             auth_token = auth_tokenArr[1]
             payload = self.decode_auth_token(auth_token)
             if not isinstance(payload, str):
                 user = Users.get(payload['data']['id'])
                 if (user is None):
                     return '找不到该用户信息'
                 else:
                     if (user.updated_at == payload['data']['updated_at']):
                         result = payload
                     else:
                         return 'Token已更改,请重新登录获取'
             else:
                 result = payload
     else:
         return '没有提供认证token'
     return result
예제 #2
0
def get():
    '''
    *获取用户信息 
    *jwt中修改error处理方法,统一响应头
    *_default_jwt_error_handler
    '''
    result = UsersAuthJWT().identify(request)
    if isinstance(result, str):
        return BaseController().error(result)
    if (result['data']):
        user = Users.get(result['data']['id'])
        returnUser = {
            'id': user.id,
            'name': user.name,
            'email': user.email,
            'login_time': user.updated_at
        }
        return BaseController().successData(returnUser)
    return BaseController().error('未找到用户')