resource request parameter and audience in access token#13622
resource request parameter and audience in access token#13622cgeorgilakis wants to merge 1 commit intokeycloak:mainfrom
Conversation
sguilhen
left a comment
There was a problem hiding this comment.
The problem is that the changes in the MapExportImportManager are not checking if the representation value is null before setting them, and defaultAudValueForAccessToken with null values are stored in the DB.
Later on, when the realm entity is converted into representation, the MapRealmAdapter.getAttributes() method collects the stored attributes into a Map and hits a NPE because the value mapper function is resulting in null.
Adding a null check in the MapExportImportManager should be enough to prevent this from happening.
|
I've also opened #13658 to revisit the |
9530662 to
1dde269
Compare
|
@sguilhen Thanks for your help. I have corrected it. Moreover, I create related keycloak-admin-ui PR for changes in new Admin Console. |
|
Glad it worked, @cgeorgilakis ! As for the thorough review I'll leave it to @mposolda to do it or assign someone to take a look. |
mposolda
left a comment
There was a problem hiding this comment.
@cgeorgilakis Sorry for the late review.
Instead of adding new 1st level switch at the realm level and changing the core code, I suggest to introduce new "protocol mapper" implementation. For example Resource To Audience Mapper (or better name if you have an idea).
The resource parameter can be added as a client note to AuthenticationSessionModel (in authorization grant) and/or to AuthenticatedClientSessionModel (in some other grants). For example see:
AuthorizationEndpoint.updateAuthenticationSession()for how it is done in Authorization endpoint.- Constants
AuthzEndpointRequestParser.KNOWN_REQ_PARAMSandBackchannelAuthenticationEndpointRequestParser.KNOWN_REQ_PARAMSwhereresourceparameter probably should be added - https://github.com/keycloak/keycloak/blob/20.0.1/services/src/main/java/org/keycloak/protocol/oidc/endpoints/TokenEndpoint.java#L613-L614 how it is done for some other grants
Protocol mapper can then consume it from client session note and add to the aud . The default audience can be added as configuration option of the protocol mapper, but I suggest to add just if you need it for your own use-case.
The validation can be also done by protocol mapper itself and moved directly to protocol mapper.
For the tests, you can add some client scope and add your protocol mapper as protocol mapper to it (and possibly remove other client scopes, which are already adding audience by default).
This approach won't require too much changes in the core codebase (besides adding resource to the session note) and will allow to more flexibility as the resource parameter will be possible to consume by other places (For example someone may want to add his own protocol mapper, which will do the same like yours, but with some additional validations on top of it).
Also it won't change default behaviour - as I think that it is not correct that by default, the content of resource is added to the aud (it is possibly fine just for some deployments and those can add protocol mapper to their client scopes on demand)
Is it possible to update PR for this approach?
| // This cannot be moved higher because it acts on differently based on environment (e.g. sometimes it checks | ||
| // scheme, sometimes it doesn't). | ||
| if (requireValidUrl) { | ||
| URL ignored = uri.toURL(); // throws an exception |
There was a problem hiding this comment.
Minor: For validation, you can just call uri.toURL() without creating any variable?
Our goal is Keycloak be compliant to OAuth specifications together with the need not to need any migration. From JSON Web Token (JWT) Profile for OAuth 2.0 Access Tokens :
As you can see aud is required. A default value is needed if not requested. We make it default empty if someone does not want it and for keep Keycloak behaviour same as old versions. From Resource Indicators for OAuth 2.0 :
So validation for resource parameter must be, Use "resource" value for the audience should be done (you must have good reason for not doing it). We use 'exact "resource" value as the audience'. We could discuss enhancement. I believe that proposed new protocol mapper make things more complex, need lots of code changes and need a default 'Include In Token Scope' value equal to false Client Scope ( user must not see an extra scope for this). If someone does not want 'resource' as desribed in the Resource Indicators for OAuth 2.0, he must not use this request parameter. Keycloak now omit 'resource' request parameter |
Keycloak already has some flexibility how is But you're right that
This specification does not imply that Keycloak should take I don't know how exactly this check might be done. But as step 1, we can possibly stick to the behaviour you added (just add the resources to the audience without any further checks) as long as it is done by the protocol mapper. It definitely should not be the default behaviour as adding random audiences, which user added himself in the In general, we are trying to rely on the protocol mappers and client policies in Keycloak to allow some flexibility. So we're rather moving away from hardcoding the behaviour for the token content in the |
|
@cgeorgilakis No further response, so I am closing this PR. I don't think that this issue should be addressed in a way as did in this PR. Feel free to re-open, but please consider to use the approach based on protocol mapper in that case (or suggest some other approach, which will allow to address this with some good flexibility, and won't change the default Keycloak behaviour regarding handling of audience) |
Closes #13614
Tests for (postgres) undertow-map-jpa are failed. I can not understand why. Could you help me? What am I missing for these tests?