Skip to content

Commit c144df6

Browse files
MarcialRosalesmergify[bot]
authored andcommitted
Move most javascript logic to helper.js
(cherry picked from commit b996629)
1 parent dd905da commit c144df6

File tree

3 files changed

+34
-17
lines changed

3 files changed

+34
-17
lines changed

deps/rabbitmq_management/priv/www/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<link href="favicon.ico" rel="shortcut icon" type="image/x-icon"/>
2323

2424
<script type="module">
25-
window.oauth = oauth_initialize_if_required("index");
25+
window.oauth = oauth_initialize_if_required();
2626

2727
</script>
2828

deps/rabbitmq_management/priv/www/js/oidc-oauth/helper.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,29 @@ function auth_settings_apply_defaults(authSettings) {
8686
return authSettings;
8787
}
8888

89+
var oauth_settings = { oauth_enabled : false}
90+
91+
export function set_oauth_settings(settings) {
92+
oauth_settings = settings
93+
}
94+
function get_oauth_settings() {
95+
return oauth_settings
96+
}
97+
98+
export function oauth_initialize_if_required(state = "index") {
99+
let oauth = oauth_initialize(get_oauth_settings())
100+
if (!oauth.enabled) return oauth;
101+
switch (state) {
102+
case 'login-callback':
103+
oauth_completeLogin(); break;
104+
case 'logout-callback':
105+
oauth_completeLogout(); break;
106+
default:
107+
oauth = oauth_initiate(oauth);
108+
}
109+
return oauth;
110+
}
111+
89112
export function oauth_initiate(oauth) {
90113
if (oauth.enabled) {
91114
if (!oauth.sp_initiated) {

deps/rabbitmq_management/src/rabbit_mgmt_oauth_bootstrap.erl

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,18 @@ init(Req0, State) ->
2424
bootstrap_oauth(Req0, State) ->
2525
AuthSettings = rabbit_mgmt_wm_auth:authSettings(),
2626
Dependencies = oauth_dependencies(),
27-
JSContent = case proplists:get_value(oauth_enabled, AuthSettings, false) of
28-
false -> declare_oauth_initialize_if_required(AuthSettings);
29-
true -> import_dependencies(Dependencies) ++
30-
declare_oauth_initialize_if_required(AuthSettings) ++
31-
set_token_auth(Req0) ++
32-
export_dependencies(Dependencies)
33-
34-
end ++ export_dependencies(["oauth_initialize_if_required"]),
27+
JSContent = import_dependencies(Dependencies) ++
28+
set_oauth_settings(AuthSettings) ++
29+
case proplists:get_value(oauth_enabled, AuthSettings, false) of
30+
true -> set_token_auth(Req0) ++ export_dependencies(oauth_dependencies());
31+
false -> export_dependencies(["oauth_initialize_if_required", "set_oauth_settings"])
32+
end,
3533
{ok, cowboy_req:reply(200, #{<<"content-type">> => <<"text/javascript; charset=utf-8">>}, JSContent, Req0), State}.
3634

37-
declare_oauth_initialize_if_required(AuthSettings) ->
35+
set_oauth_settings(AuthSettings) ->
3836
case proplists:get_value(oauth_enabled, AuthSettings, false) of
39-
true -> ["export default function oauth_initialize_if_required(state) { ",
40-
"let oauth = oauth_initialize(", rabbit_json:encode(rabbit_mgmt_format:format_nulls(AuthSettings)), "); ",
41-
"if (!oauth.enabled) return oauth;"
42-
"switch (state) { case 'login-callback': oauth_completeLogin(); break; case 'logout-callback': oauth_completeLogout(); break; default: oauth = oauth_initiate(oauth);}",
43-
"return oauth; }"];
44-
false -> ["export default function oauth_initialize_if_required(state) { return {oauth_enabled: false}; }"]
37+
true -> ["set_oauth_settings(", rabbit_json:encode(rabbit_mgmt_format:format_nulls(AuthSettings)), "); "];
38+
false -> ["set_oauth_settings({oauth_enabled: false});"]
4539
end.
4640

4741
set_token_auth(Req0) ->
@@ -58,7 +52,7 @@ import_dependencies(Dependencies) ->
5852
["import {", string:join(Dependencies, ","), "} from './helper.js';"].
5953

6054
oauth_dependencies() ->
61-
["hasAnyResourceServerReady", "oauth_initialize", "oauth_initiate", "oauth_initiateLogin", "oauth_initiateLogout", "oauth_completeLogin", "oauth_completeLogout"].
55+
["oauth_initialize_if_required", "hasAnyResourceServerReady", "oauth_initialize", "oauth_initiate", "oauth_initiateLogin", "oauth_initiateLogout", "oauth_completeLogin", "oauth_completeLogout", "set_oauth_settings"].
6256

6357
export_dependencies(Dependencies) ->
6458
[ io_lib:format("window.~s = ~s;", [Dep, Dep]) || Dep <- Dependencies ].

0 commit comments

Comments
 (0)