0

I am working on WSO2IS, and had been able to get a self contained access token out of WSO2IS by Oauth2 "password" grant type by following this post

I am also able to verify the signature of the token in application (see this post)

yet there is still one final step that I can not pass

here is a sample of access token I have got out of WSO2IS

{iss=https://localhost:9443/oauth2/token, sub=wjz@carbon.super, aud=[J3lbMMMJFwXB6neKzXv030S9lfga], exp=1488710173, iat=1488706573, azp=J3lbMMMJFwXB6neKzXv030S9lfga}

you can see that value of "sub" is a username, which correspond to the claim " http://wso2.org/claims/username".

I want to change the configure in WSO2IS so that the "sub" correspond to claim " http://wso2.org/claims/userid"

I changed the "Claim Configuration" under "Service Providers"; I also changed the "sub" in the "http://wso2.org/oidc/claim" under "Claims". but can not get any success.

are there anything I have missed?

please advise

thanks

Community
  • 1
  • 1
George Wang
  • 765
  • 2
  • 13
  • 28
  • what is the identity server version you are using? – Maduranga Siriwardena Mar 11 '17 at 02:30
  • Can you go to "Local & Outbound Authentication Configuration" section of the service provider and un-check "Use tenant domain in local subject identifier" and check what is the result. – Maduranga Siriwardena Mar 11 '17 at 02:34
  • I am using WSO2 IS 5.3.0 – George Wang Mar 11 '17 at 07:15
  • I went to "Local & Outbound Authentication Configuration", and found out that "Use tenant domain in local subject identifier" was actually "unchecked". I checked it, update , restart and test, and got the same results as above . I did the same with "Use tenant domain in local subject identifier" unchecked. and the result are the same as above. it seems that "Use tenant domain in local subject identifier" has not effect on the access token "sub" value – George Wang Mar 11 '17 at 07:55

1 Answers1

0

I finally have this problem solved by coding instead of configuring.

I had implemented an extension for a Self-Contained Access Token (JWT in Oauth2) Generator by followed this post . I build the jar, and upload the jar under /repository/components/lib/

I just checkout this repo , and made the following changes

 /**
     * For a locally authenticated user, subject identifier is supposed to be as below.
     * <userstore_domain>/<username>@<tenant_domain>.
     * 
     * yet somehow, what I got is <username>@<tenant_domain>
     * @param SubjectId
     * @return
     * @throws IdentityOAuth2Exception 
     */
    private static SubjectTriple parseSubjectId(String subjectId) throws IdentityOAuth2Exception{
        if (StringUtils.isBlank(subjectId)){
            throw new IdentityOAuth2Exception("invalid subject identifier");

        }
        /*
         * domain may not present
         */
        String sid = null;
        SubjectTriple st = new SubjectTriple();
        if(StringUtils.contains(subjectId, '/')){
            st.domain = StringUtils.substringBeforeLast(subjectId, "/");
            sid = StringUtils.substringAfterLast(subjectId, "/");
        }else{
            sid = subjectId;
        }

        st.username = StringUtils.substringBeforeLast(sid, "@");
        st.profile = StringUtils.substringAfterLast(sid, "@");

        return st;
    }

    /**
     * To build id token from OauthToken request message context
     *
     * @param request Token request message context
     * @return Signed jwt string.
     * @throws IdentityOAuth2Exception
     */
    protected String buildIDToken(OAuthTokenReqMessageContext request)
            throws IdentityOAuth2Exception {

        String issuer = OAuth2Util.getIDTokenIssuer();
        long lifetimeInMillis = OAuthServerConfiguration.getInstance().
                getApplicationAccessTokenValidityPeriodInSeconds() * 1000;
        long curTimeInMillis = Calendar.getInstance().getTimeInMillis();


        SubjectTriple triple = parseSubjectId(request.getAuthorizedUser().getAuthenticatedSubjectIdentifier());
        String userId = null;
        try {
            userId = CarbonContext.getThreadLocalCarbonContext().getUserRealm().getUserStoreManager()
                    .getUserClaimValue(triple.username, Constants.LOCAL_CLAIM__UserID, triple.profile);
        } catch (UserStoreException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        String clientId = request.getOauth2AccessTokenReqDTO().getClientId();
        // Set claims to jwt token.
        JWTClaimsSet jwtClaimsSet = new JWTClaimsSet();
        jwtClaimsSet.setIssuer(issuer);
        jwtClaimsSet.setSubject(userId);
        jwtClaimsSet.setAudience(Arrays.asList(clientId));
        jwtClaimsSet.setClaim(Constants.AUTHORIZATION_PARTY, clientId);
        jwtClaimsSet.setExpirationTime(new Date(curTimeInMillis + lifetimeInMillis));
        jwtClaimsSet.setIssueTime(new Date(curTimeInMillis));

        if (JWSAlgorithm.NONE.getName().equals(signatureAlgorithm.getName())) {
            return new PlainJWT(jwtClaimsSet).serialize();
        }
        return signJWT(jwtClaimsSet, request);
    }

    /**
     * Build a signed jwt token from authorization request message context
     *
     * @param request Oauth authorization message context
     * @return Signed jwt string
     * @throws IdentityOAuth2Exception
     */
    protected String buildIDToken(OAuthAuthzReqMessageContext request)
            throws IdentityOAuth2Exception {

        String issuer = OAuth2Util.getIDTokenIssuer();
        long lifetimeInMillis = OAuthServerConfiguration.getInstance().
                getApplicationAccessTokenValidityPeriodInSeconds() * 1000;
        long curTimeInMillis = Calendar.getInstance().getTimeInMillis();
        OAuth2AuthorizeReqDTO dto = request.getAuthorizationReqDTO();

        SubjectTriple triple = parseSubjectId(dto.getUser().getAuthenticatedSubjectIdentifier());

        String userId = null;
        try {
            userId = CarbonContext.getThreadLocalCarbonContext().getUserRealm().getUserStoreManager()
                    .getUserClaimValue(triple.username, Constants.LOCAL_CLAIM__UserID, triple.profile);
        } catch (UserStoreException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        String consumerKey = dto.getConsumerKey();
        JWTClaimsSet jwtClaimsSet = new JWTClaimsSet();
        jwtClaimsSet.setIssuer(issuer);
        jwtClaimsSet.setSubject(userId);
        jwtClaimsSet.setAudience(Arrays.asList(consumerKey));
        jwtClaimsSet.setClaim(Constants.AUTHORIZATION_PARTY,consumerKey);
        jwtClaimsSet.setExpirationTime(new Date(curTimeInMillis + lifetimeInMillis));
        jwtClaimsSet.setIssueTime(new Date(curTimeInMillis));

        if (JWSAlgorithm.NONE.getName().equals(signatureAlgorithm.getName())) {
            return new PlainJWT(jwtClaimsSet).serialize();
        }
        return signJWT(jwtClaimsSet, request);
    }

the imports

import com.nimbusds.jose.Algorithm;
import com.nimbusds.jose.JOSEException;
import com.nimbusds.jose.JWSAlgorithm;
import com.nimbusds.jose.JWSHeader;
import com.nimbusds.jose.JWSSigner;
import com.nimbusds.jose.crypto.RSASSASigner;
import com.nimbusds.jwt.JWTClaimsSet;
import com.nimbusds.jwt.PlainJWT;
import com.nimbusds.jwt.SignedJWT;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
import org.wso2.carbon.base.MultitenantConstants;
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.core.util.KeyStoreManager;
import org.wso2.carbon.identity.core.util.IdentityTenantUtil;
import org.wso2.carbon.identity.oauth.config.OAuthServerConfiguration;
import org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception;
import org.wso2.carbon.identity.oauth2.authz.OAuthAuthzReqMessageContext;
import org.wso2.carbon.identity.oauth2.dto.OAuth2AuthorizeReqDTO;
import org.wso2.carbon.identity.oauth2.token.OAuthTokenReqMessageContext;
import org.wso2.carbon.identity.oauth2.token.OauthTokenIssuerImpl;
import org.wso2.carbon.identity.oauth2.util.OAuth2Util;
import org.wso2.carbon.user.api.UserStoreException;

import java.security.Key;
import java.security.interfaces.RSAPrivateKey;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
George Wang
  • 765
  • 2
  • 13
  • 28