8000 KEYCLOAK-10761 Access-Token Encryption Support by thomasdarimont · Pull Request #7341 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content

KEYCLOAK-10761 Access-Token Encryption Support - #7341

Closed
thomasdarimont wants to merge 5 commits into
keycloak:masterfrom
thomasdarimont:issue/KEYCLOAK-10761-AccessToken-Encryption-Support
Closed

thomasdarimont wants to merge 5 commits into
keycloak:masterfrom
thomasdarimont:issue/KEYCLOAK-10761-AccessToken-Encryption-Support

Conversation

@thomasdarimont
@thomasdarimont thomasdarimont commented Aug 15, 2020 •
Copy link
Copy Markdown
Contributor
  • Leveraged existing IDToken encryption support for AccessToken encryption.
    Note: Signed & Encrypted JWTs need cty: "JWT" (the content-type) in the JWE header.
  • Enhanced TokenVerifier to support JWE decryption.
  • Added appropriate client configuration options to "Advanced OIDC Client Settings" in admin-console.
    We now can configure signature / encryption algorithms and a JWE keystore to support decryption of JWEs by keycloak.
    This can be used with the UserInfo- and TokenIntrospection-endpoint in combination with a confidential client.

See: draft-ietf-oauth-jwt-introspection-response-09
Under "3. Resource Server Management" Page 2.:

...
   To support encrypted token introspection response JWTs, the
   authorization server MUST also be provided with the respective
   resource server encryption keys and algorithms.
...

A comprehensive example for this can be found in the spring-boot-keycloak-jwe-example repository.

This is a new attempt in the vein of the outdated PR #6796

Leveraged existing IDToken encryption support for AccessToken encryption.
Note: Signed & Encrypted JWTs need cty: "JWT" (the content-type) in the JWE header.
@thomasdarimont
thomasdarimont commented Aug 15, 2020 •
Copy link
Copy Markdown
Contributor Author

The following is based on the example in the spring-boot-keycloak-jwe-example repository and demonstrates token introspection and userinfo endpoint access.
Note that you need to following the instructions from the readme in the repository to import the jwedemo realm.

Additionally you might need to create a user "tester" with password "test" and configure the JWE keystore setting in the jweclient client configuration under "Fine Grain OpenID Connect Configuration".
You need to configure the path to the Keystore from the example. The password for the Keystore and Key is geheim.

Fine Grain OpenID Connect Configuration JWE Keystore configuration

Retrieving tokens via ROPC

KC_USERNAME=tester                                                              
KC_PASSWORD=test                
KC_CLIENT_ID=jweclient                  
KC_CLIENT_SECRET=418d630c-44cb-4f11-9dcc-a0c72dfc9f85

8000
KC_ISSUER=http://localhost:8081/auth/realms/jwedemo
  
KC_RESPONSE=$( \
curl \
  -d "client_id=$KC_CLIENT_ID" \
  -d "client_secret=$KC_CLIENT_SECRET" \
  -d "username=$KC_USERNAME" \
  -d "password=$KC_PASSWORD" \
  -d "grant_type=password" \
  -d "scope=profile openid" \
  "$KC_ISSUER/protocol/openid-connect/token" \
)
echo $KC_RESPONSE | jq -C .

KC_ID_TOKEN=$(echo $KC_RESPONSE | jq -r .id_token)
KC_ACCESS_TOKEN=$(echo $KC_RESPONSE | jq -r .access_token)

Example token response

