1(function ($, Drupal, drupalSettings) { 2 Drupal.behaviors.myCitizenProfileBehavior = { 3 attach: function (context, settings) { 4 // Capture any widget that is present or will be present on the webpage. 5 vl.widget.client.capture(function (widget) { 6 // Only process the widget if widget is a global header. 7 if (widget.getPluginTypeId() === 'global_header') { 8 // Get the Citizen Profile Session extension from the global header widget. 9 widget.getExtension('citizen_profile.session').then(function (session) { 10 /** 11 * Event handler which extends a Citizen Profile session. 12 */ 13 function activityEventHandler() { 14 // Inform the Citizen Profile Session extension about activity. 15 session.extend(); 16 } 17 18 // Build a list of event names which should be used for activity tracking. 19 var eventNames = [ 20 'mousedown', 21 'mousemove', 22 'mousewheel', 23 'DOMMouseScroll', 24 'scroll', 25 'wheel', 26 'keydown', 27 'keypress', 28 'touchmove', 29 'touchstart' 30 ]; 31 // Iterate through the events names to enable activity tracking. 32 for (var i = 0; i < eventNames.length; i++) { 33 // Register our event handler given event name. 34 window.addEventListener(eventNames[i], activityEventHandler); 35 } 36 37 if (drupalSettings.acm.enabled) { 38 // Ensure the fallback login button uses the session instead of redirect to login page. 39 var loginButtonList = document.getElementsByClassName('acm-login-button'); 40 for (var loginButton of loginButtonList) { 41 loginButton.addEventListener('click', function (event) { 42 // Start the login flow using the Citizen Profile Session extension. 43 session.login(); 44 45 // Prevent default behavior as the redirect is no longer required. 46 event.preventDefault(); 47 }); 48 } 49 50 // Ensure the fallback logout button uses the session instead of redirect to logout page. 51 var logoutButtonList = document.getElementsByClassName('acm-logout-button'); 52 for (var logoutButton of logoutButtonList) { 53 logoutButton.addEventListener('click', function (event) { 54 // Start the logout flow using the Citizen Profile Session extension. 55 session.logout(); 56 57 // Prevent default behavior as the redirect is no longer required. 58 event.preventDefault(); 59 }); 60 } 61 62 // This variable is purely to indicate which values are allowed (true / false). 63 var websiteHasAuthenticatedSession = drupalSettings.acm.has_session; 64 65 // Inform the session extension about the current session state of the website. Keep in mind 66 // this operation can only be performed once on every page load. 67 session.configure({ 68 active: websiteHasAuthenticatedSession, 69 endpoints: { 70 loginUrl: '/openid/login', 71 loginRedirectUrl: window.location.href, 72 logoutUrl: '/openid/logout' 73 } 74 }); 75 76 // Also add the current URL to local storage in case we use the fallback scenario without modal. 77 try { 78 window.localStorage.setItem('mcp.redirect', window.location.href); 79 } 80 catch (e) { 81 // No localStorage available. 82 } 83 } 84 }); 85 86 if (drupalSettings.acm.enabled) { 87 // Register for session extend event. 88 widget.on('citizen_profile.session.extend', function (event) { 89 // Perform custom website specific session extend logic. 90 fetch('/openid/validate'); 91 }); 92 93 // Get the Citizen Profile Session extension from the global header widget. 94 widget.on('citizen_profile.session.logout.request', function (logoutRequest) { 95 // Acknowledge the logout request to prevent the session extension from performing default 96 // action due to response timeout (5 seconds). 97 logoutRequest.acknowledge(); 98 99 // Evaluate the type of logout request.
100 switch (logoutRequest.getRequest().getReason()) { 101 // Logout was requested because the citizen profile extension has detected an expired 102 // session which prevents the user from accessing citizen profile without a step-up. 103 // This could be the result of an application logout or external logout. 104 case 'expired': 105 // Validate whether our application still has a valid session. Keep in mind that this 106 // implementation is purely as an example and does not enforce any rules on how a session 107 // state is detected. In our example the API call will return 204 if session is present, 108 // otherwise 401. 109 fetch('/openid/validate') 110 .then(function (response) { 111 // Check whether the application backend reported an active session. 112 if (response.status === 204) { 113 // Reject the logout request as our application still has an active session. 114 logoutRequest.reject(); 115 } 116 else { 117 // Accept the logout request as our application has no active session or failed to 118 // generate a valid response. 119 logoutRequest.accept(); 120 } 121 }) 122 .catch(function () { 123 // Failed to determine the session state accept the request to ensure no session 124 // inconsistency. 125 logoutRequest.accept(); 126 }); 127 break; 128 129 // Logout was requested by the user. This request should never be rejected in normal 130 // circumstances. 131 case 'user': 132 // Logout was requested as the citizen profile extension has detected inactivity from 133 // the user. 134 case 'inactivity': 135 // Accept the request for website logout. 136 logoutRequest.accept(); 137 break; 138 139 default: 140 // Reject the request for website logout. 141 logoutRequest.reject(); 142 break; 143 } 144 }); 145 } 146 } 147 }); 148 } 149 }; 150})(jQuery, Drupal, drupalSettings);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.