contentdb/app/blueprints/api/auth.py

47 lines
1.3 KiB
Python
Raw Normal View History

2020-07-12 17:34:25 +02:00
# ContentDB
2019-11-22 15:33:22 +01:00
# Copyright (C) 2019 rubenwardy
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
2020-12-04 03:23:04 +01:00
from functools import wraps
from flask import request, abort
2019-11-22 15:33:22 +01:00
from app.models import APIToken
from .support import error
2020-12-04 03:23:04 +01:00
2019-11-22 15:33:22 +01:00
def is_api_authd(f):
@wraps(f)
def decorated_function(*args, **kwargs):
token = None
value = request.headers.get("authorization")
if value is None:
pass
elif value[0:7].lower() == "bearer ":
access_token = value[7:]
if len(access_token) < 10:
error(400, "API token is too short")
2019-11-22 15:33:22 +01:00
token = APIToken.query.filter_by(access_token=access_token).first()
if token is None:
error(403, "Unknown API token")
2019-11-22 15:33:22 +01:00
else:
abort(403, "Unsupported authentication method")
2019-11-22 15:33:22 +01:00
return f(token=token, *args, **kwargs)
return decorated_function