gpt4 book ai didi

python - 如何在受用户限制的资源访问保护的 python eve api 中创建新的用户帐户

转载 作者:太空狗 更新时间:2023-10-30 02:59:56 24 4
gpt4 key购买 nike

我首先使用 python-eve 框架创建了一个 web api,没有身份验证或用户帐户,而且效果很好!我现在正在尝试添加身份验证和用户帐户,但遇到了一些困难。我想使用用户限制的资源访问权限,但如果资源受到限制,用户如何创建新的用户帐户?我错过了什么?

我一直在努力关注Restful Account Management Tutorial以及Authentication and Authorization的介绍在 python-eve.org 上,我搜索了 stackoverflow,包括这个 answer here .

这是我的实现:

运行.py

import os.path
from eve import Eve
import my_auth
from flask.ext.bootstrap import Bootstrap
from eve_docs import eve_docs

app = Eve(auth=my_auth.BCryptAuth, settings = 'deployed_settings.py')

app.on_insert_accounts += my_auth.create_user
Bootstrap(app)
app.register_blueprint(eve_docs, url_prefix='/docs')

if __name__ == '__main__':
app.run()

我的授权.py

import bcrypt
from eve import Eve
from eve.auth import BasicAuth

class BCryptAuth(BasicAuth):
def check_auth(self, username, password, allowed_roles, resource, method):
# use Eve's own db driver; no additional connections/resources are used
accounts = Eve.app.data.driver.db['accounts']
account = accounts.find_one({'username': username})
if account and 'user_id' in account:
self.set_request_auth_value(account['user_id'])
return account and bcrypt.hashpw(
password.encode('utf-8'),account['salt'].encode('utf-8')) == account['password']

def create_user(documents):
for document in documents:
document['salt'] = bcrypt.gensalt().encode('utf-8')
password = document['password'].encode('utf-8')
document['password'] = bcrypt.hashpw(password, document['salt'])

部署设置.py

# We are running on a local machine, so just use the local mongod instance.
# Note that MONGO_HOST and MONGO_PORT could very well be left
# out as they already default to a bare bones local 'mongod' instance.
MONGO_HOST = 'localhost'
MONGO_PORT = 27017
MONGO_USERNAME = ''
MONGO_PASSWORD = ''
MONGO_DBNAME = 'practice'

# Name of the field used to store the owner of each document
AUTH_FIELD = 'user_id'

# Enable reads (GET), inserts (POST) and DELETE for resources/collections
# (if you omit this line, the API will default to ['GET'] and provide
# read-only access to the endpoint).
RESOURCE_METHODS = ['GET', 'POST', 'DELETE']

# Enable reads (GET), edits (PATCH), replacements (PUT) and deletes of
# individual items (defaults to read-only item access).
ITEM_METHODS = ['GET', 'PATCH', 'PUT', 'DELETE']
IF_MATCH = False # When set to false, older versions may potentially replace newer versions

XML = False # disable xml output

# Schemas for data objects are defined here:

classes = {
# ... Contents omitted for this question
}

people = {
# ... Contents omitted for this question
}

logs = {
# ... Contents omitted for this question
}

sessions = {
# ... Contents omitted for this question
}


accounts = {
# the standard account entry point is defined as '/accounts/<ObjectId>'.
# an additional read-only entry point is accessible at '/accounts/<username>'.
'additional_lookup': {
'url': 'regex("[\w]+")',
'field': 'username',
},

# disable endpoint caching to prevent apps from caching account data
'cache_control': '',
'cache_expires': 0,

# schema for the accounts endpoint
'schema': {
'username': {
'type': 'string',
'required': True,
'unique': True,
},
'password': {
'type': 'string',
'required': True,
},
},
}

# The DOMAIN dict explains which resources will be available and how they will
# be accessible to the API consumer.
DOMAIN = {
'classes': classes,
'people': people,
'logs': logs,
'sessions': sessions,
'accounts': accounts,
}

最佳答案

一个简单的解决方案是限制您的用户创建方法。像这样:

class BCryptAuth(BasicAuth):
def check_auth(self, username, password, allowed_roles, resource, method):

# allow anyone to create a new account.
if resource == 'accounts' and method == 'POST':
return True

accounts = Eve.app.data.driver.db['accounts']
account = accounts.find_one({'username': username})
if account and 'user_id' in account:
self.set_request_auth_value(account['user_id'])
return account and bcrypt.hashpw(password.encode('utf-8'),account['salt'].encode('utf-8')) == account['password']

或者,尤其是如果您只允许 POST 到 account 端点,您可以选择退出对端点的身份验证:

'accounts': {
# or you could provide a different custom class here,
# so you don't need the guard in the general-purpose auth class.
'authentication': None,
...
}

希望这对您有所帮助。

关于python - 如何在受用户限制的资源访问保护的 python eve api 中创建新的用户帐户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30677825/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com