use of org.wso2.carbon.apimgt.impl.AMDefaultKeyManagerImpl in project carbon-apimgt by wso2.
the class AbstractKeyManagerTestCase method buildFromJSONTest.
@Test
public void buildFromJSONTest() throws APIManagementException {
AbstractKeyManager keyManager = new AMDefaultKeyManagerImpl();
KeyManagerConnectorConfiguration keyManagerConnectorConfiguration = Mockito.mock(DefaultKeyManagerConnectorConfiguration.class);
ServiceReferenceHolder serviceReferenceHolder = PowerMockito.mock(ServiceReferenceHolder.class);
PowerMockito.mockStatic(ServiceReferenceHolder.class);
PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
Mockito.when(serviceReferenceHolder.getKeyManagerConnectorConfiguration(APIConstants.KeyManager.DEFAULT_KEY_MANAGER_TYPE)).thenReturn(keyManagerConnectorConfiguration);
// test with empty json payload
assertNotNull(keyManager.buildFromJSON(new OAuthApplicationInfo(), "{}"));
// test with valid json
String jsonPayload2 = "{ \"callbackUrl\": \"www.google.lk\", \"client_id\": \"XBPcXSfGK47WiEX7enchoP2Dcvga\"," + "\"client_secret\": \"4UD8VX8NaQMtrHCwqzI1tHJLPoca\", \"owner\": \"admin\", \"grantType\": \"password" + " refresh_token\", " + "\"validityPeriod\": \"3600\" }";
OAuthApplicationInfo oAuthApplicationInfo1 = keyManager.buildFromJSON(new OAuthApplicationInfo(), jsonPayload2);
assertEquals("XBPcXSfGK47WiEX7enchoP2Dcvga", oAuthApplicationInfo1.getClientId());
// test with invalid json
try {
keyManager.buildFromJSON(new OAuthApplicationInfo(), "{invalid}");
assertTrue(false);
} catch (APIManagementException e) {
assertEquals("Error occurred while parsing JSON String", e.getMessage());
}
// test with invalid additionalProperties
OAuthApplicationInfo applicationInfo = new OAuthApplicationInfo();
applicationInfo.addParameter("additionalProperties", "{invalid}");
try {
keyManager.buildFromJSON(applicationInfo, "{}");
fail();
} catch (APIManagementException e) {
assertEquals("Error while parsing the addition properties of OAuth application", e.getMessage());
}
}
use of org.wso2.carbon.apimgt.impl.AMDefaultKeyManagerImpl in project carbon-apimgt by wso2.
the class RestApiUtil method registerOAuthApplication.
public static OAuthApplicationInfo registerOAuthApplication(OAuthAppRequest appRequest) {
// Create Oauth Application - Dynamic client registration service
AMDefaultKeyManagerImpl impl = new AMDefaultKeyManagerImpl();
OAuthApplicationInfo returnedAPP = null;
try {
returnedAPP = impl.createApplication(appRequest);
} catch (APIManagementException e) {
log.error("Cannot create OAuth application from provided information, for APP name: " + appRequest.getOAuthApplicationInfo().getClientName(), e);
}
return returnedAPP;
}
use of org.wso2.carbon.apimgt.impl.AMDefaultKeyManagerImpl in project carbon-apimgt by wso2.
the class AbstractKeyManagerTestCase method buildAccessTokenRequestFromOAuthAppTest.
@Test
public void buildAccessTokenRequestFromOAuthAppTest() throws APIManagementException {
AbstractKeyManager keyManager = new AMDefaultKeyManagerImpl();
// test null flow
assertNull(keyManager.buildAccessTokenRequestFromOAuthApp(null, null));
// test without client id and secret
try {
keyManager.buildAccessTokenRequestFromOAuthApp(new OAuthApplicationInfo(), new AccessTokenRequest());
assertTrue(false);
} catch (APIManagementException e) {
assertEquals("Consumer key or Consumer Secret missing.", e.getMessage());
}
// test with all the parameters
OAuthApplicationInfo oAuthApplicationInfo = new OAuthApplicationInfo();
oAuthApplicationInfo.setClientId("XBPcXSfGK47WiEX7enchoP2Dcvga");
oAuthApplicationInfo.setClientSecret("4UD8VX8NaQMtrHCwqzI1tHJLPoca");
oAuthApplicationInfo.addParameter("tokenScope", new String[] { "view", "update" });
oAuthApplicationInfo.addParameter("validityPeriod", "1200");
AccessTokenRequest accessTokenRequest = keyManager.buildAccessTokenRequestFromOAuthApp(oAuthApplicationInfo, null);
assertNotNull(accessTokenRequest);
assertEquals("XBPcXSfGK47WiEX7enchoP2Dcvga", accessTokenRequest.getClientId());
assertEquals("4UD8VX8NaQMtrHCwqzI1tHJLPoca", accessTokenRequest.getClientSecret());
assertEquals(1200, accessTokenRequest.getValidityPeriod());
}
use of org.wso2.carbon.apimgt.impl.AMDefaultKeyManagerImpl in project carbon-apimgt by wso2.
the class AbstractKeyManagerTestCase method buildAccessTokenRequestFromJSONTest.
@Test
public void buildAccessTokenRequestFromJSONTest() throws APIManagementException {
String jsonPayload = "{ \"callbackUrl\": \"www.google.lk\", \"clientName\": \"rest_api_publisher\", " + "\"tokenScope\": \"Production\", \"owner\": \"admin\", \"grantType\": \"password refresh_token\", " + "\"saasApp\": true }";
AbstractKeyManager keyManager = new AMDefaultKeyManagerImpl();
// test AccessTokenRequest null scenario
AccessTokenRequest accessTokenRequest1 = keyManager.buildAccessTokenRequestFromJSON(jsonPayload, null);
Assert.notNull(accessTokenRequest1);
// test json payload without required parameters
AccessTokenRequest accessTokenRequest2 = keyManager.buildAccessTokenRequestFromJSON(jsonPayload, accessTokenRequest1);
Assert.notNull(accessTokenRequest2);
assertNull(accessTokenRequest2.getClientId());
// test json payload null
assertNull(keyManager.buildAccessTokenRequestFromJSON(null, null));
String jsonPayload2 = "{ \"callbackUrl\": \"www.google.lk\", \"client_id\": \"XBPcXSfGK47WiEX7enchoP2Dcvga\"," + "\"client_secret\": \"4UD8VX8NaQMtrHCwqzI1tHJLPoca\", \"owner\": \"admin\", \"grantType\": \"password" + " refresh_token\", " + "\"validityPeriod\": \"3600\" }";
AccessTokenRequest accessTokenRequest3 = keyManager.buildAccessTokenRequestFromJSON(jsonPayload2, new AccessTokenRequest());
assertEquals("XBPcXSfGK47WiEX7enchoP2Dcvga", accessTokenRequest3.getClientId());
assertEquals("4UD8VX8NaQMtrHCwqzI1tHJLPoca", accessTokenRequest3.getClientSecret());
assertEquals(3600, accessTokenRequest3.getValidityPeriod());
// Error path with invalid json
try {
keyManager.buildAccessTokenRequestFromJSON("{dd}", null);
assertTrue(false);
} catch (APIManagementException e) {
assertEquals("Error occurred while parsing JSON String", e.getMessage());
}
// Error path with empty JSON
assertNull(keyManager.buildAccessTokenRequestFromJSON("{}", null));
keyManager.buildAccessTokenRequestFromJSON(null, new AccessTokenRequest());
}
use of org.wso2.carbon.apimgt.impl.AMDefaultKeyManagerImpl 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