Search in sources :

Example 91 with JsonValue

use of org.forgerock.json.JsonValue in project OpenAM by OpenRock.

the class STSInstanceConfig method marshalFromAttributeMap.

public static STSInstanceConfig marshalFromAttributeMap(Map<String, Set<String>> attributeMap) {
    Map<String, Object> jsonAttributes = MapMarshallUtils.toJsonValueMap(attributeMap);
    /*
        If SAML2Config state is not present, null will be returned. That will tell me that no SAML2Config
        had been present initially.
         */
    SAML2Config saml2Config = SAML2Config.marshalFromAttributeMap(attributeMap);
    if (saml2Config != null) {
        jsonAttributes.put(SAML2_CONFIG, saml2Config.toJson());
    }
    OpenIdConnectTokenConfig openIdConnectTokenConfig = OpenIdConnectTokenConfig.marshalFromAttributeMap(attributeMap);
    if (openIdConnectTokenConfig != null) {
        jsonAttributes.put(OIDC_ID_TOKEN_CONFIG, openIdConnectTokenConfig.toJson());
    }
    return fromJson(new JsonValue(jsonAttributes));
}
Also used : JsonValue(org.forgerock.json.JsonValue)

Example 92 with JsonValue

use of org.forgerock.json.JsonValue in project OpenAM by OpenRock.

the class RestSTSInstanceConfig method fromJson.

public static RestSTSInstanceConfig fromJson(JsonValue json) {
    if (json == null) {
        throw new NullPointerException("JsonValue cannot be null!");
    }
    STSInstanceConfig baseConfig = STSInstanceConfig.fromJson(json);
    RestSTSInstanceConfigBuilderBase<?> builder = RestSTSInstanceConfig.builder().saml2Config(baseConfig.getSaml2Config()).oidcIdTokenConfig(baseConfig.getOpenIdConnectTokenConfig()).persistIssuedTokensInCTS(baseConfig.persistIssuedTokensInCTS()).deploymentConfig(DeploymentConfig.fromJson(json.get(DEPLOYMENT_CONFIG)));
    JsonValue supportedTranslations = json.get(SUPPORTED_TOKEN_TRANSFORMS);
    if (!supportedTranslations.isNull()) {
        if (!supportedTranslations.isList()) {
            throw new IllegalStateException("Unexpected value for the " + SUPPORTED_TOKEN_TRANSFORMS + " field: " + supportedTranslations.asString());
        }
        List<TokenTransformConfig> transformConfigList = new ArrayList<>();
        for (Object translation : supportedTranslations.asList()) {
            transformConfigList.add(TokenTransformConfig.fromJson(new JsonValue(translation)));
        }
        builder.setSupportedTokenTransforms(transformConfigList);
    }
    JsonValue customTranslations = json.get(CUSTOM_TOKEN_TRANSFORMS);
    if (!customTranslations.isNull()) {
        if (!customTranslations.isList()) {
            throw new IllegalStateException("Unexpected value for the " + CUSTOM_TOKEN_TRANSFORMS + " field: " + customTranslations.asString());
        }
        List<TokenTransformConfig> transformConfigList = new ArrayList<>();
        for (Object translation : customTranslations.asList()) {
            transformConfigList.add(TokenTransformConfig.fromJson(new JsonValue(translation)));
        }
        builder.setCustomTokenTransforms(transformConfigList);
    }
    JsonValue customValidators = json.get(CUSTOM_TOKEN_VALIDATORS);
    if (!customValidators.isNull()) {
        if (!customValidators.isList()) {
            throw new IllegalStateException("Unexpected value for the " + CUSTOM_TOKEN_VALIDATORS + " field: " + customValidators.asString());
        }
        List<CustomTokenOperation> customValidatorsList = new ArrayList<>();
        for (Object translation : customValidators.asList()) {
            customValidatorsList.add(CustomTokenOperation.fromJson(new JsonValue(translation)));
        }
        builder.setCustomValidators(customValidatorsList);
    }
    JsonValue customProviders = json.get(CUSTOM_TOKEN_PROVIDERS);
    if (!customProviders.isNull()) {
        if (!customProviders.isList()) {
            throw new IllegalStateException("Unexpected value for the " + CUSTOM_TOKEN_PROVIDERS + " field: " + customProviders.asString());
        }
        List<CustomTokenOperation> customProvidersList = new ArrayList<>();
        for (Object translation : customProviders.asList()) {
            customProvidersList.add(CustomTokenOperation.fromJson(new JsonValue(translation)));
        }
        builder.setCustomProviders(customProvidersList);
    }
    return builder.build();
}
Also used : JsonValue(org.forgerock.json.JsonValue) CustomTokenOperation(org.forgerock.openam.sts.config.user.CustomTokenOperation) STSInstanceConfig(org.forgerock.openam.sts.config.user.STSInstanceConfig)

