Search in sources :

Example 1 with ExternalIntrospectionContext

use of org.gluu.oxauth.service.external.context.ExternalIntrospectionContext in project oxAuth by GluuFederation.

the class IntrospectionWebService method introspect.

private Response introspect(String p_authorization, String p_token, String tokenTypeHint, String responseAsJwt, HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
    try {
        log.trace("Introspect token, authorization: {}, token to introspect: {}, tokenTypeHint: {}", p_authorization, p_token, tokenTypeHint);
        AuthorizationGrant authorizationGrant = validateAuthorization(p_authorization, p_token);
        if (StringUtils.isBlank(p_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 IntrospectionResponse response = new IntrospectionResponse(false);
        final AuthorizationGrant grantOfIntrospectionToken = authorizationGrantList.getAuthorizationGrantByAccessToken(p_token);
        AbstractToken tokenToIntrospect = null;
        if (grantOfIntrospectionToken != null) {
            tokenToIntrospect = grantOfIntrospectionToken.getAccessToken(p_token);
            response.setActive(tokenToIntrospect.isValid());
            response.setExpiresAt(ServerUtil.dateToSeconds(tokenToIntrospect.getExpirationDate()));
            response.setIssuedAt(ServerUtil.dateToSeconds(tokenToIntrospect.getCreationDate()));
            response.setAcrValues(grantOfIntrospectionToken.getAcrValues());
            // #433
            response.setScope(grantOfIntrospectionToken.getScopes() != null ? grantOfIntrospectionToken.getScopes() : Lists.newArrayList());
            response.setClientId(grantOfIntrospectionToken.getClientId());
            response.setSub(grantOfIntrospectionToken.getSub());
            response.setUsername(grantOfIntrospectionToken.getUserId());
            response.setIssuer(appConfiguration.getIssuer());
            response.setAudience(grantOfIntrospectionToken.getClientId());
            if (tokenToIntrospect instanceof AccessToken) {
                AccessToken accessToken = (AccessToken) tokenToIntrospect;
                response.setTokenType(accessToken.getTokenType() != null ? accessToken.getTokenType().getName() : TokenType.BEARER.getName());
            }
        } else {
            log.debug("Failed to find grant for access_token: " + p_token + ". Return 200 with active=false.");
        }
        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 extenal 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) {
        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 : AbstractToken(org.gluu.oxauth.model.common.AbstractToken) JSONObject(org.json.JSONObject) WebApplicationException(javax.ws.rs.WebApplicationException) IntrospectionResponse(org.gluu.oxauth.model.common.IntrospectionResponse) AccessToken(org.gluu.oxauth.model.common.AccessToken) ExternalIntrospectionContext(org.gluu.oxauth.service.external.context.ExternalIntrospectionContext) AuthorizationGrant(org.gluu.oxauth.model.common.AuthorizationGrant) JSONException(org.json.JSONException) IOException(java.io.IOException) WebApplicationException(javax.ws.rs.WebApplicationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 2 with ExternalIntrospectionContext

use of org.gluu.oxauth.service.external.context.ExternalIntrospectionContext in project oxAuth by GluuFederation.

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(org.gluu.oxauth.service.external.context.ExternalIntrospectionContext)

Aggregations

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