Search in sources :

Example 86 with Scope

use of org.wso2.ballerinalang.compiler.semantics.model.Scope in project carbon-apimgt by wso2.

the class APIPublisherImpl method saveSwagger20Definition.

/**
 * {@inheritDoc}
 */
@Override
public void saveSwagger20Definition(String apiId, String jsonText) throws APIManagementException {
    try {
        LocalDateTime localDateTime = LocalDateTime.now();
        Map<String, String> oldScopes = apiDefinitionFromSwagger20.getScopesFromSecurityDefinition(getApiSwaggerDefinition(apiId));
        Map<String, String> newScopes = apiDefinitionFromSwagger20.getScopesFromSecurityDefinition(jsonText);
        Map<String, String> updatedScopes = new HashMap<>(newScopes);
        updatedScopes.keySet().retainAll(oldScopes.keySet());
        oldScopes.keySet().removeAll(updatedScopes.keySet());
        newScopes.keySet().removeAll(updatedScopes.keySet());
        for (Map.Entry<String, String> scopeEntry : newScopes.entrySet()) {
            getKeyManager().registerScope(new Scope(scopeEntry.getKey(), scopeEntry.getValue()));
        }
        for (Map.Entry<String, String> scopeEntry : oldScopes.entrySet()) {
            getKeyManager().deleteScope(scopeEntry.getKey());
        }
        for (Map.Entry<String, String> scopeEntry : updatedScopes.entrySet()) {
            Scope scope = getKeyManager().retrieveScope(scopeEntry.getKey());
            scope.setDescription(scopeEntry.getValue());
            getKeyManager().updateScope(scope);
        }
        API api = getAPIbyUUID(apiId);
        Map<String, UriTemplate> oldUriTemplateMap = api.getUriTemplates();
        List<APIResource> apiResourceList = apiDefinitionFromSwagger20.parseSwaggerAPIResources(new StringBuilder(jsonText));
        Map<String, UriTemplate> updatedUriTemplateMap = new HashMap<>();
        for (APIResource apiResource : apiResourceList) {
            updatedUriTemplateMap.put(apiResource.getUriTemplate().getTemplateId(), apiResource.getUriTemplate());
        }
        Map<String, UriTemplate> uriTemplateMapNeedTobeUpdate = APIUtils.getMergedUriTemplates(oldUriTemplateMap, updatedUriTemplateMap);
        API.APIBuilder apiBuilder = new API.APIBuilder(api);
        apiBuilder.uriTemplates(uriTemplateMapNeedTobeUpdate);
        createUriTemplateList(apiBuilder, true);
        apiBuilder.updatedBy(getUsername());
        apiBuilder.lastUpdatedTime(localDateTime);
        api = apiBuilder.build();
        GatewaySourceGenerator gatewaySourceGenerator = getGatewaySourceGenerator();
        APIConfigContext apiConfigContext = new APIConfigContext(apiBuilder.build(), config.getGatewayPackageName());
        gatewaySourceGenerator.setApiConfigContext(apiConfigContext);
        String existingGatewayConfig = getApiGatewayConfig(apiId);
        String updatedGatewayConfig = gatewaySourceGenerator.getGatewayConfigFromSwagger(existingGatewayConfig, jsonText);
        getApiDAO().updateAPI(apiId, api);
        getApiDAO().updateApiDefinition(apiId, jsonText, getUsername());
        getApiDAO().updateGatewayConfig(apiId, updatedGatewayConfig, getUsername());
    } catch (APIMgtDAOException e) {
        String errorMsg = "Couldn't update the Swagger Definition";
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, e.getErrorHandler());
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) HashMap(java.util.HashMap) APIResource(org.wso2.carbon.apimgt.core.models.APIResource) UriTemplate(org.wso2.carbon.apimgt.core.models.UriTemplate) GatewaySourceGenerator(org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator) Scope(org.wso2.carbon.apimgt.core.models.Scope) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) API(org.wso2.carbon.apimgt.core.models.API) Map(java.util.Map) HashMap(java.util.HashMap) APIConfigContext(org.wso2.carbon.apimgt.core.template.APIConfigContext)

