More cookie/auth code cleanup

This commit is contained in:
Alexandre Aubin 2021-08-28 00:53:52 +02:00
parent 9c13472703
commit 6e1e034e10
2 changed files with 16 additions and 12 deletions

View file

@ -28,7 +28,7 @@ class BaseAuthenticator(object):
# Virtual methods # Virtual methods
# Each authenticator classes must implement these methods. # Each authenticator classes must implement these methods.
def authenticate_credentials(self, credentials, store_session=False): def authenticate_credentials(self, credentials):
try: try:
# Attempt to authenticate # Attempt to authenticate

View file

@ -4,6 +4,7 @@ import re
import errno import errno
import logging import logging
import argparse import argparse
from json import dumps as json_encode from json import dumps as json_encode
from tempfile import mkdtemp from tempfile import mkdtemp
from shutil import rmtree from shutil import rmtree
@ -17,7 +18,7 @@ from bottle import abort
from moulinette import m18n, Moulinette from moulinette import m18n, Moulinette
from moulinette.actionsmap import ActionsMap from moulinette.actionsmap import ActionsMap
from moulinette.core import MoulinetteError, MoulinetteValidationError from moulinette.core import MoulinetteError, MoulinetteValidationError, MoulinetteAuthenticationError
from moulinette.interfaces import ( from moulinette.interfaces import (
BaseActionsMapParser, BaseActionsMapParser,
ExtendedArgumentParser, ExtendedArgumentParser,
@ -82,7 +83,7 @@ class APIQueueHandler(logging.Handler):
self.queues = LogQueues() self.queues = LogQueues()
def emit(self, record): def emit(self, record):
s_id = Session.get_infos()["id"] s_id = Session.get_infos(raise_if_no_session_exists=False)["id"]
try: try:
queue = self.queues[s_id] queue = self.queues[s_id]
except KeyError: except KeyError:
@ -247,14 +248,16 @@ class Session:
# samesite="strict", # Bottle 0.12 doesn't support samesite, to be added in next versions # samesite="strict", # Bottle 0.12 doesn't support samesite, to be added in next versions
) )
def get_infos(): def get_infos(raise_if_no_session_exists=True):
try: try:
infos = request.get_cookie( infos = request.get_cookie(
f"session.{Session.actionsmap_name}", secret=Session.secret, default={} f"session.{Session.actionsmap_name}", secret=Session.secret, default={}
) )
except Exception: except Exception:
infos = {} if not raise_if_no_session_exists:
return {"id": random_ascii()}
raise MoulinetteAuthenticationError("unable_authenticate")
if "id" not in infos: if "id" not in infos:
infos["id"] = random_ascii() infos["id"] = random_ascii()
@ -397,17 +400,14 @@ class _ActionsMapPlugin(object):
authenticator = self.actionsmap.get_authenticator(profile) authenticator = self.actionsmap.get_authenticator(profile)
try: try:
auth_info = authenticator.authenticate_credentials( auth_info = authenticator.authenticate_credentials(credentials)
credentials, store_session=True session_infos = Session.get_infos(raise_if_no_session_exists=False)
)
session_infos = Session.get_infos()
session_infos[profile] = auth_info session_infos[profile] = auth_info
except MoulinetteError as e: except MoulinetteError as e:
try: try:
self.logout() self.logout()
except Exception: except Exception:
pass pass
# FIXME : replace with MoulinetteAuthenticationError !?
raise HTTPResponse(e.strerror, 401) raise HTTPResponse(e.strerror, 401)
else: else:
Session.set_infos(session_infos) Session.set_infos(session_infos)
@ -418,7 +418,11 @@ class _ActionsMapPlugin(object):
try: try:
session_infos = Session.get_infos()[authenticator.name] session_infos = Session.get_infos()[authenticator.name]
except KeyError:
# Here, maybe we want to re-authenticate the session via the authenticator
# For example to check that the username authenticated is still in the admin group...
except Exception as e:
msg = m18n.g("authentication_required") msg = m18n.g("authentication_required")
raise HTTPResponse(msg, 401) raise HTTPResponse(msg, 401)
@ -518,7 +522,7 @@ class _ActionsMapPlugin(object):
def display(self, message, style="info"): def display(self, message, style="info"):
s_id = Session.get_infos()["id"] s_id = Session.get_infos(raise_if_no_session_exists=False)["id"]
try: try:
queue = self.log_queues[s_id] queue = self.log_queues[s_id]
except KeyError: except KeyError: