将 Auth0 装饰器与 Flask-RESTful 资源一起使用

Use Auth0 decorator with Flask-RESTful resource

我需要为我的 Flask-RESTful 应用程序使用 Auth0。 Auth0 有一个 example 在视图函数上使用 requires_auth 装饰器。

@app.route('/secured/ping')
@cross_origin(headers=['Content-Type', 'Authorization'])
@requires_auth
def securedPing():
    return "All good. You only get this message if you're authenticated"

Flask-RESTful 我使用 add_resourceResource class,而不是 app.route 和视图函数。如何将 requires_auth 应用到 Version

app = Flask(__name__)
API = Api(app)
CORS = CORS(app, resources={r'/api/*': {'origins': '*'}})
API.add_resource(Version, '/api/v1')

Flask-Restful 文档描述了如何 specify decorators for a resource

There is a property on the Resource class called method_decorators. You can subclass the Resource and add your own decorators that will be added to all method functions in resource.

class AuthResource(Resource):
    method_decorators = [requires_auth]

# inherit AuthResource instead of Resource to define Version