Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored permission mapping in the TokenDecoder #58

Merged
merged 3 commits into from
Oct 14, 2024

Conversation

dusktreader
Copy link
Member

The previous methodology used JMESPaths supplied to the TokenDecoder to map parts of the decoded token to top-level claims in the final TokenPayload. This was added because Keycloak tokens do not put the permission roles in the tokens as a top-level claim. Instead, they are nested within the "resource_access" claim. The previous payload_claim_mapping worked to get the permissions set correctly for the TokenPayload, but ONLY if you knew ahead of time what the name of the client would be.

For example, this trimmed down keycloak token has its roles found at resource_access.my-client.roles:

{
  "exp": 1728627701,
  "iat": 1728626801,
  "jti": "24fdb7ef-d773-4e6b-982a-b8126dd58af7",
  "sub": "dfa64115-40b5-46ab-924c-c376e73f631d",
  "azp": "my-client",
  "resource_access": {
    "my-client": {
      "roles": [
        "read:stuff"
      ]
    },
  },
}

Not that the key for the roles within the resource_access claim matches the client claim (azp). Thus, we can provide an extractor for Keycloak tokens that does not need to know ahead of time what the client ID is.

Additionally, this allows us to remove the dependency on JMESPath.

The previous methodology used JMESPaths supplied to the TokenDecoder
to map parts of the decoded token to top-level claims in the final
TokenPayload. This was added because Keycloak tokens do not put the
permission roles in the tokens as a top-level claim. Instead, they are
nested within the "resource_access" claim. The previous
`payload_claim_mapping` worked to get the permissions set correctly for
the TokenPayload, but ONLY if you knew ahead of time what the name of
the client would be.

For example, this trimmed down keycloak token has its roles found at
`resource_access.my-client.roles`:

```
{
  "exp": 1728627701,
  "iat": 1728626801,
  "jti": "24fdb7ef-d773-4e6b-982a-b8126dd58af7",
  "sub": "dfa64115-40b5-46ab-924c-c376e73f631d",
  "azp": "my-client",
  "resource_access": {
    "my-client": {
      "roles": [
        "read:stuff"
      ]
    },
  },
}
```

Not that the key for the roles within the `resource_access` claim
matches the client claim (azp). Thus, we can provide an extractor for
Keycloak tokens that does not need to know ahead of time what the client
ID is.

Additionally, this allows us to remove the dependency on JMESPath.
@dusktreader dusktreader merged commit 0e5dc9e into main Oct 14, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants