【问题标题】:EVE REST- Issue with AuthToken in python Eve framework error 401EVE REST-python Eve 框架错误 401 中的 AuthToken 问题
【发布时间】:2015-06-23 09:26:03
【问题描述】:

我目前正在使用 python-eve 库来创建一个 RESTful API,但是当我按照本教程实现“令牌身份验证”时遇到了一些问题,我收到错误 401 说“请提供正确的凭据”。

这是我的用户架构:

RESOURCE_METHODS = ['GET', 'POST']
ITEM_METHODS = ['GET','PATCH','DELETE']

DOMAIN = {
'user': {
    'additional_lookup': {
            'url': 'regex("[\w]+")',
            'field': 'username',
            #'url': '[\w]+',
        },
    'schema': {
        'firstname': {
            'type': 'string'
        },
        'lastname': {
            'type': 'string'
        },
        'phone': {
            'type': 'string'
        },
        'username': {
            'type': 'string',
            'required': True,
            'unique': True,
        },
        'password': {
            'type': 'string',
            'required': True,
        },
        'roles': {
            'type': 'list',
            'allowed': ['user', 'superuser', 'admin'],
            'required': True,
        },
        'token': {
            'type': 'string',
            'required': True,
        }
    },

    'cache_control': '',
    'cache_expires': 0,
    'allowed_roles': ['superuser', 'admin'],
    },

'item': {
    'schema': {
        'name':{
            'type': 'string'
            },
        'username': {
            'type': 'string'
            }
        }
    },

}

这是我的 app.py

from eve import Eve
from eve.auth import TokenAuth
import random
import string


class RolesAuth(TokenAuth):
def check_auth(self, token,  allowed_roles, resource, method):
    accounts = app.data.driver.db['eve']
    lookup = {'token': token}
    if allowed_roles:
        lookup['roles'] = {'$in': allowed_roles}
    account = accounts.find_one(lookup)
    return account

def add_token(documents):
for document in documents:
    document["token"] = (''.join(random.choice(string.ascii_uppercase)
                                 for x in range(10)))

app = Eve(settings='settings.py')

if __name__ == '__main__':
    app = Eve(auth=RolesAuth)
    app.on_insert_accounts += add_token
    app.run()

任何想法为什么会以 401 告终。

我正在使用 python 3.4

如果可能,请向我提供工作代码。我是这个领域的菜鸟。

谢谢!

【问题讨论】:

  • 我不确定要使用 base64 编码哪一个以及如何进行编码。我有一个“用户名”=ab 和“令牌”=54321 的集合

标签: python mongodb authentication flask eve


【解决方案1】:

您需要对令牌进行如下编码:

echo "54321:" | base64

请不要忘记最后:

由于您直接查找token(根据您的代码),因此不需要username

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-19
    • 2023-03-11
    • 2014-01-28
    • 2014-04-11
    • 2015-02-14
    • 1970-01-01
    相关资源
    最近更新 更多