示例#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))))
示例#2
0
def queryUser():
    _openid = request.json.get('openid')
    _account = request.json.get('account')
    user_dao = UserDao()
    res = user_dao.query_by_openid_account(_account, _openid)
    size = len(res)
    if size > 0:
        return json.dumps(return_success(UserDao.to_dict(res)))
    else:
        return json.dumps(return_unsuccess('Error: No such user'))
示例#3
0
 def test1(self):
     userdao = UserDao()
     result = userdao.query_all()
     print(result)
     j = json.dumps(return_success(UserDao.to_dict(result)))
     print(j)
示例#4
0
def login():
    # token登陆
    if request.method == 'GET':
        return decode_token()

    _json = request.json
    login_type = _json.get('type')
    account = _json.get('account')
    password = _json.get('passwd')
    web = _json.get('web')
    # 生成token
    login_time = int(time.time())
    token = Auth.create_jwt({'account': account, 'login_time': login_time})
    # 账号密码登陆
    if login_type == 0:
        store_in = base64.b64decode(password)
        if not web:
            store_in = binascii.hexlify(store_in)
        strpass = str(store_in, 'utf-8')
        print(strpass)

        user_dao = UserDao()
        res = user_dao.query_check_login(account, strpass)
        size = len(res)
        if size == 1:
            resp = return_success(UserDao.to_dict(res))
            resp['token'] = token
            return jsonify(resp)
        else:
            return jsonify(return_unsuccess('账号或密码错误'))
    # 验证码登陆
    elif login_type == 1:
        true_veri = redis_store.get('veri' + account)
        if not true_veri:
            return jsonify(return_unsuccess("验证码过期"))
        elif password != true_veri:
            return jsonify(return_unsuccess("验证码错误"))
        res = json.loads(check_account())
        suc = res.get("success")
        if not suc:
            print(res)
            redis_store.delete('veri' + account)
            user = UserDao().query_by_account(account)
            resp = return_success(UserDao.to_dict(user))
            resp['token'] = token
        else:
            resp = return_unsuccess('Error: No such user')
        return jsonify(resp)
    # openid登陆
    elif login_type == 2:
        openid = _json.get("openid")
        user_dao = UserDao()
        res = user_dao.query_by_openid_account(account, openid)
        size = len(res)
        if size == 1:
            resp = return_success(UserDao.to_dict(res))
            token = Auth.create_jwt({
                'account': res[0][0],
                'login_time': login_time
            })
            resp['token'] = token
            return jsonify(resp)
        else:
            return jsonify(return_unsuccess('Error: No such user'))
    else:
        return jsonify(return_unsuccess('Error: Wrong Login Method'))