Search in sources :

Example 1 with InvalidTokenException

use of org.forgerock.oauth2.core.exceptions.InvalidTokenException in project OpenAM by OpenRock.

the class OpenAMOpenIdConnectClientRegistrationService method getRegistration.

/**
     * {@inheritDoc}
     */
public JsonValue getRegistration(String clientId, String accessToken, OAuth2Request request) throws InvalidRequestException, InvalidClientMetadata, InvalidTokenException {
    if (clientId != null) {
        final Client client;
        try {
            client = clientDAO.read(clientId, request);
        } catch (UnauthorizedClientException e) {
            logger.error("ConnectClientRegistration.Validate(): Unable to create client", e);
            throw new InvalidClientMetadata();
        }
        if (!client.getAccessToken().equals(accessToken)) {
            //client access token doesn't match the access token supplied in the request
            logger.error("ConnectClientRegistration.getClient(): Invalid accessToken");
            throw new InvalidTokenException();
        }
        //remove the client fields that don't need to be reported.
        client.remove(REGISTRATION_ACCESS_TOKEN.getType());
        final JsonValue response = new JsonValue(convertClientReadResponseFormat(client.asMap()));
        response.put(EXPIRES_AT, 0);
        return response;
    } else {
        logger.error("ConnectClientRegistration.readRequest(): No client id sent");
        throw new InvalidRequestException();
    }
}
Also used : InvalidTokenException(org.forgerock.oauth2.core.exceptions.InvalidTokenException) UnauthorizedClientException(org.forgerock.oauth2.core.exceptions.UnauthorizedClientException) JsonValue(org.forgerock.json.JsonValue) InvalidRequestException(org.forgerock.oauth2.core.exceptions.InvalidRequestException) Client(org.forgerock.openidconnect.Client) InvalidClientMetadata(org.forgerock.openidconnect.exceptions.InvalidClientMetadata)

Example 2 with InvalidTokenException

use of org.forgerock.oauth2.core.exceptions.InvalidTokenException in project OpenAM by OpenRock.

the class TokenInfoServiceImpl method getTokenInfo.

/**
     * {@inheritDoc}
     */
public JsonValue getTokenInfo(OAuth2Request request) throws InvalidTokenException, InvalidRequestException, ExpiredTokenException, ServerException, BadRequestException, InvalidGrantException, NotFoundException {
    final AccessTokenVerifier.TokenState headerToken = headerTokenVerifier.verify(request);
    final AccessTokenVerifier.TokenState queryToken = queryTokenVerifier.verify(request);
    final Map<String, Object> response = new HashMap<String, Object>();
    if (!headerToken.isValid() && !queryToken.isValid()) {
        logger.error("Access Token not valid");
        throw new InvalidRequestException("Access Token not valid");
    } else if (headerToken.isValid() && queryToken.isValid()) {
        logger.error("Access Token provided in both query and header in request");
        throw new InvalidRequestException("Access Token cannot be provided in both query and header");
    } else {
        final AccessToken accessToken = request.getToken(AccessToken.class);
        logger.trace("In Validator resource - got token = " + accessToken);
        final OAuth2ProviderSettings providerSettings = providerSettingsFactory.get(request);
        final Map<String, Object> scopeEvaluation = providerSettings.evaluateScope(accessToken);
        response.putAll(accessToken.getTokenInfo());
        response.putAll(scopeEvaluation);
        return new JsonValue(response);
    }
}
Also used : HashMap(java.util.HashMap) JsonValue(org.forgerock.json.JsonValue) InvalidRequestException(org.forgerock.oauth2.core.exceptions.InvalidRequestException) HashMap(java.util.HashMap) Map(java.util.Map) AccessTokenVerifier(org.forgerock.oauth2.core.AccessTokenVerifier)

Example 3 with InvalidTokenException

use of org.forgerock.oauth2.core.exceptions.InvalidTokenException in project OpenAM by OpenRock.

the class AccessTokenProtectionFilter method beforeHandle.

