use of org.wso2.carbon.apimgt.core.models.OAuthApplicationInfo in project carbon-apimgt by wso2.
the class ApplicationKeyMappingUtilTestCase method testFromApplicationKeysToDTO.
@Test
public void testFromApplicationKeysToDTO() {
String keyType = "PRODUCTION";
List<String> grantTypes = new ArrayList<>();
grantTypes.add("password");
grantTypes.add("jwt");
OAuthApplicationInfo oAuthApplicationInfo = new OAuthApplicationInfo();
oAuthApplicationInfo.setKeyType(keyType);
oAuthApplicationInfo.setClientId("clientID");
oAuthApplicationInfo.setClientSecret("clientSecret");
oAuthApplicationInfo.setGrantTypes(grantTypes);
ApplicationKeysDTO applicationKeysDTO = ApplicationKeyMappingUtil.fromApplicationKeysToDTO(oAuthApplicationInfo);
Assert.assertEquals(applicationKeysDTO.getKeyType().toString(), keyType);
}
use of org.wso2.carbon.apimgt.core.models.OAuthApplicationInfo in project carbon-apimgt by wso2.
the class APIStoreImpl method getApplicationKeys.
@Override
public List<OAuthApplicationInfo> getApplicationKeys(String applicationId) throws APIManagementException {
if (log.isDebugEnabled()) {
log.debug("Getting keys of App: " + applicationId);
}
if (StringUtils.isEmpty(applicationId)) {
String msg = "Input value is null or empty. Application Id: " + applicationId;
log.error(msg);
throw new APIManagementException(msg, ExceptionCodes.OAUTH2_APP_RETRIEVAL_FAILED);
}
try {
List<OAuthApplicationInfo> allKeysFromDB = getApplicationDAO().getApplicationKeys(applicationId);
for (OAuthApplicationInfo keys : allKeysFromDB) {
OAuthApplicationInfo oAuthApp = getKeyManager().retrieveApplication(keys.getClientId());
keys.setClientSecret(oAuthApp.getClientSecret());
keys.setGrantTypes(oAuthApp.getGrantTypes());
keys.setCallBackURL(oAuthApp.getCallBackURL());
}
if (log.isDebugEnabled()) {
log.debug("Retrieved all keys of App: " + applicationId);
}
return allKeysFromDB;
} catch (APIMgtDAOException e) {
String errorMsg = "Error occurred while getting keys of application: " + applicationId;
log.error(errorMsg, e);
throw new APIManagementException(errorMsg, e, e.getErrorHandler());
}
}
use of org.wso2.carbon.apimgt.core.models.OAuthApplicationInfo in project carbon-apimgt by wso2.
the class APIStoreImpl method generateApplicationKeys.
@Override
public OAuthApplicationInfo generateApplicationKeys(String applicationId, String keyType, String callbackUrl, List<String> grantTypes) throws APIManagementException {
if (log.isDebugEnabled()) {
log.debug("Generating application keys for application: " + applicationId);
}
Application application = getApplicationByUuid(applicationId);
OAuthAppRequest oauthAppRequest = new OAuthAppRequest(application.getName(), callbackUrl, keyType, grantTypes);
OAuthApplicationInfo oauthAppInfo = getKeyManager().createApplication(oauthAppRequest);
if (log.isDebugEnabled()) {
log.debug("Application key generation was successful for application: " + application.getName() + " Client Id: " + oauthAppInfo.getClientId());
}
try {
getApplicationDAO().addApplicationKeys(applicationId, keyType, oauthAppInfo.getClientId());
} catch (APIMgtDAOException e) {
String errorMsg = "Error occurred while saving key data for application: " + application.getName();
log.error(errorMsg, e);
throw new APIManagementException(errorMsg, e, e.getErrorHandler());
}
if (log.isDebugEnabled()) {
log.debug("Application keys are successfully saved in the database for application: " + application.getName() + " Client Id: " + oauthAppInfo.getClientId());
}
List<SubscriptionValidationData> subscriptionValidationData = getApiSubscriptionDAO().getAPISubscriptionsOfAppForValidation(applicationId, keyType);
if (subscriptionValidationData != null && !subscriptionValidationData.isEmpty()) {
getApiGateway().addAPISubscription(subscriptionValidationData);
}
return oauthAppInfo;
}
use of org.wso2.carbon.apimgt.core.models.OAuthApplicationInfo in project carbon-apimgt by wso2.
the class ApplicationDAOImplIT method testAddAndGetApplicationKeys.
@Test
public void testAddAndGetApplicationKeys() throws Exception {
ApplicationDAO applicationDAO = DAOFactory.getApplicationDAO();
// add test app
Application app = TestUtil.addTestApplicationWithPermissions();
String appId = app.getId();
String prodConsumerKey = "prod-xxx";
String sandConsumerKey = "sand-yyy";
// add prod key
applicationDAO.addApplicationKeys(appId, KeyManagerConstants.OAUTH_CLIENT_PRODUCTION, prodConsumerKey);
// get by key type
OAuthApplicationInfo keysFromDB = applicationDAO.getApplicationKeys(appId, KeyManagerConstants.OAUTH_CLIENT_PRODUCTION);
Assert.assertEquals(keysFromDB.getClientId(), prodConsumerKey);
// add sand key
applicationDAO.addApplicationKeys(appId, KeyManagerConstants.OAUTH_CLIENT_SANDBOX, sandConsumerKey);
// get all keys
List<OAuthApplicationInfo> allKeysFromDB = applicationDAO.getApplicationKeys(appId);
Assert.assertEquals(allKeysFromDB.size(), 2, "Wrong number of keys are returned.");
// this should stay 0 at the end
int i = 0;
for (OAuthApplicationInfo oAuthApplicationInfo : allKeysFromDB) {
switch(oAuthApplicationInfo.getKeyType()) {
case KeyManagerConstants.OAUTH_CLIENT_PRODUCTION:
Assert.assertEquals(oAuthApplicationInfo.getClientId(), prodConsumerKey);
i++;
break;
case KeyManagerConstants.OAUTH_CLIENT_SANDBOX:
Assert.assertEquals(oAuthApplicationInfo.getClientId(), sandConsumerKey);
i--;
break;
default:
Assert.fail("Invalid key type.");
break;
}
}
Assert.assertEquals(i, 0, "Received key counts of each type is not 1");
}
use of org.wso2.carbon.apimgt.core.models.OAuthApplicationInfo in project carbon-apimgt by wso2.
the class AuthenticatorServiceTestCase method testGetAuthenticationConfigurationsForPublisher.
@Test
public void testGetAuthenticationConfigurationsForPublisher() 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/publisher");
// // Expected data object to be passed to the front-end
JsonObject oAuthData = new JsonObject();
String scopes = "apim:api_view apim:api_create apim:api_update apim:api_delete apim:apidef_update " + "apim:api_publish apim:subscription_view apim:subscription_block openid " + "apim:external_services_discover apim:dedicated_gateway";
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:9443/oauth2/authorize");
oAuthData.addProperty(AuthenticatorConstants.SSO_ENABLED, ServiceReferenceHolder.getInstance().getAPIMAppConfiguration().isSsoEnabled());
KeyManager keyManager = Mockito.mock(KeyManager.class);
MultiEnvironmentOverview multiEnvironmentOverview = new MultiEnvironmentOverview();
environmentConfigurations.setMultiEnvironmentOverview(multiEnvironmentOverview);
multiEnvironmentOverview.setEnabled(true);
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("publisher");
String[] scopesActual = responseOAuthDataObj.get(KeyManagerConstants.TOKEN_SCOPES).toString().split(" ");
String[] scopesExpected = oAuthData.get(KeyManagerConstants.TOKEN_SCOPES).toString().split(" ");
Assert.assertEquals(scopesActual.length, scopesExpected.length);
// Error Path - 500 - When OAuthApplicationInfo is null
JsonObject emptyOAuthDataObj = new JsonObject();
Mockito.when(keyManager.createApplication(Mockito.any())).thenReturn(null);
JsonObject responseEmptyOAuthDataObj = authenticatorService.getAuthenticationConfigurations("publisher");
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("publisher");
} catch (APIManagementException e) {
Assert.assertEquals(e.getMessage(), "Error while creating the keys for OAuth application : publisher");
}
}
Aggregations