Allow Keycloak JS to be initialized without passing options (#33950)

Closes #8935

Signed-off-by: Jon Koops <jonkoops@gmail.com>
This commit is contained in:
Jon Koops 2024-10-15 17:46:15 +02:00 committed by GitHub
parent b76f4f9c1b
commit 228c21a7a0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 129 additions and 123 deletions

View file

@ -542,7 +542,7 @@ declare class Keycloak {
* @param initOptions Initialization options.
* @returns A promise to set functions to be invoked on success or error.
*/
init(initOptions: KeycloakInitOptions): Promise<boolean>;
init(initOptions?: KeycloakInitOptions): Promise<boolean>;
/**
* Redirects to login form.

View file

@ -60,7 +60,7 @@ function Keycloak (config) {
);
}
kc.init = function (initOptions) {
kc.init = function (initOptions = {}) {
if (kc.didInitialize) {
throw new Error("A 'Keycloak' instance can only be initialized once.");
}
@ -72,9 +72,9 @@ function Keycloak (config) {
callbackStorage = createCallbackStorage();
var adapters = ['default', 'cordova', 'cordova-native'];
if (initOptions && adapters.indexOf(initOptions.adapter) > -1) {
if (adapters.indexOf(initOptions.adapter) > -1) {
adapter = loadAdapter(initOptions.adapter);
} else if (initOptions && typeof initOptions.adapter === "object") {
} else if (typeof initOptions.adapter === "object") {
adapter = initOptions.adapter;
} else {
if (window.Cordova || window.cordova) {
@ -84,7 +84,6 @@ function Keycloak (config) {
}
}
if (initOptions) {
if (typeof initOptions.useNonce !== 'undefined') {
useNonce = initOptions.useNonce;
}
@ -179,7 +178,6 @@ function Keycloak (config) {
} else {
kc.messageReceiveTimeout = 10000;
}
}
if (!kc.responseMode) {
kc.responseMode = 'fragment';
@ -207,7 +205,7 @@ function Keycloak (config) {
options.prompt = 'none';
}
if (initOptions && initOptions.locale) {
if (initOptions.locale) {
options.locale = initOptions.locale;
}
kc.login(options).then(function () {
@ -281,7 +279,8 @@ function Keycloak (config) {
}).catch(function (error) {
initPromise.setError(error);
});
} else if (initOptions) {
}
if (initOptions.token && initOptions.refreshToken) {
setToken(initOptions.token, initOptions.refreshToken, initOptions.idToken);
@ -317,9 +316,6 @@ function Keycloak (config) {
} else {
initPromise.setSuccess();
}
} else {
initPromise.setSuccess();
}
}
configPromise.then(function () {

View file

@ -51,7 +51,7 @@
globalThis.output = output;
globalThis.event = event;
keycloak.init({}).then((authenticated) => {
keycloak.init().then((authenticated) => {
output('Init Success (' + (authenticated ? 'Authenticated' : 'Not Authenticated') + ')');
}).catch(function() {
output('Init Error');

View file

@ -203,7 +203,7 @@ public class JavascriptTestExecutor {
configure();
}
String arguments = argumentsBuilder.build();
String arguments = argumentsBuilder != null ? argumentsBuilder.build() : "";
String script = "var callback = arguments[arguments.length - 1];" +
" window.keycloak.init(" + arguments + ").then(function (authenticated) {" +

View file

@ -238,6 +238,16 @@ public class JavascriptAdapterTest extends AbstractJavascriptTest {
, SuiteContext.BROWSER_STRICT_COOKIES ? this::assertInitNotAuth : this::assertInitAuth);
}
@Test
public void testInitNoOptions() {
testExecutor.init(null, this::assertInitNotAuth)
.login(this::assertOnLoginPage)
.loginForm(testUser, this::assertOnTestAppUrl)
.init(null, this::assertInitAuth)
.logout(this::assertOnTestAppUrl)
.init(null, this::assertInitNotAuth);
}
@Test
public void testCheckSso() {
JSObjectBuilder checkSSO = defaultArguments().checkSSOOnLoad();