use of org.wso2.carbon.identity.configuration.mgt.core.model.Resources in project carbon-apimgt by wso2.
the class DefaultIdentityProviderImpl method getIdOfUser.
@Override
public String getIdOfUser(String userName) throws IdentityProviderException {
// should not user id outside this domain and should not log that id.
try {
userName = userNameMapper.getLoggedInUserIDFromPseudoName(userName);
} catch (APIManagementException e) {
throw new IdentityProviderException(e.getMessage(), ExceptionCodes.USER_MAPPING_RETRIEVAL_FAILED);
}
Response userResponse = scimServiceStub.searchUsers(FILTER_PREFIX_USER + userName);
String userId;
if (userResponse == null) {
String errorMessage = "Error occurred while retrieving Id of user " + userName + ". Error : Response is null.";
log.error(errorMessage);
throw new IdentityProviderException(errorMessage, ExceptionCodes.RESOURCE_RETRIEVAL_FAILED);
}
if (userResponse.status() == APIMgtConstants.HTTPStatusCodes.SC_200_OK) {
String responseBody = userResponse.body().toString();
JsonParser parser = new JsonParser();
JsonObject parsedResponseBody = (JsonObject) parser.parse(responseBody);
JsonArray user = (JsonArray) parsedResponseBody.get(RESOURCES);
JsonObject scimUser = (JsonObject) user.get(0);
userId = scimUser.get(ID).getAsString();
String message = "Id " + userId + " of user " + scimUser.get(USERNAME).getAsString() + " is successfully retrieved from SCIM endpoint.";
if (log.isDebugEnabled()) {
log.debug(message);
}
} else {
String errorMessage = "Error occurred while retrieving Id of user " + userName + ". Error : " + getErrorMessage(userResponse);
log.error(errorMessage);
throw new IdentityProviderException(errorMessage, ExceptionCodes.RESOURCE_RETRIEVAL_FAILED);
}
return userId;
}
use of org.wso2.carbon.identity.configuration.mgt.core.model.Resources in project carbon-apimgt by wso2.
the class DefaultIdentityProviderImpl method getRoleId.
@Override
public String getRoleId(String roleName) throws IdentityProviderException {
Response roleResponse = scimServiceStub.searchGroups(FILTER_PREFIX_ROLE + roleName);
String roleId;
if (roleResponse == null) {
String errorMessage = "Error occurred while retrieving Id of role " + roleName + ". Error : Response is null.";
log.error(errorMessage);
throw new IdentityProviderException(errorMessage, ExceptionCodes.RESOURCE_RETRIEVAL_FAILED);
}
if (roleResponse.status() == APIMgtConstants.HTTPStatusCodes.SC_200_OK) {
String responseBody = roleResponse.body().toString();
JsonParser parser = new JsonParser();
JsonObject parsedResponseBody = (JsonObject) parser.parse(responseBody);
JsonArray role = (JsonArray) parsedResponseBody.get(RESOURCES);
JsonObject scimGroup = (JsonObject) role.get(0);
roleId = scimGroup.get(ID).getAsString();
String message = "Id " + roleId + " of role " + scimGroup.get(GROUPNAME).getAsString() + " is successfully retrieved from SCIM endpoint.";
if (log.isDebugEnabled()) {
log.debug(message);
}
} else {
String errorMessage = "Error occurred while retrieving Id of role " + roleName + ". Error : " + getErrorMessage(roleResponse);
log.error(errorMessage);
throw new IdentityProviderException(errorMessage, ExceptionCodes.RESOURCE_RETRIEVAL_FAILED);
}
return roleId;
}
use of org.wso2.carbon.identity.configuration.mgt.core.model.Resources in project carbon-apimgt by wso2.
the class GatewaySourceGeneratorImpl method getConfigStringFromTemplate.
@Override
public String getConfigStringFromTemplate(List<TemplateBuilderDTO> apiResources) throws APITemplateException {
StringWriter writer = new StringWriter();
String templatePath = "resources" + File.separator + "template" + File.separator + "template.xml";
try {
// build the context for template and apply the necessary decorators
apiConfigContext.validate();
ConfigContext configContext = new ResourceConfigContext(apiConfigContext, apiResources);
VelocityContext context = configContext.getContext();
VelocityEngine velocityengine = new VelocityEngine();
velocityengine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
velocityengine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
velocityengine.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM, new CommonsLogLogChute());
velocityengine.init();
Template template = velocityengine.getTemplate(templatePath);
template.merge(context, writer);
} catch (ResourceNotFoundException e) {
log.error("Template " + templatePath + " not Found", e);
throw new APITemplateException("Template " + templatePath + " not Found", ExceptionCodes.TEMPLATE_EXCEPTION);
} catch (ParseErrorException e) {
log.error("Syntax error in " + templatePath, e);
throw new APITemplateException("Syntax error in " + templatePath, ExceptionCodes.TEMPLATE_EXCEPTION);
}
return writer.toString();
}
use of org.wso2.carbon.identity.configuration.mgt.core.model.Resources in project carbon-apimgt by wso2.
the class GatewaySourceGeneratorImpl method getCompositeAPIConfigStringFromTemplate.
@Override
public String getCompositeAPIConfigStringFromTemplate(List<TemplateBuilderDTO> apiResources, List<CompositeAPIEndpointDTO> compositeApiEndpoints) throws APITemplateException {
StringWriter writer = new StringWriter();
String templatePath = "resources" + File.separator + "template" + File.separator + "composite_template.xml";
try {
// build the context for template and apply the necessary decorators
apiConfigContext.validate();
CompositeAPIConfigContext configContext = new CompositeAPIConfigContext(apiConfigContext, apiResources, compositeApiEndpoints);
VelocityContext context = configContext.getContext();
VelocityEngine velocityengine = new VelocityEngine();
velocityengine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
velocityengine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
velocityengine.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM, new CommonsLogLogChute());
velocityengine.init();
Template template = velocityengine.getTemplate(templatePath);
template.merge(context, writer);
} catch (ResourceNotFoundException e) {
log.error("Template " + templatePath + " not Found", e);
throw new APITemplateException("Template " + templatePath + " not Found", ExceptionCodes.TEMPLATE_EXCEPTION);
} catch (ParseErrorException e) {
log.error("Syntax error in " + templatePath, e);
throw new APITemplateException("Syntax error in " + templatePath, ExceptionCodes.TEMPLATE_EXCEPTION);
}
return writer.toString();
}
use of org.wso2.carbon.identity.configuration.mgt.core.model.Resources in project carbon-apimgt by wso2.
the class APIDefinitionFromSwagger20TestCase method testAddNewScopeToExistingOauthSecurity.
@Test()
public void testAddNewScopeToExistingOauthSecurity() throws IOException, APIManagementException {
APIDefinitionFromSwagger20 apiDefinitionFromSwagger20 = new APIDefinitionFromSwagger20();
String filePath = "src" + File.separator + "test" + File.separator + "resources" + File.separator + "swagger" + File.separator + "swaggerWithAuthorization.yaml";
File file = Paths.get(filePath).toFile();
String sampleApi = IOUtils.toString(new FileInputStream(file));
Scope scope = new Scope();
scope.setName("apim:api_delete");
scope.setDescription("Delete API");
String scopeAddedSwagger = apiDefinitionFromSwagger20.addScopeToSwaggerDefinition(sampleApi, scope);
Map<String, String> scopes = apiDefinitionFromSwagger20.getScopesFromSecurityDefinition(scopeAddedSwagger);
Assert.assertTrue(scopes.containsKey("apim:api_delete"));
}
Aggregations