Search in sources :

Example 1 with ExternalIntrospectionContext

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

the class AuthorizationGrant method runIntrospectionScriptAndInjectValuesIntoJwt.

private void runIntrospectionScriptAndInjectValuesIntoJwt(Jwt jwt, ExecutionContext executionContext) {
    JSONObject responseAsJsonObject = new JSONObject();
    ExternalIntrospectionContext context = new ExternalIntrospectionContext(this, executionContext.getHttpRequest(), executionContext.getHttpResponse(), appConfiguration, attributeService);
    context.setAccessTokenAsJwt(jwt);
    if (externalIntrospectionService.executeExternalModifyResponse(responseAsJsonObject, context)) {
        log.trace("Successfully run external introspection scripts.");
        if (context.isTranferIntrospectionPropertiesIntoJwtClaims()) {
            log.trace("Transfering claims into jwt ...");
            JwtUtil.transferIntoJwtClaims(responseAsJsonObject, jwt);
            log.trace("Transfered.");
        }
    }
}
Also used : JSONObject(org.json.JSONObject) ExternalIntrospectionContext(io.jans.as.server.service.external.context.ExternalIntrospectionContext)

Example 2 with ExternalIntrospectionContext

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

the class IntrospectionWebService method introspect.

private Response introspect(String authorization, String token, String tokenTypeHint, String responseAsJwt, HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
    try {
        if (log.isTraceEnabled()) {
            log.trace("Introspect token, authorization: {}, token to introspect: {}, tokenTypeHint: {}", escapeLog(authorization), escapeLog(token), escapeLog(tokenTypeHint));
        }
        AuthorizationGrant authorizationGrant = validateAuthorization(authorization, token);
        if (StringUtils.isBlank(token)) {
            log.trace("Bad request: Token is blank.");
            return Response.status(Response.Status.BAD_REQUEST).type(MediaType.APPLICATION_JSON_TYPE).entity(errorResponseFactory.errorAsJson(AuthorizeErrorResponseType.INVALID_REQUEST, "")).build();
        }
        final io.jans.as.model.common.IntrospectionResponse response = new io.jans.as.model.common.IntrospectionResponse(false);
        final AuthorizationGrant grantOfIntrospectionToken = authorizationGrantList.getAuthorizationGrantByAccessToken(token);
        AbstractToken tokenToIntrospect = fillResponse(token, response, grantOfIntrospectionToken);
        JSONObject responseAsJsonObject = createResponseAsJsonObject(response, tokenToIntrospect);
        ExternalIntrospectionContext context = new ExternalIntrospectionContext(authorizationGrant, httpRequest, httpResponse, appConfiguration, attributeService);
        context.setGrantOfIntrospectionToken(grantOfIntrospectionToken);
        if (externalIntrospectionService.executeExternalModifyResponse(responseAsJsonObject, context)) {
            log.trace("Successfully run external introspection scripts.");
        } else {
            responseAsJsonObject = createResponseAsJsonObject(response, tokenToIntrospect);
            log.trace("Canceled changes made by external introspection script since method returned `false`.");
        }
        // Make scopes conform as required by spec, see #1499
        if (response.getScope() != null && !appConfiguration.getIntrospectionResponseScopesBackwardCompatibility()) {
            String scopes = StringUtils.join(response.getScope().toArray(), " ");
            responseAsJsonObject.put("scope", scopes);
        }
        if (Boolean.TRUE.toString().equalsIgnoreCase(responseAsJwt)) {
            return Response.status(Response.Status.OK).entity(createResponseAsJwt(responseAsJsonObject, grantOfIntrospectionToken)).build();
        }
        return Response.status(Response.Status.OK).entity(responseAsJsonObject.toString()).type(MediaType.APPLICATION_JSON_TYPE).build();
    } catch (WebApplicationException e) {
        if (log.isErrorEnabled()) {
            log.error(e.getMessage(), e);
        }
        throw e;
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).type(MediaType.APPLICATION_JSON_TYPE).build();
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) IntrospectionResponse(io.jans.as.model.common.IntrospectionResponse) JSONException(org.json.JSONException) WebApplicationException(javax.ws.rs.WebApplicationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) AbstractToken(io.jans.as.server.model.common.AbstractToken) JSONObject(org.json.JSONObject) IntrospectionResponse(io.jans.as.model.common.IntrospectionResponse) ExternalIntrospectionContext(io.jans.as.server.service.external.context.ExternalIntrospectionContext) AuthorizationGrant(io.jans.as.server.model.common.AuthorizationGrant)

Aggregations

ExternalIntrospectionContext (io.jans.as.server.service.external.context.ExternalIntrospectionContext)2 JSONObject (org.json.JSONObject)2 IntrospectionResponse (io.jans.as.model.common.IntrospectionResponse)1 AbstractToken (io.jans.as.server.model.common.AbstractToken)1 AuthorizationGrant (io.jans.as.server.model.common.AuthorizationGrant)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 JSONException (org.json.JSONException)1