use of io.jans.as.model.uma.UmaResource in project jans by JanssenProject.
the class ResourceRegistrar method register.
private void register(RsResource rsResource) {
try {
for (Condition condition : rsResource.getConditions()) {
Key key = new Key(rsResource.getPath(), condition.getHttpMethods());
UmaResource resource = new UmaResource();
resource.setName(key.getResourceName());
if (condition.getScopeExpression() != null && JsonLogicNodeParser.isNodeValid(condition.getScopeExpression().toString())) {
resource.setScopeExpression(condition.getScopeExpression().toString());
resource.setScopes(JsonLogicNodeParser.parseNode(condition.getScopeExpression().toString()).getData());
} else {
resource.setScopes(condition.getScopes());
}
// set creation and expiration timestamp
if (isSafeToInt(rsResource.getIat())) {
resource.setIat(rsResource.getIat());
}
if (isSafeToInt(rsResource.getExp())) {
resource.setExp(rsResource.getExp());
}
UmaResourceResponse resourceResponse = serviceProvider.getResourceService().addResource("Bearer " + patProvider.getPatToken(), resource);
Preconditions.checkNotNull(resourceResponse.getId(), "Resource ID can not be null.");
resourceMap.put(key, rsResource);
idMap.put(key, resourceResponse.getId());
LOG.debug("Registered resource, path: " + key.getPath() + ", http methods: " + condition.getHttpMethods() + ", id: " + resourceResponse.getId());
}
} catch (Exception ex) {
LOG.error(ex.getMessage(), ex);
throw ex;
}
}
use of io.jans.as.model.uma.UmaResource in project jans by JanssenProject.
the class RegisterResourceFlowHttpTest method modifyNotExistingResource.
/**
* Test non existing UMA resource description modification
*/
@Test(dependsOnMethods = { "modifyResource" })
public void modifyNotExistingResource() throws Exception {
showTitle("modifyNotExistingResource");
try {
UmaResource resource = new UmaResource();
resource.setName("Photo Album 3");
resource.setIconUri("http://www.example.com/icons/flower.png");
resource.setScopes(Arrays.asList("http://photoz.example.com/dev/scopes/view", "http://photoz.example.com/dev/scopes/all"));
getResourceService().updateResource("Bearer " + pat.getAccessToken(), "fake_resource_id", resource);
} catch (ClientErrorException ex) {
System.err.println(ex.getResponse().readEntity(String.class));
int status = ex.getResponse().getStatus();
assertTrue(status != Response.Status.OK.getStatusCode(), "Unexpected response status");
}
}
use of io.jans.as.model.uma.UmaResource in project jans by JanssenProject.
the class RegisterResourceFlowHttpTest method modifyResource.
/**
* Resource modification
*/
@Test(dependsOnMethods = { "addResource" })
public void modifyResource() throws Exception {
showTitle("modifyResource");
// Modify resource description
UmaResourceResponse resourceStatus = null;
try {
UmaResource resource = new UmaResource();
resource.setName("Photo Album 2");
resource.setIconUri("http://www.example.com/icons/flower.png");
resource.setScopes(Arrays.asList("http://photoz.example.com/dev/scopes/view", "http://photoz.example.com/dev/scopes/all"));
resource.setType("myType");
resourceStatus = getResourceService().updateResource("Bearer " + pat.getAccessToken(), this.resourceId, resource);
} catch (ClientErrorException ex) {
System.err.println(ex.getResponse().readEntity(String.class));
throw ex;
}
try {
UmaResource resource = new UmaResource();
resource.setName("Photo Album 2");
resource.setIconUri("http://www.example.com/icons/flower.png");
resource.setScopeExpression(MODIFY_SCOPE_EXPRESSION);
resource.setType("myType");
resourceStatus = getResourceService().updateResource("Bearer " + pat.getAccessToken(), this.resourceIdWithScopeExpression, resource);
} catch (ClientErrorException ex) {
System.err.println(ex.getResponse().readEntity(String.class));
throw ex;
}
assertNotNull(resourceStatus, "Resource status is null");
assertNotNull(this.resourceId, "Resource description id is null");
}
use of io.jans.as.model.uma.UmaResource in project jans by JanssenProject.
the class RsModifyOperation method execute.
@Override
public IOpResponse execute(final RsModifyParams params) throws Exception {
validate(params);
Rp rp = getRp();
PatProvider patProvider = new PatProvider() {
@Override
public String getPatToken() {
return getUmaTokenService().getPat(params.getRpId()).getToken();
}
@Override
public void clearPat() {
// do nothing
}
};
io.jans.ca.server.model.UmaResource umaResource = rp.umaResource(params.getPath(), params.getHttpMethod());
if (umaResource == null) {
final ErrorResponse error = new ErrorResponse("invalid_request");
error.setErrorDescription("Resource is not protected with path: " + params.getPath() + " and httpMethod: " + params.getHttpMethod() + ". Please protect your resource first with uma_rs_modify command. Check details on " + CoreUtils.DOC_URL);
LOG.error(error.getErrorDescription());
throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).type(MediaType.APPLICATION_JSON_TYPE).entity(Jackson2.asJson(error)).build());
}
UmaMetadata discovery = getDiscoveryService().getUmaDiscoveryByRpId(params.getRpId());
UmaResourceService resourceService = UmaClientFactory.instance().createResourceService(discovery, getHttpService().getClientEngine());
UmaResource opUmaResource = getResource(resourceService, params, umaResource.getId());
try {
String pat = getUmaTokenService().getPat(params.getRpId()).getToken();
return update(pat, umaResource.getId(), rp, resourceService, opUmaResource);
} catch (ClientErrorException e) {
LOG.debug("Failed to update resource. Entity: " + e.getResponse().readEntity(String.class) + ", status: " + e.getResponse().getStatus(), e);
if (e.getResponse().getStatus() == 400 || e.getResponse().getStatus() == 401) {
LOG.debug("Try maybe PAT is lost on AS, force refresh PAT and re-try ...");
return update(getUmaTokenService().obtainPat(params.getRpId()).getToken(), umaResource.getId(), rp, resourceService, opUmaResource);
} else {
throw e;
}
} catch (Exception e) {
LOG.error(e.getMessage(), e);
throw e;
}
}
Aggregations