use of org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo in project carbon-apimgt by wso2.
the class AuthenticatorService method getAuthenticationConfigurations.
/**
* This method returns the details of a DCR application.
*
* @param appName Name of the application to be created
* @return oAuthData - A JsonObject with DCR application details, scopes, auth endpoint, and SSO is enabled or not
* @throws APIManagementException When creating DCR application fails
*/
public JsonObject getAuthenticationConfigurations(String appName) throws APIManagementException {
JsonObject oAuthData = new JsonObject();
MultiEnvironmentOverview multiEnvironmentOverviewConfigs = apimConfigurationService.getEnvironmentConfigurations().getMultiEnvironmentOverview();
boolean isMultiEnvironmentOverviewEnabled = multiEnvironmentOverviewConfigs.isEnabled();
List<String> grantTypes = new ArrayList<>();
grantTypes.add(KeyManagerConstants.PASSWORD_GRANT_TYPE);
grantTypes.add(KeyManagerConstants.AUTHORIZATION_CODE_GRANT_TYPE);
grantTypes.add(KeyManagerConstants.REFRESH_GRANT_TYPE);
if (isMultiEnvironmentOverviewEnabled) {
grantTypes.add(KeyManagerConstants.JWT_GRANT_TYPE);
}
APIMAppConfigurations appConfigs = apimAppConfigurationService.getApimAppConfigurations();
String callBackURL = appConfigs.getApimBaseUrl() + AuthenticatorConstants.AUTHORIZATION_CODE_CALLBACK_URL + appName;
// Get scopes of the application
String scopes = getApplicationScopes(appName);
log.debug("Set scopes for {} application using swagger definition.", appName);
OAuthApplicationInfo oAuthApplicationInfo;
try {
oAuthApplicationInfo = createDCRApplication(appName, callBackURL, grantTypes);
if (oAuthApplicationInfo != null) {
log.debug("Created DCR Application successfully for {}.", appName);
String oAuthApplicationClientId = oAuthApplicationInfo.getClientId();
String oAuthApplicationCallBackURL = oAuthApplicationInfo.getCallBackURL();
oAuthData.addProperty(KeyManagerConstants.OAUTH_CLIENT_ID, oAuthApplicationClientId);
oAuthData.addProperty(KeyManagerConstants.OAUTH_CALLBACK_URIS, oAuthApplicationCallBackURL);
oAuthData.addProperty(KeyManagerConstants.TOKEN_SCOPES, scopes);
oAuthData.addProperty(KeyManagerConstants.AUTHORIZATION_ENDPOINT, appConfigs.getAuthorizationEndpoint());
oAuthData.addProperty(AuthenticatorConstants.SSO_ENABLED, appConfigs.isSsoEnabled());
oAuthData.addProperty(AuthenticatorConstants.MULTI_ENVIRONMENT_OVERVIEW_ENABLED, isMultiEnvironmentOverviewEnabled);
} else {
String errorMsg = "No information available in OAuth application.";
log.error(errorMsg, ExceptionCodes.OAUTH2_APP_CREATION_FAILED);
}
} catch (APIManagementException e) {
String errorMsg = "Error while creating the keys for OAuth application : " + appName;
log.error(errorMsg, e, ExceptionCodes.OAUTH2_APP_CREATION_FAILED);
throw new APIManagementException(errorMsg, e, ExceptionCodes.OAUTH2_APP_CREATION_FAILED);
}
return oAuthData;
}
use of org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo in project carbon-apimgt by wso2.
the class AbstractKeyManager method buildAccessTokenRequestFromOAuthApp.
public AccessTokenRequest buildAccessTokenRequestFromOAuthApp(OAuthApplicationInfo oAuthApplication, AccessTokenRequest tokenRequest) throws APIManagementException {
if (oAuthApplication == null) {
return tokenRequest;
}
if (tokenRequest == null) {
tokenRequest = new AccessTokenRequest();
}
if (oAuthApplication.getClientId() == null || oAuthApplication.getClientSecret() == null) {
throw new APIManagementException("Consumer key or Consumer Secret missing.");
}
tokenRequest.setClientId(oAuthApplication.getClientId());
tokenRequest.setClientSecret(oAuthApplication.getClientSecret());
if (oAuthApplication.getParameter("tokenScope") != null) {
String[] tokenScopes = (String[]) oAuthApplication.getParameter("tokenScope");
tokenRequest.setScope(tokenScopes);
oAuthApplication.addParameter("tokenScope", Arrays.toString(tokenScopes));
}
if (oAuthApplication.getParameter(ApplicationConstants.VALIDITY_PERIOD) != null) {
tokenRequest.setValidityPeriod(Long.parseLong((String) oAuthApplication.getParameter(ApplicationConstants.VALIDITY_PERIOD)));
}
return tokenRequest;
}
use of org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo in project carbon-apimgt by wso2.
the class APIConsumerImplTest method testRequestApprovalForApplicationRegistration.
@Test
public void testRequestApprovalForApplicationRegistration() throws APIManagementException, UserStoreException {
Scope scope1 = new Scope();
scope1.setName("api_view");
Scope scope2 = new Scope();
scope2.setName("api_create");
Set<Scope> scopes = new HashSet<Scope>();
scopes.add(scope1);
scopes.add(scope2);
PowerMockito.when(MultitenantUtils.getTenantDomain(Mockito.anyString())).thenReturn("abc.org");
KeyManagerConfigurationDTO keyManagerConfigurationsDto = new KeyManagerConfigurationDTO();
keyManagerConfigurationsDto.setEnabled(true);
Mockito.when(apiMgtDAO.getKeyManagerConfigurationByName("abc.org", "default")).thenReturn(keyManagerConfigurationsDto);
Mockito.when(tenantManager.getTenantId(Mockito.anyString())).thenThrow(UserStoreException.class).thenReturn(-1234, 1);
APIConsumerImpl apiConsumer = new APIConsumerImplWrapper(apiMgtDAO);
Application app = new Application("app1", new Subscriber("1"));
app.setGroupId("2");
app.setUUID(UUID.randomUUID().toString());
Mockito.when(userStoreManager.getRoleListOfUser(Mockito.anyString())).thenThrow(UserStoreException.class).thenReturn(new String[] { "role1", "role2" });
Application application = Mockito.mock(Application.class);
Subscriber subscriber = Mockito.mock(Subscriber.class);
Mockito.when(subscriber.getName()).thenReturn("1");
Mockito.when(application.getSubscriber()).thenReturn(subscriber);
Mockito.when(ApplicationUtils.retrieveApplication(Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn(application);
try {
apiConsumer.requestApprovalForApplicationRegistration("1", app, "access", "identity.com/auth", null, "3600", "api_view", null, "default", null, false);
Assert.fail("API management exception not thrown for invalid token type");
} catch (APIManagementException e) {
Assert.assertTrue(e.getMessage().contains("Invalid Token Type"));
}
scope1.setRoles("role1");
scope2.setRoles("role2");
OAuthApplicationInfo oAuthApplicationInfo = new OAuthApplicationInfo();
OAuthAppRequest oAuthAppRequest = new OAuthAppRequest();
oAuthAppRequest.setOAuthApplicationInfo(oAuthApplicationInfo);
application = new Application("app1", new Subscriber("1"));
BDDMockito.when(ApplicationUtils.createOauthAppRequest(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn(oAuthAppRequest);
BDDMockito.when(ApplicationUtils.retrieveApplication(Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn(application);
Map<String, Object> result = apiConsumer.requestApprovalForApplicationRegistration("1", app, APIConstants.API_KEY_TYPE_PRODUCTION, "identity.com/auth", null, "3600", "api_view", null, "default", null, false);
Assert.assertEquals(result.size(), 10);
Assert.assertEquals(result.get("keyState"), "APPROVED");
result = apiConsumer.requestApprovalForApplicationRegistration("1", app, APIConstants.API_KEY_TYPE_SANDBOX, "", null, "3600", "api_view", null, "default", null, false);
Assert.assertEquals(result.size(), 10);
Assert.assertEquals(result.get("keyState"), "APPROVED");
}
use of org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo in project carbon-apimgt by wso2.
the class APIConsumerImplTest method testMapExistingOAuthClient.
@Test
public void testMapExistingOAuthClient() throws APIManagementException {
APIConsumerImpl apiConsumer = new APIConsumerImplWrapper(apiMgtDAO);
apiConsumer.tenantDomain = "carbon.super";
OAuthApplicationInfo oAuthApplicationInfo = new OAuthApplicationInfo();
OAuthAppRequest oAuthAppRequest = new OAuthAppRequest();
oAuthAppRequest.setOAuthApplicationInfo(oAuthApplicationInfo);
BDDMockito.when(ApplicationUtils.createOauthAppRequest(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn(oAuthAppRequest);
Mockito.when(apiMgtDAO.isKeyMappingExistsForConsumerKeyOrApplication(Mockito.anyInt(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn(true, false);
Mockito.when(keyManager.mapOAuthApplication((OAuthAppRequest) Mockito.any())).thenReturn(oAuthApplicationInfo);
Mockito.doNothing().when(apiMgtDAO).createApplicationKeyTypeMappingForManualClients(Mockito.anyString(), Mockito.anyInt(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
KeyManagerConfigurationDTO keyManagerConfigurationsDto = new KeyManagerConfigurationDTO();
keyManagerConfigurationsDto.setUuid(UUID.randomUUID().toString());
keyManagerConfigurationsDto.setEnabled(true);
Mockito.when(apiMgtDAO.isKeyManagerConfigurationExistByName("default", "carbon.super")).thenReturn(true);
Mockito.when(apiMgtDAO.getKeyManagerConfigurationByName("carbon.super", "default")).thenReturn(keyManagerConfigurationsDto);
AccessTokenRequest accessTokenRequest = new AccessTokenRequest();
AccessTokenInfo accessTokenInfo = new AccessTokenInfo();
KeyManagerConfiguration keyManagerConfiguration = new KeyManagerConfiguration();
Mockito.when(keyManager.getKeyManagerConfiguration()).thenReturn(keyManagerConfiguration);
BDDMockito.when(ApplicationUtils.createAccessTokenRequest(keyManager, oAuthApplicationInfo, null)).thenReturn(accessTokenRequest);
Mockito.when(keyManager.getNewApplicationAccessToken(accessTokenRequest)).thenReturn(accessTokenInfo);
try {
apiConsumer.mapExistingOAuthClient("", "admin", "1", "app1", "refresh", "DEFAULT", "Resident Key Manager", "carbon.super");
Assert.fail("Exception is not thrown when client id is already mapped to an application");
} catch (APIManagementException e) {
Assert.assertTrue(e.getMessage().contains("Key Mappings already exists for application"));
}
Assert.assertEquals(8, apiConsumer.mapExistingOAuthClient("", "admin", "1", "app1", "PRODUCTION", "DEFAULT", "Resident Key Manager", "carbon.super").size());
}
use of org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo in project carbon-apimgt by wso2.
the class APIConsumerImplTest method testUpdateAuthClient.
@Test
public void testUpdateAuthClient() throws APIManagementException {
String consumerKey = "aNTf-EFga";
OAuthApplicationInfo oAuthApplicationInfo = new OAuthApplicationInfo();
OAuthAppRequest oAuthAppRequest = new OAuthAppRequest();
oAuthAppRequest.setOAuthApplicationInfo(oAuthApplicationInfo);
BDDMockito.when(ApplicationUtils.createOauthAppRequest(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn(oAuthAppRequest);
Mockito.when(apiMgtDAO.getConsumerKeyByApplicationIdKeyTypeKeyManager(Mockito.anyInt(), Mockito.anyString(), Mockito.anyString())).thenReturn(consumerKey);
OAuthApplicationInfo updatedAppInfo = new OAuthApplicationInfo();
String clientName = "sample client";
updatedAppInfo.setClientName(clientName);
Mockito.when(keyManager.updateApplication((OAuthAppRequest) Mockito.any())).thenReturn(updatedAppInfo);
KeyManagerConfigurationDTO keyManagerConfiguration = new KeyManagerConfigurationDTO();
keyManagerConfiguration.setEnabled(true);
Mockito.when(apiMgtDAO.getKeyManagerConfigurationByName(Mockito.anyString(), Mockito.anyString())).thenReturn(keyManagerConfiguration);
System.setProperty(CARBON_HOME, "");
APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
APIManagerConfigurationService apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
Mockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
Mockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.KEYMANAGER_SERVERURL)).thenReturn("http://localhost");
Application application = Mockito.mock(Application.class);
application.setUUID(UUID.nameUUIDFromBytes("app1".getBytes()).toString());
Subscriber subscriber = Mockito.mock(Subscriber.class);
Mockito.when(ApplicationUtils.retrieveApplication("app1", "1", null)).thenReturn(application);
Mockito.when(application.getSubscriber()).thenReturn(subscriber);
Mockito.when(subscriber.getName()).thenReturn("1");
APIConsumerImpl apiConsumer = new APIConsumerImplWrapper(apiMgtDAO);
apiConsumer.tenantDomain = SAMPLE_TENANT_DOMAIN_1;
Assert.assertEquals(apiConsumer.updateAuthClient("1", application, "access", "www.host.com", new String[0], null, null, null, null, "default").getClientName(), clientName);
}
Aggregations