Search in sources :

Example 1 with TokenTypeHint

use of io.jans.as.model.common.TokenTypeHint in project jans by JanssenProject.

the class RevokeRestWebServiceImpl method requestAccessToken.

@Override
public Response requestAccessToken(String tokenString, String tokenTypeHint, String clientId, HttpServletRequest request, HttpServletResponse response, SecurityContext sec) {
    log.debug("Attempting to revoke token: token = {}, tokenTypeHint = {}, isSecure = {}", tokenString, tokenTypeHint, sec.isSecure());
    errorResponseFactory.validateComponentEnabled(ComponentType.REVOKE_TOKEN);
    OAuth2AuditLog oAuth2AuditLog = new OAuth2AuditLog(ServerUtil.getIpAddress(request), Action.TOKEN_REVOCATION);
    validateToken(tokenString);
    Response.ResponseBuilder builder = Response.ok();
    SessionClient sessionClient = identity.getSessionClient();
    Client client = sessionClient != null ? sessionClient.getClient() : null;
    if (client == null) {
        client = clientService.getClient(clientId);
        if (!clientService.isPublic(client)) {
            log.trace("Client is not public and not authenticated. Skip revoking.");
            return response(builder, oAuth2AuditLog);
        }
    }
    if (client == null) {
        log.trace("Client is not unknown. Skip revoking.");
        return response(builder, oAuth2AuditLog);
    }
    oAuth2AuditLog.setClientId(client.getClientId());
    ExecutionContext executionContext = new ExecutionContext(request, response);
    executionContext.setClient(client);
    executionContext.setResponseBuilder(builder);
    final boolean scriptResult = externalRevokeTokenService.revokeTokenMethods(executionContext);
    if (!scriptResult) {
        log.trace("Revoke is forbidden by 'Revoke Token' custom script (method returned false). Exit without revoking.");
        return response(builder, oAuth2AuditLog);
    }
    TokenTypeHint tth = TokenTypeHint.getByValue(tokenTypeHint);
    boolean isAll = Constants.ALL.equalsIgnoreCase(tokenString) && appConfiguration.getAllowAllValueForRevokeEndpoint();
    if (isAll) {
        removeAllTokens(tth, executionContext);
        return response(builder, oAuth2AuditLog);
    }
    String[] tokens = tokenString.split(" ");
    if (ArrayUtils.isEmpty(tokens)) {
        throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST.getStatusCode()).type(MediaType.APPLICATION_JSON_TYPE).entity(errorResponseFactory.errorAsJson(TokenRevocationErrorResponseType.INVALID_REQUEST, "Failed to validate token.")).build());
    }
    boolean isSingle = tokens.length == 1;
    for (String token : tokens) {
        final Response removeTokenResponse = removeToken(token, executionContext, tth, oAuth2AuditLog, isSingle);
        if (removeTokenResponse != null) {
            return removeTokenResponse;
        }
    }
    return response(builder, oAuth2AuditLog);
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(javax.ws.rs.core.Response) ExecutionContext(io.jans.as.server.model.common.ExecutionContext) WebApplicationException(javax.ws.rs.WebApplicationException) SessionClient(io.jans.as.server.model.session.SessionClient) OAuth2AuditLog(io.jans.as.server.model.audit.OAuth2AuditLog) TokenTypeHint(io.jans.as.model.common.TokenTypeHint) Client(io.jans.as.common.model.registration.Client) SessionClient(io.jans.as.server.model.session.SessionClient)

Aggregations

Client (io.jans.as.common.model.registration.Client)1 TokenTypeHint (io.jans.as.model.common.TokenTypeHint)1 OAuth2AuditLog (io.jans.as.server.model.audit.OAuth2AuditLog)1 ExecutionContext (io.jans.as.server.model.common.ExecutionContext)1 SessionClient (io.jans.as.server.model.session.SessionClient)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 Response (javax.ws.rs.core.Response)1