Search in sources :

Example 1 with ServiceUnavailableException

use of org.forgerock.json.resource.ServiceUnavailableException in project OpenAM by OpenRock.

the class TokenResource method deleteToken.

/**
     * Deletes the token with the provided token id.
     *
     * @param context The context.
     * @param tokenId The token id.
     * @param deleteRefreshToken Whether to delete associated refresh token, if token id is for an access token.
     * @return {@code Void} if the token has been deleted.
     */
private Promise<Void, ResourceException> deleteToken(Context context, String tokenId, boolean deleteRefreshToken) {
    try {
        AMIdentity uid = getUid(context);
        JsonValue token = tokenStore.read(tokenId);
        if (token == null) {
            if (debug.errorEnabled()) {
                debug.error("TokenResource :: DELETE : No token with ID, " + tokenId + " found to delete");
            }
            throw new NotFoundException("Token Not Found", null);
        }
        String username = getAttributeValue(token, USERNAME);
        if (username == null || username.isEmpty()) {
            if (debug.errorEnabled()) {
                debug.error("TokenResource :: DELETE : No username associated with " + "token with ID, " + tokenId + ".");
            }
            throw new PermanentException(HttpURLConnection.HTTP_NOT_FOUND, "Not Found", null);
        }
        String grantType = getAttributeValue(token, GRANT_TYPE);
        if (grantType != null && grantType.equalsIgnoreCase(CLIENT_CREDENTIALS)) {
            if (deleteRefreshToken) {
                deleteAccessTokensRefreshToken(token);
            }
            tokenStore.delete(tokenId);
        } else {
            String realm = getAttributeValue(token, REALM);
            AMIdentity uid2 = identityManager.getResourceOwnerIdentity(username, realm);
            if (uid.equals(uid2) || uid.equals(adminUserId)) {
                if (deleteRefreshToken) {
                    deleteAccessTokensRefreshToken(token);
                }
                tokenStore.delete(tokenId);
            } else {
                if (debug.errorEnabled()) {
                    debug.error("TokenResource :: DELETE : Only the resource owner or an administrator may perform " + "a delete on the token with ID, " + tokenId + ".");
                }
                throw new PermanentException(401, "Unauthorized", null);
            }
        }
        return newResultPromise(null);
    } catch (CoreTokenException e) {
        return new ServiceUnavailableException(e.getMessage(), e).asPromise();
    } catch (ResourceException e) {
        return e.asPromise();
    } catch (SSOException e) {
        debug.error("TokenResource :: DELETE : Unable to retrieve identity of the requesting user. Unauthorized.");
        return new PermanentException(401, "Unauthorized", e).asPromise();
    } catch (IdRepoException e) {
        debug.error("TokenResource :: DELETE : Unable to retrieve identity of the requesting user. Unauthorized.");
        return new PermanentException(401, "Unauthorized", e).asPromise();
    } catch (UnauthorizedClientException e) {
        debug.error("TokenResource :: DELETE : Requesting user is unauthorized.");
        return new PermanentException(401, "Unauthorized", e).asPromise();
    }
}
Also used : AMIdentity(com.sun.identity.idm.AMIdentity) PermanentException(org.forgerock.json.resource.PermanentException) UnauthorizedClientException(org.forgerock.oauth2.core.exceptions.UnauthorizedClientException) JsonValue(org.forgerock.json.JsonValue) IdRepoException(com.sun.identity.idm.IdRepoException) NotFoundException(org.forgerock.json.resource.NotFoundException) CoreTokenException(org.forgerock.openam.cts.exceptions.CoreTokenException) ResourceException(org.forgerock.json.resource.ResourceException) SSOException(com.iplanet.sso.SSOException) ServiceUnavailableException(org.forgerock.json.resource.ServiceUnavailableException)

Example 2 with ServiceUnavailableException

use of org.forgerock.json.resource.ServiceUnavailableException in project OpenAM by OpenRock.

the class AuditEventPublisherImpl method publishForRealm.

private void publishForRealm(String realm, String topic, AuditEvent auditEvent) throws ResourceException {
    AMAuditService auditService = auditServiceProvider.getAuditService(realm);
    Connection connection = newInternalConnection(auditService);
    CreateRequest request = newCreateRequest(topic, auditEvent.getValue());
    try {
        connection.create(new RootContext(), request);
    } catch (ServiceUnavailableException e) {
        debug.message("Audit Service for realm {} is unavailable. Trying the default Audit Service.", realm, e);
        publishToDefault(topic, auditEvent);
    }
}
Also used : RootContext(org.forgerock.services.context.RootContext) CreateRequest(org.forgerock.json.resource.CreateRequest) Requests.newCreateRequest(org.forgerock.json.resource.Requests.newCreateRequest) Connection(org.forgerock.json.resource.Connection) Resources.newInternalConnection(org.forgerock.json.resource.Resources.newInternalConnection) ServiceUnavailableException(org.forgerock.json.resource.ServiceUnavailableException)