Example 87 with Scope

use of org.wso2.ballerinalang.compiler.semantics.model.Scope in project carbon-apimgt by wso2.

the class DefaultScopeRegistrationImplTest method getScopeInfo.

private ScopeInfo getScopeInfo(Scope scope) {
    ScopeInfo scopeInfo = new ScopeInfo();
    scopeInfo.setName(scope.getName());
    scopeInfo.setDescription(scope.getDescription());
    scopeInfo.setBindings(scope.getBindings());
    return scopeInfo;
}
Also used : ScopeInfo(org.wso2.carbon.apimgt.core.auth.dto.ScopeInfo)

Example 88 with Scope

use of org.wso2.ballerinalang.compiler.semantics.model.Scope in project carbon-apimgt by wso2.

the class DefaultScopeRegistrationImplTest method testUpdateScope.

@Test
public void testUpdateScope() throws KeyManagementException {
    DefaultScopeRegistrationServiceStub defaultScopeRegistrationServiceStub = Mockito.mock(DefaultScopeRegistrationServiceStub.class);
    DefaultScopeRegistrationImpl defaultScopeRegistration = new DefaultScopeRegistrationImpl(defaultScopeRegistrationServiceStub);
    ScopeInfo scopeInfo = new ScopeInfo();
    scopeInfo.setDescription("cde");
    scopeInfo.setDisplayName("abc");
    Scope scope = new Scope();
    scope.setName("abc");
    scope.setDescription("cde");
    Mockito.when(defaultScopeRegistrationServiceStub.updateScope(Mockito.any(ScopeInfo.class), Mockito.anyString())).thenReturn(Response.builder().status(200).headers(new HashMap<>()).body(new Gson().toJson(scopeInfo), feign.Util.UTF_8).build());
    boolean status = defaultScopeRegistration.updateScope(scope);
    Assert.assertTrue(status);
}
Also used : Scope(org.wso2.carbon.apimgt.core.models.Scope) HashMap(java.util.HashMap) Gson(com.google.gson.Gson) ScopeInfo(org.wso2.carbon.apimgt.core.auth.dto.ScopeInfo) Test(org.testng.annotations.Test)

Example 89 with Scope

use of org.wso2.ballerinalang.compiler.semantics.model.Scope in project carbon-apimgt by wso2.

the class DefaultScopeRegistrationImplTest method testScopeRegistrationFailed.

@Test
public void testScopeRegistrationFailed() {
    DefaultScopeRegistrationServiceStub defaultScopeRegistrationServiceStub = Mockito.mock(DefaultScopeRegistrationServiceStub.class);
    DefaultScopeRegistrationImpl defaultScopeRegistration = new DefaultScopeRegistrationImpl(defaultScopeRegistrationServiceStub);
    ScopeInfo scopeInfo = new ScopeInfo();
    scopeInfo.setName("abc");
    scopeInfo.setDescription("cde");
    Mockito.when(defaultScopeRegistrationServiceStub.registerScope(scopeInfo)).thenReturn(Response.builder().status(400).headers(new HashMap<>()).body(new Gson().toJson(scopeInfo), feign.Util.UTF_8).build());
    Scope scope = new Scope();
    scope.setName("abc");
    scope.setDescription("cde");
    try {
        defaultScopeRegistration.registerScope(scope);
        Assert.fail();
    } catch (KeyManagementException e) {
        Assert.assertEquals(e.getMessage(), "Scope Registration Failed");
    }
}
Also used : Scope(org.wso2.carbon.apimgt.core.models.Scope) HashMap(java.util.HashMap) Gson(com.google.gson.Gson) ScopeInfo(org.wso2.carbon.apimgt.core.auth.dto.ScopeInfo) KeyManagementException(org.wso2.carbon.apimgt.core.exception.KeyManagementException) Test(org.testng.annotations.Test)