Example 93 with JsonValue

use of org.forgerock.json.JsonValue in project OpenAM by OpenRock.

the class RestSTSInstanceConfig method marshalFromAttributeMap.

/*
    When we are marshaling back from a Map<String, Set<String>>, this Map contains all of the values, also those
    contributed by encapsulated complex objects. So the structure must be 'un-flattened', where the top-level map
    is passed to encapsulated complex-objects, so that they may re-constitute themselves, and then the top-level json entry
    key is set to point at these re-constituted complex objects.

    Not that the marshalToAttributeMap first calls toJson to obtain the map representation, albeit with hierarchical
    elements, which must be subsequently flattened. The 'flattening' performed by the marshalToAttributeMap must then
     be 'inverted' by this method, where all complex objects are re-constituted, using the state in the flattened map.

     */
public static RestSTSInstanceConfig marshalFromAttributeMap(Map<String, Set<String>> attributeMap) {
    DeploymentConfig deploymentConfig = DeploymentConfig.marshalFromAttributeMap(attributeMap);
    Map<String, Object> jsonAttributes = MapMarshallUtils.toJsonValueMap(attributeMap);
    jsonAttributes.remove(DEPLOYMENT_CONFIG);
    jsonAttributes.put(DEPLOYMENT_CONFIG, deploymentConfig.toJson());
    SAML2Config saml2Config = SAML2Config.marshalFromAttributeMap(attributeMap);
    if (saml2Config != null) {
        jsonAttributes.remove(SAML2_CONFIG);
        jsonAttributes.put(SAML2_CONFIG, saml2Config.toJson());
    }
    OpenIdConnectTokenConfig openIdConnectTokenConfig = OpenIdConnectTokenConfig.marshalFromAttributeMap(attributeMap);
    if (openIdConnectTokenConfig != null) {
        jsonAttributes.remove(OIDC_ID_TOKEN_CONFIG);
        jsonAttributes.put(OIDC_ID_TOKEN_CONFIG, openIdConnectTokenConfig.toJson());
    }
    /*
         The SUPPORTED_TOKEN_TRANSFORMS, CUSTOM_TOKEN_TRANSFORMS, CUSTOM_TOKEN_VALIDATORS, and CUSTOM_TOKEN_PROVIDERS
          are currently each in a String representation in the Set<String> map entry corresponding
         to their respective key. I need to marshal each back into a TokenTransformConfig instance, and then
         call toJson on each, and put them in a JsonValue wrapping a list.
         */
    ArrayList<JsonValue> jsonTranslationsList = new ArrayList<>();
    JsonValue jsonTranslations = new JsonValue(jsonTranslationsList);
    jsonAttributes.remove(SUPPORTED_TOKEN_TRANSFORMS);
    jsonAttributes.put(SUPPORTED_TOKEN_TRANSFORMS, jsonTranslations);
    Set<String> stringTokenTranslations = attributeMap.get(SUPPORTED_TOKEN_TRANSFORMS);
    for (String translation : stringTokenTranslations) {
        jsonTranslationsList.add(TokenTransformConfig.fromSMSString(translation).toJson());
    }
    ArrayList<JsonValue> jsonCustomTranslationsList = new ArrayList<>();
    JsonValue jsonCustomTranslations = new JsonValue(jsonCustomTranslationsList);
    jsonAttributes.remove(CUSTOM_TOKEN_TRANSFORMS);
    jsonAttributes.put(CUSTOM_TOKEN_TRANSFORMS, jsonCustomTranslations);
    Set<String> stringCustomTranslations = attributeMap.get(CUSTOM_TOKEN_TRANSFORMS);
    for (String translation : stringCustomTranslations) {
        jsonCustomTranslationsList.add(TokenTransformConfig.fromSMSString(translation).toJson());
    }
    ArrayList<JsonValue> jsonCustomValidatorsList = new ArrayList<>();
    JsonValue jsonCustomValidators = new JsonValue(jsonCustomValidatorsList);
    jsonAttributes.remove(CUSTOM_TOKEN_VALIDATORS);
    jsonAttributes.put(CUSTOM_TOKEN_VALIDATORS, jsonCustomValidators);
    Set<String> stringCustomValidators = attributeMap.get(CUSTOM_TOKEN_VALIDATORS);
    for (String validator : stringCustomValidators) {
        jsonCustomValidatorsList.add(CustomTokenOperation.fromSMSString(validator).toJson());
    }
    ArrayList<JsonValue> jsonCustomProvidersList = new ArrayList<>();
    JsonValue jsonCustomProviders = new JsonValue(jsonCustomProvidersList);
    jsonAttributes.remove(CUSTOM_TOKEN_PROVIDERS);
    jsonAttributes.put(CUSTOM_TOKEN_PROVIDERS, jsonCustomProviders);
    Set<String> stringCustomProviders = attributeMap.get(CUSTOM_TOKEN_PROVIDERS);
    for (String provider : stringCustomProviders) {
        jsonCustomProvidersList.add(CustomTokenOperation.fromSMSString(provider).toJson());
    }
    return fromJson(new JsonValue(jsonAttributes));
}
Also used : SAML2Config(org.forgerock.openam.sts.config.user.SAML2Config) JsonValue(org.forgerock.json.JsonValue) DeploymentConfig(org.forgerock.openam.sts.config.user.DeploymentConfig) OpenIdConnectTokenConfig(org.forgerock.openam.sts.config.user.OpenIdConnectTokenConfig)

