From b9966295bf0af1f504035999be997dc495b81887 Mon Sep 17 00:00:00 2001 From: Marcial Rosales Date: Wed, 19 Jun 2024 18:31:49 +0200 Subject: [PATCH] Move most javascript logic to helper.js --- deps/rabbitmq_management/priv/www/index.html | 2 +- .../priv/www/js/oidc-oauth/helper.js | 23 ++++++++++++++++ .../src/rabbit_mgmt_oauth_bootstrap.erl | 26 +++++++------------ 3 files changed, 34 insertions(+), 17 deletions(-) diff --git a/deps/rabbitmq_management/priv/www/index.html b/deps/rabbitmq_management/priv/www/index.html index 659fa9efea05..b72e0cda9c80 100644 --- a/deps/rabbitmq_management/priv/www/index.html +++ b/deps/rabbitmq_management/priv/www/index.html @@ -22,7 +22,7 @@ diff --git a/deps/rabbitmq_management/priv/www/js/oidc-oauth/helper.js b/deps/rabbitmq_management/priv/www/js/oidc-oauth/helper.js index 48689c115ade..1f4e3ae5599b 100644 --- a/deps/rabbitmq_management/priv/www/js/oidc-oauth/helper.js +++ b/deps/rabbitmq_management/priv/www/js/oidc-oauth/helper.js @@ -86,6 +86,29 @@ function auth_settings_apply_defaults(authSettings) { return authSettings; } +var oauth_settings = { oauth_enabled : false} + +export function set_oauth_settings(settings) { + oauth_settings = settings +} +function get_oauth_settings() { + return oauth_settings +} + +export function oauth_initialize_if_required(state = "index") { + let oauth = oauth_initialize(get_oauth_settings()) + if (!oauth.enabled) return oauth; + switch (state) { + case 'login-callback': + oauth_completeLogin(); break; + case 'logout-callback': + oauth_completeLogout(); break; + default: + oauth = oauth_initiate(oauth); + } + return oauth; +} + export function oauth_initiate(oauth) { if (oauth.enabled) { if (!oauth.sp_initiated) { diff --git a/deps/rabbitmq_management/src/rabbit_mgmt_oauth_bootstrap.erl b/deps/rabbitmq_management/src/rabbit_mgmt_oauth_bootstrap.erl index 6603a2d4ad42..4856b57dba3c 100644 --- a/deps/rabbitmq_management/src/rabbit_mgmt_oauth_bootstrap.erl +++ b/deps/rabbitmq_management/src/rabbit_mgmt_oauth_bootstrap.erl @@ -18,24 +18,18 @@ init(Req0, State) -> bootstrap_oauth(Req0, State) -> AuthSettings = rabbit_mgmt_wm_auth:authSettings(), Dependencies = oauth_dependencies(), - JSContent = case proplists:get_value(oauth_enabled, AuthSettings, false) of - false -> declare_oauth_initialize_if_required(AuthSettings); - true -> import_dependencies(Dependencies) ++ - declare_oauth_initialize_if_required(AuthSettings) ++ - set_token_auth(Req0) ++ - export_dependencies(Dependencies) - - end ++ export_dependencies(["oauth_initialize_if_required"]), + JSContent = import_dependencies(Dependencies) ++ + set_oauth_settings(AuthSettings) ++ + case proplists:get_value(oauth_enabled, AuthSettings, false) of + true -> set_token_auth(Req0) ++ export_dependencies(oauth_dependencies()); + false -> export_dependencies(["oauth_initialize_if_required", "set_oauth_settings"]) + end, {ok, cowboy_req:reply(200, #{<<"content-type">> => <<"text/javascript; charset=utf-8">>}, JSContent, Req0), State}. -declare_oauth_initialize_if_required(AuthSettings) -> +set_oauth_settings(AuthSettings) -> case proplists:get_value(oauth_enabled, AuthSettings, false) of - true -> ["export default function oauth_initialize_if_required(state) { ", - "let oauth = oauth_initialize(", rabbit_json:encode(rabbit_mgmt_format:format_nulls(AuthSettings)), "); ", - "if (!oauth.enabled) return oauth;" - "switch (state) { case 'login-callback': oauth_completeLogin(); break; case 'logout-callback': oauth_completeLogout(); break; default: oauth = oauth_initiate(oauth);}", - "return oauth; }"]; - false -> ["export default function oauth_initialize_if_required(state) { return {oauth_enabled: false}; }"] + true -> ["set_oauth_settings(", rabbit_json:encode(rabbit_mgmt_format:format_nulls(AuthSettings)), "); "]; + false -> ["set_oauth_settings({oauth_enabled: false});"] end. set_token_auth(Req0) -> @@ -52,7 +46,7 @@ import_dependencies(Dependencies) -> ["import {", string:join(Dependencies, ","), "} from './helper.js';"]. oauth_dependencies() -> - ["hasAnyResourceServerReady", "oauth_initialize", "oauth_initiate", "oauth_initiateLogin", "oauth_initiateLogout", "oauth_completeLogin", "oauth_completeLogout"]. + ["oauth_initialize_if_required", "hasAnyResourceServerReady", "oauth_initialize", "oauth_initiate", "oauth_initiateLogin", "oauth_initiateLogout", "oauth_completeLogin", "oauth_completeLogout", "set_oauth_settings"]. export_dependencies(Dependencies) -> [ io_lib:format("window.~s = ~s;", [Dep, Dep]) || Dep <- Dependencies ].