Search in sources :

Example 6 with CorrectRptIntrospectionResponse

use of io.jans.ca.common.introspection.CorrectRptIntrospectionResponse 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)

Aggregations

CorrectRptIntrospectionResponse (io.jans.ca.common.introspection.CorrectRptIntrospectionResponse)6 RpGetRptResponse (io.jans.ca.common.response.RpGetRptResponse)3 ClientInterface (io.jans.ca.client.ClientInterface)2 IntrospectRptParams (io.jans.ca.common.params.IntrospectRptParams)2 RegisterSiteResponse (io.jans.ca.common.response.RegisterSiteResponse)2 HttpException (io.jans.ca.server.HttpException)2 Response (javax.ws.rs.core.Response)2 Parameters (org.testng.annotations.Parameters)2 Test (org.testng.annotations.Test)2 UmaClientFactory (io.jans.as.client.uma.UmaClientFactory)1 UmaMetadata (io.jans.as.model.uma.UmaMetadata)1 UmaTokenResponse (io.jans.as.model.uma.UmaTokenResponse)1 CorrectUmaPermission (io.jans.ca.common.introspection.CorrectUmaPermission)1 IOpResponse (io.jans.ca.common.response.IOpResponse)1 POJOResponse (io.jans.ca.common.response.POJOResponse)1 RsCheckAccessResponse (io.jans.ca.common.response.RsCheckAccessResponse)1 PatProvider (io.jans.ca.rs.protect.resteasy.PatProvider)1 ResourceRegistrar (io.jans.ca.rs.protect.resteasy.ResourceRegistrar)1 RptPreProcessInterceptor (io.jans.ca.rs.protect.resteasy.RptPreProcessInterceptor)1 ServiceProvider (io.jans.ca.rs.protect.resteasy.ServiceProvider)1