mirror of
https://github.com/simple-login/app.git
synced 2024-11-17 22:21:38 +08:00
support user already authenticated in verify_api_key
This commit is contained in:
parent
ae353dbb25
commit
78e94da08c
1 changed files with 13 additions and 10 deletions
|
@ -2,7 +2,7 @@ from functools import wraps
|
||||||
|
|
||||||
import arrow
|
import arrow
|
||||||
from flask import Blueprint, request, jsonify, g
|
from flask import Blueprint, request, jsonify, g
|
||||||
|
from flask_login import current_user
|
||||||
from app.extensions import db
|
from app.extensions import db
|
||||||
from app.models import ApiKey
|
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):
|
def verify_api_key(f):
|
||||||
@wraps(f)
|
@wraps(f)
|
||||||
def decorated(*args, **kwargs):
|
def decorated(*args, **kwargs):
|
||||||
api_code = request.headers.get("Authentication")
|
if current_user.is_authenticated:
|
||||||
api_key = ApiKey.get_by(code=api_code)
|
g.user = current_user
|
||||||
|
else:
|
||||||
|
api_code = request.headers.get("Authentication")
|
||||||
|
api_key = ApiKey.get_by(code=api_code)
|
||||||
|
|
||||||
if not api_key:
|
if not api_key:
|
||||||
return jsonify(error="Wrong api key"), 401
|
return jsonify(error="Wrong api key"), 401
|
||||||
|
|
||||||
# Update api key stats
|
# Update api key stats
|
||||||
api_key.last_used = arrow.now()
|
api_key.last_used = arrow.now()
|
||||||
api_key.times += 1
|
api_key.times += 1
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
g.user = api_key.user
|
g.user = api_key.user
|
||||||
|
|
||||||
return f(*args, **kwargs)
|
return f(*args, **kwargs)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue