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. * @param initOptions Initialization options.
* @returns A promise to set functions to be invoked on success or error. * @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. * Redirects to login form.

View file

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

View file

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

View file

@ -203,7 +203,7 @@ public class JavascriptTestExecutor {
configure(); configure();
} }
String arguments = argumentsBuilder.build(); String arguments = argumentsBuilder != null ? argumentsBuilder.build() : "";
String script = "var callback = arguments[arguments.length - 1];" + String script = "var callback = arguments[arguments.length - 1];" +
" window.keycloak.init(" + arguments + ").then(function (authenticated) {" + " 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); , 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 @Test
public void testCheckSso() { public void testCheckSso() {
JSObjectBuilder checkSSO = defaultArguments().checkSSOOnLoad(); JSObjectBuilder checkSSO = defaultArguments().checkSSOOnLoad();