首页 > 解决方案 > 我们如何在自定义端点上使用 python-eve @requires_auth 装饰器?

问题描述

按照 Nicola https://nicolaiarocci.com/building-custom-endpoint-handlers-with-eve/关于 requires_auth 装饰器的示例,我无法让它按预期工作,我需要一些帮助。

本质上,它似乎适用于默认身份验证方法,但我需要它与我的自定义身份验证类一起使用。

settings.py我定义了一个资源..

my = {
    'schema': {
        'test': {
            'type': 'string',
        },
    },
    'authentication': clientAuth,
    'resource_methods': ['GET', 'POST'],
}

DOMAIN = {
            'my': my,
          }

然后我定义了这样的clientAuth类..

class clientAuth(TokenAuth):
    def check_auth(self, token, allowed_roles, resource, method):
        clients = app.data.driver.db['clients']
        lookup = {'token': token}
        if allowed_roles:
            lookup['roles'] = {'$in': allowed_roles}
        client = clients.find_one(lookup)
        if client:

            slug = client['slug']
            add_db_to_config(app, slug)
            self.set_mongo_prefix(slug)
        return client

我的自定义端点看起来像这样..

@app.route('/test')
@requires_auth("my")
def my():
    # stuff

我希望(可能是错误的)装饰器中的参数应该使用域中定义的身份验证my方法?requires_authmy

如果我错了,如何在我的自定义端点中使用自定义身份验证类?

谢谢!

请参阅如何使用 BasicAuth 保护自定义端点?

https://github.com/pyeve/eve/issues/860

标签: pythoneve

解决方案


推荐阅读