Sitemap
Maroš Ivančo

Maroš Ivančo

Software architect, leader, designer. Fascinated with Kaizen, long lasting quality sustainability, progressive enhancements and continuous improvement.

Keyclops JS: IAM Integration Made Responsive

8 min readAug 15, 2019

--

Press enter or click to view image in full size
http://www.desktopwallpapers4.me/digital-art/green-eye-5417/

Introduction

Most applications these days deal with responsiveness in a way. One topic often overlooked is the impact of IAM integration on the application responsiveness. The article compares two client libraries used for integration with IAM server and their impact on responsiveness.

Keycloak is an open source Identity and Access Management solution. It comes with its own JS client — Keycloak JS. Unfortunately, the client is unsuitable for creating responsive applications. Mostly because it messes with the critical rendering path during the application bootstrap.

Keyclops is its replacement for web applications, designed specifically with responsiveness in mind. It implements OpenID Connect hybrid flow.

If you create mobile-first client-side web applications and care for responsiveness, you may find their comparison useful.

What is a responsive application?

Well, it is an application, which responds swiftly to the user interactions. It has following properties (among others):

  • Short time to first interaction (TTFI)
  • Short response time of user interactions

The terms “swiftly” and “short time”, are subjective. The concrete value (in ms) varies case by case. But in general, the goal of responsive application is to provide the feeling of uninterrupted user experience.

While timing aspect is the major one, there are also aspects of “ergonomics” and multi-device support hidden in the term “responsiveness”. The motivation is to speed up intent-to-action time, or at the very least, make the action possible (by changing size, position… on the small devices).

For the purpose of this article I will stick with the timing aspect.

Examples

The provided examples are based on the results from two simple tests apps — one with the Keycloak JS, the other with the Keyclops as the OIDC client. Each app detects whether the user is authenticated. If so, a page with an icon and logout link is displayed. Otherwise the user is redirected to the Keycloak authentication screens.

The following picture summarizes the deployment:

Test deployment

The test app sources and documentation of Keycloak server configuration are part of the Keyclops project.

The results are captured in Chrome DevTools, with 4 x CPU slowdown, simulated Fast 3G network, and disabled cache. The motivation is to see how well both libraries fare on mobile devices for the first-time visitors. The test focuses on this case because even though often overlooked, it defines the experience of a new user. The first impression matters!

Time to First Interaction

When user navigates to the application, in order to be responsive, the app should load swiftly to actually enable the user interactions.

This means loading as little resources as necessary (JavaScript, CSS, HTML, fonts, images), and performing only necessary service requests.

For instance, if your app requires the user to be authenticated, the first screen for the unauthenticated user will be the login screen, and the first interaction will be the actual login. Anything, that is not part of the login screen or login process consumes network and CPU resources, thus increasing TTFI. Similarly, the app should make only such service requests that support the login (for instance load configuration, list identity providers), anything else just increases TTFI.

Unauthenticated user

When Keycloak screens are used for authentication, the first interaction of an unauthenticated user is outside of your application (Keycloak login screen).

In such case, your app needs to detect that user is not authenticated and redirect him/her to Keycloak as-soon-as-possible to avoid unnecessary resource/service requests, and the application bootstrap.

The following picture describes the situation in the test app using Keycloak JS client:

Press enter or click to view image in full size
App to Keycloak redirect with Keycloak JS

First, the index.html¹ is loaded. It triggers loading of keycloak.min.js², app³ and vendors⁴ chunks. Keycloak JS detects that user is not authenticated and redirects user to Keycloak login screen⁵.

It takes approximately 1.5s to issue the redirect.

With Keyclops, the situation is slightly different:

Press enter or click to view image in full size
App to Keycloak redirect with Keyclops

The index.html¹ is loaded and triggers app² and vendors³ chunk loading. The detection, however, happens much sooner, and the redirect⁴ is issued approximately at 700ms.

That gives us 1500 — 700 = 800ms. Which is difference very well perceivable by the user.

It is worth noting, that in Chrome, the app and vendor chunks are still loaded, consuming the network bandwidth unnecessarily. In Firefox, however, the chunk loading does not start at all.

Authenticated user

After successful authentication, the user is redirected to the application. In such case (or when user reloads the app), TTFI is defined by app loading and bootstrap.

Get Maroš Ivančo’s stories in your inbox

Join Medium for free to get updates from this writer.