Example 3 with ServiceUnavailableException

use of org.forgerock.json.resource.ServiceUnavailableException in project OpenAM by OpenRock.

the class AuditServiceProviderImpl method refreshDefaultAuditService.

private void refreshDefaultAuditService() {
    AMAuditServiceConfiguration configuration = configProvider.getDefaultConfiguration();
    AuditServiceBuilder builder = AuditServiceBuilder.newAuditService().withEventTopicsMetaData(eventTopicsMetaData).withConfiguration(configuration);
    if (configuration.isAuditEnabled()) {
        configureEventHandlers(builder, configProvider.getDefaultEventHandlerConfigurations());
    }
    try {
        defaultAuditService.setDelegate(builder.build(), configuration);
    } catch (ServiceUnavailableException e) {
        debug.error("Default Audit Service configuration failed.", e);
    }
}
Also used : AuditServiceBuilder(org.forgerock.audit.AuditServiceBuilder) AMAuditServiceConfiguration(org.forgerock.openam.audit.configuration.AMAuditServiceConfiguration) ServiceUnavailableException(org.forgerock.json.resource.ServiceUnavailableException)

Example 4 with ServiceUnavailableException

use of org.forgerock.json.resource.ServiceUnavailableException in project OpenAM by OpenRock.

the class TokenResource method queryCollection.

@Override
public Promise<QueryResponse, ResourceException> queryCollection(Context context, QueryRequest queryRequest, QueryResourceHandler handler) {
    try {
        JsonValue response;
        Collection<QueryFilter<CoreTokenField>> query = new ArrayList<QueryFilter<CoreTokenField>>();
        //get uid of submitter
        AMIdentity uid;
        try {
            uid = getUid(context);
            if (!uid.equals(adminUserId)) {
                query.add(QueryFilter.equalTo(USERNAME_FIELD, uid.getName()));
                query.add(QueryFilter.equalTo(REALM_FIELD, DNMapper.orgNameToRealmName(uid.getRealm())));
            }
        } catch (Exception e) {
            if (debug.errorEnabled()) {
                debug.error("TokenResource :: QUERY : Unable to query collection as no UID discovered " + "for requesting user.");
            }
            return new PermanentException(401, "Unauthorized", e).asPromise();
        }
        String id = queryRequest.getQueryId();
        String queryString;
        if (id.equals("access_token")) {
            queryString = "tokenName=access_token";
        } else {
            queryString = id;
        }
        String[] constraints = queryString.split(",");
        boolean userNamePresent = false;
        for (String constraint : constraints) {
            String[] params = constraint.split("=");
            if (params.length == 2) {
                if (OAuthTokenField.USER_NAME.getOAuthField().equals(params[0])) {
                    userNamePresent = true;
                }
                query.add(QueryFilter.equalTo(getOAuth2TokenField(params[0]), params[1]));
            }
        }
        if (adminUserId.equals(uid)) {
            if (!userNamePresent) {
                return new BadRequestException("userName field MUST be set in _queryId").asPromise();
            }
        } else if (userNamePresent) {
            return new BadRequestException("userName field MUST NOT be set in _queryId").asPromise();
        }
        response = tokenStore.query(QueryFilter.and(query));
        return handleResponse(handler, response, context);
    } catch (UnauthorizedClientException e) {
        debug.error("TokenResource :: QUERY : Unable to query collection as the client is not authorized.", e);
        return new PermanentException(401, e.getMessage(), e).asPromise();
    } catch (CoreTokenException e) {
        debug.error("TokenResource :: QUERY : Unable to query collection as the token store is not available.", e);
        return new ServiceUnavailableException(e.getMessage(), e).asPromise();
    } catch (InternalServerErrorException e) {
        debug.error("TokenResource :: QUERY : Unable to query collection as writing the response failed.", e);
        return e.asPromise();
    } catch (NotFoundException e) {
        debug.error("TokenResource :: QUERY : Unable to query collection as realm does not have OAuth 2 provider.", e);
        return e.asPromise();
    }
}
Also used : JsonValue(org.forgerock.json.JsonValue) ArrayList(java.util.ArrayList) CoreTokenException(org.forgerock.openam.cts.exceptions.CoreTokenException) NotFoundException(org.forgerock.json.resource.NotFoundException) CoreTokenField(org.forgerock.openam.tokens.CoreTokenField) ServiceUnavailableException(org.forgerock.json.resource.ServiceUnavailableException) PermanentException(org.forgerock.json.resource.PermanentException) InternalServerErrorException(org.forgerock.json.resource.InternalServerErrorException) ServerException(org.forgerock.oauth2.core.exceptions.ServerException) ServiceUnavailableException(org.forgerock.json.resource.ServiceUnavailableException) UnauthorizedClientException(org.forgerock.oauth2.core.exceptions.UnauthorizedClientException) SSOException(com.iplanet.sso.SSOException) NotFoundException(org.forgerock.json.resource.NotFoundException) BadRequestException(org.forgerock.json.resource.BadRequestException) IdRepoException(com.sun.identity.idm.IdRepoException) SMSException(com.sun.identity.sm.SMSException) ResourceException(org.forgerock.json.resource.ResourceException) CoreTokenException(org.forgerock.openam.cts.exceptions.CoreTokenException) QueryFilter(org.forgerock.util.query.QueryFilter) AMIdentity(com.sun.identity.idm.AMIdentity) PermanentException(org.forgerock.json.resource.PermanentException) UnauthorizedClientException(org.forgerock.oauth2.core.exceptions.UnauthorizedClientException) BadRequestException(org.forgerock.json.resource.BadRequestException) InternalServerErrorException(org.forgerock.json.resource.InternalServerErrorException)

