diff options
author | rubenwardy <rw@rubenwardy.com> | 2019-11-22 14:33:22 +0000 |
---|---|---|
committer | rubenwardy <rw@rubenwardy.com> | 2019-11-27 01:06:58 +0000 |
commit | 4ce388c8aa5d5502408609983535a9812d41d6d1 (patch) | |
tree | 5ad9123949ca2068dfe975284d0f1b3acdf5b437 /app/blueprints/api/auth.py | |
parent | cb5451fe5d49e0eda379e3cd636c54e8ea1a3f8e (diff) | |
download | cheatdb-4ce388c8aa5d5502408609983535a9812d41d6d1.tar.xz |
Add API Token creation
Diffstat (limited to 'app/blueprints/api/auth.py')
-rw-r--r-- | app/blueprints/api/auth.py | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/app/blueprints/api/auth.py b/app/blueprints/api/auth.py new file mode 100644 index 0000000..6eeadde --- /dev/null +++ b/app/blueprints/api/auth.py @@ -0,0 +1,42 @@ +# Content DB +# 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/>. + +from flask import request, make_response, jsonify, abort +from app.models import APIToken +from functools import wraps + +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: + abort(400) + + token = APIToken.query.filter_by(access_token=access_token).first() + if token is None: + abort(403) + else: + abort(403) + + return f(token=token, *args, **kwargs) + + return decorated_function |