Example 94 with JsonValue

use of org.forgerock.json.JsonValue in project OpenAM by OpenRock.

the class SoapDelegationConfig method marshalFromAttributeMap.

public static SoapDelegationConfig marshalFromAttributeMap(Map<String, Set<String>> attributeMap) {
    //first check to see if the relevant attributes are present, indicating that a non-null instance can be created
    if (CollectionUtils.isEmpty(attributeMap.get(DELEGATION_TOKEN_VALIDATORS)) && CollectionUtils.isEmpty(attributeMap.get(CUSTOM_DELEGATION_TOKEN_HANDLERS))) {
        return null;
    }
    Map<String, Object> jsonAttributes = MapMarshallUtils.toJsonValueMap(attributeMap);
    /*
         The DELEGATION_TOKEN_VALIDATORS are currently each in a String representation in the Set<String> map entry corresponding
         to the DELEGATION_TOKEN_VALIDATORS key. I need to marshal each back into a TokenValidationConfig instance, and then
         call toJson on each, and put them in a JsonValue wrapping a list.
         */
    if (attributeMap.get(DELEGATION_TOKEN_VALIDATORS) != null) {
        ArrayList<JsonValue> jsonValidationConfigList = new ArrayList<>();
        JsonValue jsonTranslations = new JsonValue(jsonValidationConfigList);
        jsonAttributes.remove(DELEGATION_TOKEN_VALIDATORS);
        jsonAttributes.put(DELEGATION_TOKEN_VALIDATORS, jsonTranslations);
        Set<String> stringTokenTranslations = attributeMap.get(DELEGATION_TOKEN_VALIDATORS);
        for (String translation : stringTokenTranslations) {
            jsonValidationConfigList.add(TokenValidationConfig.fromSMSString(translation).toJson());
        }
    }
    /*
        Ultimately, the CUSTOM_DELEGATION_TOKEN_HANDLERS is a set, but it's set type gets stripped by the MapMarshalUtils.toJsonValueMap
        method. Thus it is a 'complex' object, which must be reconstituted in this method. Note also that the map may not
        have an entry if the instance was first marshaled to json, which is the first step in marshaling to an attribute map.
         */
    if (attributeMap.get(CUSTOM_DELEGATION_TOKEN_HANDLERS) != null) {
        Set<String> jsonHandlerSet = new HashSet<>();
        JsonValue jsonHandlerTypes = new JsonValue(jsonHandlerSet);
        jsonAttributes.remove(CUSTOM_DELEGATION_TOKEN_HANDLERS);
        jsonAttributes.put(CUSTOM_DELEGATION_TOKEN_HANDLERS, jsonHandlerTypes);
        Set<String> handlerClasses = attributeMap.get(CUSTOM_DELEGATION_TOKEN_HANDLERS);
        for (String handlerClass : handlerClasses) {
            jsonHandlerSet.add(handlerClass);
        }
    }
    return fromJson(new JsonValue(jsonAttributes));
}
Also used : ArrayList(java.util.ArrayList) JsonValue(org.forgerock.json.JsonValue) HashSet(java.util.HashSet)

