Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

JSON Web Token (JWT) is used to represent claims that are transferred between two parties such as the enduser and the backend.

A claim is an attribute of the user that is mapped to the underlying user store. It is encoded as a JavaScript Object Notation (JSON) object that is used as the payload of a JSON Web Signature (JWS) structure, or as the plain text of a JSON Web Encryption (JWE) structure. This enables claims to be digitally signed.

A set of claims is called a dialect (e.g.,   http://wso2.org/claims ).  The general format of a JWT is  {token infor}.{claims list}.{signature}. The API implementation uses information such as logging, content filtering and authentication/authorization that is stored in this token. The token is Base64-encoded and sent to the API implementation in a HTTP header variable.  The JWT is self-contained and is divided into three parts as the header, the payload and the signature. For more information on JWT, see  http://openid.net/specs/draft-jones-json-web-token-07.html#anchor3 .

To authenticate endusers, the API Manager passes attributes of the API invoker to the backend API implementation using JWT. In most production deployments, service calls go through the API Manager or a proxy service. If you enable JWT generation in the API Manager, each API request will carry a JWT to the back-end service. When the request goes through the API manager, the JWT is appended as a transport header to the outgoing message. The back-end service fetches the JWT and retrieves the required information about the user, application, or token.

...

ElementDescriptionDefault Value
<EnableTokenGeneration>
Set this value to true to enable JWT. Note that if you publish APIs before JWT is enabled, you have to republish them.false
<SecurityContextHeader/>  The name of the HTTP header to which the JWT is attached.X-JWT-Assertion
<ClaimsRetrieverImplClass/>

By default, the < ClaimsRetrieverImplClass> parameter is commented out in the api-manager.xml file. Enable it to add all user claims in the JWT token:

Code Block
languagexml
<ClaimsRetrieverImplClass>org.wso2.carbon.apimgt.impl.token.DefaultClaimsRetriever</ClaimsRetrieverImplClass> 

By default, t he following are encoded to the JWT:

  • subscriber name
  • application name 
  • API context
  • API version
  • authorised resource owner name

In addition, you can also write your own class by extending the interface org.wso2.carbon.apimgt.impl.token.ClaimsRetriever and implementing the following methods of the interface:

MethodDescription

void init() throws APIManagementException;

Used to perform initialization tasks. Is executed once, right before the very first request.

SortedMap<String,String> getClaims(String endUserName) throws APIManagementException;

Returns a sorted map of claims. The key of the map indicates the user attribute name and the value indicates the corresponding user attribute value. The order in which these keys and values are encoded depends on the ordering defined by the sorted map.

String getDialectURI(String endUserName);

The dialect URI to which the attribute names returned by the getClaims() method are appended to. For example,
if the getClaims method returns {email:user1@wso2.com, gender:male} and the getDialectURI() returns http://wso2.org/claims , the JWT will contain "http://wso2.org/claims/gender”:“user1@wso2.com”,“httpgender":"male","http://wso2.org/claims/email”:“male”email":"user1@wso2.com" as part of the body.

The default implementation (org.wso2.carbon.apimgt.impl.token.DefaultClaimsRetriever) returns the user's attributes defined under the dialect URI http://wso2.org/claims and the JWT will also be encoded with the same dialect URI. The order of encoding the user's attributes is the natural order of the attributes. If no value is specified, no additional claims will be encoded, except the 6 default attributes.

org.wso2.carbon.apimgt.impl.token.DefaultClaimsRetriever
<ConsumerDialectURI/>  

The dialect URI under which the user's claims are be looked for. Only works with the default value of <ClaimsRetrieverImplClass> element defined above.

The JWT token contains all claims define in the <ConsumerDialectURI> element. The default value of this element is http://wso2.org/claims . To get a list of users to be included in the JWT, simply uncomment this element after enabling the JWT. It will include all claims in http://wso2.org/claims to the JWT token.

Excerpt
hiddentrue

Taken from the shared space: http://docs.wso2.org/display/FAQ/APIM+Common#APIMCommon-HowtogetarolelistofuserstobeincludedintheJWT

http://wso2.org/claims
<SignatureAlgorithm/>  

The signing algorithm used to sign the JWT. The general format of the JWT is {token infor}.{claims list}.{signature}. When NONE is specified as the algorithm, signing is turned off and the JWT looks as {token infor}.{claims list} with two strings delimited by a period and a period at the end.

This element can have only two values- the default value, which is SHA256WITHRSA or NONE.

SHA256WITHRSA

...

The JWT that is generated by default (see example above) has predefined attributes that are passed to the backend. These include basic application-specific details, subscription details, and user information that are defined in the JWT generation class that comes with the API Manager by the name org.wso2.carbon.apimgt.impl.token.JWTGeneratorIf you want to pass additional attributes to the backend with the JWT or completely change the default JWT generation logic, do the following:

  1. Write your own custom JWT implementation class by extending the default JWTGenerator class. A typical example of implementing your own claim generator is given below. It implements the populateCustomClaims()  method to generate some custom claims and adds them to the JWT. 

    Code Block
    languagejava
    import org.wso2.carbon.apimgt.impl.APIConstants;
    import org.wso2.carbon.apimgt.impl.dto.APIKeyValidationInfoDTO;
    import org.wso2.carbon.apimgt.impl.token.JWTGenerator;
    import org.wso2.carbon.apimgt.api.*;
    
    import java.util.Map;
    
    public class CustomTokenGenerator extends JWTGenerator {
    
        public Map populateStandardClaims(APIKeyValidationInfoDTO keyValidationInfoDTO, String apiContext, String version)
                throws APIManagementException {
            Map claims = super.populateStandardClaims(keyValidationInfoDTO, apiContext, version);
            boolean isApplicationToken =
                    keyValidationInfoDTO.getUserType().equalsIgnoreCase(APIConstants.ACCESS_TOKEN_USER_TYPE_APPLICATION) ? true : false;
            String dialect = getDialectURI();
            if (claims.get(dialect + "/enduser") != null) {
                if (isApplicationToken) {
                    claims.put(dialect + "/enduser", "null");
                    claims.put(dialect + "/enduserTenantId", "null");
                } else {
                    String enduser = claims.get(dialect + "/enduser");
                    if (enduser.endsWith("@carbon.super")) {
                        enduser = enduser.replace("@carbon.super", "");
                        claims.put(dialect + "/enduser", enduser);
                    }
                }
            }
    
            return claims;
    
        }
    
        public Map populateCustomClaims(APIKeyValidationInfoDTO keyValidationInfoDTO, String apiContext, String version, String accessToken)
                throws APIManagementException {
            Long time = System.currentTimeMillis();
            String text = "This is custom JWT";
            Map map = new HashMap();
            map.put("current_timestamp", time.toString());
            map.put("messge" , text);
            return map;
        }
    }
  2. Build your class and add the JAR file to <APIM_HOME>/repository/components/lib directory.
  3. Add your class in the <TokenGeneratorImpl> element of the <APIM_HOME>/repository/conf/api-manager.xml  file. 

    Code Block
    languagexml
    <APIConsumerAuthentication>
       ....
       <TokenGeneratorImpl>org.wso2.carbon.test.CustomTokenGenerator</TokenGeneratorImpl>
       ....
    </APIConsumerAuthentication>
  4. Set the <EnableTokenGeneration> element to true in the api-manager.xml file. 
  5. Restart the server.