From 449a625fb7e2a76d00b13dd51262349dbbffec02 Mon Sep 17 00:00:00 2001 From: Maros Ivanco Date: Thu, 12 Sep 2019 11:24:51 +0200 Subject: [PATCH] Keyclops logs to console only if the logging is enabled. --- README.md | 1 + src/bootstrap.js | 5 ++--- src/flows/hybrid.js | 31 ++++++++++++++++++------------- test/cloak.js | 2 +- 4 files changed, 22 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index af6c546..f6c1666 100644 --- a/README.md +++ b/README.md @@ -302,6 +302,7 @@ new Keyclops(options); - `interval` - if SSO is enabled, the interval in seconds at which the SSO occurrence is checked. - `checkNonce` - _Optional_. Default `false`. Whether to check that nonce in tokens equals to nonce used to start the authentication process. Setting this to `true` makes sense if you can ensure, that the authentication process _cannot_ be started outside of your application. + - `logging` - _Optional_. Default `false`. Wheter to log various messages to the console. **Example** diff --git a/src/bootstrap.js b/src/bootstrap.js index a9775e5..6f4e1ba 100644 --- a/src/bootstrap.js +++ b/src/bootstrap.js @@ -1,6 +1,4 @@ -console.log("[KEYCLOPS] bootstrap"); - -function Keyclops({ url, realm, clientId, sso, checkNonce }) { +function Keyclops({ url, realm, clientId, sso, checkNonce, logging }) { if (!clientId) throw "clientId missing"; if (!realm) throw "realm missing"; if (!url) throw "url missing"; @@ -8,6 +6,7 @@ function Keyclops({ url, realm, clientId, sso, checkNonce }) { this.url = url; this.realm = realm; this.clientId = clientId; + this.logging = logging; // setupOidcEndpoints const ppath = `${url}/realms/${encodeURIComponent(realm)}/protocol/openid-connect`; this.endpoints = { diff --git a/src/flows/hybrid.js b/src/flows/hybrid.js index b83092a..9ba5382 100644 --- a/src/flows/hybrid.js +++ b/src/flows/hybrid.js @@ -34,20 +34,25 @@ function decodePayload(token) { const euc = encodeURIComponent; const Keyclops = window.Keyclops; +Keyclops.prototype.log = function(...args) { + if (this.logging) { + console.log(...args); + } +}; Keyclops.prototype.init = function(options) { if (!options) throw new Error("init options parameter is mandatory."); - console.log("[KEYCLOPS] Implicit init params: flow = hybrid, responseMode = fragment, checkLoginIframe = true"); - console.log("[KEYCLOPS] Explicit init params:", options); + this.log("[KEYCLOPS] Implicit init params: flow = hybrid, responseMode = fragment, checkLoginIframe = true"); + this.log("[KEYCLOPS] Explicit init params:", options); const oauth = this.getOAuthParams(location.href); - console.log("[KEYCLOPS] init oauth params:", JSON.stringify(oauth)); + this.log("[KEYCLOPS] init oauth params:", JSON.stringify(oauth)); if (oauth.newUrl) { - console.log("[KEYCLOPS] history replaceState: ", oauth.newUrl); + this.log("[KEYCLOPS] history replaceState: ", oauth.newUrl); window.history.replaceState({}, null, oauth.newUrl); - console.log("[KEYCLOPS] location: ", window.location); + this.log("[KEYCLOPS] location: ", window.location); } this.setupSSOIframe().catch(() => { - console.error("[KEYCLOPS] Unable to setup SSO iframe. "); + this.error("[KEYCLOPS] Unable to setup SSO iframe. "); }); let ret; if (oauth.continue) { @@ -62,7 +67,7 @@ Keyclops.prototype.init = function(options) { }; Keyclops.prototype.update = function() { - console.log("[KEYCLOPS] update"); + this.log("[KEYCLOPS] update"); if (!this.refreshToken) return Promise.reject(); return fetch(this.endpoints.token, { @@ -83,7 +88,7 @@ Keyclops.prototype.update = function() { return data; }) .catch(response => { - console.warn("[KEYCLOPS] Failed to refresh tokens."); + this.warn("[KEYCLOPS] Failed to refresh tokens."); if (response.status === 400) { this.clear(); } @@ -92,7 +97,7 @@ Keyclops.prototype.update = function() { }; Keyclops.prototype.clear = function() { - console.log("[KEYCLOPS] Clearing tokens."); + this.log("[KEYCLOPS] Clearing tokens."); this.refreshToken = this.refreshTokenPayload = this.idToken = this.idTokenPayload = this.accessToken = this.accessTokenPayload = null; }; Keyclops.prototype.set = function(accessToken, refreshToken, idToken) { @@ -180,18 +185,18 @@ Keyclops.prototype.getOAuthParams = function(url) { return oauth; } let oauthStateStored = JSON.parse(sessionStorage.getItem("kc")); - console.log("[KEYCLOPS] getOAuthParams stored oauthState", oauthStateStored); + this.log("[KEYCLOPS] getOAuthParams stored oauthState", oauthStateStored); sessionStorage.removeItem("kc"); const oauthStateParsed = parseOAuth(url); if (!oauthStateStored) { oauthStateStored = { redirectUrl: oauthStateParsed.newUrl }; - console.log("[KEYCLOPS] getOAuthParams deduced oauthState", oauthStateStored); + this.log("[KEYCLOPS] getOAuthParams deduced oauthState", oauthStateStored); } return Object.assign(oauthStateParsed, oauthStateStored); }; Keyclops.prototype.setupSSOIframe = function() { - console.log("[KEYCLOPS] setupSSOIframe", this.sso.enable); + this.log("[KEYCLOPS] setupSSOIframe", this.sso.enable); if (!this.sso.enable || this.sso.iframe) return Promise.resolve(); return new Promise(resolve => { @@ -200,7 +205,7 @@ Keyclops.prototype.setupSSOIframe = function() { this.sso.iframe = iframe; iframe.onload = () => { const authUrl = this.endpoints.authorize; - console.log("[KEYCLOPS] iframe onload authUrl", authUrl); + this.log("[KEYCLOPS] iframe onload authUrl", authUrl); // TODO: externalize getOrigin const origin = location.origin || `${location.protocol}//${location.host}`; this.sso.origin = authUrl.charAt(0) === "/" ? origin : authUrl.substring(0, authUrl.indexOf("/", 8)); diff --git a/test/cloak.js b/test/cloak.js index acf5a28..2fdc0d3 100644 --- a/test/cloak.js +++ b/test/cloak.js @@ -28,7 +28,7 @@ } return `${protocol}//${parts.join(".")}/auth`; } - const keyclops = new Keyclops({ url: providerUrl(), realm, clientId }); + const keyclops = new Keyclops({ url: providerUrl(), realm, clientId, logging: true }); window.keyclops = keyclops; function logout() { setCookie("Access-Token", "", new Date(0));