8000 Ensure consent is granted before token-exchange by cavuugroup · Pull Request #26908 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.keycloak.jose.jws.JWSInput;
import org.keycloak.jose.jws.JWSInputException;
import org.keycloak.models.ClientModel;
import org.keycloak.models.ClientScopeModel;
import org.keycloak.models.ClientSessionContext;
import org.keycloak.models.FederatedIdentityModel;
import org.keycloak.models.IdentityProviderMapperModel;
Expand Down Expand Up @@ -284,7 +285,7 @@ protected Response exchangeToIdentityProvider(UserModel targetUser, UserSessionM
}

protected Response exchangeClientToClient(UserModel targetUser, UserSessionModel targetUserSession,
AccessToken token, boolean disallowOnHolderOfTokenMismatch) {
AccessToken token, boolean disallowOnHolderOfTokenMismatch) {
String requestedTokenType = formParams.getFirst(OAuth2Constants.REQUESTED_TOKEN_TYPE);
if (requestedTokenType == null) {
requestedTokenType = OAuth2Constants.REFRESH_TOKEN_TYPE;
Expand All @@ -311,12 +312,6 @@ protected Response exchangeClientToClient(UserModel targetUser, UserSessionModel
}
}

if (targetClient.isConsentRequired()) {
event.detail(Details.REASON, "audience requires consent");
event.error(Errors.CONSENT_DENIED);
throw new CorsErrorResponseException(cors, OAuthErrorException.INVALID_CLIENT, "Client requires user consent", Response.Status.BAD_REQUEST);
}

boolean isClientTheAudience = client.equals(targetClient);

if (isClientTheAudience) {
Expand Down Expand Up @@ -365,6 +360,19 @@ protected Response exchangeClientToClient(UserModel targetUser, UserSessionModel
scope = Arrays.stream(scope.split(" ")).filter(s -> "openid".equals(s) || (targetClientScopes.contains(Profile.isFeatureEnabled(Profile.Feature.DYNAMIC_SCOPES) ? s.split(":")[0] : s))).collect(Collectors.joining(" "));
}

if (scope != null) {
// verification of existing users consents for requested scopes of target client
if (!TokenManager.verifyConsentStillAvailable(session, targetUser, targetClient, TokenManager.getRequestedClientScopes(scope, client))) {
event.error(Errors.NOT_ALLOWED);
event.detail(Details.REASON, String.format(
"Missing some consents of [%s] for audience [%s]",
TokenManager.getRequestedClientScopes(scope, client)
.map(ClientScopeModel::getName)
.collect(Collectors.joining(",")),
targetClient.getClientId()));
throw new CorsErrorResponseException(cors, OAuthErrorException.INVALID_SCOPE, "Client requires user consent", Response.Status.BAD_REQUEST);
}
}
try {
session.getContext().setClient(targetClient);
switch (requestedTokenType) {
Expand Down
0