Search in sources :

Example 1 with KeyManagerConfiguration

use of org.wso2.carbon.apimgt.api.model.KeyManagerConfiguration in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisApiIdSwaggerPut.

/**
 * Updates the swagger defnition of an API
 *
 * @param apiId             UUID of API
 * @param apiDefinition     updated swagger defintion
 * @param ifMatch           If-Match header value
 * @param ifUnmodifiedSince If-Unmodified-Since header value
 * @param request           msf4j request object
 * @return Updated swagger definition
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response apisApiIdSwaggerPut(String apiId, String apiDefinition, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIPublisher apiPublisher = RestAPIPublisherUtil.getApiPublisher(username);
        String existingFingerprint = apisApiIdSwaggerGetFingerprint(apiId, null, null, request);
        if (!StringUtils.isEmpty(ifMatch) && !StringUtils.isEmpty(existingFingerprint) && !ifMatch.contains(existingFingerprint)) {
            return Response.status(Response.Status.PRECONDITION_FAILED).build();
        }
        KeyMgtConfigurations keyManagerConfiguration = APIMConfigurationService.getInstance().getApimConfigurations().getKeyManagerConfigs();
        Map<String, String> scopes = new APIDefinitionFromSwagger20().getScopesFromSecurityDefinition(apiDefinition);
        for (String scopeName : scopes.keySet()) {
            if (scopeName.contains(keyManagerConfiguration.getProductRestApiScopesKeyWord())) {
                String message = "scope name couldn't have the restricted keyword " + keyManagerConfiguration.getProductRestApiScopesKeyWord();
                ErrorDTO errorDTO = RestApiUtil.getErrorDTO(message, 900313L, message);
                return Response.status(Response.Status.PRECONDITION_FAILED).entity(errorDTO).build();
            }
        }
        apiPublisher.saveSwagger20Definition(apiId, apiDefinition);
        String apiSwagger = apiPublisher.getApiSwaggerDefinition(apiId);
        String newFingerprint = apisApiIdSwaggerGetFingerprint(apiId, null, null, request);
        return Response.ok().header(HttpHeaders.ETAG, "\"" + newFingerprint + "\"").entity(apiSwagger).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while put swagger for API : " + apiId;
        HashMap<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : KeyMgtConfigurations(org.wso2.carbon.apimgt.core.configuration.models.KeyMgtConfigurations) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) APIDefinitionFromSwagger20(org.wso2.carbon.apimgt.core.impl.APIDefinitionFromSwagger20) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher)

Example 2 with KeyManagerConfiguration

use of org.wso2.carbon.apimgt.api.model.KeyManagerConfiguration in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisApiIdScopesNameGet.

@Override
public Response apisApiIdScopesNameGet(String apiId, String name, String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIPublisher apiPublisher = RestAPIPublisherUtil.getApiPublisher(username);
        Scope scope = apiPublisher.getScopeInformationOfApi(apiId, name);
        KeyMgtConfigurations keyManagerConfiguration = APIMConfigurationService.getInstance().getApimConfigurations().getKeyManagerConfigs();
        ScopeDTO scopeDTO = MappingUtil.scopeDto(scope, keyManagerConfiguration.getScopeBindingType());
        return Response.ok().entity(scopeDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving swagger definition of API : " + apiId;
        HashMap<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : Scope(org.wso2.carbon.apimgt.core.models.Scope) KeyMgtConfigurations(org.wso2.carbon.apimgt.core.configuration.models.KeyMgtConfigurations) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ScopeDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.ScopeDTO) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher)

Example 3 with KeyManagerConfiguration

use of org.wso2.carbon.apimgt.api.model.KeyManagerConfiguration in project carbon-apimgt by wso2.

the class ScopeRegistrationServiceStubFactoryTest method testGetScopeRegistrationForWso2Is.

@Test
public void testGetScopeRegistrationForWso2Is() throws Exception {
    KeyMgtConfigurations keyManagerConfiguration = new KeyMgtConfigurations();
    keyManagerConfiguration.setKeyManagerImplClass(WSO2ISKeyManagerImpl.class.getCanonicalName());
    ConfigProvider configProvider = Mockito.mock(ConfigProvider.class);
    APIMConfigurations apimConfigurations = new APIMConfigurations();
    apimConfigurations.setKeyManagerConfigs(keyManagerConfiguration);
    Mockito.when(configProvider.getConfigurationObject(APIMConfigurations.class)).thenReturn(apimConfigurations);
    ServiceReferenceHolder.getInstance().setConfigProvider(configProvider);
    ScopeRegistration scopeRegistration = ScopeRegistrationServiceStubFactory.getScopeRegistration();
    Assert.assertTrue(scopeRegistration instanceof WSO2ISScopeRegistrationImpl);
}
Also used : KeyMgtConfigurations(org.wso2.carbon.apimgt.core.configuration.models.KeyMgtConfigurations) ConfigProvider(org.wso2.carbon.config.provider.ConfigProvider) APIMConfigurations(org.wso2.carbon.apimgt.core.configuration.models.APIMConfigurations) WSO2ISKeyManagerImpl(org.wso2.carbon.apimgt.core.impl.WSO2ISKeyManagerImpl) Test(org.testng.annotations.Test)

Example 4 with KeyManagerConfiguration

use of org.wso2.carbon.apimgt.api.model.KeyManagerConfiguration in project carbon-apimgt by wso2.

the class AbstractKeyManagerTestCase method testCanHandleTokenWithConfigurationJWTAndOpaueDisableOne.

@Test
public void testCanHandleTokenWithConfigurationJWTAndOpaueDisableOne() throws APIManagementException {
    KeyManagerConfiguration keyManagerConfiguration = new KeyManagerConfiguration();
    keyManagerConfiguration.addParameter(APIConstants.KeyManager.TOKEN_FORMAT_STRING, "[{\"enable\": true,\"type\": \"JWT\",\"value\": {\"body\": {\"iss\": \"https://localhost:9443\"}}}," + "{\"enable\": false,\"type\": \"REFERENCE\",\"value\": " + "\"[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0" + "-9a-fA-F]{3}-[89ab][0-9a-fA-F]{3}-[0-9a-fA-F]{12}\"}]");
    KeyManager keyManager = new ModelKeyManagerForTest();
    keyManager.loadConfiguration(keyManagerConfiguration);
    assertTrue(keyManager.canHandleToken("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9" + ".eyJpc3MiOiJodHRwczovL2xvY2FsaG9zdDo5NDQzIiwiaWF0IjoxNTkwMTM0NzIyLCJleHAiOjE2MjE2NzA3MjAsImF1ZC" + "I6Ind3dy5leGFtcGxlLmNvbSIsInN1YiI6Impyb2NrZXRAZXhhbXBsZS5jb20iLCJFbWFpbCI6ImJlZUBleGFtcGxlLmNvb" + "SJ9.HIxL7_WqeLPkxYdROAwRyL0YEY1YNJRfLghsaHEc7C4"));
    assertFalse(keyManager.canHandleToken(UUID.randomUUID().toString()));
}
Also used : ModelKeyManagerForTest(org.wso2.carbon.apimgt.impl.factory.ModelKeyManagerForTest) KeyManager(org.wso2.carbon.apimgt.api.model.KeyManager) KeyManagerConfiguration(org.wso2.carbon.apimgt.api.model.KeyManagerConfiguration) Test(org.junit.Test) ModelKeyManagerForTest(org.wso2.carbon.apimgt.impl.factory.ModelKeyManagerForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 5 with KeyManagerConfiguration

use of org.wso2.carbon.apimgt.api.model.KeyManagerConfiguration in project carbon-apimgt by wso2.

the class AbstractKeyManagerTestCase method testCanHandleTokenWithConfigurationJWTMultipleClaim.

@Test
public void testCanHandleTokenWithConfigurationJWTMultipleClaim() throws APIManagementException {
    KeyManagerConfiguration keyManagerConfiguration = new KeyManagerConfiguration();
    keyManagerConfiguration.addParameter(APIConstants.KeyManager.TOKEN_FORMAT_STRING, "[{\"enable\": true,\"type\": \"JWT\",\"value\": {\"body\": {\"iss\": \"https://localhost:9443\"," + "\"domain\": \"abc.com\"}}}]");
    KeyManager keyManager = new ModelKeyManagerForTest();
    keyManager.loadConfiguration(keyManagerConfiguration);
    assertTrue(keyManager.canHandleToken("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL2xvY2FsaG9zdDo5NDQ0IiwiaWF0IjoxN" + "TkwMTM0NzIyLCJleHAiOjE2MjE2NzA3MjAsImF1ZCI6Ind3dy5leGFtcGxlLmNvbSIsInN1YiI6Impyb2NrZXRAZXhhbX" + "BsZS5jb20iLCJkb21haW4iOiJhYmMuY29tIn0.pHI2MUhvdGjcOj2yJ-05cHMwtx5kanMhO71m0wFhjic"));
    assertFalse(keyManager.canHandleToken("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9" + ".eyJpc3MiOiJodHRwczovL2xvY2FsaG9zdDo5NDQzIiwiaWF0IjoxNTkwMTM0NzIyLCJleHAiOjE2MjE2NzA3MjAsImF1ZC" + "I6Ind3dy5leGFtcGxlLmNvbSIsInN1YiI6Impyb2NrZXRAZXhhbXBsZS5jb20iLCJFbWFpbCI6ImJlZUBleGFtcGxlLmNvb" + "SJ9.HIxL7_WqeLPkxYdROAwRyL0YEY1YNJRfLghsaHEc7C4"));
}
Also used : ModelKeyManagerForTest(org.wso2.carbon.apimgt.impl.factory.ModelKeyManagerForTest) KeyManager(org.wso2.carbon.apimgt.api.model.KeyManager) KeyManagerConfiguration(org.wso2.carbon.apimgt.api.model.KeyManagerConfiguration) Test(org.junit.Test) ModelKeyManagerForTest(org.wso2.carbon.apimgt.impl.factory.ModelKeyManagerForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

KeyManager (org.wso2.carbon.apimgt.api.model.KeyManager)14 KeyManagerConfiguration (org.wso2.carbon.apimgt.api.model.KeyManagerConfiguration)14 KeyManagerConfigurationDTO (org.wso2.carbon.apimgt.api.dto.KeyManagerConfigurationDTO)12 Test (org.junit.Test)11 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)11 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)10 ModelKeyManagerForTest (org.wso2.carbon.apimgt.impl.factory.ModelKeyManagerForTest)8 HashMap (java.util.HashMap)7 OAuthAppRequest (org.wso2.carbon.apimgt.api.model.OAuthAppRequest)7 OAuthApplicationInfo (org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo)7 JSONObject (org.json.simple.JSONObject)4 KeyMgtConfigurations (org.wso2.carbon.apimgt.core.configuration.models.KeyMgtConfigurations)4 Gson (com.google.gson.Gson)3 AccessTokenInfo (org.wso2.carbon.apimgt.api.model.AccessTokenInfo)3 AccessTokenRequest (org.wso2.carbon.apimgt.api.model.AccessTokenRequest)3 Application (org.wso2.carbon.apimgt.api.model.Application)3 Subscriber (org.wso2.carbon.apimgt.api.model.Subscriber)3 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2