Example 5 with ServiceUnavailableException

use of org.forgerock.json.resource.ServiceUnavailableException in project OpenAM by OpenRock.

the class AuditServiceProviderImpl method refreshRealmAuditService.

private void refreshRealmAuditService(String realm) {
    AMAuditServiceConfiguration configuration = configProvider.getRealmConfiguration(realm);
    AuditServiceBuilder builder = AuditServiceBuilder.newAuditService().withEventTopicsMetaData(eventTopicsMetaData).withConfiguration(configuration);
    if (configuration.isAuditEnabled()) {
        configureEventHandlers(builder, configProvider.getRealmEventHandlerConfigurations(realm));
    }
    AMAuditService auditService = auditServices.get(realm);
    try {
        if (auditService == null) {
            auditService = new RealmAuditServiceProxy(builder.build(), defaultAuditService, configuration);
            auditService.startup();
            auditServices.put(realm, auditService);
        } else {
            auditService.setDelegate(builder.build(), configuration);
        }
    } catch (ServiceUnavailableException e) {
        debug.error("New Audit Service configuration for realm {} failed.", e, realm);
        // remove it so that we can fall back to the default service
        auditServices.remove(realm);
    }
}
Also used : AuditServiceBuilder(org.forgerock.audit.AuditServiceBuilder) AMAuditServiceConfiguration(org.forgerock.openam.audit.configuration.AMAuditServiceConfiguration) ServiceUnavailableException(org.forgerock.json.resource.ServiceUnavailableException)

Aggregations

ServiceUnavailableException (org.forgerock.json.resource.ServiceUnavailableException)6 AuditServiceBuilder (org.forgerock.audit.AuditServiceBuilder)3 AMAuditServiceConfiguration (org.forgerock.openam.audit.configuration.AMAuditServiceConfiguration)3 SSOException (com.iplanet.sso.SSOException)2 AMIdentity (com.sun.identity.idm.AMIdentity)2 IdRepoException (com.sun.identity.idm.IdRepoException)2 JsonValue (org.forgerock.json.JsonValue)2 NotFoundException (org.forgerock.json.resource.NotFoundException)2 PermanentException (org.forgerock.json.resource.PermanentException)2 ResourceException (org.forgerock.json.resource.ResourceException)2 UnauthorizedClientException (org.forgerock.oauth2.core.exceptions.UnauthorizedClientException)2 CoreTokenException (org.forgerock.openam.cts.exceptions.CoreTokenException)2 SMSException (com.sun.identity.sm.SMSException)1 ArrayList (java.util.ArrayList)1 BadRequestException (org.forgerock.json.resource.BadRequestException)1 Connection (org.forgerock.json.resource.Connection)1 CreateRequest (org.forgerock.json.resource.CreateRequest)1 InternalServerErrorException (org.forgerock.json.resource.InternalServerErrorException)1 Requests.newCreateRequest (org.forgerock.json.resource.Requests.newCreateRequest)1 Resources.newInternalConnection (org.forgerock.json.resource.Resources.newInternalConnection)1