KEYCLOAK-11193 Allow JavaScript logging to be disabled

This commit is contained in:
Jon Koops 2019-08-26 15:20:32 +02:00 committed by Bruno Oliveira da Silva
parent f35d3a8ec9
commit 0b9c6192a3
2 changed files with 28 additions and 9 deletions

View file

@ -134,6 +134,12 @@ declare namespace Keycloak {
* If not configured, PKCE will not be used. * If not configured, PKCE will not be used.
*/ */
pkceMethod?: KeycloakPkceMethod; pkceMethod?: KeycloakPkceMethod;
/**
* Disables any information from being logged to the console.
* @default false
*/
disableLogging?: boolean
} }
interface KeycloakLoginOptions { interface KeycloakLoginOptions {

View file

@ -75,7 +75,9 @@
} }
var useNonce = true; var useNonce = true;
var logInfo = createLogger(console.info);
var logWarn = createLogger(console.warn);
kc.init = function (initOptions) { kc.init = function (initOptions) {
kc.authenticated = false; kc.authenticated = false;
@ -161,6 +163,9 @@
kc.pkceMethod = initOptions.pkceMethod; kc.pkceMethod = initOptions.pkceMethod;
} }
if (typeof initOptions.disableLogging === 'boolean') {
kc.disableLogging = initOptions.disableLogging;
}
} }
if (!kc.responseMode) { if (!kc.responseMode) {
@ -557,7 +562,7 @@
} }
if (kc.timeSkew == null) { if (kc.timeSkew == null) {
console.info('[KEYCLOAK] Unable to determine if token is expired as timeskew is not set'); logInfo('[KEYCLOAK] Unable to determine if token is expired as timeskew is not set');
return true; return true;
} }
@ -585,10 +590,10 @@
var refreshToken = false; var refreshToken = false;
if (minValidity == -1) { if (minValidity == -1) {
refreshToken = true; refreshToken = true;
console.info('[KEYCLOAK] Refreshing token: forced refresh'); logInfo('[KEYCLOAK] Refreshing token: forced refresh');
} else if (!kc.tokenParsed || kc.isTokenExpired(minValidity)) { } else if (!kc.tokenParsed || kc.isTokenExpired(minValidity)) {
refreshToken = true; refreshToken = true;
console.info('[KEYCLOAK] Refreshing token: token expired'); logInfo('[KEYCLOAK] Refreshing token: token expired');
} }
if (!refreshToken) { if (!refreshToken) {
@ -616,7 +621,7 @@
req.onreadystatechange = function () { req.onreadystatechange = function () {
if (req.readyState == 4) { if (req.readyState == 4) {
if (req.status == 200) { if (req.status == 200) {
console.info('[KEYCLOAK] Token refreshed'); logInfo('[KEYCLOAK] Token refreshed');
timeLocal = (timeLocal + new Date().getTime()) / 2; timeLocal = (timeLocal + new Date().getTime()) / 2;
@ -629,7 +634,7 @@
p.setSuccess(true); p.setSuccess(true);
} }
} else { } else {
console.warn('[KEYCLOAK] Failed to refresh token'); logWarn('[KEYCLOAK] Failed to refresh token');
if (req.status == 400) { if (req.status == 400) {
kc.clearToken(); kc.clearToken();
@ -760,7 +765,7 @@
(kc.refreshTokenParsed && kc.refreshTokenParsed.nonce != oauth.storedNonce) || (kc.refreshTokenParsed && kc.refreshTokenParsed.nonce != oauth.storedNonce) ||
(kc.idTokenParsed && kc.idTokenParsed.nonce != oauth.storedNonce))) { (kc.idTokenParsed && kc.idTokenParsed.nonce != oauth.storedNonce))) {
console.info('[KEYCLOAK] Invalid nonce, clearing token'); logInfo('[KEYCLOAK] Invalid nonce, clearing token');
kc.clearToken(); kc.clearToken();
promise && promise.setError(); promise && promise.setError();
} else { } else {
@ -967,11 +972,11 @@
} }
if (kc.timeSkew != null) { if (kc.timeSkew != null) {
console.info('[KEYCLOAK] Estimated time difference between browser and server is ' + kc.timeSkew + ' seconds'); logInfo('[KEYCLOAK] Estimated time difference between browser and server is ' + kc.timeSkew + ' seconds');
if (kc.onTokenExpired) { if (kc.onTokenExpired) {
var expiresIn = (kc.tokenParsed['exp'] - (new Date().getTime() / 1000) + kc.timeSkew) * 1000; var expiresIn = (kc.tokenParsed['exp'] - (new Date().getTime() / 1000) + kc.timeSkew) * 1000;
console.info('[KEYCLOAK] Token expires in ' + Math.round(expiresIn / 1000) + ' s'); logInfo('[KEYCLOAK] Token expires in ' + Math.round(expiresIn / 1000) + ' s');
if (expiresIn <= 0) { if (expiresIn <= 0) {
kc.onTokenExpired(); kc.onTokenExpired();
} else { } else {
@ -1675,6 +1680,14 @@
return new CookieStorage(); return new CookieStorage();
} }
function createLogger(fn) {
return function() {
if (!kc.disableLogging) {
fn.apply(console, Array.prototype.slice.call(arguments));
}
};
}
} }
return Keycloak; return Keycloak;