Skip to content

Commit

Permalink
Add processCallback, parseCallback and createPromise to public API of…
Browse files Browse the repository at this point in the history
… keycloak-js

Closes keycloak#10174
  • Loading branch information
luchsamapparat committed Feb 12, 2022
1 parent 1b77358 commit 2b4dc60
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 37 deletions.
47 changes: 47 additions & 0 deletions adapters/oidc/js/dist/keycloak.d.ts
Expand Up @@ -282,6 +282,14 @@ declare namespace Keycloak {
error(callback: KeycloakPromiseCallback<TError>): KeycloakPromise<TSuccess, TError>;
}

interface KeycloakPromiseWrapper<SuccessType, ErrorType> {
promise: KeycloakPromise<SuccessType, ErrorType>;
setSuccess(value: SuccessType): void;
setError(value: ErrorType): void;
resolve(value: SuccessType): void;
reject(value: ErrorType): void;
}

interface KeycloakError {
error: string;
error_description: string;
Expand Down Expand Up @@ -332,6 +340,26 @@ declare namespace Keycloak {
roles: string[];
}

interface KeycloakOAuth {
code?: string,
state?: string,
session_state?: string,
kc_action_status?: string,
access_token?: string,
token_type?: string,
id_token?: string,
expires_in?: string,
error?: string,
error_description?: string,
error_uri?: string,
newUrl?: string
valid?: boolean;
redirectUri?: string;
storedNonce?: string;
prompt?: string;
pkceCodeVerifier?: string;
}

/**
* A client for the Keycloak authentication server.
* @see {@link https://keycloak.gitbooks.io/securing-client-applications-guide/content/topics/oidc/javascript-adapter.html|Keycloak JS adapter documentation}
Expand Down Expand Up @@ -590,6 +618,25 @@ declare namespace Keycloak {
*/
clearToken(): void;

/**
* Processes the parameters from a callback URL.
* @param oauth The parameters from the callback URL as returned by parseCallback().
* @param promise A promise to be resolved when the callback has been processed.
*/
processCallback(oauth: KeycloakOAuth, promise: KeycloakPromiseWrapper<void, void>): void;

/**
* Extracts the OAuth parameters from the given callback URL.
* @param url A callback URL.
* @returns The OAuth parameters.
*/
parseCallback(url: string): KeycloakOAuth;

/**
* Creates a Keycloak-specific Promise wrapper.
*/
createPromise<SuccessType = void, ErrorType = void>(): KeycloakPromiseWrapper<SuccessType, ErrorType>;

/**
* Returns true if the token has the given realm role.
* @param role A realm role name.
Expand Down
74 changes: 37 additions & 37 deletions adapters/oidc/js/src/keycloak.js
Expand Up @@ -167,9 +167,9 @@ function Keycloak (config) {
kc.flow = 'standard';
}

var promise = createPromise();
var promise = kc.createPromise();

var initPromise = createPromise();
var initPromise = kc.createPromise();
initPromise.promise.then(function() {
kc.onReady && kc.onReady(kc.authenticated);
promise.setSuccess(kc.authenticated);
Expand Down Expand Up @@ -205,8 +205,8 @@ function Keycloak (config) {
return;
}

var oauth = parseCallback(event.data);
processCallback(oauth, initPromise);
var oauth = kc.parseCallback(event.data);
kc.processCallback(oauth, initPromise);

document.body.removeChild(ifrm);
window.removeEventListener("message", messageCallback);
Expand Down Expand Up @@ -243,15 +243,15 @@ function Keycloak (config) {
}

function processInit() {
var callback = parseCallback(window.location.href);
var callback = kc.parseCallback(window.location.href);

if (callback) {
window.history.replaceState(window.history.state, null, callback.newUrl);
}

if (callback && callback.valid) {
return setupCheckLoginIframe().then(function() {
processCallback(callback, initPromise);
kc.processCallback(callback, initPromise);
}).catch(function (error) {
initPromise.setError(error);
});
Expand Down Expand Up @@ -297,7 +297,7 @@ function Keycloak (config) {
}

function domReady() {
var promise = createPromise();
var promise = kc.createPromise();

var checkReadyState = function () {
if (document.readyState === 'interactive' || document.readyState === 'complete') {
Expand Down Expand Up @@ -518,7 +518,7 @@ function Keycloak (config) {
req.setRequestHeader('Accept', 'application/json');
req.setRequestHeader('Authorization', 'bearer ' + kc.token);

var promise = createPromise();
var promise = kc.createPromise();

req.onreadystatechange = function () {
if (req.readyState == 4) {
Expand All @@ -543,7 +543,7 @@ function Keycloak (config) {
req.setRequestHeader('Accept', 'application/json');
req.setRequestHeader('Authorization', 'bearer ' + kc.token);

var promise = createPromise();
var promise = kc.createPromise();

req.onreadystatechange = function () {
if (req.readyState == 4) {
Expand Down Expand Up @@ -582,7 +582,7 @@ function Keycloak (config) {
}

kc.updateToken = function(minValidity) {
var promise = createPromise();
var promise = kc.createPromise();

if (!kc.refreshToken) {
promise.setError();
Expand Down Expand Up @@ -698,7 +698,7 @@ function Keycloak (config) {
}
}

function processCallback(oauth, promise) {
kc.processCallback = function processCallback(oauth, promise) {
var code = oauth.code;
var error = oauth.error;
var prompt = oauth.prompt;
Expand Down Expand Up @@ -779,7 +779,7 @@ function Keycloak (config) {
}

function loadConfig(url) {
var promise = createPromise();
var promise = kc.createPromise();
var configUrl;

if (!config) {
Expand Down Expand Up @@ -1034,7 +1034,7 @@ function Keycloak (config) {
return uuid;
}

function parseCallback(url) {
kc.parseCallback = function parseCallback(url) {
var oauth = parseCallbackUrl(url);
if (!oauth) {
return;
Expand Down Expand Up @@ -1130,7 +1130,7 @@ function Keycloak (config) {
return result;
}

function createPromise() {
kc.createPromise = function createPromise() {
// Need to create a native Promise which also preserves the
// interface of the custom promise type previously used by the API
var p = {
Expand Down Expand Up @@ -1185,7 +1185,7 @@ function Keycloak (config) {
}

function setupCheckLoginIframe() {
var promise = createPromise();
var promise = kc.createPromise();

if (!loginIframe.enable) {
promise.setSuccess();
Expand Down Expand Up @@ -1262,7 +1262,7 @@ function Keycloak (config) {
}

function checkLoginIframe() {
var promise = createPromise();
var promise = kc.createPromise();

if (loginIframe.iframe && loginIframe.iframeOrigin ) {
var msg = kc.clientId + ' ' + (kc.sessionId ? kc.sessionId : '');
Expand All @@ -1279,7 +1279,7 @@ function Keycloak (config) {
}

function check3pCookiesSupported() {
var promise = createPromise();
var promise = kc.createPromise();

if (loginIframe.enable || kc.silentCheckSsoRedirectUri) {
var iframe = document.createElement('iframe');
Expand Down Expand Up @@ -1322,17 +1322,17 @@ function Keycloak (config) {
return {
login: function(options) {
window.location.replace(kc.createLoginUrl(options));
return createPromise().promise;
return kc.createPromise().promise;
},

logout: function(options) {
window.location.replace(kc.createLogoutUrl(options));
return createPromise().promise;
return kc.createPromise().promise;
},

register: function(options) {
window.location.replace(kc.createRegisterUrl(options));
return createPromise().promise;
return kc.createPromise().promise;
},

accountManagement : function() {
Expand All @@ -1342,7 +1342,7 @@ function Keycloak (config) {
} else {
throw "Not supported by the OIDC server";
}
return createPromise().promise;
return kc.createPromise().promise;
},

redirectUri: function(options, encodeHash) {
Expand Down Expand Up @@ -1401,7 +1401,7 @@ function Keycloak (config) {

return {
login: function(options) {
var promise = createPromise();
var promise = kc.createPromise();

var cordovaOptions = createCordovaOptions(options);
var loginUrl = kc.createLoginUrl(options);
Expand All @@ -1416,8 +1416,8 @@ function Keycloak (config) {

ref.addEventListener('loadstart', function(event) {
if (event.url.indexOf('http://localhost') == 0) {
var callback = parseCallback(event.url);
processCallback(callback, promise);
var callback = kc.parseCallback(event.url);
kc.processCallback(callback, promise);
closeBrowser();
completed = true;
}
Expand All @@ -1426,8 +1426,8 @@ function Keycloak (config) {
ref.addEventListener('loaderror', function(event) {
if (!completed) {
if (event.url.indexOf('http://localhost') == 0) {
var callback = parseCallback(event.url);
processCallback(callback, promise);
var callback = kc.parseCallback(event.url);
kc.processCallback(callback, promise);
closeBrowser();
completed = true;
} else {
Expand All @@ -1449,7 +1449,7 @@ function Keycloak (config) {
},

logout: function(options) {
var promise = createPromise();
var promise = kc.createPromise();

var logoutUrl = kc.createLogoutUrl(options);
var ref = cordovaOpenWindowWrapper(logoutUrl, '_blank', 'location=no,hidden=yes,clearcache=yes');
Expand Down Expand Up @@ -1484,15 +1484,15 @@ function Keycloak (config) {
},

register : function(options) {
var promise = createPromise();
var promise = kc.createPromise();
var registerUrl = kc.createRegisterUrl();
var cordovaOptions = createCordovaOptions(options);
var ref = cordovaOpenWindowWrapper(registerUrl, '_blank', cordovaOptions);
ref.addEventListener('loadstart', function(event) {
if (event.url.indexOf('http://localhost') == 0) {
ref.close();
var oauth = parseCallback(event.url);
processCallback(oauth, promise);
var oauth = kc.parseCallback(event.url);
kc.processCallback(oauth, promise);
}
});
return promise.promise;
Expand Down Expand Up @@ -1523,22 +1523,22 @@ function Keycloak (config) {

return {
login: function(options) {
var promise = createPromise();
var promise = kc.createPromise();
var loginUrl = kc.createLoginUrl(options);

universalLinks.subscribe('keycloak', function(event) {
universalLinks.unsubscribe('keycloak');
window.cordova.plugins.browsertab.close();
var oauth = parseCallback(event.url);
processCallback(oauth, promise);
var oauth = kc.parseCallback(event.url);
kc.processCallback(oauth, promise);
});

window.cordova.plugins.browsertab.openUrl(loginUrl);
return promise.promise;
},

logout: function(options) {
var promise = createPromise();
var promise = kc.createPromise();
var logoutUrl = kc.createLogoutUrl(options);

universalLinks.subscribe('keycloak', function(event) {
Expand All @@ -1553,13 +1553,13 @@ function Keycloak (config) {
},

register : function(options) {
var promise = createPromise();
var promise = kc.createPromise();
var registerUrl = kc.createRegisterUrl(options);
universalLinks.subscribe('keycloak' , function(event) {
universalLinks.unsubscribe('keycloak');
window.cordova.plugins.browsertab.close();
var oauth = parseCallback(event.url);
processCallback(oauth, promise);
var oauth = kc.parseCallback(event.url);
kc.processCallback(oauth, promise);
});
window.cordova.plugins.browsertab.openUrl(registerUrl);
return promise.promise;
Expand Down

0 comments on commit 2b4dc60

Please sign in to comment.