keycloak-js
Advanced tools
Comparing version 2.5.1 to 2.5.4
{ | ||
"name": "keycloak", | ||
"version": "2.5.1", | ||
"version": "2.5.4", | ||
"main": "dist/keycloak.js", | ||
@@ -5,0 +5,0 @@ "ignore": [ |
@@ -90,2 +90,6 @@ /* | ||
} | ||
if (initOptions.timeSkew != null) { | ||
kc.timeSkew = initOptions.timeSkew; | ||
} | ||
} | ||
@@ -166,8 +170,4 @@ | ||
}).error(function () { | ||
kc.onAuthError && kc.onAuthError(); | ||
if (initOptions.onLoad) { | ||
onLoad(); | ||
} else { | ||
initPromise.setError(); | ||
} | ||
setToken(null, null, null); | ||
initPromise.setSuccess(); | ||
}); | ||
@@ -374,2 +374,7 @@ }); | ||
if (kc.timeSkew == null) { | ||
console.info('[KEYCLOAK] Unable to determine if token is expired as timeskew is not set'); | ||
return true; | ||
} | ||
var expiresIn = kc.tokenParsed['exp'] - Math.ceil(new Date().getTime() / 1000) + kc.timeSkew; | ||
@@ -659,8 +664,3 @@ if (minValidity) { | ||
kc.tokenParsed = decodeToken(token); | ||
var sessionId = kc.realm + '/' + kc.tokenParsed.sub; | ||
if (kc.tokenParsed.session_state) { | ||
sessionId = sessionId + '/' + kc.tokenParsed.session_state; | ||
} | ||
kc.sessionId = sessionId; | ||
kc.sessionId = kc.tokenParsed.session_state; | ||
kc.authenticated = true; | ||
@@ -673,2 +673,5 @@ kc.subject = kc.tokenParsed.sub; | ||
kc.timeSkew = Math.floor(timeLocal / 1000) - kc.tokenParsed.iat; | ||
} | ||
if (kc.timeSkew != null) { | ||
console.info('[KEYCLOAK] Estimated time difference between browser and server is ' + kc.timeSkew + ' seconds'); | ||
@@ -685,7 +688,3 @@ | ||
} | ||
} else { | ||
kc.updateToken(-1); | ||
} | ||
} else if (refreshToken) { | ||
kc.updateToken(-1); | ||
} else { | ||
@@ -692,0 +691,0 @@ delete kc.token; |
@@ -1,1 +0,1 @@ | ||
(function(a,c){var b=function(z){if(!(this instanceof b)){return new b(z)}var h=this;var p;var u=[];var f;var v={enable:true,callbackList:[],interval:5};h.init=function(E){h.authenticated=false;f=q();if(E&&E.adapter==="cordova"){p=x("cordova")}else{if(E&&E.adapter==="default"){p=x()}else{if(a.Cordova){p=x("cordova")}else{p=x()}}}if(E){if(typeof E.checkLoginIframe!=="undefined"){v.enable=E.checkLoginIframe}if(E.checkLoginIframeInterval){v.interval=E.checkLoginIframeInterval}if(E.onLoad==="login-required"){h.loginRequired=true}if(E.responseMode){if(E.responseMode==="query"||E.responseMode==="fragment"){h.responseMode=E.responseMode}else{throw"Invalid value for responseMode"}}if(E.flow){switch(E.flow){case"standard":h.responseType="code";break;case"implicit":h.responseType="id_token token";break;case"hybrid":h.responseType="code id_token token";break;default:throw"Invalid value for flow"}h.flow=E.flow}}if(!h.responseMode){h.responseMode="fragment"}if(!h.responseType){h.responseType="code";h.flow="standard"}var F=d();var B=d();B.promise.success(function(){h.onReady&&h.onReady(h.authenticated);F.setSuccess(h.authenticated)}).error(function(G){F.setError(G)});var D=j(z);function C(){var H=function(I){if(!I){G.prompt="none"}h.login(G).success(function(){B.setSuccess()}).error(function(){B.setError()})};var G={};switch(E.onLoad){case"check-sso":if(v.enable){y().success(function(){l().success(function(){H(false)}).error(function(){B.setSuccess()})})}else{H(false)}break;case"login-required":H(true);break;default:throw"Invalid value for onLoad"}}function A(){var G=g(a.location.href);if(G){y();a.history.replaceState({},null,G.newUrl);n(G,B);return}else{if(E){if(E.refreshToken){t(E.token,E.refreshToken,E.idToken);if(v.enable){y().success(function(){l().success(function(){h.onAuthSuccess&&h.onAuthSuccess();B.setSuccess()}).error(function(){h.onAuthError&&h.onAuthError();if(E.onLoad){C()}else{B.setError()}})})}else{h.updateToken(-1).success(function(){h.onAuthSuccess&&h.onAuthSuccess();B.setSuccess()}).error(function(){h.onAuthError&&h.onAuthError();if(E.onLoad){C()}else{B.setError()}})}}else{if(E.onLoad){C()}else{B.setSuccess()}}}else{B.setSuccess()}}}D.success(A);D.error(function(){F.setError()});return F.promise};h.login=function(A){return p.login(A)};h.createLoginUrl=function(C){var G=e();var E=e();var H=p.redirectUri(C);var A={state:G,nonce:E,redirectUri:encodeURIComponent(H),};if(C&&C.prompt){A.prompt=C.prompt}f.add(A);var F="auth";if(C&&C.action=="register"){F="registrations"}var D=(C&&C.scope)?"openid "+C.scope:"openid";var B=o()+"/protocol/openid-connect/"+F+"?client_id="+encodeURIComponent(h.clientId)+"&redirect_uri="+encodeURIComponent(H)+"&state="+encodeURIComponent(G)+"&nonce="+encodeURIComponent(E)+"&response_mode="+encodeURIComponent(h.responseMode)+"&response_type="+encodeURIComponent(h.responseType)+"&scope="+encodeURIComponent(D);if(C&&C.prompt){B+="&prompt="+encodeURIComponent(C.prompt)}if(C&&C.maxAge){B+="&max_age="+encodeURIComponent(C.maxAge)}if(C&&C.loginHint){B+="&login_hint="+encodeURIComponent(C.loginHint)}if(C&&C.idpHint){B+="&kc_idp_hint="+encodeURIComponent(C.idpHint)}if(C&&C.locale){B+="&ui_locales="+encodeURIComponent(C.locale)}return B};h.logout=function(A){return p.logout(A)};h.createLogoutUrl=function(B){var A=o()+"/protocol/openid-connect/logout?redirect_uri="+encodeURIComponent(p.redirectUri(B,false));return A};h.register=function(A){return p.register(A)};h.createRegisterUrl=function(A){if(!A){A={}}A.action="register";return h.createLoginUrl(A)};h.createAccountUrl=function(B){var A=o()+"/account?referrer="+encodeURIComponent(h.clientId)+"&referrer_uri="+encodeURIComponent(p.redirectUri(B));return A};h.accountManagement=function(){return p.accountManagement()};h.hasRealmRole=function(B){var A=h.realmAccess;return !!A&&A.roles.indexOf(B)>=0};h.hasResourceRole=function(C,B){if(!h.resourceAccess){return false}var A=h.resourceAccess[B||h.clientId];return !!A&&A.roles.indexOf(C)>=0};h.loadUserProfile=function(){var A=o()+"/account";var B=new XMLHttpRequest();B.open("GET",A,true);B.setRequestHeader("Accept","application/json");B.setRequestHeader("Authorization","bearer "+h.token);var C=d();B.onreadystatechange=function(){if(B.readyState==4){if(B.status==200){h.profile=JSON.parse(B.responseText);C.setSuccess(h.profile)}else{C.setError()}}};B.send();return C.promise};h.loadUserInfo=function(){var A=o()+"/protocol/openid-connect/userinfo";var B=new XMLHttpRequest();B.open("GET",A,true);B.setRequestHeader("Accept","application/json");B.setRequestHeader("Authorization","bearer "+h.token);var C=d();B.onreadystatechange=function(){if(B.readyState==4){if(B.status==200){h.userInfo=JSON.parse(B.responseText);C.setSuccess(h.userInfo)}else{C.setError()}}};B.send();return C.promise};h.isTokenExpired=function(A){if(!h.tokenParsed||(!h.refreshToken&&h.flow!="implicit")){throw"Not authenticated"}var B=h.tokenParsed.exp-Math.ceil(new Date().getTime()/1000)+h.timeSkew;if(A){B-=A}return B<0};h.updateToken=function(A){var D=d();if(!h.refreshToken){D.setError();return D.promise}A=A||5;var B=function(){var G=false;if(A==-1){G=true;console.info("[KEYCLOAK] Refreshing token: forced refresh")}else{if(!h.tokenParsed||h.isTokenExpired(A)){G=true;console.info("[KEYCLOAK] Refreshing token: token expired")}}if(!G){D.setSuccess(false)}else{var I="grant_type=refresh_token&refresh_token="+h.refreshToken;var F=o()+"/protocol/openid-connect/token";u.push(D);if(u.length==1){var H=new XMLHttpRequest();H.open("POST",F,true);H.setRequestHeader("Content-type","application/x-www-form-urlencoded");H.withCredentials=true;if(h.clientId&&h.clientSecret){H.setRequestHeader("Authorization","Basic "+btoa(h.clientId+":"+h.clientSecret))}else{I+="&client_id="+encodeURIComponent(h.clientId)}var E=new Date().getTime();H.onreadystatechange=function(){if(H.readyState==4){if(H.status==200){console.info("[KEYCLOAK] Token refreshed");E=(E+new Date().getTime())/2;var K=JSON.parse(H.responseText);t(K.access_token,K.refresh_token,K.id_token,E);h.onAuthRefreshSuccess&&h.onAuthRefreshSuccess();for(var J=u.pop();J!=null;J=u.pop()){J.setSuccess(true)}}else{console.warn("[KEYCLOAK] Failed to refresh token");h.onAuthRefreshError&&h.onAuthRefreshError();for(var J=u.pop();J!=null;J=u.pop()){J.setError(true)}}}};H.send(I)}}};if(v.enable){var C=l();C.success(function(){B()}).error(function(){D.setError()})}else{B()}return D.promise};h.clearToken=function(){if(h.token){t(null,null,null);h.onAuthLogout&&h.onAuthLogout();if(h.loginRequired){h.login()}}};function o(){if(h.authServerUrl.charAt(h.authServerUrl.length-1)=="/"){return h.authServerUrl+"realms/"+encodeURIComponent(h.realm)}else{return h.authServerUrl+"/realms/"+encodeURIComponent(h.realm)}}function w(){if(!a.location.origin){return a.location.protocol+"//"+a.location.hostname+(a.location.port?":"+a.location.port:"")}else{return a.location.origin}}function n(H,K){var D=H.code;var I=H.error;var E=H.prompt;var C=new Date().getTime();if(I){if(E!="none"){var B={error:I,error_description:H.error_description};h.onAuthError&&h.onAuthError(B);K&&K.setError(B)}else{K&&K.setSuccess()}return}else{if((h.flow!="standard")&&(H.access_token||H.id_token)){G(H.access_token,null,H.id_token,true)}}if((h.flow!="implicit")&&D){var F="code="+D+"&grant_type=authorization_code";var A=o()+"/protocol/openid-connect/token";var J=new XMLHttpRequest();J.open("POST",A,true);J.setRequestHeader("Content-type","application/x-www-form-urlencoded");if(h.clientId&&h.clientSecret){J.setRequestHeader("Authorization","Basic "+btoa(h.clientId+":"+h.clientSecret))}else{F+="&client_id="+encodeURIComponent(h.clientId)}F+="&redirect_uri="+H.redirectUri;J.withCredentials=true;J.onreadystatechange=function(){if(J.readyState==4){if(J.status==200){var L=JSON.parse(J.responseText);G(L.access_token,L.refresh_token,L.id_token,h.flow==="standard")}else{h.onAuthError&&h.onAuthError();K&&K.setError()}}};J.send(F)}function G(L,M,O,N){C=(C+new Date().getTime())/2;t(L,M,O,C);if((h.tokenParsed&&h.tokenParsed.nonce!=H.storedNonce)||(h.refreshTokenParsed&&h.refreshTokenParsed.nonce!=H.storedNonce)||(h.idTokenParsed&&h.idTokenParsed.nonce!=H.storedNonce)){console.info("[KEYCLOAK] Invalid nonce, clearing token");h.clearToken();K&&K.setError()}else{if(N){h.onAuthSuccess&&h.onAuthSuccess();K&&K.setSuccess()}}}}function j(C){var F=d();var B;if(!z){B="keycloak.json"}else{if(typeof z==="string"){B=z}}if(B){var E=new XMLHttpRequest();E.open("GET",B,true);E.setRequestHeader("Accept","application/json");E.onreadystatechange=function(){if(E.readyState==4){if(E.status==200){var G=JSON.parse(E.responseText);h.authServerUrl=G["auth-server-url"];h.realm=G.realm;h.clientId=G.resource;h.clientSecret=(G.credentials||{})["secret"];F.setSuccess()}else{F.setError()}}};E.send()}else{if(!z.url){var A=document.getElementsByTagName("script");for(var D=0;D<A.length;D++){if(A[D].src.match(/.*keycloak\.js/)){z.url=A[D].src.substr(0,A[D].src.indexOf("/js/keycloak.js"));break}}}if(!z.realm){throw"realm missing"}if(!z.clientId){throw"clientId missing"}h.authServerUrl=z.url;h.realm=z.realm;h.clientId=z.clientId;h.clientSecret=(z.credentials||{}).secret;F.setSuccess()}return F.promise}function t(D,C,F,B){if(h.tokenTimeoutHandle){clearTimeout(h.tokenTimeoutHandle);h.tokenTimeoutHandle=null}if(C){h.refreshToken=C;h.refreshTokenParsed=r(C)}else{delete h.refreshToken;delete h.refreshTokenParsed}if(F){h.idToken=F;h.idTokenParsed=r(F)}else{delete h.idToken;delete h.idTokenParsed}if(D){h.token=D;h.tokenParsed=r(D);var E=h.realm+"/"+h.tokenParsed.sub;if(h.tokenParsed.session_state){E=E+"/"+h.tokenParsed.session_state}h.sessionId=E;h.authenticated=true;h.subject=h.tokenParsed.sub;h.realmAccess=h.tokenParsed.realm_access;h.resourceAccess=h.tokenParsed.resource_access;if(B){h.timeSkew=Math.floor(B/1000)-h.tokenParsed.iat;console.info("[KEYCLOAK] Estimated time difference between browser and server is "+h.timeSkew+" seconds");if(h.onTokenExpired){var A=(h.tokenParsed.exp-(new Date().getTime()/1000)+h.timeSkew)*1000;console.info("[KEYCLOAK] Token expires in "+Math.round(A/1000)+" s");if(A<=0){h.onTokenExpired()}else{h.tokenTimeoutHandle=setTimeout(h.onTokenExpired,A)}}}else{h.updateToken(-1)}}else{if(C){h.updateToken(-1)}else{delete h.token;delete h.tokenParsed;delete h.subject;delete h.realmAccess;delete h.resourceAccess;h.authenticated=false}}}function r(A){A=A.split(".")[1];A=A.replace("/-/g","+");A=A.replace("/_/g","/");switch(A.length%4){case 0:break;case 2:A+="==";break;case 3:A+="=";break;default:throw"Invalid token"}A=(A+"===").slice(0,A.length+(A.length%4));A=A.replace(/-/g,"+").replace(/_/g,"/");A=decodeURIComponent(escape(atob(A)));A=JSON.parse(A);return A}function e(){var D=[];var A="0123456789abcdef";for(var B=0;B<36;B++){D[B]=A.substr(Math.floor(Math.random()*16),1)}D[14]="4";D[19]=A.substr((D[19]&3)|8,1);D[8]=D[13]=D[18]=D[23]="-";var C=D.join("");return C}h.callback_id=0;function s(){var A="<id: "+(h.callback_id++)+(Math.random())+">";return A}function g(B){var A=new k(B,h.responseMode).parseUri();var C=f.get(A.state);if(C&&(A.code||A.error||A.access_token||A.id_token)){A.redirectUri=C.redirectUri;A.storedNonce=C.nonce;A.prompt=C.prompt;if(A.fragment){A.newUrl+="#"+A.fragment}return A}}function d(){var A={setSuccess:function(B){A.success=true;A.result=B;if(A.successCallback){A.successCallback(B)}},setError:function(B){A.error=true;A.result=B;if(A.errorCallback){A.errorCallback(B)}},promise:{success:function(B){if(A.success){B(A.result)}else{if(!A.error){A.successCallback=B}}return A.promise},error:function(B){if(A.error){B(A.result)}else{if(!A.success){A.errorCallback=B}}return A.promise}}};return A}function y(){var E=d();if(!v.enable){E.setSuccess();return E.promise}if(v.iframe){E.setSuccess();return E.promise}var C=document.createElement("iframe");v.iframe=C;C.onload=function(){var F=o();if(F.charAt(0)==="/"){v.iframeOrigin=w()}else{v.iframeOrigin=F.substring(0,F.indexOf("/",8))}E.setSuccess();setTimeout(A,v.interval*1000)};var D=o()+"/protocol/openid-connect/login-status-iframe.html";C.setAttribute("src",D);C.style.display="none";document.body.appendChild(C);var B=function(H){if(H.origin!==v.iframeOrigin){return}if(H.data!="unchanged"){h.clearToken()}var G=v.callbackList.splice(0,v.callbackList.length);for(var F=G.length-1;F>=0;--F){var I=G[F];if(H.data=="unchanged"){I.setSuccess()}else{I.setError()}}};a.addEventListener("message",B,false);var A=function(){l();if(h.token){setTimeout(A,v.interval*1000)}};return E.promise}function l(){var C=d();if(v.iframe&&v.iframeOrigin){var B=h.clientId+" "+h.sessionId;v.callbackList.push(C);var A=v.iframeOrigin;if(v.callbackList.length==1){v.iframe.contentWindow.postMessage(B,A)}}else{C.setSuccess()}return C.promise}function x(A){if(!A||A=="default"){return{login:function(B){a.location.href=h.createLoginUrl(B);return d().promise},logout:function(B){a.location.href=h.createLogoutUrl(B);return d().promise},register:function(B){a.location.href=h.createRegisterUrl(B);return d().promise},accountManagement:function(){a.location.href=h.createAccountUrl();return d().promise},redirectUri:function(B,C){if(arguments.length==1){C=true}if(B&&B.redirectUri){return B.redirectUri}else{if(h.redirectUri){return h.redirectUri}else{var D=location.href;if(location.hash&&C){D=D.substring(0,location.href.indexOf("#"));D+=(D.indexOf("?")==-1?"?":"&")+"redirect_fragment="+encodeURIComponent(location.hash.substring(1))}return D}}}}}if(A=="cordova"){v.enable=false;return{login:function(B){var G=d();var F="location=no";if(B&&B.prompt=="none"){F+=",hidden=yes"}var E=h.createLoginUrl(B);var D=a.open(E,"_blank",F);var C=false;D.addEventListener("loadstart",function(H){if(H.url.indexOf("http://localhost")==0){var I=g(H.url);n(I,G);D.close();C=true}});D.addEventListener("loaderror",function(H){if(!C){if(H.url.indexOf("http://localhost")==0){var I=g(H.url);n(I,G);D.close();C=true}else{G.setError();D.close()}}});return G.promise},logout:function(D){var F=d();var B=h.createLogoutUrl(D);var E=a.open(B,"_blank","location=no,hidden=yes");var C;E.addEventListener("loadstart",function(G){if(G.url.indexOf("http://localhost")==0){E.close()}});E.addEventListener("loaderror",function(G){if(G.url.indexOf("http://localhost")==0){E.close()}else{C=true;E.close()}});E.addEventListener("exit",function(G){if(C){F.setError()}else{h.clearToken();F.setSuccess()}});return F.promise},register:function(){var B=h.createRegisterUrl();var C=a.open(B,"_blank","location=no");C.addEventListener("loadstart",function(D){if(D.url.indexOf("http://localhost")==0){C.close()}})},accountManagement:function(){var B=h.createAccountUrl();var C=a.open(B,"_blank","location=no");C.addEventListener("loadstart",function(D){if(D.url.indexOf("http://localhost")==0){C.close()}})},redirectUri:function(B){return"http://localhost"}}}throw"invalid adapter type: "+A}var m=function(){if(!(this instanceof m)){return new m()}localStorage.setItem("kc-test","test");localStorage.removeItem("kc-test");var A=this;function B(){var H=new Date().getTime();for(var E=0;E<localStorage.length;E++){var D=localStorage.key(E);if(D&&D.indexOf("kc-callback-")==0){var G=localStorage.getItem(D);if(G){try{var C=JSON.parse(G).expires;if(!C||C<H){localStorage.removeItem(D)}}catch(F){localStorage.removeItem(D)}}}}}A.get=function(E){if(!E){return}var C="kc-callback-"+E;var D=localStorage.getItem(C);if(D){localStorage.removeItem(C);D=JSON.parse(D)}B();return D};A.add=function(D){B();var C="kc-callback-"+D.state;D.expires=new Date().getTime()+(60*60*1000);localStorage.setItem(C,JSON.stringify(D))}};var i=function(){if(!(this instanceof i)){return new i()}var C=this;C.get=function(F){if(!F){return}var E=A("kc-callback-"+F);B("kc-callback-"+F,"",D(-100));if(E){return JSON.parse(E)}};C.add=function(E){B("kc-callback-"+E.state,JSON.stringify(E),D(60))};C.removeItem=function(E){B(E,"",D(-100))};var D=function(E){var F=new Date();F.setTime(F.getTime()+(E*60*1000));return F};var A=function(H){var F=H+"=";var E=document.cookie.split(";");for(var G=0;G<E.length;G++){var I=E[G];while(I.charAt(0)==" "){I=I.substring(1)}if(I.indexOf(F)==0){return I.substring(F.length,I.length)}}return""};var B=function(G,H,E){var F=G+"="+H+"; expires="+E.toUTCString()+"; ";document.cookie=F}};function q(){try{return new m()}catch(A){}return new i()}var k=function(A,D){if(!(this instanceof k)){return new k(A,D)}var F=this;var B=function(){var J=null;var K=null;var I=null;var G=A.indexOf("?");var H=A.indexOf("#",G+1);if(G==-1&&H==-1){J=A}else{if(G!=-1){J=A.substring(0,G);K=A.substring(G+1);if(H!=-1){H=K.indexOf("#");I=K.substring(H+1);K=K.substring(0,H)}}else{J=A.substring(0,H);I=A.substring(H+1)}}return{baseUri:J,queryString:K,fragmentString:I}};var C=function(M){var G={};var L=M.split("&");for(var H=0;H<L.length;H++){var J=L[H].split("=");var I=decodeURIComponent(J[0]);var K=decodeURIComponent(J[1]);G[I]=K}return G};var E=function(J,K,G){var I=["code","state","error","error_description"];for(var H=0;H<I.length;H++){if(J===I[H]){G[J]=K;return true}}return false};F.parseUri=function(){var K=B();var I={};if(K.queryString){I=C(K.queryString)}var G={newUrl:K.baseUri};for(var J in I){switch(J){case"redirect_fragment":G.fragment=I[J];break;default:if(D!="query"||!E(J,I[J],G)){G.newUrl+=(G.newUrl.indexOf("?")==-1?"?":"&")+J+"="+I[J]}break}}if(D==="fragment"){var H={};if(K.fragmentString){H=C(K.fragmentString)}for(var J in H){G[J]=H[J]}}return G}}};if(typeof module==="object"&&module&&typeof module.exports==="object"){module.exports=b}else{a.Keycloak=b;if(typeof define==="function"&&define.amd){define("keycloak",[],function(){return b})}}})(window); | ||
(function(a,c){var b=function(z){if(!(this instanceof b)){return new b(z)}var h=this;var p;var u=[];var f;var v={enable:true,callbackList:[],interval:5};h.init=function(E){h.authenticated=false;f=q();if(E&&E.adapter==="cordova"){p=x("cordova")}else{if(E&&E.adapter==="default"){p=x()}else{if(a.Cordova){p=x("cordova")}else{p=x()}}}if(E){if(typeof E.checkLoginIframe!=="undefined"){v.enable=E.checkLoginIframe}if(E.checkLoginIframeInterval){v.interval=E.checkLoginIframeInterval}if(E.onLoad==="login-required"){h.loginRequired=true}if(E.responseMode){if(E.responseMode==="query"||E.responseMode==="fragment"){h.responseMode=E.responseMode}else{throw"Invalid value for responseMode"}}if(E.flow){switch(E.flow){case"standard":h.responseType="code";break;case"implicit":h.responseType="id_token token";break;case"hybrid":h.responseType="code id_token token";break;default:throw"Invalid value for flow"}h.flow=E.flow}if(E.timeSkew!=null){h.timeSkew=E.timeSkew}}if(!h.responseMode){h.responseMode="fragment"}if(!h.responseType){h.responseType="code";h.flow="standard"}var F=d();var B=d();B.promise.success(function(){h.onReady&&h.onReady(h.authenticated);F.setSuccess(h.authenticated)}).error(function(G){F.setError(G)});var D=j(z);function C(){var H=function(I){if(!I){G.prompt="none"}h.login(G).success(function(){B.setSuccess()}).error(function(){B.setError()})};var G={};switch(E.onLoad){case"check-sso":if(v.enable){y().success(function(){l().success(function(){H(false)}).error(function(){B.setSuccess()})})}else{H(false)}break;case"login-required":H(true);break;default:throw"Invalid value for onLoad"}}function A(){var G=g(a.location.href);if(G){y();a.history.replaceState({},null,G.newUrl);n(G,B);return}else{if(E){if(E.refreshToken){t(E.token,E.refreshToken,E.idToken);if(v.enable){y().success(function(){l().success(function(){h.onAuthSuccess&&h.onAuthSuccess();B.setSuccess()}).error(function(){t(null,null,null);B.setSuccess()})})}else{h.updateToken(-1).success(function(){h.onAuthSuccess&&h.onAuthSuccess();B.setSuccess()}).error(function(){h.onAuthError&&h.onAuthError();if(E.onLoad){C()}else{B.setError()}})}}else{if(E.onLoad){C()}else{B.setSuccess()}}}else{B.setSuccess()}}}D.success(A);D.error(function(){F.setError()});return F.promise};h.login=function(A){return p.login(A)};h.createLoginUrl=function(C){var G=e();var E=e();var H=p.redirectUri(C);var A={state:G,nonce:E,redirectUri:encodeURIComponent(H),};if(C&&C.prompt){A.prompt=C.prompt}f.add(A);var F="auth";if(C&&C.action=="register"){F="registrations"}var D=(C&&C.scope)?"openid "+C.scope:"openid";var B=o()+"/protocol/openid-connect/"+F+"?client_id="+encodeURIComponent(h.clientId)+"&redirect_uri="+encodeURIComponent(H)+"&state="+encodeURIComponent(G)+"&nonce="+encodeURIComponent(E)+"&response_mode="+encodeURIComponent(h.responseMode)+"&response_type="+encodeURIComponent(h.responseType)+"&scope="+encodeURIComponent(D);if(C&&C.prompt){B+="&prompt="+encodeURIComponent(C.prompt)}if(C&&C.maxAge){B+="&max_age="+encodeURIComponent(C.maxAge)}if(C&&C.loginHint){B+="&login_hint="+encodeURIComponent(C.loginHint)}if(C&&C.idpHint){B+="&kc_idp_hint="+encodeURIComponent(C.idpHint)}if(C&&C.locale){B+="&ui_locales="+encodeURIComponent(C.locale)}return B};h.logout=function(A){return p.logout(A)};h.createLogoutUrl=function(B){var A=o()+"/protocol/openid-connect/logout?redirect_uri="+encodeURIComponent(p.redirectUri(B,false));return A};h.register=function(A){return p.register(A)};h.createRegisterUrl=function(A){if(!A){A={}}A.action="register";return h.createLoginUrl(A)};h.createAccountUrl=function(B){var A=o()+"/account?referrer="+encodeURIComponent(h.clientId)+"&referrer_uri="+encodeURIComponent(p.redirectUri(B));return A};h.accountManagement=function(){return p.accountManagement()};h.hasRealmRole=function(B){var A=h.realmAccess;return !!A&&A.roles.indexOf(B)>=0};h.hasResourceRole=function(C,B){if(!h.resourceAccess){return false}var A=h.resourceAccess[B||h.clientId];return !!A&&A.roles.indexOf(C)>=0};h.loadUserProfile=function(){var A=o()+"/account";var B=new XMLHttpRequest();B.open("GET",A,true);B.setRequestHeader("Accept","application/json");B.setRequestHeader("Authorization","bearer "+h.token);var C=d();B.onreadystatechange=function(){if(B.readyState==4){if(B.status==200){h.profile=JSON.parse(B.responseText);C.setSuccess(h.profile)}else{C.setError()}}};B.send();return C.promise};h.loadUserInfo=function(){var A=o()+"/protocol/openid-connect/userinfo";var B=new XMLHttpRequest();B.open("GET",A,true);B.setRequestHeader("Accept","application/json");B.setRequestHeader("Authorization","bearer "+h.token);var C=d();B.onreadystatechange=function(){if(B.readyState==4){if(B.status==200){h.userInfo=JSON.parse(B.responseText);C.setSuccess(h.userInfo)}else{C.setError()}}};B.send();return C.promise};h.isTokenExpired=function(A){if(!h.tokenParsed||(!h.refreshToken&&h.flow!="implicit")){throw"Not authenticated"}if(h.timeSkew==null){console.info("[KEYCLOAK] Unable to determine if token is expired as timeskew is not set");return true}var B=h.tokenParsed.exp-Math.ceil(new Date().getTime()/1000)+h.timeSkew;if(A){B-=A}return B<0};h.updateToken=function(A){var D=d();if(!h.refreshToken){D.setError();return D.promise}A=A||5;var B=function(){var G=false;if(A==-1){G=true;console.info("[KEYCLOAK] Refreshing token: forced refresh")}else{if(!h.tokenParsed||h.isTokenExpired(A)){G=true;console.info("[KEYCLOAK] Refreshing token: token expired")}}if(!G){D.setSuccess(false)}else{var I="grant_type=refresh_token&refresh_token="+h.refreshToken;var F=o()+"/protocol/openid-connect/token";u.push(D);if(u.length==1){var H=new XMLHttpRequest();H.open("POST",F,true);H.setRequestHeader("Content-type","application/x-www-form-urlencoded");H.withCredentials=true;if(h.clientId&&h.clientSecret){H.setRequestHeader("Authorization","Basic "+btoa(h.clientId+":"+h.clientSecret))}else{I+="&client_id="+encodeURIComponent(h.clientId)}var E=new Date().getTime();H.onreadystatechange=function(){if(H.readyState==4){if(H.status==200){console.info("[KEYCLOAK] Token refreshed");E=(E+new Date().getTime())/2;var K=JSON.parse(H.responseText);t(K.access_token,K.refresh_token,K.id_token,E);h.onAuthRefreshSuccess&&h.onAuthRefreshSuccess();for(var J=u.pop();J!=null;J=u.pop()){J.setSuccess(true)}}else{console.warn("[KEYCLOAK] Failed to refresh token");h.onAuthRefreshError&&h.onAuthRefreshError();for(var J=u.pop();J!=null;J=u.pop()){J.setError(true)}}}};H.send(I)}}};if(v.enable){var C=l();C.success(function(){B()}).error(function(){D.setError()})}else{B()}return D.promise};h.clearToken=function(){if(h.token){t(null,null,null);h.onAuthLogout&&h.onAuthLogout();if(h.loginRequired){h.login()}}};function o(){if(h.authServerUrl.charAt(h.authServerUrl.length-1)=="/"){return h.authServerUrl+"realms/"+encodeURIComponent(h.realm)}else{return h.authServerUrl+"/realms/"+encodeURIComponent(h.realm)}}function w(){if(!a.location.origin){return a.location.protocol+"//"+a.location.hostname+(a.location.port?":"+a.location.port:"")}else{return a.location.origin}}function n(H,K){var D=H.code;var I=H.error;var E=H.prompt;var C=new Date().getTime();if(I){if(E!="none"){var B={error:I,error_description:H.error_description};h.onAuthError&&h.onAuthError(B);K&&K.setError(B)}else{K&&K.setSuccess()}return}else{if((h.flow!="standard")&&(H.access_token||H.id_token)){G(H.access_token,null,H.id_token,true)}}if((h.flow!="implicit")&&D){var F="code="+D+"&grant_type=authorization_code";var A=o()+"/protocol/openid-connect/token";var J=new XMLHttpRequest();J.open("POST",A,true);J.setRequestHeader("Content-type","application/x-www-form-urlencoded");if(h.clientId&&h.clientSecret){J.setRequestHeader("Authorization","Basic "+btoa(h.clientId+":"+h.clientSecret))}else{F+="&client_id="+encodeURIComponent(h.clientId)}F+="&redirect_uri="+H.redirectUri;J.withCredentials=true;J.onreadystatechange=function(){if(J.readyState==4){if(J.status==200){var L=JSON.parse(J.responseText);G(L.access_token,L.refresh_token,L.id_token,h.flow==="standard")}else{h.onAuthError&&h.onAuthError();K&&K.setError()}}};J.send(F)}function G(L,M,O,N){C=(C+new Date().getTime())/2;t(L,M,O,C);if((h.tokenParsed&&h.tokenParsed.nonce!=H.storedNonce)||(h.refreshTokenParsed&&h.refreshTokenParsed.nonce!=H.storedNonce)||(h.idTokenParsed&&h.idTokenParsed.nonce!=H.storedNonce)){console.info("[KEYCLOAK] Invalid nonce, clearing token");h.clearToken();K&&K.setError()}else{if(N){h.onAuthSuccess&&h.onAuthSuccess();K&&K.setSuccess()}}}}function j(C){var F=d();var B;if(!z){B="keycloak.json"}else{if(typeof z==="string"){B=z}}if(B){var E=new XMLHttpRequest();E.open("GET",B,true);E.setRequestHeader("Accept","application/json");E.onreadystatechange=function(){if(E.readyState==4){if(E.status==200){var G=JSON.parse(E.responseText);h.authServerUrl=G["auth-server-url"];h.realm=G.realm;h.clientId=G.resource;h.clientSecret=(G.credentials||{})["secret"];F.setSuccess()}else{F.setError()}}};E.send()}else{if(!z.url){var A=document.getElementsByTagName("script");for(var D=0;D<A.length;D++){if(A[D].src.match(/.*keycloak\.js/)){z.url=A[D].src.substr(0,A[D].src.indexOf("/js/keycloak.js"));break}}}if(!z.realm){throw"realm missing"}if(!z.clientId){throw"clientId missing"}h.authServerUrl=z.url;h.realm=z.realm;h.clientId=z.clientId;h.clientSecret=(z.credentials||{}).secret;F.setSuccess()}return F.promise}function t(D,C,E,B){if(h.tokenTimeoutHandle){clearTimeout(h.tokenTimeoutHandle);h.tokenTimeoutHandle=null}if(C){h.refreshToken=C;h.refreshTokenParsed=r(C)}else{delete h.refreshToken;delete h.refreshTokenParsed}if(E){h.idToken=E;h.idTokenParsed=r(E)}else{delete h.idToken;delete h.idTokenParsed}if(D){h.token=D;h.tokenParsed=r(D);h.sessionId=h.tokenParsed.session_state;h.authenticated=true;h.subject=h.tokenParsed.sub;h.realmAccess=h.tokenParsed.realm_access;h.resourceAccess=h.tokenParsed.resource_access;if(B){h.timeSkew=Math.floor(B/1000)-h.tokenParsed.iat}if(h.timeSkew!=null){console.info("[KEYCLOAK] Estimated time difference between browser and server is "+h.timeSkew+" seconds");if(h.onTokenExpired){var A=(h.tokenParsed.exp-(new Date().getTime()/1000)+h.timeSkew)*1000;console.info("[KEYCLOAK] Token expires in "+Math.round(A/1000)+" s");if(A<=0){h.onTokenExpired()}else{h.tokenTimeoutHandle=setTimeout(h.onTokenExpired,A)}}}}else{delete h.token;delete h.tokenParsed;delete h.subject;delete h.realmAccess;delete h.resourceAccess;h.authenticated=false}}function r(A){A=A.split(".")[1];A=A.replace("/-/g","+");A=A.replace("/_/g","/");switch(A.length%4){case 0:break;case 2:A+="==";break;case 3:A+="=";break;default:throw"Invalid token"}A=(A+"===").slice(0,A.length+(A.length%4));A=A.replace(/-/g,"+").replace(/_/g,"/");A=decodeURIComponent(escape(atob(A)));A=JSON.parse(A);return A}function e(){var D=[];var A="0123456789abcdef";for(var B=0;B<36;B++){D[B]=A.substr(Math.floor(Math.random()*16),1)}D[14]="4";D[19]=A.substr((D[19]&3)|8,1);D[8]=D[13]=D[18]=D[23]="-";var C=D.join("");return C}h.callback_id=0;function s(){var A="<id: "+(h.callback_id++)+(Math.random())+">";return A}function g(B){var A=new k(B,h.responseMode).parseUri();var C=f.get(A.state);if(C&&(A.code||A.error||A.access_token||A.id_token)){A.redirectUri=C.redirectUri;A.storedNonce=C.nonce;A.prompt=C.prompt;if(A.fragment){A.newUrl+="#"+A.fragment}return A}}function d(){var A={setSuccess:function(B){A.success=true;A.result=B;if(A.successCallback){A.successCallback(B)}},setError:function(B){A.error=true;A.result=B;if(A.errorCallback){A.errorCallback(B)}},promise:{success:function(B){if(A.success){B(A.result)}else{if(!A.error){A.successCallback=B}}return A.promise},error:function(B){if(A.error){B(A.result)}else{if(!A.success){A.errorCallback=B}}return A.promise}}};return A}function y(){var E=d();if(!v.enable){E.setSuccess();return E.promise}if(v.iframe){E.setSuccess();return E.promise}var C=document.createElement("iframe");v.iframe=C;C.onload=function(){var F=o();if(F.charAt(0)==="/"){v.iframeOrigin=w()}else{v.iframeOrigin=F.substring(0,F.indexOf("/",8))}E.setSuccess();setTimeout(A,v.interval*1000)};var D=o()+"/protocol/openid-connect/login-status-iframe.html";C.setAttribute("src",D);C.style.display="none";document.body.appendChild(C);var B=function(H){if(H.origin!==v.iframeOrigin){return}if(H.data!="unchanged"){h.clearToken()}var G=v.callbackList.splice(0,v.callbackList.length);for(var F=G.length-1;F>=0;--F){var I=G[F];if(H.data=="unchanged"){I.setSuccess()}else{I.setError()}}};a.addEventListener("message",B,false);var A=function(){l();if(h.token){setTimeout(A,v.interval*1000)}};return E.promise}function l(){var C=d();if(v.iframe&&v.iframeOrigin){var B=h.clientId+" "+h.sessionId;v.callbackList.push(C);var A=v.iframeOrigin;if(v.callbackList.length==1){v.iframe.contentWindow.postMessage(B,A)}}else{C.setSuccess()}return C.promise}function x(A){if(!A||A=="default"){return{login:function(B){a.location.href=h.createLoginUrl(B);return d().promise},logout:function(B){a.location.href=h.createLogoutUrl(B);return d().promise},register:function(B){a.location.href=h.createRegisterUrl(B);return d().promise},accountManagement:function(){a.location.href=h.createAccountUrl();return d().promise},redirectUri:function(B,C){if(arguments.length==1){C=true}if(B&&B.redirectUri){return B.redirectUri}else{if(h.redirectUri){return h.redirectUri}else{var D=location.href;if(location.hash&&C){D=D.substring(0,location.href.indexOf("#"));D+=(D.indexOf("?")==-1?"?":"&")+"redirect_fragment="+encodeURIComponent(location.hash.substring(1))}return D}}}}}if(A=="cordova"){v.enable=false;return{login:function(B){var G=d();var F="location=no";if(B&&B.prompt=="none"){F+=",hidden=yes"}var E=h.createLoginUrl(B);var D=a.open(E,"_blank",F);var C=false;D.addEventListener("loadstart",function(H){if(H.url.indexOf("http://localhost")==0){var I=g(H.url);n(I,G);D.close();C=true}});D.addEventListener("loaderror",function(H){if(!C){if(H.url.indexOf("http://localhost")==0){var I=g(H.url);n(I,G);D.close();C=true}else{G.setError();D.close()}}});return G.promise},logout:function(D){var F=d();var B=h.createLogoutUrl(D);var E=a.open(B,"_blank","location=no,hidden=yes");var C;E.addEventListener("loadstart",function(G){if(G.url.indexOf("http://localhost")==0){E.close()}});E.addEventListener("loaderror",function(G){if(G.url.indexOf("http://localhost")==0){E.close()}else{C=true;E.close()}});E.addEventListener("exit",function(G){if(C){F.setError()}else{h.clearToken();F.setSuccess()}});return F.promise},register:function(){var B=h.createRegisterUrl();var C=a.open(B,"_blank","location=no");C.addEventListener("loadstart",function(D){if(D.url.indexOf("http://localhost")==0){C.close()}})},accountManagement:function(){var B=h.createAccountUrl();var C=a.open(B,"_blank","location=no");C.addEventListener("loadstart",function(D){if(D.url.indexOf("http://localhost")==0){C.close()}})},redirectUri:function(B){return"http://localhost"}}}throw"invalid adapter type: "+A}var m=function(){if(!(this instanceof m)){return new m()}localStorage.setItem("kc-test","test");localStorage.removeItem("kc-test");var A=this;function B(){var H=new Date().getTime();for(var E=0;E<localStorage.length;E++){var D=localStorage.key(E);if(D&&D.indexOf("kc-callback-")==0){var G=localStorage.getItem(D);if(G){try{var C=JSON.parse(G).expires;if(!C||C<H){localStorage.removeItem(D)}}catch(F){localStorage.removeItem(D)}}}}}A.get=function(E){if(!E){return}var C="kc-callback-"+E;var D=localStorage.getItem(C);if(D){localStorage.removeItem(C);D=JSON.parse(D)}B();return D};A.add=function(D){B();var C="kc-callback-"+D.state;D.expires=new Date().getTime()+(60*60*1000);localStorage.setItem(C,JSON.stringify(D))}};var i=function(){if(!(this instanceof i)){return new i()}var C=this;C.get=function(F){if(!F){return}var E=A("kc-callback-"+F);B("kc-callback-"+F,"",D(-100));if(E){return JSON.parse(E)}};C.add=function(E){B("kc-callback-"+E.state,JSON.stringify(E),D(60))};C.removeItem=function(E){B(E,"",D(-100))};var D=function(E){var F=new Date();F.setTime(F.getTime()+(E*60*1000));return F};var A=function(H){var F=H+"=";var E=document.cookie.split(";");for(var G=0;G<E.length;G++){var I=E[G];while(I.charAt(0)==" "){I=I.substring(1)}if(I.indexOf(F)==0){return I.substring(F.length,I.length)}}return""};var B=function(G,H,E){var F=G+"="+H+"; expires="+E.toUTCString()+"; ";document.cookie=F}};function q(){try{return new m()}catch(A){}return new i()}var k=function(A,D){if(!(this instanceof k)){return new k(A,D)}var F=this;var B=function(){var J=null;var K=null;var I=null;var G=A.indexOf("?");var H=A.indexOf("#",G+1);if(G==-1&&H==-1){J=A}else{if(G!=-1){J=A.substring(0,G);K=A.substring(G+1);if(H!=-1){H=K.indexOf("#");I=K.substring(H+1);K=K.substring(0,H)}}else{J=A.substring(0,H);I=A.substring(H+1)}}return{baseUri:J,queryString:K,fragmentString:I}};var C=function(M){var G={};var L=M.split("&");for(var H=0;H<L.length;H++){var J=L[H].split("=");var I=decodeURIComponent(J[0]);var K=decodeURIComponent(J[1]);G[I]=K}return G};var E=function(J,K,G){var I=["code","state","error","error_description"];for(var H=0;H<I.length;H++){if(J===I[H]){G[J]=K;return true}}return false};F.parseUri=function(){var K=B();var I={};if(K.queryString){I=C(K.queryString)}var G={newUrl:K.baseUri};for(var J in I){switch(J){case"redirect_fragment":G.fragment=I[J];break;default:if(D!="query"||!E(J,I[J],G)){G.newUrl+=(G.newUrl.indexOf("?")==-1?"?":"&")+J+"="+I[J]}break}}if(D==="fragment"){var H={};if(K.fragmentString){H=C(K.fragmentString)}for(var J in H){G[J]=H[J]}}return G}}};if(typeof module==="object"&&module&&typeof module.exports==="object"){module.exports=b}else{a.Keycloak=b;if(typeof define==="function"&&define.amd){define("keycloak",[],function(){return b})}}})(window); |
{ | ||
"name": "keycloak-js", | ||
"version": "2.5.1", | ||
"version": "2.5.4", | ||
"description": "Keycloak Adapter", | ||
@@ -5,0 +5,0 @@ "main": "dist/keycloak.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
86023
1210