Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Keycloak - Identity and Access Management for Modern Applications - Second Edition

You're reading from  Keycloak - Identity and Access Management for Modern Applications - Second Edition

Product type Book
Published in Jul 2023
Publisher Packt
ISBN-13 9781804616444
Pages 350 pages
Edition 2nd Edition
Languages
Authors (2):
Stian Thorgersen Stian Thorgersen
Profile icon Stian Thorgersen
Pedro Igor Silva Pedro Igor Silva
Profile icon Pedro Igor Silva
View More author details

Table of Contents (18) Chapters

Preface 1. Getting Started with Keycloak 2. Securing Your First Application 3. Brief Introduction to Standards 4. Authenticating Users with OpenID Connect 5. Authorizing Access with OAuth 2.0 6. Securing Different Application Types 7. Integrating Applications with Keycloak 8. Authorization Strategies 9. Configuring Keycloak for Production 10. Managing Users 11. Authenticating Users 12. Managing Tokens and Sessions 13. Extending Keycloak 14. Securing Keycloak and Applications 15. Assessments 16. Other Books You May Enjoy
17. Index

Managing Tokens and Sessions

In addition to acting as a centralized authentication and authorization service, Keycloak is, at its core, a session and token management system.

As part of the authentication process, Keycloak may create server-side sessions and correlate them with tokens. By relying on these sessions, Keycloak is able to keep the state of the authentication context where sessions originated, track users’ and clients’ activity, check the validity of tokens, and decide when users and clients should re-authenticate.

In this chapter, we are going to look at how Keycloak allows you to manage tokens and their underlying sessions, as well as understanding the different aspects that you should be aware of when doing so. For that, we are going to cover the following topics:

  • Managing sessions
  • Managing tokens

Technical requirements

During this chapter, you are going to use the Keycloak administration console to follow some of the examples provided herein; therefore, make sure you have Keycloak up and running as per what you learned from Chapter 1, Getting Started with Keycloak.

Managing sessions

Session management has a direct impact on some key aspects such as user experience, security, high availability, and performance.

From a user experience perspective, Keycloak relies on sessions to determine whether users and clients are authenticated, for how long they should be authenticated, and when it is time to re-authenticate them. This characteristic of sessions is basically what gives users the single sign-on (SSO) experience when authenticating to different clients within the same realm, and what makes a unified authentication experience possible.

From a security perspective, sessions provide a security layer for tracking and controlling user activity and making sure that tokens issued to clients are still valid passports to act on behalf of users. They are also important for limiting and controlling the amount of time for which users can stay connected to a realm and its clients, helping to reduce the attack surface when sessions or tokens are leaked...

Managing tokens

As you learned from the previous section, tokens are usually bound to sessions. Therefore, the token validity – not necessarily their lifetimes – depends on sessions.

Tokens have their own lifetime and how long they are considered valid depends on how they are validated. By leveraging JSON Web Token (JWT) as a format for tokens, Keycloak enables applications to validate and inspect tokens locally without any additional roundtrip to the server. However, this capability has a consequence where tokens, although within their lifetime, might not be valid anymore if their sessions have expired.

Without taking this into account, you might end up in a situation where tokens are no longer valid (the sessions they are bound to have expired) but are still accepted by applications because they are within their lifetime, therefore increasing the attack surface if tokens are leaked. As you are going to learn in this section, you should always consider a clear...

Summary

In this chapter, you learned about some key aspects of token and session management. By leveraging what you learned from this chapter, you should be able to define clear policies for session expiration and token revocation considering their impacts on security, user experience, and the performance of applications and Keycloak.

In the next chapter, you are going to look at one of the main aspects of Keycloak – extensibility – and how to extend it to adapt and fulfill unmet needs.

Questions

  1. Does Keycloak store sessions in the database?
  2. What is the difference between user and client sessions?
  3. How do you proactively revoke tokens and expire sessions?
  4. How can applications validate tokens?
  5. How does the access token lifetime impact clients?

Further reading

Refer to the following links for more information on the topics covered in this chapter:

Join our community on Discord

Join our community’s Discord space for discussions with the authors and other readers:

https://packt.link/SecNet

lock icon The rest of the chapter is locked
You have been reading a chapter from
Keycloak - Identity and Access Management for Modern Applications - Second Edition
Published in: Jul 2023 Publisher: Packt ISBN-13: 9781804616444
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}