1(function () { 2 'use strict'; 3 4 /** 5 * Get the site parameter from the script's query string 6 */ 7 function getSiteFromQueryString() { 8 // Try currentScript first 9 if (document.currentScript && document.currentScript.src) { 10 const url = new URL(document.currentScript.src); 11 return url.searchParams.get('site'); 12 } 13 14 // Fallback: loop through all scripts 15 const scripts = document.getElementsByTagName('script'); 16 for (let i = 0; i < scripts.length; i++) { 17 const src = scripts[i].src; 18 if (src && src.includes('nca-rampart.js')) { 19 const url = new URL(src); 20 return url.searchParams.get('site'); 21 } 22 } 23 24 return null; 25 } 26 27 /** 28 * Get a cookie value by name 29 */ 30 function getCookie(name) { 31 const value = `; ${document.cookie}`; 32 const parts = value.split(`; ${name}=`); 33 if (parts.length === 2) return parts.pop().split(';').shift(); 34 return null; 35 } 36 37 /** 38 * Configuration mapping for different sites 39 * Each entry contains: 40 * - rampartScriptUrl: The URL to the Rampart script that needs to be loaded 41 * - callbackUri: The callback URI for authentication 42 */ 43 const siteConfig = { 44 'cairnspost': { 45 rampartScriptUrl: 'https://www.cairnspost.com.au/remote/identity/rampart/latest/rampart.js', 46 callbackUri: 'https://www.cairnspost.com.au/remote/identity/auth/latest/login/callback.html', 47 //logoutUri: "https://www.cairnspost.com.au/remote/identity/auth/latest/logout/callback.html" 48 logoutUri: "https://www.news.com.au/remote/identity/auth/latest/logout/callback.html" 49 }, 50 'cairnspostsit': { 51 rampartScriptUrl: 'https://www.cairnspostsit.com.au/remote/identity/rampart/latest/rampart.js', 52 callbackUri: 'https://www.cairnspostsit.com.au/remote/identity/auth/latest/login/callback.html', 53 //logoutUri: "https://www.cairnspost.com.au/remote/identity/auth/latest/logout/callback.html" 54 logoutUri: "https://www.newssit.com.au/remote/identity/auth/latest/logout/callback.html" 55 }, 56 'cairnspostuat': { 57 rampartScriptUrl: 'https://www.cairnspostuat.com.au/remote/identity/rampart/latest/rampart.js', 58 callbackUri: 'https://www.cairnspostuat.com.au/remote/identity/auth/latest/login/callback.html', 59 //logoutUri: "https://www.cairnspost.com.au/remote/identity/auth/latest/logout/callback.html" 60 logoutUri: "https://www.newsuat.com.au/remote/identity/auth/latest/logout/callback.html" 61 }, 62 "thechronicle": { 63 rampartScriptUrl: 'https://www.thechronicle.com.au/remote/identity/rampart/latest/rampart.js', 64 callbackUri: 'https://www.thechronicle.com.au/remote/identity/auth/latest/login/callback.html', 65 //logoutUri: "https://www.thechronicle.com.au/remote/identity/auth/latest/logout/callback.html" 66 logoutUri: "https://www.news.com.au/remote/identity/auth/latest/logout/callback.html" 67 }, 68 "thechroniclesit": { 69 rampartScriptUrl: 'https://www.thechroniclesit.com.au/remote/identity/rampart/latest/rampart.js', 70 callbackUri: 'https://www.thechroniclesit.com.au/remote/identity/auth/latest/login/callback.html', 71 //logoutUri: "https://www.thechronicle.com.au/remote/identity/auth/latest/logout/callback.html" 72 logoutUri: "https://www.newssit.com.au/remote/identity/auth/latest/logout/callback.html" 73 }, 74 "thechronicleuat": { 75 rampartScriptUrl: 'https://www.thechronicleuat.com.au/remote/identity/rampart/latest/rampart.js', 76 callbackUri: 'https://www.thechronicleuat.com.au/remote/identity/auth/latest/login/callback.html', 77 //logoutUri: "https://www.thechronicle.com.au/remote/identity/auth/latest/logout/callback.html" 78 logoutUri: "https://www.newsuat.com.au/remote/identity/auth/latest/logout/callback.html" 79 }, 80 'theaustralian': { 81 rampartScriptUrl: 'https://www.theaustralian.com.au/remote/identity/rampart/latest/rampart.js', 82 callbackUri: 'https://www.theaustralian.com.au/remote/identity/auth/latest/login/callback.html', 83 //logoutUri: "https://www.theaustralian.com.au/remote/identity/auth/latest/logout/callback.html" 84 logoutUri: "https://www.news.com.au/remote/identity/auth/latest/logout/callback.html" 85 }, 86 "couriermail": { 87 rampartScriptUrl: 'https://www.couriermail.com.au/remote/identity/rampart/latest/rampart.js', 88 callbackUri: 'https://www.couriermail.com.au/remote/identity/auth/latest/login/callback.html', 89 logoutUri: "https://www.couriermail.com.au/remote/identity/auth/latest/logout/callback.html" 90 } 91 }; 92 93 // Get the site key from the query string 94 const siteKey = getSiteFromQueryString(); 95 let hasRampartScriptLoaded = false;
96 let rampart; 97 98 if (!siteKey) { 99 console.warn('[PageSuite/NCA Rampart] No site parameter provided in script URL'); 100 return; 101 } 102 103 // Get the configuration for this site 104 const config = siteConfig[siteKey]; 105 106 if (!config) { 107 console.warn('[PageSuite/NCA Rampart] No configuration found for site:', siteKey); 108 return; 109 } 110 111 console.log('[PageSuite/NCA Rampart] Loaded configuration for', siteKey); 112 113 /** 114 * Load the Rampart SDK script, then call the callback once ready. 115 * If already loaded, calls back immediately. 116 */ 117 function loadRampartSDK(callback) { 118 if (hasRampartScriptLoaded && rampart) { 119 callback(); 120 return; 121 } 122 123 const rampartScript = document.createElement('script'); 124 rampartScript.onload = () => { 125 hasRampartScriptLoaded = true; 126 rampart = new Rampart({ 127 site: siteKey, 128 callbackUri: config.callbackUri, 129 logoutUri: config.logoutUri 130 }); 131 console.log('[PageSuite/NCA Rampart] SDK loaded'); 132 callback(); 133 }; 134 135 rampartScript.onerror = () => { 136 console.warn('[PageSuite/NCA Rampart] Failed to load Rampart script'); 137 }; 138 139 rampartScript.src = config.rampartScriptUrl; 140 rampartScript.async = true; 141 document.body.appendChild(rampartScript); 142 } 143 144 /** 145 * Check Rampart login status and authorise with PageSuite. 146 * 147 * @param {boolean} requireLogin - If true (edition behind paywall), locks the 148 * reader and redirects to Rampart login when not logged in. If false (free 149 * edition), silently checks login status and syncs auth if the user happens 150 * to be logged in, but never locks or redirects. 151 */ 152 function checkLoginStatus(requireLogin) { 153 if (requireLogin) { 154 window.psLockReader(); 155 console.log('[PageSuite/NCA Rampart] Reader locked'); 156 } 157 158 rampart.getLoginStatus() 159 .then(async function (payload) { 160 161 if (payload.status === 'loggedin') { 162 console.log('[PageSuite/NCA Rampart] User logged in'); 163 const nkCookie = getCookie('nk'); 164 165 let payloadObj = { 166 "token": payload.id_token, 167 "request_url": window.location.href 168 } 169 170 if(!!nkCookie) { 171 payloadObj.session = nkCookie; 172 } 173 174 let authResponse = await window.psAuthorise(payloadObj); 175 if (authResponse.access) { 176 console.log('[PageSuite/NCA Rampart] User authorized - reader will unlock automatically'); 177 } else if (requireLogin && authResponse.redirectUrl) { 178 window.location.href = authResponse.redirectUrl; 179 } else if (requireLogin && !window.psIsAuthorisedForResource()) { 180 console.log('[PageSuite/NCA Rampart] User NOT authorized', authResponse); 181 window.psRedirectToFailureForResource({ 182 error: authResponse?.error, 183 errorCode: authResponse?.errorCode, 184 token: authResponse?.token, 185 }); 186 } else { 187 console.log('[PageSuite/NCA Rampart] User logged in but not authorized (no paywall, skipping redirect)'); 188 } 189 } else if (requireLogin) { 190 console.log('[PageSuite/NCA Rampart] User not logged in - redirecting to login'); 191 rampart.login(window.location.href); 192 } else { 193 console.log('[PageSuite/NCA Rampart] User not logged in (no paywall, skipping redirect)'); 194 } 195 }) 196 .catch(function (err) { 197 198 if (requireLogin) { 199 console.error('[PageSuite/NCA Rampart] Login status check failed:', err); 200 rampart.login(window.location.href); 201 } else { 202 console.log('[PageSuite/NCA Rampart] Login status check failed (no paywall, skipping redirect):', err); 203 } 204 }); 205 } 206 207 function setupLoginListener() { 208 if (window.setupPageSuiteEventListener) { 209 window.setupPageSuiteEventListener("com.pagesuite.loginButtonClicked", function () { 210 try { 211 // User explicitly clicked login - always require login 212 checkLoginStatus(true); 213 } catch (error) { 214 console.error('[PageSuite/NCA Rampart] Login error:', error); 215 } 216 }); 217 } 218 } 219 220 function setupLogoutListener() { 221 if (window.setupPageSuiteEventListener) { 222 window.setupPageSuiteEventListener("com.pagesuite.logoutButtonClicked", function () { 223 // Extract /shortcode/[shortcode] from the current URL for redirect 224 var match = window.location.pathname.match(/\/shortcode\/[^\/]+/); 225 var redirectUrl = match && match[0] ? match[0] : window.location.origin + '/'; 226 rampart.logout("https://" + window.location.host + redirectUrl); 227 }); 228 } 229 } 230 231 /** 232 * Handle pageView / articleView events. 233 * Mirrors the piano.js pattern: register, check auth, set up listeners, then 234 * remove the pageView/articleView listeners so this only runs once. 235 * 236 * Always checks Rampart login status so that if the user has just returned 237 * from a rampart.login() redirect, we pick up their session and call 238 * psAuthorise. The requireLogin flag controls whether we lock the reader 239 * and redirect to login when the user is NOT logged in. 240 */ 241 function handlePageViewEvent() { 242 window.psRegister(); 243 console.log('[PageSuite/NCA Rampart] Script registered successfully'); 244 245 var isAuthorized = window.psIsAuthorisedForResource(); 246 247 loadRampartSDK(function () { 248 setupLoginListener(); 249 setupLogoutListener(); 250 251 // Always check login status so we pick up a returning user's session. 252 // Only lock the reader / redirect to login when the edition is behind 253 // the paywall (i.e. not already authorized by PS). 254 checkLoginStatus(!isAuthorized); 255 }); 256 257 // Remove pageView/articleView listeners - only need to run once 258 if (window.removePageSuiteEventListener) { 259 window.removePageSuiteEventListener("com.pagesuite.pageView", handlePageViewEvent); 260 window.removePageSuiteEventListener("com.pagesuite.articleView", handlePageViewEvent); 261 } 262 } 263 264 window.setupPageSuiteEventListener("com.pagesuite.pageView", handlePageViewEvent); 265 window.setupPageSuiteEventListener("com.pagesuite.articleView", handlePageViewEvent); 266})();
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.