Search in sources :

Example 1 with ExternalUmaRptClaimsContext

use of io.jans.as.server.service.external.context.ExternalUmaRptClaimsContext in project jans by JanssenProject.

the class UmaRptIntrospectionWS method introspect.

private Response introspect(String authorization, String token, HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
    try {
        errorResponseFactory.validateComponentEnabled(ComponentType.UMA);
        umaValidationService.assertHasProtectionScope(authorization);
        final UmaRPT rpt = rptService.getRPTByCode(token);
        if (!isValid(rpt)) {
            return Response.status(Response.Status.OK).entity(new RptIntrospectionResponse(false)).cacheControl(ServerUtil.cacheControl(true)).build();
        }
        final List<io.jans.as.model.uma.UmaPermission> permissions = buildStatusResponsePermissions(rpt);
        // active status
        final RptIntrospectionResponse statusResponse = new RptIntrospectionResponse();
        statusResponse.setActive(true);
        statusResponse.setExpiresAt(ServerUtil.dateToSeconds(rpt.getExpirationDate()));
        statusResponse.setIssuedAt(ServerUtil.dateToSeconds(rpt.getCreationDate()));
        statusResponse.setPermissions(permissions);
        statusResponse.setClientId(rpt.getClientId());
        statusResponse.setAud(rpt.getClientId());
        statusResponse.setSub(rpt.getUserId());
        final List<UmaPermission> rptPermissions = rptService.getRptPermissions(rpt);
        if (!rptPermissions.isEmpty()) {
            UmaPermission permission = rptPermissions.iterator().next();
            String pctCode = permission.getAttributes().get(UmaPermission.PCT);
            if (StringHelper.isNotEmpty(pctCode)) {
                UmaPCT pct = pctService.getByCode(pctCode);
                if (pct != null) {
                    statusResponse.setPctClaims(pct.getClaims().toMap());
                } else {
                    log.error("Failed to find PCT with code: {} which is taken from permission object: {}", pctCode, permission.getDn());
                }
            } else {
                log.trace("PCT code is blank for RPT: {}", rpt.getCode());
            }
        }
        JSONObject rptAsJson = new JSONObject(ServerUtil.asJson(statusResponse));
        ExternalUmaRptClaimsContext context = new ExternalUmaRptClaimsContext(clientService.getClient(rpt.getClientId()), httpRequest, httpResponse);
        if (externalUmaRptClaimsService.externalModify(rptAsJson, context)) {
            log.trace("Successfully run external RPT Claims script associated with {}", rpt.getClientId());
        } else {
            rptAsJson = new JSONObject(ServerUtil.asJson(statusResponse));
            log.trace("Canceled changes made by external RPT Claims script since method returned `false`.");
        }
        return Response.status(Response.Status.OK).entity(rptAsJson.toString()).type(MediaType.APPLICATION_JSON_TYPE).cacheControl(ServerUtil.cacheControl(true)).build();
    } catch (Exception ex) {
        log.error("Exception happened", ex);
        if (ex instanceof WebApplicationException) {
            throw (WebApplicationException) ex;
        }
        throw errorResponseFactory.createWebApplicationException(Response.Status.INTERNAL_SERVER_ERROR, UmaErrorResponseType.SERVER_ERROR, "Internal error.");
    }
}
Also used : UmaPCT(io.jans.as.server.uma.authorization.UmaPCT) ExternalUmaRptClaimsContext(io.jans.as.server.service.external.context.ExternalUmaRptClaimsContext) WebApplicationException(javax.ws.rs.WebApplicationException) WebApplicationException(javax.ws.rs.WebApplicationException) RptIntrospectionResponse(io.jans.as.model.uma.RptIntrospectionResponse) UmaRPT(io.jans.as.server.uma.authorization.UmaRPT) JSONObject(org.json.JSONObject) UmaPermission(io.jans.as.model.uma.persistence.UmaPermission)

Example 2 with ExternalUmaRptClaimsContext

use of io.jans.as.server.service.external.context.ExternalUmaRptClaimsContext in project jans by JanssenProject.

the class UmaRptService method runScriptAndInjectValuesIntoJwt.

private void runScriptAndInjectValuesIntoJwt(Jwt jwt, ExecutionContext executionContext) {
    JSONObject responseAsJsonObject = new JSONObject();
    ExternalUmaRptClaimsContext context = new ExternalUmaRptClaimsContext(executionContext);
    if (externalUmaRptClaimsService.externalModify(responseAsJsonObject, context)) {
        log.trace("Successfully run external RPT Claim scripts.");
        if (context.isTranferPropertiesIntoJwtClaims()) {
            log.trace("Transfering claims into jwt ...");
            JwtUtil.transferIntoJwtClaims(responseAsJsonObject, jwt);
            log.trace("Transfered.");
        }
    }
}
Also used : ExternalUmaRptClaimsContext(io.jans.as.server.service.external.context.ExternalUmaRptClaimsContext) JSONObject(org.json.JSONObject)

Aggregations

ExternalUmaRptClaimsContext (io.jans.as.server.service.external.context.ExternalUmaRptClaimsContext)2 JSONObject (org.json.JSONObject)2 RptIntrospectionResponse (io.jans.as.model.uma.RptIntrospectionResponse)1 UmaPermission (io.jans.as.model.uma.persistence.UmaPermission)1 UmaPCT (io.jans.as.server.uma.authorization.UmaPCT)1 UmaRPT (io.jans.as.server.uma.authorization.UmaRPT)1 WebApplicationException (javax.ws.rs.WebApplicationException)1