Example #1
0
def decode_token():
    #token = 'JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9token.eyJleHAiOjE1NzI2NzU2NzksImlhdCI6MTU2NzQ5MTY3OSwiZGF0YSI6eyJhY2NvdW50IjoiMTU3NzEwMDA1ODciLCJsb2dpbl90aW1lIjoxNTY3NDkxNjc5fX0.kY-_AHxJ7IQ35NJ80IkTr9kDk-LV3wdc6tByVSIQ1KE'
    token = request.headers.get('Authorization')
    if not token:
        _json = request.json
        token = _json.get('token')
    token_arr = token.split(' ')
    if (not token_arr) or (token_arr[0] != "JWT") or (len(token_arr) != 2):
        return json.dumps(return_unsuccess('验证头信息不正确'), ensure_ascii=False)
    else:
        auth_token = token_arr[1]
        try:
            data = Auth.decode_jwt(auth_token).get('data')
        except Exception as e:
            return json.dumps(return_unsuccess('token解码失败: ' + str(e)),
                              ensure_ascii=False)
        else:
            account = data.get('account')
            user_dao = UserDao()
            try:
                res = user_dao.query_by_account(account)
                if len(res) == 1:
                    return json.dumps(return_success(UserDao.to_dict(res)),
                                      ensure_ascii=False)
                else:
                    return json.dumps(
                        (return_unsuccess("Error: No such user")))
            except Exception as e:
                return json.dumps((return_unsuccess("Error: " + str(e))))
Example #2
0
def check_account():
    account = request.json.get('account')
    # 到数据库中进行查询
    user_dao = UserDao()
    result = user_dao.query_by_account(account)
    size = len(result)
    if size == 0:
        return json.dumps(return_success(""))
    else:
        return json.dumps(return_unsuccess("Error Account Duplicate"))