【发布时间】:2019-07-02 06:55:04
【问题描述】:
我正在尝试为我的所有 Flask app.routes 设置身份验证装饰器。每次调用与路由关联的函数时,我都会检查请求是否传递了正确的键。
我已经创建了一个装饰器(这可能是错误的)来做到这一点。首先它说它没有配置返回值,我后来修复了它。当我尝试再次运行它时,错误提示:
"TypeError: unregisterApi() 接受 0 个位置参数,但给出了 2 个 视图函数未返回有效响应。返回类型必须是字符串、元组、响应实例或 WSGI 可调用的,但它是一个函数。 ”。
我确定问题出在我的装饰器上,但我不知道如何解决。
以下是装饰器和被装饰函数的代码:
装饰者:
def requestAuth(func):
def funcWrapper():
data = request.get_json(force=True)
apiAddr = request.remote_addr
apiKey = data["API Register Key"]
conn = sqlite3.connect("database/controllerConfiguration.db")
cursor = conn.cursor()
cursor.execute('select apikey from SystemAPI where apihost = \"{0}\";'.format(apiAddr))
result = cursor.fetchall()
if result[0][0] != apiKey:
return "ERROR - Authentication with the controller failed",400
elif result[0][0] == apiKey:
return func
return funcWrapper
它修饰的路由关联函数:
@app.route('/unregister',methods=['POST'])
@requestAuth
def unregisterApi():
data = request.get_json(force=True)
apiKey = data["API Register Key"]
apiAddr = request.remote_addr
conn = sqlite3.connect('database/controllerConfiguration.db')
cursor = conn.cursor()
cursor.execute('delete from SystemAPI where apikey = \"{}\";'.format(apiKey))
conn.commit()
conn.close()
return jsonify({"Response":"Success"})
我希望达到的是:
每次调用路由 (/unregister,/register,...) 函数时,@requestAuth 装饰器都会检查是否传递了正确的键,如果是,它将恢复到路由函数并执行它应该执行的操作,如果键不匹配它会简单地返回错误字符串以及相关的状态码(string: "ERROR - Authentication with the controller failed", Status code: 400).
【问题讨论】:
标签: python python-3.x flask python-decorators