use of org.wso2.carbon.apimgt.core.dao.SystemApplicationDao in project carbon-apimgt by wso2.
the class SystemApplicationDaoImplIT method testAddAndGetApplicationKey.
@Test
public void testAddAndGetApplicationKey() throws Exception {
SystemApplicationDao systemApplicationDao = DAOFactory.getSystemApplicationDao();
String consumerKey = UUID.randomUUID().toString();
String appName = "publisher";
systemApplicationDao.addApplicationKey(appName, consumerKey);
String retrievedConsumerKey = systemApplicationDao.getConsumerKeyForApplication(appName);
Assert.assertNotNull(retrievedConsumerKey);
Assert.assertEquals(retrievedConsumerKey, consumerKey);
Assert.assertTrue(systemApplicationDao.isConsumerKeyExistForApplication(appName));
systemApplicationDao.removeConsumerKeyForApplication(appName);
Assert.assertFalse(systemApplicationDao.isConsumerKeyExistForApplication(appName));
try {
systemApplicationDao.getConsumerKeyForApplication(appName);
Assert.fail("System application exist");
} catch (APIMgtDAOException e) {
Assert.assertTrue(true);
}
}
use of org.wso2.carbon.apimgt.core.dao.SystemApplicationDao in project carbon-apimgt by wso2.
the class AuthenticatorAPIFactory method getService.
/**
* Get an instance of AuthenticatorService
*
* @return AuthenticatorService
* @throws APIMgtDAOException if failed to initialize SystemApplicationDao
* @throws IdentityProviderException if failed to initialize IdentityProvider
*/
public synchronized AuthenticatorService getService() throws APIMgtDAOException, IdentityProviderException {
if (service == null) {
IdentityProvider identityProvider = APIManagerFactory.getInstance().getIdentityProvider();
SystemApplicationDao systemApplicationDao = DAOFactory.getSystemApplicationDao();
APIMConfigurationService apimConfigurationService = APIMConfigurationService.getInstance();
APIMAppConfigurationService apimAppConfigurationService = APIMAppConfigurationService.getInstance();
service = new AuthenticatorService(identityProvider, systemApplicationDao, apimConfigurationService, apimAppConfigurationService);
}
return service;
}
use of org.wso2.carbon.apimgt.core.dao.SystemApplicationDao in project carbon-apimgt by wso2.
the class AuthenticatorServiceTestCase method testGetAuthenticationConfigurations.
@Test
public void testGetAuthenticationConfigurations() throws Exception {
// Happy Path - 200
// // Mocked response object from DCR api
SystemApplicationDao systemApplicationDao = Mockito.mock(SystemApplicationDao.class);
Mockito.when(systemApplicationDao.isConsumerKeyExistForApplication("store")).thenReturn(false);
APIMConfigurationService apimConfigurationService = Mockito.mock(APIMConfigurationService.class);
EnvironmentConfigurations environmentConfigurations = new EnvironmentConfigurations();
Mockito.when(apimConfigurationService.getEnvironmentConfigurations()).thenReturn(environmentConfigurations);
APIMAppConfigurationService apimAppConfigurationService = Mockito.mock(APIMAppConfigurationService.class);
APIMAppConfigurations apimAppConfigurations = new APIMAppConfigurations();
Mockito.when(apimAppConfigurationService.getApimAppConfigurations()).thenReturn(apimAppConfigurations);
OAuthApplicationInfo oAuthApplicationInfo = new OAuthApplicationInfo();
oAuthApplicationInfo.setClientId("xxx-client-id-xxx");
oAuthApplicationInfo.setCallBackURL("https://localhost/9292/login/callback/store");
// // Expected data object to be passed to the front-end
JsonObject oAuthData = new JsonObject();
String scopes = "apim:self-signup apim:dedicated_gateway apim:subscribe openid";
oAuthData.addProperty(KeyManagerConstants.OAUTH_CLIENT_ID, oAuthApplicationInfo.getClientId());
oAuthData.addProperty(KeyManagerConstants.OAUTH_CALLBACK_URIS, oAuthApplicationInfo.getCallBackURL());
oAuthData.addProperty(KeyManagerConstants.TOKEN_SCOPES, scopes);
oAuthData.addProperty(KeyManagerConstants.AUTHORIZATION_ENDPOINT, "https://localhost:9080/oauth2/authorize");
oAuthData.addProperty(AuthenticatorConstants.SSO_ENABLED, ServiceReferenceHolder.getInstance().getAPIMAppConfiguration().isSsoEnabled());
oAuthData.addProperty(AuthenticatorConstants.MULTI_ENVIRONMENT_OVERVIEW_ENABLED, APIMConfigurationService.getInstance().getEnvironmentConfigurations().getMultiEnvironmentOverview().isEnabled());
MultiEnvironmentOverview multiEnvironmentOverview = new MultiEnvironmentOverview();
environmentConfigurations.setMultiEnvironmentOverview(multiEnvironmentOverview);
KeyManager keyManager = Mockito.mock(KeyManager.class);
AuthenticatorService authenticatorService = new AuthenticatorService(keyManager, systemApplicationDao, apimConfigurationService, apimAppConfigurationService);
// // Get data object to be passed to the front-end
Mockito.when(keyManager.createApplication(Mockito.any())).thenReturn(oAuthApplicationInfo);
JsonObject responseOAuthDataObj = authenticatorService.getAuthenticationConfigurations("store");
Assert.assertEquals(responseOAuthDataObj, oAuthData);
// Error Path - 500 - When OAuthApplicationInfo is null
JsonObject emptyOAuthDataObj = new JsonObject();
Mockito.when(keyManager.createApplication(Mockito.any())).thenReturn(null);
JsonObject responseEmptyOAuthDataObj = authenticatorService.getAuthenticationConfigurations("store");
Assert.assertEquals(responseEmptyOAuthDataObj, emptyOAuthDataObj);
// Error Path - When DCR application creation fails and throws an APIManagementException
Mockito.when(keyManager.createApplication(Mockito.any())).thenThrow(KeyManagementException.class);
try {
authenticatorService.getAuthenticationConfigurations("store");
} catch (APIManagementException e) {
Assert.assertEquals(e.getMessage(), "Error while creating the keys for OAuth application : store");
}
}
use of org.wso2.carbon.apimgt.core.dao.SystemApplicationDao in project carbon-apimgt by wso2.
the class AuthenticatorServiceTestCase method testGetUIServiceRedirectionURI.
@Test
public void testGetUIServiceRedirectionURI() throws URISyntaxException, UnsupportedEncodingException {
// Happy Path
APIMConfigurationService apimConfigurationService = Mockito.mock(APIMConfigurationService.class);
EnvironmentConfigurations environmentConfigurations = new EnvironmentConfigurations();
Mockito.when(apimConfigurationService.getEnvironmentConfigurations()).thenReturn(environmentConfigurations);
APIMAppConfigurationService apimAppConfigurationService = Mockito.mock(APIMAppConfigurationService.class);
APIMAppConfigurations apimAppConfigurations = new APIMAppConfigurations();
Mockito.when(apimAppConfigurationService.getApimAppConfigurations()).thenReturn(apimAppConfigurations);
SystemApplicationDao systemApplicationDao = Mockito.mock(SystemApplicationDao.class);
KeyManager keyManager = Mockito.mock(KeyManager.class);
AuthenticatorService authenticatorService = new AuthenticatorService(keyManager, systemApplicationDao, apimConfigurationService, apimAppConfigurationService);
// // empty string for first value in allowed list
environmentConfigurations.setAllowedHosts(Collections.singletonList(""));
apimAppConfigurations.setApimBaseUrl("https://localhost:9443/");
URI expectedUri = new URI("https://localhost:9443/publisher");
URI actualUri = authenticatorService.getUIServiceRedirectionURI("publisher", null);
Assert.assertEquals(expectedUri, actualUri);
// // SSO callback
AuthResponseBean authResponseBean = new AuthResponseBean();
authResponseBean.setTokenValid(true);
authResponseBean.setType("bearer");
authResponseBean.setScopes("xxx-scopes-xxx");
authResponseBean.setValidityPeriod(3449);
authResponseBean.setAuthUser("admin");
authResponseBean.setIdToken("xxx-id-token-xxx");
authResponseBean.setPartialToken("xxx-partial-token-xxx");
expectedUri = new URI("https://localhost:9443/publisher/login?user_name=admin&id_token=xxx-id-token-xxx&" + "partial_token=xxx-partial-token-xxx&scopes=xxx-scopes-xxx&validity_period=3449");
actualUri = authenticatorService.getUIServiceRedirectionURI("publisher", authResponseBean);
Assert.assertEquals(expectedUri, actualUri);
// // non empty string for first value in allowed list
environmentConfigurations.setAllowedHosts(Arrays.asList("localhost:9444", "localhost: 9445", "localhost:9446"));
expectedUri = new URI("https://localhost:9444/publisher");
actualUri = authenticatorService.getUIServiceRedirectionURI("publisher", null);
Assert.assertEquals(expectedUri, actualUri);
}
use of org.wso2.carbon.apimgt.core.dao.SystemApplicationDao in project carbon-apimgt by wso2.
the class AuthenticatorServiceTestCase method testSetupRefreshTokenParts.
@Test
public void testSetupRefreshTokenParts() {
// Happy Path
APIMConfigurationService apimConfigurationService = Mockito.mock(APIMConfigurationService.class);
EnvironmentConfigurations environmentConfigurations = new EnvironmentConfigurations();
Mockito.when(apimConfigurationService.getEnvironmentConfigurations()).thenReturn(environmentConfigurations);
APIMAppConfigurationService apimAppConfigurationService = Mockito.mock(APIMAppConfigurationService.class);
SystemApplicationDao systemApplicationDao = Mockito.mock(SystemApplicationDao.class);
KeyManager keyManager = Mockito.mock(KeyManager.class);
AuthenticatorService authenticatorService = new AuthenticatorService(keyManager, systemApplicationDao, apimConfigurationService, apimAppConfigurationService);
Map<String, NewCookie> cookies = new HashMap<>();
Map<String, String> contextPaths = new HashMap<>();
contextPaths.put("APP_CONTEXT", "/store");
contextPaths.put("LOGOUT_CONTEXT", "/login/logout/store");
contextPaths.put("LOGIN_CONTEXT", "/login/token/store");
contextPaths.put("REST_API_CONTEXT", "/api/am/store");
String refreshToken = "xxx-refresh_token_part_1-xxx-xxx-refresh_token_part_2-xxx-";
environmentConfigurations.setEnvironmentLabel("Development");
Map<String, NewCookie> expectedCookies = new HashMap<>();
expectedCookies.put("APP_CONTEXT", new NewCookie("WSO2_AM_REFRESH_TOKEN_1_Development", "xxx-refresh_token_part_1-xxx-; path=/store; Secure; "));
expectedCookies.put("LOGIN_CONTEXT", new NewCookie("WSO2_AM_REFRESH_TOKEN_2_Development", "xxx-refresh_token_part_2-xxx-; path=/login/token/store; HttpOnly; Secure; "));
authenticatorService.setupRefreshTokenParts(cookies, refreshToken, contextPaths);
Assert.assertEquals(expectedCookies, cookies);
}
Aggregations