Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@mposolda
Copy link
Contributor

@mposolda mposolda commented Feb 25, 2025

closes #37118

PR is adding some context information to be available in the access-token without a need to introduce any additional custom claims. So far, it provides info about:

  • session type (online, offline, transient), which was used to create the token
  • token type (regular or lightweight token)
  • grant type

These informations can be useful when token is later sent to some Keycloak endpoints (for example introspection, userInfo, admin/account REST API) to optimize something. For example:

  • session-type will allow to lookup only correct session type based on the type of the session, which token was created from. As for example today, we need to lookup "online" session and if it is not found, then fallback to "offline" session. So possible unnecessary lookup. Related follow-up issue for optimize that: Optimize session lookup #37662

  • Grant type knowledge can be useful for example for the token-exchange use-case: Can we avoid creating client-session for the case when access-token requested? #37117

  • Lightweight access token: We will be able to recognize more reliably if access-token is lightweight or not and do something based on that (EG. lookup roles from model instead of lookup roles from the token)

In this PR, I've made token ID to look something like onrtac:1f65761e-1c69-40a2-82da-2d8aa5e39a8a .

  • The first 2 characters is shortcut for "session type" - on is shortcut for "online" session
  • The next 2 characters is shortcut for "token type" - rt is shortcut for regular token (not lightweight token)
  • The next 2 characters is shortcut for grant type - ac is shortcut for "authorization code" grant

Shortcuts are used, so the token ID contains important info, but at the same time, it is limited in size and does not introduce much new characters to the token.

The encoding into ID is used only for access-tokens. Not used for refresh tokens or ID tokens or any others.

Introduced dedicated SPI in the end. Wanted to "cache" some objects (EG. shortcuts for grant types etc) and it seems clearer to me to cache them at the level of factory, rather than in the static fields, which is not very pretty. Moreover, provider adds ability for someone to introduce his own provider (and eventually encode some more info or encode stuff in the different way etc).

Note

I've used 2 commits in this PR. Originally, in the 1st commit, I've made the encoding a bit longer with token ID to look something like st.on_tt.rt_gt.ac:1f65761e-1c69-40a2-82da-2d8aa5e39a8a .

  • The st is shortcut for "session type" and on is shortcut for "online" session
  • The tt is shortcut for "token type" and rt is shortcut for regular token (not lightweight token)
  • The gt is shortcut for grant type and ac is shortcut for "authorization code" grant

But assuming that every "type" has always 2 characters long shortcut, I've rather updated it in the 2nd commit to have only 7 new characters instead of 18 characters. I planned to squash before merge (unless you think that we should rather use longer format for some reason, in which case the 2nd commit can be removed from the PR).

@mposolda mposolda self-assigned this Feb 25, 2025
@mposolda mposolda force-pushed the 37118-access-token-ids-rebase branch from 9f8c06b to c088d76 Compare February 26, 2025 11:11
@mposolda mposolda force-pushed the 37118-access-token-ids-rebase branch from c088d76 to daffb05 Compare February 26, 2025 12:47
@mposolda mposolda marked this pull request as ready for review February 26, 2025 12:56
@mposolda mposolda requested review from a team as code owners February 26, 2025 12:56
Copy link
Contributor

@rmartinc rmartinc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mposolda! LGTM!

@mposolda mposolda merged commit c873872 into keycloak:main Feb 26, 2025
78 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Start encoding of access-tokens IDs

2 participants