use of org.wso2.carbon.apimgt.api.model.KeyManagerConfiguration in project carbon-apimgt by wso2.
the class AbstractKeyManagerTestCase method testCanHandleTokenWithConfigurationJWT.
@Test
public void testCanHandleTokenWithConfigurationJWT() throws APIManagementException {
KeyManagerConfiguration keyManagerConfiguration = new KeyManagerConfiguration();
keyManagerConfiguration.addParameter(APIConstants.KeyManager.TOKEN_FORMAT_STRING, "[{\"enable\": true,\"type\": \"JWT\",\"value\": {\"body\": {\"iss\": \"https://localhost:9443\"}}}]");
KeyManager keyManager = new ModelKeyManagerForTest();
keyManager.loadConfiguration(keyManagerConfiguration);
assertTrue(keyManager.canHandleToken("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9" + ".eyJpc3MiOiJodHRwczovL2xvY2FsaG9zdDo5NDQzIiwiaWF0IjoxNTkwMTM0NzIyLCJleHAiOjE2MjE2NzA3MjAsImF1ZC" + "I6Ind3dy5leGFtcGxlLmNvbSIsInN1YiI6Impyb2NrZXRAZXhhbXBsZS5jb20iLCJFbWFpbCI6ImJlZUBleGFtcGxlLmNvb" + "SJ9.HIxL7_WqeLPkxYdROAwRyL0YEY1YNJRfLghsaHEc7C4"));
assertFalse(keyManager.canHandleToken("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL2xvY2FsaG9zdDo5NDQ0IiwiaWF0IjoxN" + "TkwMTM0NzIyLCJleHAiOjE2MjE2NzA3MjAsImF1ZCI6Ind3dy5leGFtcGxlLmNvbSIsInN1YiI6Impyb2NrZXRAZXhhb" + "XBsZS5jb20iLCJFbWFpbCI6ImJlZUBleGFtcGxlLmNvbSJ9.QjwcCl7Xs0zmioqsr85VQmW5lgRnkfba-v8OgKwhKyA"));
}
use of org.wso2.carbon.apimgt.api.model.KeyManagerConfiguration in project carbon-apimgt by wso2.
the class AbstractKeyManagerTestCase method testCanHandleToken.
@Test
public void testCanHandleToken() throws APIManagementException {
KeyManagerConfiguration keyManagerConfiguration = new KeyManagerConfiguration();
KeyManager keyManager = new ModelKeyManagerForTest();
keyManager.loadConfiguration(keyManagerConfiguration);
assertTrue(keyManager.canHandleToken(UUID.randomUUID().toString()));
}
use of org.wso2.carbon.apimgt.api.model.KeyManagerConfiguration in project carbon-apimgt by wso2.
the class AbstractKeyManagerTestCase method testCanHandleTokenWithConfigurationJWTAndOpaue.
@Test
public void testCanHandleTokenWithConfigurationJWTAndOpaue() throws APIManagementException {
KeyManagerConfiguration keyManagerConfiguration = new KeyManagerConfiguration();
keyManagerConfiguration.addParameter(APIConstants.KeyManager.TOKEN_FORMAT_STRING, "[{\"enable\": true,\"type\": \"JWT\",\"value\": {\"body\": {\"iss\": \"https://localhost:9443\"}}}," + "{\"enable\": true,\"type\": \"REFERENCE\",\"value\": \"[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0" + "-9a-fA-F]{3}-[89ab][0-9a-fA-F]{3}-[0-9a-fA-F]{12}\"}]");
KeyManager keyManager = new ModelKeyManagerForTest();
keyManager.loadConfiguration(keyManagerConfiguration);
assertTrue(keyManager.canHandleToken("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9" + ".eyJpc3MiOiJodHRwczovL2xvY2FsaG9zdDo5NDQzIiwiaWF0IjoxNTkwMTM0NzIyLCJleHAiOjE2MjE2NzA3MjAsImF1ZC" + "I6Ind3dy5leGFtcGxlLmNvbSIsInN1YiI6Impyb2NrZXRAZXhhbXBsZS5jb20iLCJFbWFpbCI6ImJlZUBleGFtcGxlLmNvb" + "SJ9.HIxL7_WqeLPkxYdROAwRyL0YEY1YNJRfLghsaHEc7C4"));
assertTrue(keyManager.canHandleToken(UUID.randomUUID().toString()));
}
use of org.wso2.carbon.apimgt.api.model.KeyManagerConfiguration in project carbon-apimgt by wso2.
the class KeyManagerHolder method getJWTValidator.
private static JWTValidator getJWTValidator(KeyManagerConfiguration keyManagerConfiguration, String jwtValidatorImplementation) throws APIManagementException {
Object selfValidateJWT = keyManagerConfiguration.getParameter(APIConstants.KeyManager.SELF_VALIDATE_JWT);
if (selfValidateJWT != null && (Boolean) selfValidateJWT) {
Object issuer = keyManagerConfiguration.getParameter(APIConstants.KeyManager.ISSUER);
if (issuer != null) {
TokenIssuerDto tokenIssuerDto = new TokenIssuerDto((String) issuer);
Object claimMappings = keyManagerConfiguration.getParameter(APIConstants.KeyManager.CLAIM_MAPPING);
if (claimMappings instanceof List) {
Gson gson = new Gson();
JsonElement jsonElement = gson.toJsonTree(claimMappings);
ClaimMappingDto[] claimMappingDto = gson.fromJson(jsonElement, ClaimMappingDto[].class);
tokenIssuerDto.addClaimMappings(claimMappingDto);
}
Object consumerKeyClaim = keyManagerConfiguration.getParameter(APIConstants.KeyManager.CONSUMER_KEY_CLAIM);
if (consumerKeyClaim instanceof String && StringUtils.isNotEmpty((String) consumerKeyClaim)) {
tokenIssuerDto.setConsumerKeyClaim((String) consumerKeyClaim);
}
Object scopeClaim = keyManagerConfiguration.getParameter(APIConstants.KeyManager.SCOPES_CLAIM);
if (scopeClaim instanceof String && StringUtils.isNotEmpty((String) scopeClaim)) {
tokenIssuerDto.setScopesClaim((String) scopeClaim);
}
Object jwksEndpoint = keyManagerConfiguration.getParameter(APIConstants.KeyManager.JWKS_ENDPOINT);
if (jwksEndpoint != null) {
if (StringUtils.isNotEmpty((String) jwksEndpoint)) {
JWKSConfigurationDTO jwksConfigurationDTO = new JWKSConfigurationDTO();
jwksConfigurationDTO.setEnabled(true);
jwksConfigurationDTO.setUrl((String) jwksEndpoint);
tokenIssuerDto.setJwksConfigurationDTO(jwksConfigurationDTO);
}
}
Object certificateType = keyManagerConfiguration.getParameter(APIConstants.KeyManager.CERTIFICATE_TYPE);
Object certificateValue = keyManagerConfiguration.getParameter(APIConstants.KeyManager.CERTIFICATE_VALUE);
if (certificateType != null && StringUtils.isNotEmpty((String) certificateType) && certificateValue != null && StringUtils.isNotEmpty((String) certificateValue)) {
if (APIConstants.KeyManager.CERTIFICATE_TYPE_JWKS_ENDPOINT.equals(certificateType)) {
JWKSConfigurationDTO jwksConfigurationDTO = new JWKSConfigurationDTO();
jwksConfigurationDTO.setEnabled(true);
jwksConfigurationDTO.setUrl((String) certificateValue);
tokenIssuerDto.setJwksConfigurationDTO(jwksConfigurationDTO);
} else {
X509Certificate x509Certificate = APIUtil.retrieveCertificateFromContent((String) certificateValue);
if (x509Certificate != null) {
tokenIssuerDto.setCertificate(x509Certificate);
}
}
}
JWTValidator jwtValidator;
if (StringUtils.isEmpty(jwtValidatorImplementation)) {
jwtValidator = new JWTValidatorImpl();
} else {
try {
jwtValidator = (JWTValidator) Class.forName(jwtValidatorImplementation).newInstance();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
log.error("Error while initializing JWT Validator", e);
throw new APIManagementException("Error while initializing JWT Validator", e);
}
}
jwtValidator.loadTokenIssuerConfiguration(tokenIssuerDto);
return jwtValidator;
}
}
return null;
}
use of org.wso2.carbon.apimgt.api.model.KeyManagerConfiguration in project carbon-apimgt by wso2.
the class KeyManagerHolder method addKeyManagerConfiguration.
public static void addKeyManagerConfiguration(String organization, String name, String type, KeyManagerConfiguration keyManagerConfiguration) throws APIManagementException {
String issuer = (String) keyManagerConfiguration.getParameter(APIConstants.KeyManager.ISSUER);
OrganizationKeyManagerDto organizationKeyManagerDto = organizationWiseMap.get(organization);
if (organizationKeyManagerDto == null) {
organizationKeyManagerDto = new OrganizationKeyManagerDto();
}
if (organizationKeyManagerDto.getKeyManagerByName(name) != null) {
log.warn("Key Manager " + name + " already initialized in tenant " + organization);
}
if (keyManagerConfiguration.isEnabled() && !KeyManagerConfiguration.TokenType.EXCHANGED.equals(keyManagerConfiguration.getTokenType())) {
KeyManager keyManager = null;
JWTValidator jwtValidator = null;
APIManagerConfiguration apiManagerConfiguration = ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService().getAPIManagerConfiguration();
String defaultKeyManagerType = apiManagerConfiguration.getFirstProperty(APIConstants.DEFAULT_KEY_MANAGER_TYPE);
KeyManagerConnectorConfiguration keyManagerConnectorConfiguration = ServiceReferenceHolder.getInstance().getKeyManagerConnectorConfiguration(type);
if (keyManagerConnectorConfiguration != null) {
if (StringUtils.isNotEmpty(keyManagerConnectorConfiguration.getImplementation())) {
try {
keyManager = (KeyManager) Class.forName(keyManagerConnectorConfiguration.getImplementation()).newInstance();
keyManager.setTenantDomain(organization);
if (StringUtils.isNotEmpty(defaultKeyManagerType) && defaultKeyManagerType.equals(type)) {
keyManagerConfiguration.addParameter(APIConstants.KEY_MANAGER_USERNAME, apiManagerConfiguration.getFirstProperty(APIConstants.API_KEY_VALIDATOR_USERNAME));
keyManagerConfiguration.addParameter(APIConstants.KEY_MANAGER_PASSWORD, apiManagerConfiguration.getFirstProperty(APIConstants.API_KEY_VALIDATOR_PASSWORD));
}
keyManager.loadConfiguration(keyManagerConfiguration);
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
throw new APIManagementException("Error while loading keyManager configuration", e);
}
}
jwtValidator = getJWTValidator(keyManagerConfiguration, keyManagerConnectorConfiguration.getJWTValidator());
} else {
if (APIConstants.KeyManager.DEFAULT_KEY_MANAGER_TYPE.equals(type)) {
keyManager = new AMDefaultKeyManagerImpl();
keyManager.setTenantDomain(organization);
keyManager.loadConfiguration(keyManagerConfiguration);
jwtValidator = getJWTValidator(keyManagerConfiguration, null);
}
}
KeyManagerDto keyManagerDto = new KeyManagerDto();
keyManagerDto.setName(name);
keyManagerDto.setIssuer(issuer);
keyManagerDto.setJwtValidator(jwtValidator);
keyManagerDto.setKeyManager(keyManager);
organizationKeyManagerDto.putKeyManagerDto(keyManagerDto);
organizationWiseMap.put(organization, organizationKeyManagerDto);
}
}
Aggregations