Skip to content

Commit

Permalink
Keyclops logs to console only if the logging is enabled.
Browse files Browse the repository at this point in the history
  • Loading branch information
Maros Ivanco committed Sep 12, 2019
1 parent 2ffa58a commit 449a625
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**

Expand Down
5 changes: 2 additions & 3 deletions src/bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
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";

this.url = url;
this.realm = realm;
this.clientId = clientId;
this.logging = logging;
// setupOidcEndpoints
const ppath = `${url}/realms/${encodeURIComponent(realm)}/protocol/openid-connect`;
this.endpoints = {
Expand Down
31 changes: 18 additions & 13 deletions src/flows/hybrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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, {
Expand All @@ -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();
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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 => {
Expand All @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion test/cloak.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit 449a625

Please sign in to comment.