]> git.lizzy.rs Git - cheatdb.git/blob - app/blueprints/api/auth.py
234d481a55d6a8f813fc86dd4d2cdbb1ed2295e0
[cheatdb.git] / app / blueprints / api / auth.py
1 # Content DB
2 # Copyright (C) 2019  rubenwardy
3 #
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.
8 #
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.
13 #
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/>.
16
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
21
22 def is_api_authd(f):
23         @wraps(f)
24         def decorated_function(*args, **kwargs):
25                 token = None
26
27                 value = request.headers.get("authorization")
28                 if value is None:
29                         pass
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")
34
35                         token = APIToken.query.filter_by(access_token=access_token).first()
36                         if token is None:
37                                 error(403, "Unknown API token")
38                 else:
39                         abort(403, "Unsupported authentication method")
40
41                 return f(token=token, *args, **kwargs)
42
43         return decorated_function