【发布时间】: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