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
1719function 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-
13769function 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
328262function update_interval ( ) {
@@ -350,7 +284,11 @@ function update_interval() {
350284function 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+
354292function 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}
14861424function 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