Skip to content

Commit c67c940

Browse files
Merge pull request #11448 from rabbitmq/dynamic-load-oauth-deps-ui
Dynamically load oauth-related libraries
2 parents 7ee91ce + f0adf3a commit c67c940

File tree

27 files changed

+505
-232
lines changed

27 files changed

+505
-232
lines changed

deps/rabbitmq_management/priv/www/index.html

Lines changed: 8 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,47 +16,16 @@
1616
<script src="js/prefs.js" type="text/javascript"></script>
1717
<script src="js/formatters.js" type="text/javascript"></script>
1818
<script src="js/charts.js" type="text/javascript"></script>
19-
<script src="js/oidc-oauth/helper.js"></script>
20-
<script src="js/oidc-oauth/oidc-client-ts.js" type="text/javascript"></script>
21-
<script src="js/oidc-oauth/bootstrap.js"></script>
22-
19+
<script src="js/oidc-oauth/bootstrap.js" type="module"></script>
20+
2321
<link href="css/main.css" rel="stylesheet" type="text/css"/>
24-
<link href="favicon.ico" rel="shortcut icon" type="image/x-icon"/>
25-
26-
<script type="application/javascript">
27-
var oauth = oauth_initialize_if_required();
28-
29-
if (oauth.enabled) {
30-
if (!oauth.sp_initiated) {
31-
oauth.logged_in = has_auth_credentials();
32-
oauth.access_token = get_auth_credentials(); // DEPRECATED
33-
} else {
34-
oauth_is_logged_in().then( status => {
35-
if (status.loggedIn && !has_auth_credentials()) {
36-
oauth.logged_in = false;
37-
oauth_initiateLogout();
38-
} else {
39-
if (!status.loggedIn) {
40-
clear_auth();
41-
} else {
42-
oauth.logged_in = true;
43-
oauth.access_token = status.user.access_token; // DEPRECATED
44-
oauth.expiryDate = new Date(status.user.expires_at * 1000); // it is epoch in seconds
45-
let current = new Date();
46-
_management_logger.debug('token expires in ', (oauth.expiryDate-current)/1000,
47-
'secs at : ', oauth.expiryDate );
48-
oauth.user_name = status.user.profile['user_name'];
49-
if (!oauth.user_name || oauth.user_name == '') {
50-
oauth.user_name = status.user.profile['sub'];
51-
}
52-
oauth.scopes = status.user.scope;
53-
}
54-
}
55-
});
56-
}
57-
}
58-
22+
<link href="favicon.ico" rel="shortcut icon" type="image/x-icon"/>
23+
24+
<script type="module">
25+
window.oauth = oauth_initialize_if_required();
26+
5927
</script>
28+
6029

6130
<!--[if lte IE 8]>
6231
<script src="js/excanvas.min.js" type="text/javascript"></script>

deps/rabbitmq_management/priv/www/js/global.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,7 @@ function DisplayControl() {
698698

699699
}
700700