@Override
protected int beforeHandle(Request request, Response response) {
    ChallengeResponse challengeResponse = request.getChallengeResponse();
    Status failure = null;
    if (challengeResponse == null) {
        failure = new Status(401, new InvalidTokenException());
    } else {
        String tokenId = challengeResponse.getRawValue();
        try {
            OAuth2Request oAuth2Request = requestFactory.create(request);
            AccessToken accessToken = tokenStore.readAccessToken(oAuth2Request, tokenId);
            if (accessToken == null || accessToken.isExpired()) {
                failure = new Status(401, new InvalidTokenException());
            } else if (requiredScope != null && !accessToken.getScope().contains(requiredScope)) {
                failure = new Status(403, new InsufficientScopeException(requiredScope));
            } else {
                oAuth2Request.setToken(AccessToken.class, accessToken);
            }
        } catch (ServerException e) {
            failure = new Status(500, e);
        } catch (NotFoundException e) {
            debug.message("Error loading token with id: " + tokenId, e);
            failure = new Status(404, e);
        } catch (InvalidGrantException e) {
            debug.message("Error loading token with id: " + tokenId, e);
            failure = new Status(401, new InvalidTokenException());
        }
    }
    if (failure != null) {
        response.setStatus(failure);
        return STOP;
    }
    return super.beforeHandle(request, response);
}
Also used : Status(org.restlet.data.Status) InvalidTokenException(org.forgerock.oauth2.core.exceptions.InvalidTokenException) InsufficientScopeException(org.forgerock.oauth2.core.exceptions.InsufficientScopeException) OAuth2Request(org.forgerock.oauth2.core.OAuth2Request) ServerException(org.forgerock.oauth2.core.exceptions.ServerException) AccessToken(org.forgerock.oauth2.core.AccessToken) NotFoundException(org.forgerock.oauth2.core.exceptions.NotFoundException) InvalidGrantException(org.forgerock.oauth2.core.exceptions.InvalidGrantException) ChallengeResponse(org.restlet.data.ChallengeResponse)

Example 4 with InvalidTokenException

use of org.forgerock.oauth2.core.exceptions.InvalidTokenException in project OpenAM by OpenRock.

the class UserInfoServiceImpl method getUserInfo.

/**
     * {@inheritDoc}
     */
public JsonValue getUserInfo(OAuth2Request request) throws OAuth2Exception {
    AccessTokenVerifier.TokenState headerToken = headerTokenVerifier.verify(request);
    AccessTokenVerifier.TokenState formToken = formTokenVerifier.verify(request);
    if (!headerToken.isValid() && !formToken.isValid()) {
        logger.debug("No access token provided for this request.");
        throw new InvalidTokenException();
    }
    if (headerToken.isValid() && formToken.isValid()) {
        logger.debug("Access token provided in both form and header.");
        throw new ServerException("Access Token cannot be provided in both form and header");
    }
    final String tokenId = headerToken.isValid() ? headerToken.getTokenId() : formToken.getTokenId();
    final AccessToken token = tokenStore.readAccessToken(request, tokenId);
    final OAuth2ProviderSettings providerSettings = providerSettingsFactory.get(request);
    return new JsonValue(providerSettings.getUserInfo(token, request).getValues());
}
Also used : InvalidTokenException(org.forgerock.oauth2.core.exceptions.InvalidTokenException) ServerException(org.forgerock.oauth2.core.exceptions.ServerException) AccessToken(org.forgerock.oauth2.core.AccessToken) JsonValue(org.forgerock.json.JsonValue) OAuth2ProviderSettings(org.forgerock.oauth2.core.OAuth2ProviderSettings) AccessTokenVerifier(org.forgerock.oauth2.core.AccessTokenVerifier)

Aggregations

JsonValue (org.forgerock.json.JsonValue)3 InvalidTokenException (org.forgerock.oauth2.core.exceptions.InvalidTokenException)3 AccessToken (org.forgerock.oauth2.core.AccessToken)2 AccessTokenVerifier (org.forgerock.oauth2.core.AccessTokenVerifier)2 InvalidRequestException (org.forgerock.oauth2.core.exceptions.InvalidRequestException)2 ServerException (org.forgerock.oauth2.core.exceptions.ServerException)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 OAuth2ProviderSettings (org.forgerock.oauth2.core.OAuth2ProviderSettings)1 OAuth2Request (org.forgerock.oauth2.core.OAuth2Request)1 InsufficientScopeException (org.forgerock.oauth2.core.exceptions.InsufficientScopeException)1 InvalidGrantException (org.forgerock.oauth2.core.exceptions.InvalidGrantException)1 NotFoundException (org.forgerock.oauth2.core.exceptions.NotFoundException)1 UnauthorizedClientException (org.forgerock.oauth2.core.exceptions.UnauthorizedClientException)1 Client (org.forgerock.openidconnect.Client)1 InvalidClientMetadata (org.forgerock.openidconnect.exceptions.InvalidClientMetadata)1 ChallengeResponse (org.restlet.data.ChallengeResponse)1 Status (org.restlet.data.Status)1