2021-03-20 04:59:18 +01:00
|
|
|
export declare namespace keycloak_js {
|
|
|
|
export type KeycloakPromiseCallback<T> = (result: T) => void;
|
|
|
|
export class KeycloakPromise<TSuccess, TError> extends Promise<TSuccess> {
|
2021-10-11 21:35:40 +02:00
|
|
|
success(
|
|
|
|
callback: KeycloakPromiseCallback<TSuccess>,
|
|
|
|
): KeycloakPromise<TSuccess, TError>;
|
|
|
|
error(
|
|
|
|
callback: KeycloakPromiseCallback<TError>,
|
|
|
|
): KeycloakPromise<TSuccess, TError>;
|
2021-03-20 04:59:18 +01:00
|
|
|
}
|
|
|
|
export interface KeycloakAdapter {
|
|
|
|
login(options?: KeycloakLoginOptions): KeycloakPromise<void, void>;
|
|
|
|
logout(options?: KeycloakLogoutOptions): KeycloakPromise<void, void>;
|
|
|
|
register(options?: KeycloakLoginOptions): KeycloakPromise<void, void>;
|
|
|
|
accountManagement(): KeycloakPromise<void, void>;
|
2021-10-11 21:35:40 +02:00
|
|
|
redirectUri(
|
|
|
|
options: { redirectUri: string },
|
|
|
|
encodeHash: boolean,
|
|
|
|
): string;
|
2021-03-20 04:59:18 +01:00
|
|
|
}
|
|
|
|
export interface KeycloakLogoutOptions {
|
|
|
|
redirectUri?: string;
|
|
|
|
}
|
|
|
|
export interface KeycloakLoginOptions {
|
|
|
|
scope?: string;
|
|
|
|
redirectUri?: string;
|
2021-10-11 21:35:40 +02:00
|
|
|
prompt?: "none" | "login";
|
2021-03-20 04:59:18 +01:00
|
|
|
action?: string;
|
|
|
|
maxAge?: number;
|
|
|
|
loginHint?: string;
|
|
|
|
idpHint?: string;
|
|
|
|
locale?: string;
|
|
|
|
cordovaOptions?: { [optionName: string]: string };
|
|
|
|
}
|
|
|
|
|
|
|
|
export type KeycloakInstance = Record<
|
2021-10-11 21:35:40 +02:00
|
|
|
"createLoginUrl" | "createLogoutUrl" | "createRegisterUrl",
|
2021-03-20 04:59:18 +01:00
|
|
|
(options: KeycloakLoginOptions | undefined) => string
|
|
|
|
> & {
|
|
|
|
createAccountUrl(): string;
|
|
|
|
redirectUri?: string;
|
2021-10-11 21:35:40 +02:00
|
|
|
};
|
2021-03-20 04:59:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-10-11 21:35:40 +02:00
|
|
|
* NOTE: This is just a slightly modified version of the default adapter in keycloak-js
|
|
|
|
* The goal here is just to be able to inject search param in url before keycloak redirect.
|
|
|
|
* Our use case for it is to pass over the login screen the states of useGlobalState
|
|
|
|
* namely isDarkModeEnabled, lgn...
|
|
|
|
*/
|
|
|
|
export function createKeycloakAdapter(params: {
|
|
|
|
keycloakInstance: keycloak_js.KeycloakInstance;
|
|
|
|
transformUrlBeforeRedirect(url: string): string;
|
|
|
|
}): keycloak_js.KeycloakAdapter {
|
2021-03-20 04:59:18 +01:00
|
|
|
const { keycloakInstance, transformUrlBeforeRedirect } = params;
|
|
|
|
|
2021-10-11 21:35:40 +02:00
|
|
|
const neverResolvingPromise: keycloak_js.KeycloakPromise<void, void> =
|
|
|
|
Object.defineProperties(new Promise(() => {}), {
|
|
|
|
"success": { "value": () => {} },
|
|
|
|
"error": { "value": () => {} },
|
|
|
|
}) as any;
|
2021-03-20 04:59:18 +01:00
|
|
|
|
|
|
|
return {
|
|
|
|
"login": options => {
|
2021-10-11 21:35:40 +02:00
|
|
|
window.location.href = transformUrlBeforeRedirect(
|
|
|
|
keycloakInstance.createLoginUrl(options),
|
|
|
|
);
|
2021-03-20 04:59:18 +01:00
|
|
|
return neverResolvingPromise;
|
|
|
|
},
|
|
|
|
"logout": options => {
|
|
|
|
window.location.replace(
|
|
|
|
transformUrlBeforeRedirect(
|
2021-10-11 21:35:40 +02:00
|
|
|
keycloakInstance.createLogoutUrl(options),
|
|
|
|
),
|
2021-03-20 04:59:18 +01:00
|
|
|
);
|
|
|
|
return neverResolvingPromise;
|
|
|
|
},
|
|
|
|
"register": options => {
|
2021-10-11 21:35:40 +02:00
|
|
|
window.location.href = transformUrlBeforeRedirect(
|
|
|
|
keycloakInstance.createRegisterUrl(options),
|
|
|
|
);
|
2021-03-29 06:27:12 +02:00
|
|
|
|
2021-03-20 04:59:18 +01:00
|
|
|
return neverResolvingPromise;
|
|
|
|
},
|
|
|
|
"accountManagement": () => {
|
2021-10-11 21:35:40 +02:00
|
|
|
var accountUrl = transformUrlBeforeRedirect(
|
|
|
|
keycloakInstance.createAccountUrl(),
|
|
|
|
);
|
|
|
|
if (typeof accountUrl !== "undefined") {
|
2021-03-20 04:59:18 +01:00
|
|
|
window.location.href = accountUrl;
|
|
|
|
} else {
|
|
|
|
throw new Error("Not supported by the OIDC server");
|
|
|
|
}
|
|
|
|
return neverResolvingPromise;
|
|
|
|
},
|
|
|
|
"redirectUri": options => {
|
|
|
|
if (options && options.redirectUri) {
|
|
|
|
return options.redirectUri;
|
|
|
|
} else if (keycloakInstance.redirectUri) {
|
|
|
|
return keycloakInstance.redirectUri;
|
|
|
|
} else {
|
|
|
|
return window.location.href;
|
|
|
|
}
|
2021-10-11 21:35:40 +02:00
|
|
|
},
|
2021-03-20 04:59:18 +01:00
|
|
|
};
|
2021-10-11 21:35:40 +02:00
|
|
|
}
|