use of org.wso2.carbon.apimgt.keymgt.token.JWTGenerator in project carbon-apimgt by wso2.
the class TokenGenTest method testAbstractJWTGenerator.
@Test
@Ignore
public void testAbstractJWTGenerator() throws Exception {
JWTGenerator jwtGen = new JWTGenerator() {
@Override
protected Map<String, String> convertClaimMap(Map<ClaimMapping, String> userAttributes, String username) {
return new HashMap<>();
}
};
APIKeyValidationInfoDTO dto = new APIKeyValidationInfoDTO();
TokenValidationContext validationContext = new TokenValidationContext();
validationContext.setValidationInfoDTO(dto);
validationContext.setContext("testAPI");
validationContext.setVersion("1.5.0");
validationContext.setAccessToken("DUMMY_TOKEN_STRING");
dto.setSubscriber("sanjeewa");
dto.setApplicationName("sanjeewa-app");
dto.setApplicationId("1");
dto.setApplicationTier("UNLIMITED");
dto.setEndUserName("malalgoda");
dto.setSubscriberTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
dto.setUserType(APIConstants.ACCESS_TOKEN_USER_TYPE_APPLICATION);
// Here we will call generate token method with 4 argument.
String token = jwtGen.generateToken(validationContext);
System.out.println("Generated Token: " + token);
String header = token.split("\\.")[0];
String decodedHeader = new String(Base64Utils.decode(header));
System.out.println("Header: " + decodedHeader);
String body = token.split("\\.")[1];
String decodedBody = new String(Base64Utils.decode(body));
System.out.println("Body: " + decodedBody);
// With end user name not included
token = jwtGen.generateToken(validationContext);
System.out.println("Generated Token: " + token);
header = token.split("\\.")[0];
decodedHeader = new String(Base64Utils.decode(header));
System.out.println("Header: " + decodedHeader);
body = token.split("\\.")[1];
decodedBody = new String(Base64Utils.decode(body));
System.out.println("Body: " + decodedBody);
dto.setUserType(APIConstants.SUBSCRIPTION_USER_TYPE);
token = jwtGen.generateToken(validationContext);
System.out.println("Generated Token: " + token);
header = token.split("\\.")[0];
decodedHeader = new String(Base64Utils.decode(header));
System.out.println("Header: " + decodedHeader);
body = token.split("\\.")[1];
decodedBody = new String(Base64Utils.decode(body));
System.out.println("Body: " + decodedBody);
token = jwtGen.generateToken(validationContext);
System.out.println("Generated Token: " + token);
header = token.split("\\.")[0];
decodedHeader = new String(Base64Utils.decode(header));
System.out.println("Header: " + decodedHeader);
body = token.split("\\.")[1];
decodedBody = new String(Base64Utils.decode(body));
System.out.println("Body: " + decodedBody);
}
use of org.wso2.carbon.apimgt.keymgt.token.JWTGenerator in project carbon-apimgt by wso2.
the class TokenGenTest method testJWTGeneration.
// TODO: Have to convert to work with new JWT generation and signing
@Test
@Ignore
public void testJWTGeneration() throws Exception {
JWTGenerator jwtGen = new JWTGenerator() {
@Override
public Map<String, String> convertClaimMap(Map<ClaimMapping, String> userAttributes, String username) {
return new HashMap<>();
}
};
APIKeyValidationInfoDTO dto = new APIKeyValidationInfoDTO();
dto.setSubscriber("sastry");
dto.setApplicationName("hubapp");
dto.setApplicationId("1");
dto.setApplicationTier("UNLIMITED");
dto.setEndUserName("denis");
dto.setSubscriberTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
dto.setUserType(APIConstants.ACCESS_TOKEN_USER_TYPE_APPLICATION);
TokenValidationContext validationContext = new TokenValidationContext();
validationContext.setValidationInfoDTO(dto);
validationContext.setContext("cricScore");
validationContext.setVersion("1.9.0");
String token = jwtGen.generateToken(validationContext);
System.out.println("Generated Token: " + token);
String header = token.split("\\.")[0];
String decodedHeader = new String(Base64Utils.decode(header));
System.out.println("Header: " + decodedHeader);
String body = token.split("\\.")[1];
String decodedBody = new String(Base64Utils.decode(body));
System.out.println("Body: " + decodedBody);
// With end user name not included
token = jwtGen.generateToken(validationContext);
System.out.println("Generated Token: " + token);
header = token.split("\\.")[0];
decodedHeader = new String(Base64Utils.decode(header));
System.out.println("Header: " + decodedHeader);
body = token.split("\\.")[1];
decodedBody = new String(Base64Utils.decode(body));
System.out.println("Body: " + decodedBody);
dto.setUserType(APIConstants.SUBSCRIPTION_USER_TYPE);
token = jwtGen.generateToken(validationContext);
System.out.println("Generated Token: " + token);
header = token.split("\\.")[0];
decodedHeader = new String(Base64Utils.decode(header));
System.out.println("Header: " + decodedHeader);
body = token.split("\\.")[1];
decodedBody = new String(Base64Utils.decode(body));
System.out.println("Body: " + decodedBody);
token = jwtGen.generateToken(validationContext);
System.out.println("Generated Token: " + token);
header = token.split("\\.")[0];
decodedHeader = new String(Base64Utils.decode(header));
System.out.println("Header: " + decodedHeader);
body = token.split("\\.")[1];
decodedBody = new String(Base64Utils.decode(body));
System.out.println("Body: " + decodedBody);
// we can not do assert eaquals because body includes expiration time.
/*String expectedHeader = "{\"typ\":\"JWT\"}";
String expectedBody = "{\"iss\":\"wso2.org/products/am\", \"exp\":1349270811075, " +
"\"http://wso2.org/claims/subscriber\":\"sastry\", " +
"\"http://wso2.org/claims/applicationname\":\"hubapp\", " +
"\"http://wso2.org/claims/apicontext\":\"cricScore\", " +
"\"http://wso2.org/claims/version\":\"1.9.0\", " +
"\"http://wso2.org/claims/tier\":\"Bronze\", " +
"\"http://wso2.org/claims/enduser\":\"denis\"}";
Assert.assertEquals(expectedHeader, decodedHeader);
Assert.assertEquals(expectedBody, decodedBody);*/
// String decodedToken = new String(Base64Utils.decode(token));
// log.info(decodedToken);
// assertNotNull(decodedToken);
}
use of org.wso2.carbon.apimgt.keymgt.token.JWTGenerator in project carbon-apimgt by wso2.
the class APIKeyMgtDataHolder method initData.
public static void initData() {
try {
APIKeyMgtDataHolder.isKeyCacheEnabledKeyMgt = getInitValues(APIConstants.KEY_MANAGER_TOKEN_CACHE);
APIManagerConfiguration configuration = org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService().getAPIManagerConfiguration();
if (configuration == null) {
log.error("API Manager configuration is not initialized");
} else {
ExtendedJWTConfigurationDto jwtConfigurationDto = configuration.getJwtConfigurationDto();
if (log.isDebugEnabled()) {
log.debug("JWTGeneration enabled : " + jwtConfigurationDto.isEnabled());
}
if (jwtConfigurationDto.isEnabled()) {
if (jwtConfigurationDto.getJwtGeneratorImplClass() == null) {
tokenGenerator = new JWTGenerator();
} else {
try {
tokenGenerator = (TokenGenerator) APIUtil.getClassInstance(jwtConfigurationDto.getJwtGeneratorImplClass());
} catch (InstantiationException e) {
log.error("Error while instantiating class " + jwtConfigurationDto.getJwtGeneratorImplClass(), e);
} catch (IllegalAccessException e) {
log.error(e);
} catch (ClassNotFoundException e) {
log.error("Cannot find the class " + jwtConfigurationDto.getJwtGeneratorImplClass() + e);
}
}
}
}
} catch (Exception e) {
log.error("Error occur while initializing API KeyMgt Data Holder.Default configuration will be used." + e.toString());
}
}
Aggregations