Search in sources :

Example 46 with OAuth2ProviderSettings

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

the class OpenAMOAuth2UrisFactory method getOAuth2Uris.

private synchronized OAuth2Uris getOAuth2Uris(String absoluteRealm, String baseUrlPattern) throws NotFoundException {
    OAuth2Uris uris = urisMap.get(baseUrlPattern);
    if (uris != null) {
        return uris;
    }
    OAuth2ProviderSettings oAuth2ProviderSettings = oAuth2ProviderSettingsFactory.get(absoluteRealm);
    uris = new OAuth2UrisImpl(baseUrlPattern, absoluteRealm, oAuth2ProviderSettings);
    urisMap.put(baseUrlPattern, uris);
    return uris;
}
Also used : OAuth2Uris(org.forgerock.oauth2.core.OAuth2Uris) OAuth2ProviderSettings(org.forgerock.oauth2.core.OAuth2ProviderSettings)

Example 47 with OAuth2ProviderSettings

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

the class IdentityManager method getResourceOwnerIdentity.

/**
     * Gets a resource owner's identity.
     *
     * @param username The resource owner's username.
     * @param realm The resource owner's realm.
     * @return The resource owner's identity.
     * @throws UnauthorizedClientException If the resource owner's identity cannot be found.
     */
public AMIdentity getResourceOwnerIdentity(String username, final String realm) throws UnauthorizedClientException {
    final SSOToken token = AccessController.doPrivileged(AdminTokenAction.getInstance());
    final AMIdentity amIdentity;
    try {
        final AMIdentityRepository amIdRepo = new AMIdentityRepository(token, realm);
        final IdSearchControl idsc = new IdSearchControl();
        idsc.setRecursive(true);
        idsc.setAllReturnAttributes(true);
        // search for the identity
        final Set<AMIdentity> results = new HashSet<AMIdentity>();
        idsc.setMaxResults(0);
        IdSearchResults searchResults = amIdRepo.searchIdentities(IdType.USER, username, idsc);
        if (searchResults != null && !searchResults.getResultAttributes().isEmpty()) {
            results.addAll(searchResults.getSearchResults());
        } else {
            OAuth2ProviderSettings settings = providerSettingsFactory.get(new OAuth2Request() {

                public <T> T getRequest() {
                    throw new UnsupportedOperationException("Realm parameter only OAuth2Request");
                }

                public <T> T getParameter(String name) {
                    if ("realm".equals(name)) {
                        return (T) realm;
                    }
                    throw new UnsupportedOperationException("Realm parameter only OAuth2Request");
                }

                public JsonValue getBody() {
                    throw new UnsupportedOperationException("Realm parameter only OAuth2Request");
                }

                @Override
                public Locale getLocale() {
                    throw new UnsupportedOperationException();
                }
            });
            final Map<String, Set<String>> avPairs = toAvPairMap(settings.getResourceOwnerAuthenticatedAttributes(), username);
            idsc.setSearchModifiers(IdSearchOpModifier.OR, avPairs);
            searchResults = amIdRepo.searchIdentities(IdType.USER, "*", idsc);
            if (searchResults != null) {
                results.addAll(searchResults.getSearchResults());
            }
        }
        if (results.size() != 1) {
            logger.error("No user profile or more than one profile found.");
            throw new UnauthorizedClientException("Not able to get user from OpenAM");
        }
        amIdentity = results.iterator().next();
        //if the client is deactivated return null
        if (amIdentity.isActive()) {
            return amIdentity;
        } else {
            return null;
        }
    } catch (Exception e) {
        logger.error("Unable to get client AMIdentity: ", e);
        throw new UnauthorizedClientException("Not able to get client from OpenAM");
    }
}
Also used : Locale(java.util.Locale) SSOToken(com.iplanet.sso.SSOToken) Set(java.util.Set) HashSet(java.util.HashSet) IdSearchResults(com.sun.identity.idm.IdSearchResults) JsonValue(org.forgerock.json.JsonValue) UnauthorizedClientException(org.forgerock.oauth2.core.exceptions.UnauthorizedClientException) OAuth2Request(org.forgerock.oauth2.core.OAuth2Request) AMIdentity(com.sun.identity.idm.AMIdentity) UnauthorizedClientException(org.forgerock.oauth2.core.exceptions.UnauthorizedClientException) AMIdentityRepository(com.sun.identity.idm.AMIdentityRepository) IdSearchControl(com.sun.identity.idm.IdSearchControl) OAuth2ProviderSettings(org.forgerock.oauth2.core.OAuth2ProviderSettings) HashSet(java.util.HashSet)