Example 95 with JsonValue

use of org.forgerock.json.JsonValue in project OpenAM by OpenRock.

the class RestSTSInstanceConfigTest method testFieldPersistenceJsonMapMarshalRoundTrip.

@Test
public void testFieldPersistenceJsonMapMarshalRoundTrip() throws IOException {
    RestSTSInstanceConfig config = createInstanceConfig("/bob", WITH_TLS_OFFLOAD_CONFIG, WITH_SAML2_CONFIG, WITH_OIDC_CONFIG, !WITH_CUSTOM_VALIDATOR, !WITH_CUSTOM_PROVIDER, WITH_CTS_TOKEN_PERSISTENCE);
    Map<String, Set<String>> attributeMap = config.marshalToAttributeMap();
    JsonValue jsonMap = new JsonValue(attributeMap);
    assertEquals(config.persistIssuedTokensInCTS(), RestSTSInstanceConfig.marshalFromJsonAttributeMap(jsonMap).persistIssuedTokensInCTS());
    config = createInstanceConfig("/bob", WITH_TLS_OFFLOAD_CONFIG, WITH_SAML2_CONFIG, WITH_OIDC_CONFIG, !WITH_CUSTOM_VALIDATOR, !WITH_CUSTOM_PROVIDER, !WITH_CTS_TOKEN_PERSISTENCE);
    attributeMap = config.marshalToAttributeMap();
    jsonMap = new JsonValue(attributeMap);
    System.out.println("After marshalling to attribute map: " + attributeMap);
    assertEquals(config.persistIssuedTokensInCTS(), RestSTSInstanceConfig.marshalFromJsonAttributeMap(jsonMap).persistIssuedTokensInCTS());
    config = createInstanceConfig("/bob", WITH_TLS_OFFLOAD_CONFIG, WITH_SAML2_CONFIG, WITH_OIDC_CONFIG, !WITH_CUSTOM_VALIDATOR, !WITH_CUSTOM_PROVIDER, WITH_CTS_TOKEN_PERSISTENCE);
    assertEquals(config.persistIssuedTokensInCTS(), RestSTSInstanceConfig.marshalFromAttributeMap(config.marshalToAttributeMap()).persistIssuedTokensInCTS());
    config = createInstanceConfig("/bob", WITH_TLS_OFFLOAD_CONFIG, WITH_SAML2_CONFIG, WITH_OIDC_CONFIG, !WITH_CUSTOM_VALIDATOR, !WITH_CUSTOM_PROVIDER, !WITH_CTS_TOKEN_PERSISTENCE);
    System.out.println("After marshalling to attribute map: " + config.marshalToAttributeMap());
    assertEquals(config.persistIssuedTokensInCTS(), RestSTSInstanceConfig.marshalFromAttributeMap(config.marshalToAttributeMap()).persistIssuedTokensInCTS());
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) JsonValue(org.forgerock.json.JsonValue) Test(org.testng.annotations.Test)

Aggregations

JsonValue (org.forgerock.json.JsonValue)575 Test (org.testng.annotations.Test)333 ResourceException (org.forgerock.json.resource.ResourceException)144 ResourceResponse (org.forgerock.json.resource.ResourceResponse)123 RealmContext (org.forgerock.openam.rest.RealmContext)70 Context (org.forgerock.services.context.Context)63 HashSet (java.util.HashSet)56 SSOException (com.iplanet.sso.SSOException)54 ArrayList (java.util.ArrayList)51 BadRequestException (org.forgerock.json.resource.BadRequestException)47 Privilege (com.sun.identity.entitlement.Privilege)46 InternalServerErrorException (org.forgerock.json.resource.InternalServerErrorException)46 SSOToken (com.iplanet.sso.SSOToken)43 SMSException (com.sun.identity.sm.SMSException)42 HashMap (java.util.HashMap)42 NotFoundException (org.forgerock.json.resource.NotFoundException)41 SSOTokenContext (org.forgerock.openam.rest.resource.SSOTokenContext)41 CreateRequest (org.forgerock.json.resource.CreateRequest)40 OpenSSOPrivilege (com.sun.identity.entitlement.opensso.OpenSSOPrivilege)39 Subject (javax.security.auth.Subject)32