[keycloak-user] Keycloak.js cannot catch the case when keycloak server is down

Johnson Liang johnson.liang at appier.com
Mon Apr 23 23:17:08 EDT 2018


Hello,

I am trying to implement an error handling mechanism for my public access
type JS client, however the current implementation of keycloak.js does not
provide such feature.

To be specific, I want to achieve the following steps:
1. Initializing the JS client using keycloak.init({onLoad:
'login-required'})
2a. If the initialization succeeds, store keycloak.token to
window.localStorage.
2b. If initialization fails due to network issues (i.e. the keycloak server
is down), use the keycloak token stored in the localStorage instead.

In this way, users that has frequently logged in to our system can still be
authenticated if the keycloak server is down, which buys us time recovering
the authentication service.

However, I cannot implement the measure above because the current
keycloak.js does not reject keycloak.init()'s promise when there is network
error.

Specifically, when keycloak server is down, invoking postMessage() in
checkLoginIframe() will throw exception [1]. However the exception is not
handled by checkLoginIframe(), so processInit() can do nothing about the
initPromise [2].

As an alternative, I can implement the following health check instead:
1. Send an ajax request to
`<keycloak-server>/realms/<realm>/.well-known/openid-configuration` to test
if the keycloak is alive.
2a. If keycloak is alive, do keycloak.init()
2b. If not, use the token stored in localStorage

This alternative costs one extra RTT between the keycloak server and the JS
client. I'd like to know:

1. Is it possible for keycloak.js to catch network errors so that we can
properly handle the network error?
2. If the answer to 1 is no, is there any alternative better the one above?

Thanks,
Johnson

[1] Relevant code:
https://github.com/keycloak/keycloak-js-bower/blob/master/dist/keycloak.js#L1117
[2] Relevant code:
https://github.com/keycloak/keycloak-js-bower/blob/master/dist/keycloak.js#L183


More information about the keycloak-user mailing list