Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

keycloak-js

Package Overview
Dependencies
Maintainers
2
Versions
150
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

keycloak-js - npm Package Compare versions

Comparing version 2.1.0 to 2.2.0-cr.1

2

bower.json
{
"name": "keycloak",
"version": "2.1.0",
"version": "2.2.0-cr.1",
"main": "dist/keycloak.js",

@@ -5,0 +5,0 @@ "ignore": [

@@ -157,3 +157,3 @@ /*

if (initOptions.token || initOptions.refreshToken) {
setToken(initOptions.token, initOptions.refreshToken, initOptions.idToken, false);
setToken(initOptions.token, initOptions.refreshToken, initOptions.idToken);
kc.timeSkew = initOptions.timeSkew || 0;

@@ -164,4 +164,6 @@

checkLoginIframe().success(function () {
kc.onAuthSuccess && kc.onAuthSuccess();
initPromise.setSuccess();
}).error(function () {
kc.onAuthError && kc.onAuthError();
if (initOptions.onLoad) {

@@ -173,3 +175,11 @@ onLoad();

} else {
initPromise.setSuccess();
kc.updateToken(-1).success(function() {
kc.onAuthSuccess && kc.onAuthSuccess();
initPromise.setSuccess();
}).error(function() {
kc.onAuthError && kc.onAuthError();
if (initOptions.onLoad) {
onLoad();
}
});
}

@@ -374,3 +384,3 @@ } else if (initOptions.onLoad) {

var exec = function() {
if (!kc.isTokenExpired(minValidity)) {
if (minValidity >= 0 && !kc.isTokenExpired(minValidity)) {
promise.setSuccess(false);

@@ -387,2 +397,3 @@ } else {

req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
req.withCredentials = true;

@@ -403,6 +414,6 @@ if (kc.clientId && kc.clientSecret) {

var tokenResponse = JSON.parse(req.responseText);
setToken(tokenResponse['access_token'], tokenResponse['refresh_token'], tokenResponse['id_token'], true);
kc.timeSkew = Math.floor(timeLocal / 1000) - kc.tokenParsed.iat;
setToken(tokenResponse['access_token'], tokenResponse['refresh_token'], tokenResponse['id_token']);
kc.onAuthRefreshSuccess && kc.onAuthRefreshSuccess();

@@ -442,3 +453,3 @@ for (var p = refreshQueue.pop(); p != null; p = refreshQueue.pop()) {

if (kc.token) {
setToken(null, null, null, true);
setToken(null, null, null);
kc.onAuthLogout && kc.onAuthLogout();

@@ -524,3 +535,3 @@ if (kc.loginRequired) {

setToken(accessToken, refreshToken, idToken, true);
setToken(accessToken, refreshToken, idToken);

@@ -609,3 +620,3 @@ if ((kc.tokenParsed && kc.tokenParsed.nonce != oauth.storedNonce) ||

function setToken(token, refreshToken, idToken, useTokenTime) {
function setToken(token, refreshToken, idToken) {
if (kc.tokenTimeoutHandle) {

@@ -630,5 +641,8 @@ clearTimeout(kc.tokenTimeoutHandle);

if (kc.onTokenExpired) {
var start = useTokenTime ? kc.tokenParsed.iat : (new Date().getTime() / 1000);
var expiresIn = kc.tokenParsed.exp - start;
kc.tokenTimeoutHandle = setTimeout(kc.onTokenExpired, expiresIn * 1000);
var expiresIn = (kc.tokenParsed['exp'] - (new Date().getTime() / 1000) + kc.timeSkew) * 1000;
if (expiresIn <= 0) {
kc.onTokenExpired();
} else {
kc.tokenTimeoutHandle = setTimeout(kc.onTokenExpired, expiresIn);
}
}

@@ -1068,3 +1082,3 @@

}
var cs = this;

@@ -1071,0 +1085,0 @@

@@ -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,callbackMap:[],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.token||E.refreshToken){t(E.token,E.refreshToken,E.idToken,false);h.timeSkew=E.timeSkew||0;if(v.enable){y().success(function(){l().success(function(){B.setSuccess()}).error(function(){if(E.onLoad){C()}})})}else{B.setSuccess()}}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(B){var F=e();var D=e();var G=p.redirectUri(B);if(B&&B.prompt){G+=(G.indexOf("?")==-1?"?":"&")+"prompt="+B.prompt}f.add({state:F,nonce:D,redirectUri:encodeURIComponent(G)});var E="auth";if(B&&B.action=="register"){E="registrations"}var C=(B&&B.scope)?"openid "+B.scope:"openid";var A=o()+"/protocol/openid-connect/"+E+"?client_id="+encodeURIComponent(h.clientId)+"&redirect_uri="+encodeURIComponent(G)+"&state="+encodeURIComponent(F)+"&nonce="+encodeURIComponent(D)+"&response_mode="+encodeURIComponent(h.responseMode)+"&response_type="+encodeURIComponent(h.responseType)+"&scope="+encodeURIComponent(C);if(B&&B.prompt){A+="&prompt="+encodeURIComponent(B.prompt)}if(B&&B.maxAge){A+="&max_age="+encodeURIComponent(B.maxAge)}if(B&&B.loginHint){A+="&login_hint="+encodeURIComponent(B.loginHint)}if(B&&B.idpHint){A+="&kc_idp_hint="+encodeURIComponent(B.idpHint)}if(B&&B.locale){A+="&ui_locales="+encodeURIComponent(B.locale)}return A};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-(new Date().getTime()/1000)+h.timeSkew;if(A){B-=A}return B<0};h.updateToken=function(A){var D=d();if(!h.tokenParsed||!h.refreshToken){D.setError();return D.promise}A=A||5;var B=function(){if(!h.isTokenExpired(A)){D.setSuccess(false)}else{var H="grant_type=refresh_token&refresh_token="+h.refreshToken;var F=o()+"/protocol/openid-connect/token";u.push(D);if(u.length==1){var G=new XMLHttpRequest();G.open("POST",F,true);G.setRequestHeader("Content-type","application/x-www-form-urlencoded");if(h.clientId&&h.clientSecret){G.setRequestHeader("Authorization","Basic "+btoa(h.clientId+":"+h.clientSecret))}else{H+="&client_id="+encodeURIComponent(h.clientId)}var E=new Date().getTime();G.onreadystatechange=function(){if(G.readyState==4){if(G.status==200){E=(E+new Date().getTime())/2;var J=JSON.parse(G.responseText);t(J.access_token,J.refresh_token,J.id_token,true);h.timeSkew=Math.floor(E/1000)-h.tokenParsed.iat;h.onAuthRefreshSuccess&&h.onAuthRefreshSuccess();for(var I=u.pop();I!=null;I=u.pop()){I.setSuccess(true)}}else{h.onAuthRefreshError&&h.onAuthRefreshError();for(var I=u.pop();I!=null;I=u.pop()){I.setError(true)}}}};G.send(H)}}};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,true);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,true);if((h.tokenParsed&&h.tokenParsed.nonce!=H.storedNonce)||(h.refreshTokenParsed&&h.refreshTokenParsed.nonce!=H.storedNonce)||(h.idTokenParsed&&h.idTokenParsed.nonce!=H.storedNonce)){console.log("invalid nonce!");h.clearToken();K&&K.setError()}else{h.timeSkew=Math.floor(C/1000)-h.tokenParsed.iat;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,G,A){if(h.tokenTimeoutHandle){clearTimeout(h.tokenTimeoutHandle);h.tokenTimeoutHandle=null}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(h.onTokenExpired){var F=A?h.tokenParsed.iat:(new Date().getTime()/1000);var B=h.tokenParsed.exp-F;h.tokenTimeoutHandle=setTimeout(h.onTokenExpired,B*1000)}}else{delete h.token;delete h.tokenParsed;delete h.subject;delete h.realmAccess;delete h.resourceAccess;h.authenticated=false}if(C){h.refreshToken=C;h.refreshTokenParsed=r(C)}else{delete h.refreshToken;delete h.refreshTokenParsed}if(G){h.idToken=G;h.idTokenParsed=r(G)}else{delete h.idToken;delete h.idTokenParsed}}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;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?client_id="+encodeURIComponent(h.clientId)+"&origin="+w();C.setAttribute("src",D);C.style.display="none";document.body.appendChild(C);var B=function(G){if(G.origin!==v.iframeOrigin){return}try{var H=JSON.parse(G.data)}catch(F){return}if(!H.callbackId){return}var I=v.callbackMap[H.callbackId];if(!I){return}delete v.callbackMap[H.callbackId];if((!h.sessionId||h.sessionId==H.session)&&H.loggedIn){I.setSuccess()}else{h.clearToken();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={};B.callbackId=s();v.callbackMap[B.callbackId]=C;var A=v.iframeOrigin;v.iframe.contentWindow.postMessage(JSON.stringify(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=1;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;case"prompt":G.prompt=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,callbackMap:[],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.token||E.refreshToken){t(E.token,E.refreshToken,E.idToken);h.timeSkew=E.timeSkew||0;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{h.updateToken(-1).success(function(){h.onAuthSuccess&&h.onAuthSuccess();B.setSuccess()}).error(function(){h.onAuthError&&h.onAuthError();if(E.onLoad){C()}})}}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(B){var F=e();var D=e();var G=p.redirectUri(B);if(B&&B.prompt){G+=(G.indexOf("?")==-1?"?":"&")+"prompt="+B.prompt}f.add({state:F,nonce:D,redirectUri:encodeURIComponent(G)});var E="auth";if(B&&B.action=="register"){E="registrations"}var C=(B&&B.scope)?"openid "+B.scope:"openid";var A=o()+"/protocol/openid-connect/"+E+"?client_id="+encodeURIComponent(h.clientId)+"&redirect_uri="+encodeURIComponent(G)+"&state="+encodeURIComponent(F)+"&nonce="+encodeURIComponent(D)+"&response_mode="+encodeURIComponent(h.responseMode)+"&response_type="+encodeURIComponent(h.responseType)+"&scope="+encodeURIComponent(C);if(B&&B.prompt){A+="&prompt="+encodeURIComponent(B.prompt)}if(B&&B.maxAge){A+="&max_age="+encodeURIComponent(B.maxAge)}if(B&&B.loginHint){A+="&login_hint="+encodeURIComponent(B.loginHint)}if(B&&B.idpHint){A+="&kc_idp_hint="+encodeURIComponent(B.idpHint)}if(B&&B.locale){A+="&ui_locales="+encodeURIComponent(B.locale)}return A};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-(new Date().getTime()/1000)+h.timeSkew;if(A){B-=A}return B<0};h.updateToken=function(A){var D=d();if(!h.tokenParsed||!h.refreshToken){D.setError();return D.promise}A=A||5;var B=function(){if(A>=0&&!h.isTokenExpired(A)){D.setSuccess(false)}else{var H="grant_type=refresh_token&refresh_token="+h.refreshToken;var F=o()+"/protocol/openid-connect/token";u.push(D);if(u.length==1){var G=new XMLHttpRequest();G.open("POST",F,true);G.setRequestHeader("Content-type","application/x-www-form-urlencoded");G.withCredentials=true;if(h.clientId&&h.clientSecret){G.setRequestHeader("Authorization","Basic "+btoa(h.clientId+":"+h.clientSecret))}else{H+="&client_id="+encodeURIComponent(h.clientId)}var E=new Date().getTime();G.onreadystatechange=function(){if(G.readyState==4){if(G.status==200){E=(E+new Date().getTime())/2;var J=JSON.parse(G.responseText);h.timeSkew=Math.floor(E/1000)-h.tokenParsed.iat;t(J.access_token,J.refresh_token,J.id_token);h.onAuthRefreshSuccess&&h.onAuthRefreshSuccess();for(var I=u.pop();I!=null;I=u.pop()){I.setSuccess(true)}}else{h.onAuthRefreshError&&h.onAuthRefreshError();for(var I=u.pop();I!=null;I=u.pop()){I.setError(true)}}}};G.send(H)}}};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);if((h.tokenParsed&&h.tokenParsed.nonce!=H.storedNonce)||(h.refreshTokenParsed&&h.refreshTokenParsed.nonce!=H.storedNonce)||(h.idTokenParsed&&h.idTokenParsed.nonce!=H.storedNonce)){console.log("invalid nonce!");h.clearToken();K&&K.setError()}else{h.timeSkew=Math.floor(C/1000)-h.tokenParsed.iat;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(C,B,E){if(h.tokenTimeoutHandle){clearTimeout(h.tokenTimeoutHandle);h.tokenTimeoutHandle=null}if(C){h.token=C;h.tokenParsed=r(C);var D=h.realm+"/"+h.tokenParsed.sub;if(h.tokenParsed.session_state){D=D+"/"+h.tokenParsed.session_state}h.sessionId=D;h.authenticated=true;h.subject=h.tokenParsed.sub;h.realmAccess=h.tokenParsed.realm_access;h.resourceAccess=h.tokenParsed.resource_access;if(h.onTokenExpired){var A=(h.tokenParsed.exp-(new Date().getTime()/1000)+h.timeSkew)*1000;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}if(B){h.refreshToken=B;h.refreshTokenParsed=r(B)}else{delete h.refreshToken;delete h.refreshTokenParsed}if(E){h.idToken=E;h.idTokenParsed=r(E)}else{delete h.idToken;delete h.idTokenParsed}}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;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?client_id="+encodeURIComponent(h.clientId)+"&origin="+w();C.setAttribute("src",D);C.style.display="none";document.body.appendChild(C);var B=function(G){if(G.origin!==v.iframeOrigin){return}try{var H=JSON.parse(G.data)}catch(F){return}if(!H.callbackId){return}var I=v.callbackMap[H.callbackId];if(!I){return}delete v.callbackMap[H.callbackId];if((!h.sessionId||h.sessionId==H.session)&&H.loggedIn){I.setSuccess()}else{h.clearToken();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={};B.callbackId=s();v.callbackMap[B.callbackId]=C;var A=v.iframeOrigin;v.iframe.contentWindow.postMessage(JSON.stringify(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=1;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;case"prompt":G.prompt=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.1.0",
"version": "2.2.0-cr.1",
"description": "Keycloak Adapter",

@@ -5,0 +5,0 @@ "main": "dist/keycloak.js",

@@ -1,8 +0,14 @@

keycloak-adapter-bower
======================
Keycloak JavaScript Adapter
===========================
A [Bower](http://bower.io) project for the [Keycloak](http://www.keycloak.org/) [javascript adapter](https://keycloak.gitbooks.io/securing-client-applications-guide/content/topics/oidc/javascript-adapter.html).
JavaScript adapter for [Keycloak](http://www.keycloak.org/). For documentation see our [Securing Clients and Applications Guide](https://keycloak.gitbooks.io/securing-client-applications-guide/content/topics/oidc/javascript-adapter.html).
# Usage
## Issues
$ bower install --save keycloak
For general questions not covered by the documentation please send a mail to our [user mailing list](https://lists.jboss.org/mailman/listinfo/keycloak-user).
To report and bugs or request new features please create an issue in [JIRA](https://issues.jboss.org/browse/KEYCLOAK).
## Contributions
To provide a contribution send a PR to: https://github.com/keycloak/keycloak/blob/master/adapters/oidc/js/src/main/resources/keycloak.js.
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc