Search in sources :

Example 11 with UmaMetadata

use of io.jans.as.model.uma.UmaMetadata in project jans by JanssenProject.

the class UmaTokenService method getRpt.

public RpGetRptResponse getRpt(RpGetRptParams params) throws Exception {
    Rp rp = rpSyncService.getRp(params.getRpId());
    UmaMetadata discovery = discoveryService.getUmaDiscoveryByRpId(params.getRpId());
    if (!Strings.isNullOrEmpty(rp.getRpt()) && rp.getRptExpiresAt() != null) {
        if (!CoreUtils.isExpired(rp.getRptExpiresAt())) {
            LOG.debug("RPT from rp, RPT: " + rp.getRpt() + ", rp: " + rp);
            RpGetRptResponse result = new RpGetRptResponse();
            result.setRpt(rp.getRpt());
            result.setTokenType(rp.getRptTokenType());
            result.setPct(rp.getRptPct());
            result.setUpdated(rp.getRptUpgraded());
            return result;
        }
    }
    Builder client = opClientFactory.createClientRequest(discovery.getTokenEndpoint(), httpService.getClientEngine());
    client.header("Authorization", "Basic " + Utils.encodeCredentials(rp.getClientId(), rp.getClientSecret()));
    Form formRequest = new Form();
    formRequest.param("grant_type", GrantType.OXAUTH_UMA_TICKET.getValue());
    formRequest.param("ticket", params.getTicket());
    if (params.getClaimToken() != null) {
        formRequest.param("claim_token", params.getClaimToken());
    }
    if (params.getClaimTokenFormat() != null) {
        formRequest.param("claim_token_format", params.getClaimTokenFormat());
    }
    if (params.getPct() != null) {
        formRequest.param("pct", params.getPct());
    }
    if (params.getRpt() != null) {
        formRequest.param("rpt", params.getRpt());
    }
    if (params.getScope() != null) {
        formRequest.param("scope", Utils.joinAndUrlEncode(params.getScope()));
    }
    if (params.getParams() != null && !params.getParams().isEmpty()) {
        for (Map.Entry<String, String> p : params.getParams().entrySet()) {
            formRequest.param(p.getKey(), p.getValue());
        }
    }
    Response response = null;
    try {
        response = client.buildPost(Entity.form(formRequest)).invoke();
    } catch (Exception e) {
        LOG.error("Failed to receive RPT response for rp: " + rp, e);
        throw new HttpException(ErrorResponseCode.FAILED_TO_GET_RPT);
    }
    String entityResponse = null;
    try {
        entityResponse = response.readEntity(String.class);
    } catch (Exception e) {
        LOG.error("Failed to read RPT response for rp: " + rp, e);
        throw new HttpException(ErrorResponseCode.FAILED_TO_GET_RPT);
    } finally {
        response.close();
    }
    UmaTokenResponse tokenResponse = asTokenResponse(entityResponse);
    if (tokenResponse != null && StringUtils.isNotBlank(tokenResponse.getAccessToken())) {
        final IntrospectionService introspectionService = ServerLauncher.getInjector().getInstance(IntrospectionService.class);
        CorrectRptIntrospectionResponse status = introspectionService.introspectRpt(params.getRpId(), tokenResponse.getAccessToken());
        LOG.debug("RPT " + tokenResponse.getAccessToken() + ", status: " + status);
        if (status.getActive()) {
            LOG.debug("RPT is successfully obtained from AS. RPT: {}", tokenResponse.getAccessToken());
            rp.setRpt(tokenResponse.getAccessToken());
            rp.setRptTokenType(tokenResponse.getTokenType());
            rp.setRptPct(tokenResponse.getPct());
            rp.setRptUpgraded(tokenResponse.getUpgraded());
            rp.setRptCreatedAt(new Date(status.getIssuedAt() * 1000));
            rp.setRptExpiresAt(new Date(status.getExpiresAt() * 1000));
            rpService.updateSilently(rp);
            RpGetRptResponse result = new RpGetRptResponse();
            result.setRpt(rp.getRpt());
            result.setTokenType(rp.getRptTokenType());
            result.setPct(rp.getRptPct());
            result.setUpdated(rp.getRptUpgraded());
            return result;
        }
    } else {
        RpGetRptOperation.handleRptError(response.getStatus(), entityResponse);
    }
    LOG.error("Failed to get RPT for rp: " + rp);
    throw new HttpException(ErrorResponseCode.FAILED_TO_GET_RPT);
}
Also used : CorrectRptIntrospectionResponse(io.jans.ca.common.introspection.CorrectRptIntrospectionResponse) UmaTokenResponse(io.jans.as.model.uma.UmaTokenResponse) Form(javax.ws.rs.core.Form) Builder(javax.ws.rs.client.Invocation.Builder) HttpException(io.jans.ca.server.HttpException) IOException(java.io.IOException) RpGetRptResponse(io.jans.ca.common.response.RpGetRptResponse) CorrectRptIntrospectionResponse(io.jans.ca.common.introspection.CorrectRptIntrospectionResponse) UmaTokenResponse(io.jans.as.model.uma.UmaTokenResponse) Response(javax.ws.rs.core.Response) UmaMetadata(io.jans.as.model.uma.UmaMetadata) HttpException(io.jans.ca.server.HttpException) RpGetRptResponse(io.jans.ca.common.response.RpGetRptResponse)

Example 12 with UmaMetadata

use of io.jans.as.model.uma.UmaMetadata 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

UmaMetadata (io.jans.as.model.uma.UmaMetadata)12 HttpException (io.jans.ca.server.HttpException)6 Rp (io.jans.ca.server.service.Rp)4 Parameters (org.testng.annotations.Parameters)3 Test (org.testng.annotations.Test)3 Lists (com.google.common.collect.Lists)2 Injector (com.google.inject.Injector)2 UmaMetadataService (io.jans.as.client.uma.UmaMetadataService)2 UmaResourceService (io.jans.as.client.uma.UmaResourceService)2 Command (io.jans.ca.common.Command)2 ErrorResponseCode (io.jans.ca.common.ErrorResponseCode)2 IOpResponse (io.jans.ca.common.response.IOpResponse)2 Utils (io.jans.ca.server.Utils)2 IOException (java.io.IOException)2 List (java.util.List)2 Preconditions (com.google.common.base.Preconditions)1 Strings (com.google.common.base.Strings)1 Sets (com.google.common.collect.Sets)1 BaseTest (io.jans.as.client.BaseTest)1 RegisterClient (io.jans.as.client.RegisterClient)1