701+
701702
// Set up the above vars
702703
function setup_global_vars(overview) {
703704
rates_mode = overview.rates_mode;
@@ -713,9 +714,9 @@ function setup_global_vars(overview) {
713714
'<li>Cluster ' + (user_administrator ? '<a href="#/cluster-name">' + cluster_name + '</a>' : cluster_name) + '</li>'
714715
);
715716

716-
user_name = fmt_escape_html(user.name);
717+
user_name = fmt_escape_html(user.name);
717718
$('#header #logout').prepend(
718-
'User ' + (user_administrator && !oauth.enabled ? '<a href="#/users/' + user_name + '">' + user_name + '</a>' : user_name)
719+
'User ' + (user_administrator && user.is_internal_user ? '<a href="#/users/' + user_name + '">' + user_name + '</a>' : user_name)
719720
);
720721

721722
var product = overview.rabbitmq_version;

deps/rabbitmq_management/priv/www/js/main.js

Lines changed: 34 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11

2-
$(document).ready(function() {
3-
var url_string = window.location.href;
4-
var url = new URL(url_string);
5-
var error = url.searchParams.get('error');
6-
if (error) {
7-
renderWarningMessageInLoginStatus(fmt_escape_html(error));
8-
} else {
9-
if (oauth.enabled) {
10-
startWithOAuthLogin();
11-
} else {
12-
startWithLoginPage();
13-
}
2+
$(document).ready(function() {
3+
var url_string = window.location.href;
4+
var url = new URL(url_string);
5+
var error = url.searchParams.get('error');
6+
if (error) {
7+
if (oauth.enabled) {
8+
renderWarningMessageInLoginStatus(oauth, fmt_escape_html(error));
149
}
10+
} else {
11+
if (oauth.enabled) {
12+
startWithOAuthLogin(oauth);
13+
} else {
14+
startWithLoginPage();
15+
}
16+
}
1517
});
1618

1719
function startWithLoginPage() {
@@ -27,85 +29,18 @@ function removeDuplicates(array){
2729
}
2830
return output
2931
}
30-
function warningMessageOAuthResource(oauthResource, reason) {
31-
return "OAuth resource [<b>" + (oauthResource["label"] != null ? oauthResource.label : oauthResource.id) +
32-
"</b>] not available. OpenId Discovery endpoint " + readiness_url(oauthResource) + reason
33-
}
34-
function warningMessageOAuthResources(commonProviderURL, oauthResources, reason) {
35-
return "OAuth resources [ <b>" + oauthResources.map(resource => resource["label"] != null ? resource.label : resource.id).join("</b>,<b>")
36-
+ "</b>] not available. OpenId Discovery endpoint " + commonProviderURL + reason
37-
}
3832

39-
function startWithOAuthLogin () {
33+
34+
function startWithOAuthLogin (oauth) {
4035
store_pref("oauth-return-to", window.location.hash);
4136

4237
if (!oauth.logged_in) {
43-
44-
// Find out how many distinct oauthServers are configured
45-
let oauthServers = removeDuplicates(oauth.resource_servers.filter((resource) => resource.sp_initiated))
46-
oauthServers.forEach(function(entry) { console.log(readiness_url(entry)) })
47-
if (oauthServers.length > 0) { // some resources are sp_initiated but there could be idp_initiated too
48-
Promise.allSettled(oauthServers.map(oauthServer => fetch(readiness_url(oauthServer)).then(res => res.json())))
49-
.then(results => {
50-
results.forEach(function(entry) { console.log(entry) })
51-
let notReadyServers = []
52-
let notCompliantServers = []
53-
54-
for (let i = 0; i < results.length; i++) {
55-
switch (results[i].status) {
56-
case "fulfilled":
57-
try {
58-
validate_openid_configuration(results[i].value)
59-
}catch(e) {
60-
console.log("Unable to connect to " + oauthServers[i].oauth_provider_url + ". " + e)
61-
notCompliantServers.push(oauthServers[i].oauth_provider_url)
62-
}
63-
break
64-
case "rejected":
65-
notReadyServers.push(oauthServers[i].oauth_provider_url)
66-
break
67-
}
68-
}
69-
const spOauthServers = oauth.resource_servers.filter((resource) => resource.sp_initiated)
70-
const groupByProviderURL = spOauthServers.reduce((group, oauthServer) => {
71-
const { oauth_provider_url } = oauthServer;
72-
group[oauth_provider_url] = group[oauth_provider_url] ?? [];
73-
group[oauth_provider_url].push(oauthServer);
74-
return group;
75-
}, {})
76-
let warnings = []
77-
for(var url in groupByProviderURL){
78-
console.log(url + ': ' + groupByProviderURL[url]);
79-
const notReadyResources = groupByProviderURL[url].filter((oauthserver) => notReadyServers.includes(oauthserver.oauth_provider_url))
80-
const notCompliantResources = groupByProviderURL[url].filter((oauthserver) => notCompliantServers.includes(oauthserver.oauth_provider_url))
81-
if (notReadyResources.length == 1) {
82-
warnings.push(warningMessageOAuthResource(notReadyResources[0], " not reachable"))
83-
}else if (notReadyResources.length > 1) {
84-
warnings.push(warningMessageOAuthResources(url, notReadyResources, " not reachable"))
85-
}
86-
if (notCompliantResources.length == 1) {
87-
warnings.push(warningMessageOAuthResource(notCompliantResources[0], " not compliant"))
88-
}else if (notCompliantResources.length > 1) {
89-
warnings.push(warningMessageOAuthResources(url, notCompliantResources, " not compliant"))
90-
}
91-
}
92-
console.log("warnings:" + warnings)
93-
oauth.declared_resource_servers_count = oauth.resource_servers.length
94-
oauth.resource_servers = oauth.resource_servers.filter((resource) =>
95-
!notReadyServers.includes(resource.oauth_provider_url) && !notCompliantServers.includes(resource.oauth_provider_url))
96-
render_login_oauth(warnings)
97-
start_app_login()
98-
99-
})
100-
}else { // there are only idp_initiated resources
101-
render_login_oauth()
102-
start_app_login()
103-
}
38+
hasAnyResourceServerReady(oauth, (oauth, warnings) => { render_login_oauth(oauth, warnings); start_app_login(); })
10439
} else {
10540
start_app_login()
10641
}
10742
}
108-
function render_login_oauth(messages) {
43+
function render_login_oauth(oauth, messages) {
10944
let formatData = {}
11045
formatData.warnings = []
11146
formatData.notAuthorized = false
@@ -118,7 +53,6 @@ function render_login_oauth(messages) {
11853
} else if (typeof messages == "string") {
11954
formatData.warnings = [messages]
12055
formatData.notAuthorized = messages == "Not authorized"
121-
console.log("Single error message")
12256
}
12357
replace_content('outer', format('login_oauth', formatData))
12458

@@ -127,13 +61,11 @@ function render_login_oauth(messages) {
12761
$('#login').on('click', 'div.section h2, div.section-hidden h2', function() {
12862
toggle_visibility($(this));
12963
});
130-
13164
}
132-
function renderWarningMessageInLoginStatus(message) {
133-
render_login_oauth(message)
65+
function renderWarningMessageInLoginStatus(oauth, message) {
66+
render_login_oauth(oauth, message)
13467
}
13568

136-
13769
function dispatcher_add(fun) {
13870
dispatcher_modules.push(fun);
13971
if (dispatcher_modules.length == extension_count) {
@@ -187,9 +119,10 @@ function check_login () {
187119
if (user == false || user.error) {
188120
clear_auth();
189121
if (oauth.enabled) {
190-
hide_popup_warn();
191-
renderWarningMessageInLoginStatus('Not authorized');
122+
//hide_popup_warn();
123+
renderWarningMessageInLoginStatus(oauth, 'Not authorized');
192124
} else {
125+
//hide_popup_warn();
193126
replace_content('login-status', '<p>Login failed</p>');
194127
}
195128
return false;
@@ -323,6 +256,7 @@ function dynamic_load(filename) {
323256
element.setAttribute('type', 'text/javascript');
324257
element.setAttribute('src', 'js/' + filename);
325258
document.getElementsByTagName('head')[0].appendChild(element);
259+
return element;
326260
}
327261

328262
function update_interval() {
@@ -350,7 +284,11 @@ function update_interval() {
350284
function go_to(url) {
351285
this.location = url;
352286
}
353-
287+
function go_to_home() {
288+
// location.href = rabbit_path_prefix() + "/"
289+
location.href = "/"
290+
}
291+
354292
function set_timer_interval(interval) {
355293
timer_interval = interval;
356294
reset_timer();
@@ -1472,16 +1410,16 @@ function sync_req(type, params0, path_template, options) {
14721410
else
14731411
// rabbitmq/rabbitmq-management#732
14741412
// https://developer.mozilla.org/en-US/docs/Glossary/Truthy
1475-
return {result: true, http_status: req.status, req_params: params};
1413+
return {result: true, http_status: req.status, req_params: params, responseText: req.responseText};
14761414
}
14771415
else {
14781416
return false;
14791417
}
14801418
}
1481-
function initiate_logout(error = "") {
1419+
function initiate_logout(oauth, error = "") {
14821420
clear_pref('auth');
1483-
clear_cookie_value('auth');
1484-
renderWarningMessageInLoginStatus(error);
1421+
clear_cookie_value('auth');
1422+
renderWarningMessageInLoginStatus(oauth, error);
14851423
}
14861424
function check_bad_response(req, full_page_404) {
14871425
// 1223 == 204 - see https://www.enhanceie.com/ie/bugs.asp
@@ -1502,7 +1440,7 @@ function check_bad_response(req, full_page_404) {
15021440

15031441
if (error == 'bad_request' || error == 'not_found' || error == 'not_authorised' || error == 'not_authorized') {
15041442
if ((req.status == 401 || req.status == 403) && oauth.enabled) {
1505-
initiate_logout(reason);
1443+
initiate_logout(oauth, reason);
15061444
} else {
15071445
show_popup('warn', fmt_escape_html(reason));
15081446
}

0 commit comments

Comments
 (0)