Skip to content
Open
Show file tree
Hide file tree
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 @@ -22,6 +22,7 @@
import org.springframework.security.oauth2.core.AuthorizationGrantType;
import org.springframework.security.oauth2.core.OAuth2Token;
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
import org.springframework.security.oauth2.core.oidc.OidcIdToken;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
Expand Down Expand Up @@ -49,6 +50,8 @@ public class TokenExchangeGrantRequest extends AbstractOAuth2AuthorizationGrantR

private static final String ACCESS_TOKEN_TYPE_VALUE = "urn:ietf:params:oauth:token-type:access_token";

private static final String ID_TOKEN_TYPE_VALUE = "urn:ietf:params:oauth:token-type:id_token";

private static final String JWT_TOKEN_TYPE_VALUE = "urn:ietf:params:oauth:token-type:jwt";

private final OAuth2Token subjectToken;
Expand Down Expand Up @@ -113,6 +116,9 @@ static MultiValueMap<String, String> defaultParameters(TokenExchangeGrantRequest
}

private static String tokenType(OAuth2Token token) {
if (token instanceof OidcIdToken) {
return ID_TOKEN_TYPE_VALUE;
}
return (token instanceof Jwt) ? JWT_TOKEN_TYPE_VALUE : ACCESS_TOKEN_TYPE_VALUE;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationResponse;
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
import org.springframework.security.oauth2.core.endpoint.PkceParameterNames;
import org.springframework.security.oauth2.core.oidc.OidcIdToken;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.security.oauth2.jwt.TestJwts;
import org.springframework.util.MultiValueMap;
Expand All @@ -51,6 +52,8 @@ public class DefaultOAuth2TokenRequestParametersConverterTests {

private static final String ACCESS_TOKEN_TYPE_VALUE = "urn:ietf:params:oauth:token-type:access_token";

private static final String ID_TOKEN_TYPE_VALUE = "urn:ietf:params:oauth:token-type:id_token";

private static final String JWT_TOKEN_TYPE_VALUE = "urn:ietf:params:oauth:token-type:jwt";

private ClientRegistration.Builder clientRegistration;
Expand Down Expand Up @@ -201,4 +204,38 @@ public void convertWhenGrantRequestIsTokenExchangeThenParametersProvided() {
assertThat(parameters.get(OAuth2ParameterNames.ACTOR_TOKEN_TYPE)).containsExactly(JWT_TOKEN_TYPE_VALUE);
}

@Test
public void convertWhenGrantRequestIsTokenExchangeAndSubjectTokenIsOidcIdTokenThenSubjectTokenTypeIsIdToken() {
ClientRegistration clientRegistration = this.clientRegistration
.authorizationGrantType(AuthorizationGrantType.TOKEN_EXCHANGE)
.build();
OidcIdToken subjectToken = OidcIdToken.withTokenValue("id-token").claim("sub", "user").build();
TokenExchangeGrantRequest grantRequest = new TokenExchangeGrantRequest(clientRegistration, subjectToken, null);
// @formatter:off
DefaultOAuth2TokenRequestParametersConverter<TokenExchangeGrantRequest> parametersConverter =
new DefaultOAuth2TokenRequestParametersConverter<>();
// @formatter:on
MultiValueMap<String, String> parameters = parametersConverter.convert(grantRequest);
assertThat(parameters.get(OAuth2ParameterNames.SUBJECT_TOKEN)).containsExactly(subjectToken.getTokenValue());
assertThat(parameters.get(OAuth2ParameterNames.SUBJECT_TOKEN_TYPE)).containsExactly(ID_TOKEN_TYPE_VALUE);
}

@Test
public void convertWhenGrantRequestIsTokenExchangeAndActorTokenIsOidcIdTokenThenActorTokenTypeIsIdToken() {
ClientRegistration clientRegistration = this.clientRegistration
.authorizationGrantType(AuthorizationGrantType.TOKEN_EXCHANGE)
.build();
OAuth2Token subjectToken = TestOAuth2AccessTokens.scopes("read", "write");
OidcIdToken actorToken = OidcIdToken.withTokenValue("id-token").claim("sub", "user").build();
TokenExchangeGrantRequest grantRequest = new TokenExchangeGrantRequest(clientRegistration, subjectToken,
actorToken);
// @formatter:off
DefaultOAuth2TokenRequestParametersConverter<TokenExchangeGrantRequest> parametersConverter =
new DefaultOAuth2TokenRequestParametersConverter<>();
// @formatter:on
MultiValueMap<String, String> parameters = parametersConverter.convert(grantRequest);
assertThat(parameters.get(OAuth2ParameterNames.ACTOR_TOKEN)).containsExactly(actorToken.getTokenValue());
assertThat(parameters.get(OAuth2ParameterNames.ACTOR_TOKEN_TYPE)).containsExactly(ID_TOKEN_TYPE_VALUE);
}

}