{
  "access_token": "eyJhbGciOiJSU0EtT0FFUCIsImVuYyI6IkExMjhHQ00iLCJjdHkiOiJKV1QiLCJraWQiOiJqd2VjbGllbnQtZW5jLXYxIn0.u2GtOjlUi3dVzfDVmhP7hPFRpDFpji9prlv4Kk2VZs2Ezrfn5vNoazPk7v1DaqAF8ft1FNqzpxlAHVMit6E_pJj2yZh48cc0LLVtyKuKXKcFRiA86mlRJD5Z0Hv-E44HLeXnkodKkda4q6NrLWQp3caaKODqRVTvoROF6gQjM-TIhYE70jGYeE69PE8u08Ugl0M61hQHjIeOnL_NYWJl1Zm2YydzGfpLrB-bY3tZMCyRSjyxrhLiZ8iRsK1JQG0wCipH60s_OjJXBCXXNg4Cd-gA7dd9FEU3YR3XW5hY0TrP2IK9FT3ML1CoiOozIaTjk_KYvjIVU9SdVTPhobO9uQ.PePa4OjCcyw956jk.eGVfouT-v6hJN_2MgR61oDOQ43jAn34ESnvWZD_iqfH9svLQlmVVopYQqFp0mcN0Tl04wzR8XzxikK2-CBbumpkKyHIe1TzAZUg7K5xYnNjJovSS6NUkgxwI248Q0yEp_c8LOxYo1wbZdvnyNK8yKtTTm0rJBPzwCLzws9AWQHPu8VTXPLMjcLVl_bXxMyUq4DtC8tdXdH_SoYpBwGgWQtjGIpEy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CYnipD8plrY_s_7v7reitnaeoVIQy16-S1bokYo-dNxAWj-zws6ZHqFO1DFxWz389s488T__vJs2ylnTBb3P8ik4huWN5HAQfS_0KysG8DO1m2EQXI3S3gbIfAw5daA.hzV_2GoRbHa71qiJU-8psg",
  "expires_in": 300,
  "refresh_expires_in": 1800,
  "refresh_token": "eyJhbGciOiJIUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJiOTQ2NzdmNC00Nzk1LTQ4OWItOTZjNy1hODgyMmNhMmJiYWIifQ.eyJleHAiOjE1OTc0OTkzOTUsImlhdCI6MTU5NzQ5NzU5NSwianRpIjoiYzNhY2IyMzYtNjRiMS00ZTEwLTkxNWEtN2FiZGI3Y2VlZjY0IiwiaXNzIjoiaHR0cDovL2xvY2FsaG9zdDo4MDgxL2F1dGgvcmVhbG1zL2p3ZWRlbW8iLCJhdWQiOiJodHRwOi8vbG9jYWxob3N0OjgwODEvYXV0aC9yZWFsbXMvandlZGVtbyIsInN1YiI6ImNjZTg5MWQ0LWY3MTEtNDc3NC05MWI5LTQwZTg3ZWM0NmU0YyIsInR5cCI6IlJlZnJlc2giLCJhenAiOiJqd2VjbGllbnQiLCJzZXNzaW9uX3N0YXRlIjoiNjRmNzljYzgtNWUzMC00YWQyLTkyMGYtZDQxOTM0YjNlMzExIiwic2NvcGUiOiJvcGVuaWQgcHJvZmlsZSBlbWFpbCJ9.H0HUk6X9Ma7kFHcWq0Ey1cVyLJ0Kei6bXlzxo2woO38",
  "token_type": "bearer",
  "id_token": "eyJhbGciOiJSU0EtT0FFUCIsImVuYyI6IkExMjhHQ00iLCJjdHkiOiJKV1QiLCJraWQiOiJqd2VjbGllbnQtZW5jLXYxIn0.sa-5OJS1HjCisw1v-ACArlmnsNGzAJ2rZtePvP63ydTrRUOjHv4A-iRO556HpJzJ8NgchytLXu7dhKjhSEqme40zh84gUvWKOxGDU8ERB5VjMRvRvvPReLdm0JCu9IJ5jhY72Oy-G64oZjhNJdgNzySwokZCNDzC6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58629j8ff8_CJ3ZC3-q3rvad3ZjxRlBZaH8q22aF4bB0AKHY6VY8pCx6-qi7OOzeq9Wi_k9Gb8rmB1gOk_xWUeUUyXYm8GHKf4NzEdeGH1Plend23IQS5d82A_I2L1QDyiHkOW-EvO7LzZNOwtvotDkiNLcOTSlemkNBeExYgfGsCK6NBLZdBwun8uyWWFUsj1SbH2v3bV0E2H0MLKG7448KiXTX1cEgT6ZIsWGA5LqGnhrnlMfETPDNiohNJY5DEfq8cQxYLywP-29QgFdw76LmDUm4mvkVVKp9TZl1orEhNflmcvupkymO0uIVu1KCsV-ACGsMvv1MRi22Ce3yztVgJqFIKDYG7tIXNn_FGVcbkC_4u7Ky1jKz3kzRy7x-g0i9SvagU9yrXtHWyDJBRqznrZpYkMQSI63GlGNh3-wWo2_OjJjiFIpJl8hTygguRLPY--qziICTk5fPqTe5HPW842r0rN00BI4z96VbwxdDC0MuncO_nMJchPS86BrNXMsZpryetiasGwu2uaHlOOMhcvh0qBMbaSh3Q0VtDAHKl2Fov5F9S.l6UgVywquJfrY1mKdVlK3g",
  "not-before-policy": 0,
  "session_state": "64f79cc8-5e30-4ad2-920f-d41934b3e311",
  "scope": "openid profile email"
}

