use of org.wso2.carbon.apimgt.core.configuration.models.KeyMgtConfigurations 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();
}
}
use of org.wso2.carbon.apimgt.core.configuration.models.KeyMgtConfigurations 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();
}
}
use of org.wso2.carbon.apimgt.core.configuration.models.KeyMgtConfigurations 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);
}
use of org.wso2.carbon.apimgt.core.configuration.models.KeyMgtConfigurations in project carbon-apimgt by wso2.
the class APIDefinitionFromSwagger20 method addScopeToSwaggerDefinition.
@Override
public String addScopeToSwaggerDefinition(String resourceConfigJSON, Scope scope) {
KeyMgtConfigurations keyManagerConfigs = ServiceReferenceHolder.getInstance().getAPIMConfiguration().getKeyManagerConfigs();
SwaggerParser swaggerParser = new SwaggerParser();
Swagger swagger = swaggerParser.parse(resourceConfigJSON);
Map<String, SecuritySchemeDefinition> securitySchemeDefinitionMap = swagger.getSecurityDefinitions();
if (securitySchemeDefinitionMap != null && !securitySchemeDefinitionMap.isEmpty() && securitySchemeDefinitionMap.containsKey(APIMgtConstants.OAUTH2SECURITY)) {
OAuth2Definition oAuth2Definition = (OAuth2Definition) securitySchemeDefinitionMap.get(APIMgtConstants.OAUTH2SECURITY);
// Removing Scope from Swagger SecurityDefinition
Map<String, String> scopeMap = oAuth2Definition.getScopes();
if (scopeMap != null) {
scopeMap.put(scope.getName(), scope.getDescription());
}
} else {
OAuth2Definition oAuth2Definition = new OAuth2Definition();
oAuth2Definition.setType("oauth2");
oAuth2Definition.setFlow("password");
oAuth2Definition.setTokenUrl(keyManagerConfigs.getTokenEndpoint());
Map<String, String> scopes = new HashMap<>();
scopes.put(scope.getName(), scope.getDescription());
oAuth2Definition.setScopes(scopes);
if (securitySchemeDefinitionMap != null) {
securitySchemeDefinitionMap.put(APIMgtConstants.OAUTH2SECURITY, oAuth2Definition);
} else {
securitySchemeDefinitionMap = new HashMap<>();
securitySchemeDefinitionMap.put(APIMgtConstants.OAUTH2SECURITY, oAuth2Definition);
swagger.setSecurityDefinitions(securitySchemeDefinitionMap);
}
}
return Json.pretty(swagger);
}
use of org.wso2.carbon.apimgt.core.configuration.models.KeyMgtConfigurations in project carbon-apimgt by wso2.
the class APIDefinitionFromSwagger20 method addSecuritySchemeToSwaggerDefinition.
private void addSecuritySchemeToSwaggerDefinition(Swagger swagger, API api) {
KeyMgtConfigurations keyMgtConfigurations = ServiceReferenceHolder.getInstance().getAPIMConfiguration().getKeyManagerConfigs();
if ((api.getSecurityScheme() & 2) == 2) {
// apikey
log.debug("API security scheme : API Key Scheme");
if (swagger.getSecurityDefinitions() == null || !swagger.getSecurityDefinitions().containsKey(APIMgtConstants.SWAGGER_APIKEY)) {
swagger.securityDefinition(APIMgtConstants.SWAGGER_APIKEY, new ApiKeyAuthDefinition(APIMgtConstants.SWAGGER_APIKEY, In.HEADER));
}
}
if ((api.getSecurityScheme() & 1) == 1) {
log.debug("API security Scheme : Oauth");
OAuth2Definition oAuth2Definition = new OAuth2Definition();
oAuth2Definition = oAuth2Definition.application(keyMgtConfigurations.getTokenEndpoint());
oAuth2Definition.setScopes(Collections.emptyMap());
if (swagger.getSecurityDefinitions() == null || !swagger.getSecurityDefinitions().containsKey(APIMgtConstants.OAUTH2SECURITY)) {
swagger.securityDefinition(APIMgtConstants.OAUTH2SECURITY, oAuth2Definition);
}
}
}
Aggregations