Example 90 with Scope

use of org.wso2.ballerinalang.compiler.semantics.model.Scope in project carbon-apimgt by wso2.

the class AuthenticatorService method getApplicationScopes.

/**
 * This method returns the scopes for a given application.
 *
 * @param appName Name the application
 * @return scopes - A space separated list of scope keys
 * @throws APIManagementException When retrieving scopes from swagger definition fails
 */
private String getApplicationScopes(String appName) throws APIManagementException {
    String scopes;
    String applicationRestAPI = null;
    if (AuthenticatorConstants.STORE_APPLICATION.equals(appName)) {
        applicationRestAPI = RestApiUtil.getStoreRestAPIResource();
    } else if (AuthenticatorConstants.PUBLISHER_APPLICATION.equals(appName)) {
        applicationRestAPI = RestApiUtil.getPublisherRestAPIResource();
    } else if (AuthenticatorConstants.ADMIN_APPLICATION.equals(appName)) {
        applicationRestAPI = RestApiUtil.getAdminRestAPIResource();
    }
    try {
        if (applicationRestAPI != null) {
            APIDefinition apiDefinitionFromSwagger20 = new APIDefinitionFromSwagger20();
            Map<String, Scope> applicationScopesMap = apiDefinitionFromSwagger20.getScopesFromSecurityDefinitionForWebApps(applicationRestAPI);
            scopes = String.join(" ", applicationScopesMap.keySet());
            // Set openid scope
            if (StringUtils.isEmpty(scopes)) {
                scopes = KeyManagerConstants.OPEN_ID_CONNECT_SCOPE;
            } else {
                scopes = scopes + " " + KeyManagerConstants.OPEN_ID_CONNECT_SCOPE;
            }
        } else {
            String errorMsg = "Error while getting application rest API resource.";
            log.error(errorMsg, ExceptionCodes.INTERNAL_ERROR);
            throw new APIManagementException(errorMsg, ExceptionCodes.INTERNAL_ERROR);
        }
    } catch (APIManagementException e) {
        String errorMsg = "Error while reading scopes from swagger definition.";
        log.error(errorMsg, e, ExceptionCodes.INTERNAL_ERROR);
        throw new APIManagementException(errorMsg, e, ExceptionCodes.INTERNAL_ERROR);
    }
    return scopes;
}
Also used : Scope(org.wso2.carbon.apimgt.core.models.Scope) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) APIDefinition(org.wso2.carbon.apimgt.core.api.APIDefinition) APIDefinitionFromSwagger20(org.wso2.carbon.apimgt.core.impl.APIDefinitionFromSwagger20)

Aggregations

Scope (org.wso2.carbon.apimgt.core.models.Scope)41 HashMap (java.util.HashMap)25 RestVariable (org.wso2.carbon.bpmn.rest.engine.variable.RestVariable)25 Test (org.testng.annotations.Test)23 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)19 Response (javax.ws.rs.core.Response)16 ScopeInfo (org.wso2.carbon.apimgt.core.auth.dto.ScopeInfo)15 FileInputStream (java.io.FileInputStream)14 API (org.wso2.carbon.apimgt.core.models.API)14 ArrayList (java.util.ArrayList)13 KeyManager (org.wso2.carbon.apimgt.core.api.KeyManager)13 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)13 Map (java.util.Map)12 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)12 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)12 GatewaySourceGenerator (org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator)12 IdentityProvider (org.wso2.carbon.apimgt.core.api.IdentityProvider)11 RestResponseFactory (org.wso2.carbon.bpmn.rest.common.RestResponseFactory)11 ActivitiObjectNotFoundException (org.activiti.engine.ActivitiObjectNotFoundException)10 Scope (org.wso2.ballerinalang.compiler.semantics.model.Scope)10