Search in sources :

Example 6 with UmaResource

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;
    }
}
Also used : Condition(io.jans.ca.rs.protect.Condition) UmaResourceResponse(io.jans.as.model.uma.UmaResourceResponse) UmaResource(io.jans.as.model.uma.UmaResource)

Example 7 with UmaResource

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");
    }
}
Also used : ClientErrorException(javax.ws.rs.ClientErrorException) UmaResource(io.jans.as.model.uma.UmaResource) BaseTest(io.jans.as.client.BaseTest) Test(org.testng.annotations.Test)

Example 8 with UmaResource

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");
}
Also used : UmaResourceResponse(io.jans.as.model.uma.UmaResourceResponse) ClientErrorException(javax.ws.rs.ClientErrorException) UmaResource(io.jans.as.model.uma.UmaResource) BaseTest(io.jans.as.client.BaseTest) Test(org.testng.annotations.Test)

Example 9 with UmaResource

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;
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) UmaResourceService(io.jans.as.client.uma.UmaResourceService) ClientErrorException(javax.ws.rs.ClientErrorException) HttpException(io.jans.ca.server.HttpException) WebApplicationException(javax.ws.rs.WebApplicationException) UmaMetadata(io.jans.as.model.uma.UmaMetadata) PatProvider(io.jans.ca.rs.protect.resteasy.PatProvider) ClientErrorException(javax.ws.rs.ClientErrorException) Rp(io.jans.ca.server.service.Rp) UmaResource(io.jans.as.model.uma.UmaResource)

Aggregations

UmaResource (io.jans.as.model.uma.UmaResource)9 UmaResourceResponse (io.jans.as.model.uma.UmaResourceResponse)6 ClientErrorException (javax.ws.rs.ClientErrorException)6 Test (org.testng.annotations.Test)4 BaseTest (io.jans.as.client.BaseTest)3 UmaResourceService (io.jans.as.client.uma.UmaResourceService)1 UmaMetadata (io.jans.as.model.uma.UmaMetadata)1 UmaResourceWithId (io.jans.as.model.uma.UmaResourceWithId)1 BaseTest (io.jans.as.server.BaseTest)1 Condition (io.jans.ca.rs.protect.Condition)1 PatProvider (io.jans.ca.rs.protect.resteasy.PatProvider)1 HttpException (io.jans.ca.server.HttpException)1 Rp (io.jans.ca.server.service.Rp)1 WebApplicationException (javax.ws.rs.WebApplicationException)1