2021-09-09 12:49:23 +02:00
|
|
|
# -*- coding: UTF-8 -*
|
|
|
|
# Authentication code borrowed from Miguel Grinberg's Mega Tutorial
|
|
|
|
# (see https://github.com/miguelgrinberg/microblog)
|
2021-12-21 00:10:51 +01:00
|
|
|
# and modified for ScoDoc
|
2021-09-09 12:49:23 +02:00
|
|
|
|
|
|
|
# Under The MIT License (MIT)
|
|
|
|
|
|
|
|
# Copyright (c) 2017 Miguel Grinberg
|
|
|
|
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
# this software and associated documentation files (the "Software"), to deal in
|
|
|
|
# the Software without restriction, including without limitation the rights to
|
|
|
|
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
# the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
# subject to the following conditions:
|
|
|
|
|
|
|
|
# The above copyright notice and this permission notice shall be included in all
|
|
|
|
# copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2022-04-13 12:39:10 +02:00
|
|
|
from functools import wraps
|
|
|
|
|
2021-12-21 00:10:51 +01:00
|
|
|
from flask import g
|
2021-09-09 12:49:23 +02:00
|
|
|
from flask_httpauth import HTTPBasicAuth, HTTPTokenAuth
|
2022-07-24 15:51:13 +02:00
|
|
|
import flask_login
|
2022-07-22 16:39:21 +02:00
|
|
|
from flask_login import current_user
|
2022-05-03 08:55:56 +02:00
|
|
|
|
|
|
|
from app import log
|
2021-09-09 12:49:23 +02:00
|
|
|
from app.auth.models import User
|
|
|
|
from app.api.errors import error_response
|
|
|
|
|
|
|
|
basic_auth = HTTPBasicAuth()
|
|
|
|
token_auth = HTTPTokenAuth()
|
|
|
|
|
|
|
|
|
|
|
|
@basic_auth.verify_password
|
|
|
|
def verify_password(username, password):
|
2022-05-03 13:35:17 +02:00
|
|
|
"Verify password for this user"
|
2021-10-16 23:22:03 +02:00
|
|
|
user = User.query.filter_by(user_name=username).first()
|
2021-09-09 12:49:23 +02:00
|
|
|
if user and user.check_password(password):
|
2021-12-21 00:10:51 +01:00
|
|
|
g.current_user = user
|
2022-05-03 13:35:17 +02:00
|
|
|
# note: est aussi basic_auth.current_user()
|
2021-09-09 12:49:23 +02:00
|
|
|
return user
|
|
|
|
|
|
|
|
|
|
|
|
@basic_auth.error_handler
|
|
|
|
def basic_auth_error(status):
|
2022-05-03 13:35:17 +02:00
|
|
|
"error response (401 for invalid auth.)"
|
2021-09-09 12:49:23 +02:00
|
|
|
return error_response(status)
|
|
|
|
|
|
|
|
|
|
|
|
@token_auth.verify_token
|
2022-05-03 13:35:17 +02:00
|
|
|
def verify_token(token) -> User:
|
2022-07-22 16:39:21 +02:00
|
|
|
"""Retrouve l'utilisateur à partir du jeton.
|
|
|
|
Si la requête n'a pas de jeton, token == "".
|
|
|
|
"""
|
|
|
|
|
2021-12-21 00:10:51 +01:00
|
|
|
user = User.check_token(token) if token else None
|
2022-07-24 15:51:13 +02:00
|
|
|
flask_login.login_user(user)
|
2021-12-21 00:10:51 +01:00
|
|
|
g.current_user = user
|
|
|
|
return user
|
2021-09-09 12:49:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
@token_auth.error_handler
|
|
|
|
def token_auth_error(status):
|
2022-07-22 16:39:21 +02:00
|
|
|
"Réponse en cas d'erreur d'auth."
|
2021-09-09 12:49:23 +02:00
|
|
|
return error_response(status)
|
2021-10-28 00:52:23 +02:00
|
|
|
|
|
|
|
|
2021-12-21 00:10:51 +01:00
|
|
|
@token_auth.get_user_roles
|
|
|
|
def get_user_roles(user):
|
|
|
|
return user.roles
|
2021-10-28 00:52:23 +02:00
|
|
|
|
|
|
|
|
2022-04-13 12:39:10 +02:00
|
|
|
def token_permission_required(permission):
|
2022-07-22 16:39:21 +02:00
|
|
|
"Décorateur pour les fonctions de l'API ScoDoc"
|
2022-05-03 13:35:17 +02:00
|
|
|
|
2022-04-13 12:39:10 +02:00
|
|
|
def decorator(f):
|
|
|
|
@wraps(f)
|
|
|
|
def decorated_function(*args, **kwargs):
|
2022-05-03 13:35:17 +02:00
|
|
|
# token_auth.login_required()
|
|
|
|
current_user = basic_auth.current_user()
|
|
|
|
if not current_user or not current_user.has_permission(permission, None):
|
|
|
|
if current_user:
|
2022-07-22 16:39:21 +02:00
|
|
|
message = f"API permission denied (user {current_user})"
|
2022-05-03 08:55:56 +02:00
|
|
|
else:
|
2022-07-22 16:39:21 +02:00
|
|
|
message = f"API permission denied (no user supplied)"
|
|
|
|
log(message)
|
|
|
|
# raise werkzeug.exceptions.Forbidden(description=message)
|
|
|
|
return error_response(403, message=None)
|
2022-04-13 12:39:10 +02:00
|
|
|
return f(*args, **kwargs)
|
2021-12-21 00:10:51 +01:00
|
|
|
|
2022-05-03 13:35:17 +02:00
|
|
|
# return decorated_function(token_auth.login_required())
|
|
|
|
return decorated_function
|
2021-12-21 00:10:51 +01:00
|
|
|
|
2022-04-13 12:39:10 +02:00
|
|
|
return decorator
|
2022-07-22 16:39:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
def permission_required_api(permission_web, permission_api):
|
|
|
|
"""Décorateur pour les fonctions de l'API accessibles en mode jeton
|
|
|
|
ou en mode web.
|
|
|
|
Si cookie d'authentification web, utilise pour se logger et calculer les
|
|
|
|
permissions.
|
|
|
|
Sinon, tente le jeton jwt.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def decorator(f):
|
|
|
|
@wraps(f)
|
|
|
|
def decorated_function(*args, **kwargs):
|
|
|
|
scodoc_dept = getattr(g, "scodoc_dept", None)
|
|
|
|
if not current_user.has_permission(permission_web, scodoc_dept):
|
|
|
|
# try API
|
|
|
|
return token_auth.login_required(
|
|
|
|
token_permission_required(permission_api)(f)
|
|
|
|
)(*args, **kwargs)
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
|
|
|
|
return decorated_function
|
|
|
|
|
|
|
|
return decorator
|