2 # Copyright (C) 2019 rubenwardy
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <https://www.gnu.org/licenses/>.
17 from flask import request, make_response, jsonify, abort
18 from app.models import APIToken
19 from .support import error
20 from functools import wraps
24 def decorated_function(*args, **kwargs):
27 value = request.headers.get("authorization")
30 elif value[0:7].lower() == "bearer ":
31 access_token = value[7:]
32 if len(access_token) < 10:
33 error(400, "API token is too short")
35 token = APIToken.query.filter_by(access_token=access_token).first()
37 error(403, "Unknown API token")
39 abort(403, "Unsupported authentication method")
41 return f(token=token, *args, **kwargs)
43 return decorated_function