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

Add tokenBindingId to JWT #2352

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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 @@ -534,9 +534,8 @@ protected JWTClaimsSet createJWTClaimSet(OAuthAuthzReqMessageContext authAuthzRe
// Include token binding.
jwtClaimsSet = handleTokenBinding(jwtClaimsSetBuilder, tokenReqMessageContext);

if (tokenReqMessageContext != null && tokenReqMessageContext.getProperty(CNF) != null) {
jwtClaimsSet = handleCnf(jwtClaimsSetBuilder, tokenReqMessageContext);
}
// Include cnf.
jwtClaimsSet = handleCnf(jwtClaimsSetBuilder, tokenReqMessageContext);

return jwtClaimsSet;
}
Expand Down Expand Up @@ -582,16 +581,22 @@ private String getAuthorizedUserType(OAuthAuthzReqMessageContext authAuthzReqMes
* @return authenticated subject identifier.
*/
private String getAuthenticatedSubjectIdentifier(OAuthAuthzReqMessageContext authAuthzReqMessageContext,
OAuthTokenReqMessageContext tokenReqMessageContext) throws IdentityOAuth2Exception {
OAuthTokenReqMessageContext tokenReqMessageContext) throws IdentityOAuth2Exception {

AuthenticatedUser authenticatedUser = getAuthenticatedUser(authAuthzReqMessageContext, tokenReqMessageContext);
return authenticatedUser.getAuthenticatedSubjectIdentifier();
}

private JWTClaimsSet handleCnf(JWTClaimsSet.Builder jwtClaimsSetBuilder,
OAuthTokenReqMessageContext tokReqMsgCtx) {
private JWTClaimsSet handleCnf(JWTClaimsSet.Builder jwtClaimsSetBuilder, OAuthTokenReqMessageContext tokReqMsgCtx) {

jwtClaimsSetBuilder.claim(CNF, tokReqMsgCtx.getProperty(CNF));
if (tokReqMsgCtx != null && tokReqMsgCtx.getProperty(CNF) != null) {
jwtClaimsSetBuilder.claim(CNF, tokReqMsgCtx.getProperty(CNF));
} else if (tokReqMsgCtx != null && tokReqMsgCtx.getOauth2AccessTokenReqDTO() != null) {
if (tokReqMsgCtx.getOauth2AccessTokenReqDTO().getParameters() != null
&& tokReqMsgCtx.getOauth2AccessTokenReqDTO().getParameters().containsKey(CNF)) {
jwtClaimsSetBuilder.claim(CNF, tokReqMsgCtx.getOauth2AccessTokenReqDTO().getParameters().get(CNF));
}
}
return jwtClaimsSetBuilder.build();
}

Expand All @@ -606,7 +611,6 @@ private String getSubjectClaim(String clientId, String spTenantDomain, Authentic
*
* @param authAuthzReqMessageContext
* @param tokenReqMessageContext
*
* @return AuthenticatedUser
*/
private AuthenticatedUser getAuthenticatedUser(OAuthAuthzReqMessageContext authAuthzReqMessageContext,
Expand Down Expand Up @@ -838,6 +842,7 @@ private JWTClaimsSet handleTokenBinding(JWTClaimsSet.Builder jwtClaimsSetBuilder

/**
* Set tenant domain of user to the JWT token's realm claim if signed with user tenant.
*
* @param tenantDomain
* @param jwtClaimsSet
* @return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@
import org.wso2.carbon.identity.oauth2.dto.OAuth2AccessTokenReqDTO;
import org.wso2.carbon.identity.oauth2.model.RequestParameter;

import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static org.wso2.carbon.identity.oauth.common.OAuthConstants.CNF;
import static org.wso2.carbon.identity.oauth2.OAuth2Constants.TokenBinderType.CLIENT_REQUEST;


Expand All @@ -48,7 +47,16 @@ public Optional<String> getTokenBindingValue(OAuth2AccessTokenReqDTO oAuth2Acces
for (RequestParameter parameter : parameters) {
if (TOKEN_BINDING_ID.equals(parameter.getKey())
&& StringUtils.isNotBlank(parameter.getValue()[0])) {
return Optional.ofNullable(parameter.getValue()[0]);
// Adding the cnf parameter to the request parameters to ensure tokenBindingId
// will be added to the token.
if (oAuth2AccessTokenReqDTO.getParameters() == null) {
Map<String, String> parametersMap = new HashMap<>();
parametersMap.put(CNF, parameter.getValue()[0]);
oAuth2AccessTokenReqDTO.setParameters(parametersMap);
} else {
oAuth2AccessTokenReqDTO.getParameters().put(CNF, parameter.getValue()[0]);
}
return Optional.ofNullable(parameter.getValue()[0]);
}
}
return Optional.empty();
Expand Down
Loading