Python 如何使用BasicAuth保护自定义端点?

Python 如何使用BasicAuth保护自定义端点?,python,eve,Python,Eve,假设我已使用BasicAuth启用对资源的身份验证: class MyBasicAuth(BasicAuth): def check_auth(self,username,password,allowed_roles,resource,method): return username == 'secretusername' and password == 'secretpass' 我还有自定义路由,用于从HTML视图管理文档。如何使用相同的MyBasicAuth保护所有

假设我已使用BasicAuth启用对资源的身份验证:

class MyBasicAuth(BasicAuth):
    def check_auth(self,username,password,allowed_roles,resource,method):
        return username == 'secretusername' and password == 'secretpass'
我还有自定义路由,用于从HTML视图管理文档。如何使用相同的MyBasicAuth保护所有自定义路由?我还需要实现使用上述MyBasicAuth进行身份验证的逻辑。
请帮我做这个。这是供个人使用的,所以我更喜欢硬编码用户名和密码。

如果您使用flask blueprint来定制路线,您可以为blueprint添加一个请求前功能

首先,创建一个函数来检查蓝图的身份验证。您需要自己从flask请求中获取
授权
头,如下所示:

from flask import request, abort, current_app
from werkzeug.http import parse_authorization_header

def check_blueprint_auth():
    if 'Authorization' not in request.headers:
        print('Authorization header not found for authentication')
        return abort(401, 'Authorization header not found for authentication')
    header = parse_authorization_header(request.headers['Authorization'])
    username = None if header is None else header['username']
    password = None if header is None else header['password']

    return username == 'secretusername' and password == 'secretpass'
然后,您可以将此函数设置为在每个blueprint请求之前调用。以下是蓝图定义示例,设置请求前的
功能:

from flask import Blueprint, current_app as app
# your auth function
from auth import check_blueprint_auth

blueprint = Blueprint('prefix_uri', __name__)

# this sets the auth function to be called
blueprint.before_request(check_blueprint_auth)


@blueprint.route('/custom_route/<some_value>', methods=['POST'])
def post_something(some_value):
#  something

希望能有所帮助。

您可以利用Eve本身内部使用的
requires\u auth
decorator。这样,auth类也将用于保护自定义路由:

from eve import Eve
from eve.auth import requires_auth

app = Eve()

@app.route('/hello')
@requires_auth('resource')
def hello_world():
    return 'Hello World!'


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

如果您试图使用自定义的端点身份验证,您会发现很难使用,如下所述: 最后,我编写了一个包装器来解决“资源”没有传递给“requires\u auth”的问题:

def auth_resource(resource):
def fdec(f):
    @wraps(f)
    def wrapped(*args, **kwargs):
            return f(resource=resource, *args, **kwargs)
    return wrapped
return fdec
通过这种方式,您可以在域中定义身份验证类:

DOMAIN = {
    'testendpoint'= {'authentication':MyCustomAuthetication},
    'otherendpoints'=...
在我的应用程序中,我包装了requires_auth decorator,并将其添加为身份验证资源

@app.route('/testendpoint/<item>', methods=['GET'])
@auth_resource('testendpoint')
@requires_auth('item')
def my_end_point_function(*args, **kwargs):
    dosomthinghere
@app.route('/testendpoint/',方法=['GET'])
@验证资源('testendpoint')
@需要_auth('项目')
定义my_end_point_函数(*args,**kwargs):
多索姆辛

只要在端点的设置文件中定义了身份验证类,这也允许您重用在另一个端点中定义的任何身份验证,如果您想确保所有端点使用相同的身份验证,这可能很方便。

谢谢。我用会话身份验证修复了它。我也要试试这个。。。谢谢,不客气。如果对您有效,请确保将其标记为已解决,以便其他人可以在其他人中发现此解决方案。哦,这更简单@NicolaIarocci!它应该在eve文档中提供。@gcw它是基本的烧瓶材料,而不是eve,尽管我同意Required_auth实际上是eve人工制品:)我在我的网站上有一篇关于它的博客文章。应该放在文档的代码片段部分。
@app.route('/testendpoint/<item>', methods=['GET'])
@auth_resource('testendpoint')
@requires_auth('item')
def my_end_point_function(*args, **kwargs):
    dosomthinghere