use of org.wso2.carbon.apimgt.api.model.KeyManagerConfiguration in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method apisApiIdScopesPost.
@Override
public Response apisApiIdScopesPost(String apiId, ScopeDTO body, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
try {
String username = RestApiUtil.getLoggedInUsername(request);
APIPublisher apiPublisher = RestAPIPublisherUtil.getApiPublisher(username);
KeyMgtConfigurations keyManagerConfiguration = APIMConfigurationService.getInstance().getApimConfigurations().getKeyManagerConfigs();
if (body.getBindings() != null && StringUtils.isNotEmpty(body.getBindings().getType())) {
if (!keyManagerConfiguration.getScopeBindingType().equalsIgnoreCase(body.getBindings().getType())) {
String message = "binding type not valid";
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(message, 900313L, message);
return Response.status(Response.Status.PRECONDITION_FAILED).entity(errorDTO).build();
}
}
Scope scope = MappingUtil.toScope(body);
apiPublisher.addScopeToTheApi(apiId, scope);
URI location = new URI(RestApiConstants.RESOURCE_PATH_APIS + "/" + apiId + "/scopes/" + scope.getName());
return Response.created(location).entity(body).build();
} catch (APIManagementException e) {
String errorMessage = "Error while creating scope" + body.getName();
HashMap<String, String> paramList = new HashMap<String, String>();
paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
paramList.put(APIMgtConstants.ExceptionsConstants.SCOPE_NAME, body.getName());
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
} catch (URISyntaxException e) {
String errorMessage = "Error while retrieving source URI location of " + body.getName();
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorMessage, 900313L, errorMessage);
log.error(errorMessage, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorDTO).build();
}
}
use of org.wso2.carbon.apimgt.api.model.KeyManagerConfiguration 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.KeyManagerConfiguration 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);
}
use of org.wso2.carbon.apimgt.api.model.KeyManagerConfiguration in project carbon-apimgt by wso2.
the class AbstractKeyManagerTestCase method testCanHandleTokenEmptyConfiguration.
@Test
public void testCanHandleTokenEmptyConfiguration() throws APIManagementException {
KeyManagerConfiguration keyManagerConfiguration = new KeyManagerConfiguration();
KeyManager keyManager = new ModelKeyManagerForTest();
keyManagerConfiguration.addParameter(APIConstants.KeyManager.TOKEN_FORMAT_STRING, "[]");
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 testCanHandleTokenWithConfiguration.
@Test
public void testCanHandleTokenWithConfiguration() 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);
assertFalse(keyManager.canHandleToken(UUID.randomUUID().toString()));
}
Aggregations