From 78e94da08c83da3ab9b2f52f460b5c14eb86d06f Mon Sep 17 00:00:00 2001 From: Son NK <> Date: Fri, 24 Apr 2020 14:06:42 +0200 Subject: [PATCH] support user already authenticated in verify_api_key --- app/api/base.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/app/api/base.py b/app/api/base.py index c5021e74..2a87f661 100644 --- a/app/api/base.py +++ b/app/api/base.py @@ -2,7 +2,7 @@ from functools import wraps import arrow from flask import Blueprint, request, jsonify, g - +from flask_login import current_user from app.extensions import db from app.models import ApiKey @@ -12,18 +12,21 @@ api_bp = Blueprint(name="api", import_name=__name__, url_prefix="/api") def verify_api_key(f): @wraps(f) def decorated(*args, **kwargs): - api_code = request.headers.get("Authentication") - api_key = ApiKey.get_by(code=api_code) + if current_user.is_authenticated: + g.user = current_user + else: + api_code = request.headers.get("Authentication") + api_key = ApiKey.get_by(code=api_code) - if not api_key: - return jsonify(error="Wrong api key"), 401 + if not api_key: + return jsonify(error="Wrong api key"), 401 - # Update api key stats - api_key.last_used = arrow.now() - api_key.times += 1 - db.session.commit() + # Update api key stats + api_key.last_used = arrow.now() + api_key.times += 1 + db.session.commit() - g.user = api_key.user + g.user = api_key.user return f(*args, **kwargs)