Skip to content

Commit

Permalink
Move most javascript logic to helper.js
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcialRosales authored and michaelklishin committed Jun 20, 2024
1 parent ccb0059 commit b996629
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 17 deletions.
2 changes: 1 addition & 1 deletion deps/rabbitmq_management/priv/www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<link href="favicon.ico" rel="shortcut icon" type="image/x-icon"/>

<script type="module">
window.oauth = oauth_initialize_if_required("index");
window.oauth = oauth_initialize_if_required();

</script>

Expand Down
23 changes: 23 additions & 0 deletions deps/rabbitmq_management/priv/www/js/oidc-oauth/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
26 changes: 10 additions & 16 deletions deps/rabbitmq_management/src/rabbit_mgmt_oauth_bootstrap.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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) ->
Expand All @@ -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 ].

0 comments on commit b996629

Please sign in to comment.