pastafari2/libraries/authplugin.py
2025-04-28 00:54:41 +02:00

66 lines
2.1 KiB
Python

"""
Paramecio2fm is a series of wrappers for Flask, mako and others and construct a simple headless cms.
Copyright (C) 2023 Antonio de la Rosa Caballero
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
from flask import g, session, redirect, url_for
from functools import wraps
from paramecio2.libraries.db.webmodel import WebModel
def auth_plugin(f):
@wraps(f)
def wrapper(*args, **kwds):
"""Wrapper function for check login in your pastafari2 api
Wrapper function for check login in your pastafari2 api
Args:
*args (mixed): The args of function
**kwds (mixed): Standard python extra arguments of function
Returns:
wrapper (function): Return the wrapper.
"""
auth=request.headers.get('Authorization', None)
if auth:
#print(request.headers['Authorization'])
bearer=request.headers['Authorization'].replace('Bearer', '').strip()
db=kwargs['db']
num_token=0
with db.query('select count(*) as num_token from usertoken WHERE token=%s', [bearer]) as cursor:
num_token=cursor.fetchone()['num_token']
if num_token==0:
return abort(403, 'You need a valid bearer token for access')
else:
return abort(403, 'You need a valid bearer token for access')
code=f(*args, **kwds)
return code
return wrapper