我的应用中有用户资源,并已实施BasicAuth方案。
以下是我的安全要求 -
实现这些要求的最简单方法是访问check_auth方法中的查找字典,然后我可以将经过身份验证的用户与电子邮件参数进行比较。但我找不到任何办法这样做。相反,我想出了使用事件挂钩的以下hacky解决方案。
class UserAuth(BasicAuth):
def check_auth(self, username, password, allowed_roles, resource, method):
try:
user = db.session.query(User).filter(User.email == username).one()
return user.authenticate(password)
except Exception as e:
# TODO: Log this exception
print('Error - {}'.format(e))
return False
def authz(req, lookup):
authn_user = db.session.query(User).filter(User.email == req.authorization['username']).one()
if 'email' in lookup and lookup['email'] == req.authorization['username']:
print('User requesting their own profile')
return
elif authn_user.is_superuser:
print('Superuser requesting something')
return
else:
print('Not authorized. Sabotaging request.')
lookup['email'] = 'None'
app.on_pre_GET_users += authz
我的settings.py文件中的代码段 -
DOMAIN['users']['public_methods'] = ['POST']
DOMAIN['users']['additional_lookup'] = {'url': 'regex("[\w]+@[\w]+\.[\w]+")', 'field': 'email'}
有没有更好的方法来设计它?
答案 0 :(得分:1)
一个简单的选择是使用Flask中的request
对象:
from flask import request
class UserAuth(BasicAuth):
def check_auth(self, username, password, allowed_roles, resource, method):
# you can inspect the request object here
print request.args, request.url, request.headers
return True