From 2821c899919f7698a9bb42cc1ee432053a38bd6b Mon Sep 17 00:00:00 2001 From: Andrew Baldwin Date: Thu, 31 Oct 2024 14:56:43 +0100 Subject: [PATCH] Add auth info to auth page --- docs/extending-locust.rst | 4 +++- locust/web.py | 34 ++++++++++++++++++++++++++-- locust/webui/src/pages/Auth.tsx | 4 ++++ locust/webui/src/types/auth.types.ts | 1 + 4 files changed, 40 insertions(+), 3 deletions(-) diff --git a/docs/extending-locust.rst b/docs/extending-locust.rst index f6ddbc893f..131fc24bd1 100644 --- a/docs/extending-locust.rst +++ b/docs/extending-locust.rst @@ -154,7 +154,9 @@ to the ``login_manager``. The ``user_loader`` should return ``None`` to deny aut authentication to the app should be granted. To display errors on the login page, such as an incorrect username / password combination, you may store the ``auth_error`` -on the session object: ``session["auth_error"] = "Incorrect username or password"``. +on the session object: ``session["auth_error"] = "Incorrect username or password"``. If you have non-erroneous information +you would like to display to the user, you can opt instead to set ``auth_info`` on the session object: +``session["auth_info"] = "Successfully created new user!"`` A full example can be seen `in the auth example `_. diff --git a/locust/web.py b/locust/web.py index 365665539c..3aeb4af294 100644 --- a/locust/web.py +++ b/locust/web.py @@ -11,7 +11,7 @@ from itertools import chain from json import dumps from time import time -from typing import TYPE_CHECKING, Any +from typing import TYPE_CHECKING, Any, TypedDict import gevent from flask import ( @@ -53,6 +53,34 @@ DEFAULT_CACHE_TIME = 2.0 +class InputField(TypedDict): + label: str + name: str + default_value: bool | None + choices: list[str] | None + is_secret: bool | None + + +class CustomForm(TypedDict): + inputs: list[InputField] | None + callback_url: str + submit_button_text: str | None + + +class AuthProvider(TypedDict): + label: str | None + callback_url: str + icon_url: str | None + + +class AuthArgs(TypedDict, total=False): + custom_form: CustomForm + auth_providers: AuthProvider + username_password_callback: str + error: str + info: str + + class WebUI: """ Sets up and runs a Flask web app that can start and stop load tests using the @@ -84,7 +112,7 @@ def my_custom_route(): """Arguments used to render index.html for the web UI. Must be used with custom templates extending index.html.""" - auth_args: dict[str, Any] + auth_args: AuthArgs """Arguments used to render auth.html for the web UI auth page. Must be used when configuring auth""" def __init__( @@ -506,6 +534,7 @@ def login(): return redirect(url_for("index")) self.auth_args["error"] = session.get("auth_error", None) + self.auth_args["info"] = session.get("auth_info", None) return render_template_from( "auth.html", @@ -577,6 +606,7 @@ def wrapper(*args, **kwargs): if self.web_login: try: session["auth_error"] = None + session["auth_info"] = None return login_required(view_func)(*args, **kwargs) except Exception as e: return f"Locust auth exception: {e} See https://docs.locust.io/en/stable/extending-locust.html#adding-authentication-to-the-web-ui for configuring authentication." diff --git a/locust/webui/src/pages/Auth.tsx b/locust/webui/src/pages/Auth.tsx index cbb59ad46c..c9ceb652ce 100644 --- a/locust/webui/src/pages/Auth.tsx +++ b/locust/webui/src/pages/Auth.tsx @@ -13,6 +13,7 @@ export default function Auth({ authProviders, customForm, error, + info, usernamePasswordCallback, }: IAuthArgs) { const theme = useCreateTheme(); @@ -36,6 +37,7 @@ export default function Auth({ rowGap: 4, boxShadow: 24, borderRadius: 4, + width: 400, border: '3px solid black', p: 4, }} @@ -48,6 +50,7 @@ export default function Auth({ + {info && {info}} {error && {error}}