use of org.wso2.carbon.apimgt.core.models.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());
}
}
use of org.wso2.carbon.apimgt.core.models.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;
}
use of org.wso2.carbon.apimgt.core.models.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);
}
use of org.wso2.carbon.apimgt.core.models.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");
}
}
use of org.wso2.carbon.apimgt.core.models.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;
}
Aggregations