Using the token introspection endpoint

 curl -v \                                                                       
  -d "client_id=$KC_CLIENT_ID" \
  -d "client_secret=$KC_CLIENT_SECRET" \
  -d "token=$KC_ACCESS_TOKEN" \                      
  -d "token_type_hint=access_token" \              
  "$KC_ISSUER/protocol/openid-connect/token/introspect" | jq -C .

Example token introspection response:

{
  "exp": 1597497895,
  "iat": 1597497595,
  "jti": "8ec7bf0c-96e4-4e71-91c3-c0950488f6e2",
  "iss": "http://localhost:8081/auth/realms/jwedemo",
  "sub": "cce891d4-f711-4774-91b9-40e87ec46e4c",
  "typ": "Bearer",
  "azp": "jweclient",
  "session_state": "64f79cc8-5e30-4ad2-920f-d41934b3e311",
  "name": "Theo Tester",
  "given_name": "Theo",
  "family_name": "Tester",
  "preferred_username": "tester",
  "email": "tom+tester@localhost",
  "email_verified": false,
  "acr": "1",
  "scope": "openid profile email",
  "client_id": "jweclient",
  "username": "tester",
  "active": true
}

If on uses an invalid access_token or if the JWE Keystore configuration is missing or invalid. The token introspection will not be performed.

Example response with invalid JWE keystore configuration

{
  "active": false
}

Using the userinfo endpoint without any client authentication

curl -v \
     -H "Authorization: Bearer $KC_ACCESS_TOKEN" \
     $KC_ISSUER/protocol/openid-connect/userinfo

Response:

{"error":"invalid_token","error_description":"Token verification failed"}

Using the userinfo endpoint with client authentication via Basic Auth

curl -v \
     -u "$KC_CLIENT_ID:$KC_CLIENT_SECRET" \
     -d "access_token=$KC_ACCESS_TOKEN" \
     $KC_ISSUER/protocol/openid-connect/userinfo

Response:

{"sub":"cce891d4-f711-4774-91b9-40e87ec46e4c","email_verified":false,"name":"Theo Tester","preferred_username":"tester","given_name":"Theo","family_name":"Tester","email":"tom+tester@localhost"}

Using the userinfo endpoint with client authentication via form parameters

curl -v \
     -d "client_id=$KC_CLIENT_ID" \
     -d "client_secret=$KC_CLIENT_SECRET" \
     -d "access_token=$KC_ACCESS_TOKEN" \
     $KC_ISSUER/protocol/openid-connect/userinfo

Response:

{"sub":"cce891d4-f711-4774-91b9-40e87ec46e4c","email_verified":false,"name":"Theo Tester","preferred_username":"tester","given_name":"Theo","family_name":"Tester","email":"tom+tester@localhost"}

@thomasdarimont
thomasdarimont force-pushed the issue/KEYCLOAK-10761-AccessToken-Encryption-Support branch from 0cc60d2 to e6fa365 Compare August 15, 2020 11:41

SignatureVerifierContext verifierContext = session.getProvider(SignatureProvider.class, verifier.getHeader().getAlgorithm().name()).verifier(verifier.getHeader().getKeyId());
verifier.verifierContext(verifierContext);
try {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This feels a bit hacky, is there a better way to perform dynamic client authentication?

}

try {
KeyStore keystore = KeystoreUtil.loadKeyStore(oidcConfig.getJweTokenKeystoreLocation(), oidcConfig.getJweTokenKeystorePassword());
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This should be cached somehow. It think using something like client scoped KeyProvider would make sense here.


import java.security.PrivateKey;

public interface DecryptionKEKAccessor {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

These *AccessorInterfaces provide an indirection for resolving providers based on information of the parsed JWE or the nested JWE. Since this needs to live in keycloak-core we cannot use the built-in function interfaces from Java 8 since the module has to be compatible with Java 1.7 (for now).


public class TokenVerifierUtils {

public static <T extends JsonWebToken> TokenVerifier<T> createTokenVerifier(String token, Class<T> clazz, KeycloakSession session) throws VerificationException {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Introduced this factory method to be able to transparently return a more specific JWETokenVerifier if necessary.

@abstractj abstractj added kind/feature Categorizes a PR related to a new feature area/server area/core Core server — startup, configuration, providers/SPIs, cross-cutting concerns labels Nov 18, 2020
@stianst
stianst requested a review from mposolda September 30, 2021 11:28
@stianst
stianst commented Sep 30, 2021
Copy link
Copy Markdown
Contributor

@mposolda @thomasdarimont is this something we want to get to completion?

@thomasdarimont
Copy link
Copy Markdown
Contributor Author

This needs to be reworked again - closing this PR for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/core Core server — startup, configuration, providers/SPIs, cross-cutting concerns kind/feature Categorizes a PR related to a new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

0