Example 48 with OAuth2ProviderSettings

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

the class OAuth2UserApplications method getResourceResponse.

private ResourceResponse getResourceResponse(Context context, String clientId, Iterable<JsonValue> tokens) throws NotFoundException, InvalidClientException, ServerException, InternalServerErrorException {
    String realm = getAttributeValue(tokens.iterator().next(), REALM.getOAuthField());
    OAuth2ProviderSettings oAuth2ProviderSettings = oAuth2ProviderSettingsFactory.get(context);
    ClientRegistration clientRegistration = clientRegistrationStore.get(clientId, realm, context);
    Map<String, String> scopeDescriptions = clientRegistration.getScopeDescriptions(getLocale(context));
    Map<String, String> scopes = new HashMap<>();
    for (JsonValue token : tokens) {
        for (String scope : token.get(SCOPE.getOAuthField()).asSet(String.class)) {
            if (scopeDescriptions.containsKey(scope)) {
                scopes.put(scope, scopeDescriptions.get(scope));
            } else {
                scopes.put(scope, scope);
            }
        }
    }
    String displayName = clientRegistration.getDisplayName(getLocale(context));
    String expiryDateTime = calculateExpiryDateTime(tokens, oAuth2ProviderSettings);
    JsonValue content = json(object(field("_id", clientId), field("name", displayName), field("scopes", scopes), field("expiryDateTime", expiryDateTime)));
    return Responses.newResourceResponse(clientId, String.valueOf(content.getObject().hashCode()), content);
}
Also used : ClientRegistration(org.forgerock.oauth2.core.ClientRegistration) HashMap(java.util.HashMap) JsonValue(org.forgerock.json.JsonValue) OAuth2ProviderSettings(org.forgerock.oauth2.core.OAuth2ProviderSettings)

Example 49 with OAuth2ProviderSettings

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

the class SubjectTypeValidator method validateRequest.

@Override
public void validateRequest(OAuth2Request request) throws InvalidClientException, NotFoundException, ServerException {
    final OAuth2ProviderSettings settings = providerSettingsFactory.get(request);
    final Set<String> subjectTypesSupported = settings.getSupportedSubjectTypes();
    final String subjectType = clientRegistrationStore.get((String) request.getParameter(OAuth2Constants.Params.CLIENT_ID), request).getSubjectType().toLowerCase();
    for (String supported : subjectTypesSupported) {
        if (supported.toLowerCase().equals(subjectType)) {
            return;
        }
    }
    throw failureFactory.getException(request, "Server does not support this client's subject type.");
}
Also used : OAuth2ProviderSettings(org.forgerock.oauth2.core.OAuth2ProviderSettings)

Example 50 with OAuth2ProviderSettings

use of org.forgerock.oauth2.core.OAuth2ProviderSettings 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

OAuth2ProviderSettings (org.forgerock.oauth2.core.OAuth2ProviderSettings)39 ServerException (org.forgerock.oauth2.core.exceptions.ServerException)18 OAuth2Request (org.forgerock.oauth2.core.OAuth2Request)15 JsonValue (org.forgerock.json.JsonValue)9 AccessToken (org.forgerock.oauth2.core.AccessToken)9 HashSet (java.util.HashSet)8 OAuth2Uris (org.forgerock.oauth2.core.OAuth2Uris)8 HashMap (java.util.HashMap)7 ResourceSetStore (org.forgerock.oauth2.resources.ResourceSetStore)7 Request (org.restlet.Request)7 NotFoundException (org.forgerock.oauth2.core.exceptions.NotFoundException)6 ResourceSetDescription (org.forgerock.oauth2.resources.ResourceSetDescription)6 JSONObject (org.json.JSONObject)6 Test (org.testng.annotations.Test)6 UnauthorizedClientException (org.forgerock.oauth2.core.exceptions.UnauthorizedClientException)5 CoreTokenException (org.forgerock.openam.cts.exceptions.CoreTokenException)5 OpenIdConnectClientRegistration (org.forgerock.openidconnect.OpenIdConnectClientRegistration)5 BeforeTest (org.testng.annotations.BeforeTest)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 OAuth2ProviderSettingsFactory (org.forgerock.oauth2.core.OAuth2ProviderSettingsFactory)4