The following picture describes the situation in the test app using Keycloak JS:

Press enter or click to view image in full size
App loading and bootstrap with Keycloak JS

First, the user POSTs credentials to Keycloak¹. As the result of successful authentication the user is redirected to the app. index.html² is loaded, triggers keycloak.min.js³, app⁴, and vendors⁵ chunk loading. Keycloak JS detects that user is authenticated, triggers loading of SSO iframe⁶ and issues token update⁷.

As you can see, all three resources: keycloak.min.js, login-status-iframe.html, and token update mess with the critical rendering path during the application bootstrap. The keycloak.min.js does so by its excessively big size. The SSO iframe, and token update request just happen too early, so they compete with the application for network bandwidth and CPU.

The following picture describes the situation with Keyclops:

Press enter or click to view image in full size
App loading and bootstrap with Keyclops

Keyclops code is about 55% smaller (2.8kB minified, gzipped). The token update¹ request is delayed until DOMContentLoaded, and SSO iframe² loading is delayed even more — until load event.

The optimizations, however, do not bear as much fruit in the test app as one would expect. That’s because the app is very simple. The real-world app would look more like:

Press enter or click to view image in full size
Real App loading and bootstrap with Keyclops

Please note, that if the token update¹ request, and SSO iframe² loading were not delayed, they would compete for resources with font loading³ and app bootstrap⁴.

Response Time of User Interaction

When the application is ready, the user can interact. The interaction can be explicit — clicking the “action” button, or implicit — evaluating password strength as the user types in his/her password during registration. For the feeling of uninterrupted user experience, the response time of the interaction should be as low as possible.

When the interaction invokes a protected service, the application needs to make sure the request will be successfully authenticated. It does so by including valid access token in the service request.

Keycloak JS way of doing it is wrapping every protected service request with kind of decorator that checks access token validity and updates the token when it is about to expire:

This access token update involves additional request.

If the original service request happens in the period close to the access token expiration, the decorator blocks the request, issues token update, and after successful update, unblocks the original service request. For instance, when the access token expires in 2 minutes, there is a GRACE period during which all the requests are blocked and synced on the token update.

The following picture shows what happens when multiple service requests are issued while Keycloak JS is in GRACE period:

Press enter or click to view image in full size
Service requests in GRACE period with Keycloak JS

When a service request is issued, the Keycloak JS checks the access token. Because it is about to expire (GRACE), it issues the access token update¹. All other requests² are delayed until the token is updated.

Keyclops takes a different approach. Instead of wrapping every service request, it splits the work into separate (parallel) flows:

The service request (Service Request flow) is guaranteed to have valid access token. Because the token is valid, there is no need to check, or update it. And there is no overhead. Thus the service requests are issued without a delay:

Press enter or click to view image in full size
Service requests in GRACE period with Keyclops

Token Renewal flow is parallel to Service Request flow. It is started during the application bootstrap. Its sole purpose is to keep the access token valid. It does so by scheduling the token update based on its expiration time and some GRACE period. This grace period ensures, that the token is valid, and that the service calls can use the token while the new one is being issued. If left uninterrupted, the flow would keep the token updated forever. We however, want to stop the update, and logout the user, if s/he does not interact with the application for defined amount of time.

Record Interaction flow records the last time of user interaction. The application adds some event listeners for the specific events (click, input, keypress, or mousemove) during the bootstrap.

Check Interaction flow checks if the user’s inactivity reached MAX_INACTIVITY_TIME threshold. If so, it (interrupts Token Renewal and) logs the user out. Otherwise, it schedules the next check.

Keyclops’ no overhead is certainly appealing. It is important to note, however, that the real impact depends on the access token life span (how often the token needs to be updated) and the GRACE period. The wider the period and shorter the life span, the higher the probability of lag caused be token update.

Conclusion

As we could see, Keyclops offers:

  • fast detection of authentication state
  • minimizes collisions with critical resources during application bootstrap
  • no overhead for protected service requests

If you find that appealing, or if you are interested in solutions to some other challenges, like:

  • Accessing protected resources via link
  • SPA in multiple tabs
  • Single Sign-On among multiple apps
  • Single Sign-Out among multiple apps

then give Keyclops a try. It comes with the simple test app and docs that describe how to connect to the Keycloak server. It is open source (Apache License, Version 2.0).

The new ideas, suggestions, and PR are welcomed!

--

--