Skip to content

Commit

Permalink
Align with CAS upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
ilgrosso committed Oct 25, 2023
1 parent dc595c5 commit 534c75c
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.apache.syncope.common.lib.to.AttrRepoTO;
import org.apache.syncope.common.lib.to.Item;
import org.apache.syncope.wa.bootstrap.WARestClient;
import org.apereo.cas.configuration.CasCoreConfigurationUtils;
import org.apereo.cas.configuration.model.core.authentication.AttributeRepositoryStates;
import org.apereo.cas.configuration.model.core.authentication.StubPrincipalAttributesProperties;
import org.apereo.cas.configuration.model.support.jdbc.JdbcPrincipalAttributesProperties;
Expand All @@ -53,7 +52,7 @@ public Map<String, Object> map(final AttrRepoTO attrRepoTO, final StubAttrRepoCo
props.setOrder(attrRepoTO.getOrder());
props.setAttributes(conf.getAttributes());

return prefix("cas.authn.attribute-repository.stub.", CasCoreConfigurationUtils.asMap(props));
return prefix("cas.authn.attribute-repository.stub.", WAConfUtils.asMap(props));
}

@Override
Expand All @@ -68,7 +67,7 @@ public Map<String, Object> map(final AttrRepoTO attrRepoTO, final LDAPAttrRepoCo
collect(Collectors.toMap(Item::getIntAttrName, Item::getExtAttrName)));
fill(props, conf);

return prefix("cas.authn.attribute-repository.ldap[].", CasCoreConfigurationUtils.asMap(props));
return prefix("cas.authn.attribute-repository.ldap[].", WAConfUtils.asMap(props));
}

@Override
Expand All @@ -90,7 +89,7 @@ public Map<String, Object> map(final AttrRepoTO attrRepoTO, final JDBCAttrRepoCo
collect(Collectors.toMap(Item::getIntAttrName, Item::getExtAttrName)));
fill(props, conf);

return prefix("cas.authn.attribute-repository.jdbc[].", CasCoreConfigurationUtils.asMap(props));
return prefix("cas.authn.attribute-repository.jdbc[].", WAConfUtils.asMap(props));
}

@Override
Expand All @@ -114,6 +113,6 @@ public Map<String, Object> map(final AttrRepoTO attrRepoTO, final SyncopeAttrRep
props.setAttributeMappings(attrRepoTO.getItems().
stream().collect(Collectors.toMap(Item::getIntAttrName, Item::getExtAttrName)));

return prefix("cas.authn.attribute-repository.syncope.", CasCoreConfigurationUtils.asMap(props));
return prefix("cas.authn.attribute-repository.syncope.", WAConfUtils.asMap(props));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import org.apache.syncope.common.lib.to.Item;
import org.apache.syncope.common.lib.types.AuthModuleState;
import org.apache.syncope.wa.bootstrap.WARestClient;
import org.apereo.cas.configuration.CasCoreConfigurationUtils;
import org.apereo.cas.configuration.model.core.authentication.AuthenticationHandlerStates;
import org.apereo.cas.configuration.model.support.generic.AcceptAuthenticationProperties;
import org.apereo.cas.configuration.model.support.jaas.JaasAuthenticationProperties;
Expand Down Expand Up @@ -89,7 +88,7 @@ public Map<String, Object> map(final AuthModuleTO authModuleTO, final StaticAuth
collect(Collectors.joining(","));
props.setUsers(users);

return prefix("cas.authn.accept.", CasCoreConfigurationUtils.asMap(props));
return prefix("cas.authn.accept.", WAConfUtils.asMap(props));
}

@Override
Expand Down Expand Up @@ -119,7 +118,7 @@ public Map<String, Object> map(final AuthModuleTO authModuleTO, final LDAPAuthMo

fill(props, conf);

return prefix("cas.authn.ldap[].", CasCoreConfigurationUtils.asMap(props));
return prefix("cas.authn.ldap[].", WAConfUtils.asMap(props));
}

@Override
Expand All @@ -136,7 +135,7 @@ public Map<String, Object> map(final AuthModuleTO authModuleTO, final JDBCAuthMo
map(item -> item.getIntAttrName() + ":" + item.getExtAttrName()).collect(Collectors.toList()));
fill(props, conf);

return prefix("cas.authn.jdbc.query[].", CasCoreConfigurationUtils.asMap(props));
return prefix("cas.authn.jdbc.query[].", WAConfUtils.asMap(props));
}

@Override
Expand All @@ -151,7 +150,7 @@ public Map<String, Object> map(final AuthModuleTO authModuleTO, final JaasAuthMo
props.setLoginConfigType(conf.getLoginConfigurationFile());
props.setRealm(conf.getRealm());

return prefix("cas.authn.jaas[].", CasCoreConfigurationUtils.asMap(props));
return prefix("cas.authn.jaas[].", WAConfUtils.asMap(props));
}

@Override
Expand All @@ -173,7 +172,7 @@ public Map<String, Object> map(final AuthModuleTO authModuleTO, final OAuth20Aut
props.setProfileAttrs(authModuleTO.getItems().stream().
collect(Collectors.toMap(Item::getIntAttrName, Item::getExtAttrName)));

return prefix("cas.authn.pac4j.oauth2[].", CasCoreConfigurationUtils.asMap(props));
return prefix("cas.authn.pac4j.oauth2[].", WAConfUtils.asMap(props));
}

protected void map(
Expand Down Expand Up @@ -205,7 +204,7 @@ public Map<String, Object> map(final AuthModuleTO authModuleTO, final OIDCAuthMo
Pac4jOidcClientProperties client = new Pac4jOidcClientProperties();
client.setGeneric(props);

return prefix("cas.authn.pac4j.oidc[].generic.", CasCoreConfigurationUtils.asMap(props));
return prefix("cas.authn.pac4j.oidc[].generic.", WAConfUtils.asMap(props));
}

@Override
Expand All @@ -217,7 +216,7 @@ public Map<String, Object> map(final AuthModuleTO authModuleTO, final AzureOIDCA
Pac4jOidcClientProperties client = new Pac4jOidcClientProperties();
client.setAzure(props);

return prefix("cas.authn.pac4j.oidc[].azure.", CasCoreConfigurationUtils.asMap(props));
return prefix("cas.authn.pac4j.oidc[].azure.", WAConfUtils.asMap(props));
}

@Override
Expand All @@ -228,7 +227,7 @@ public Map<String, Object> map(final AuthModuleTO authModuleTO, final GoogleOIDC
Pac4jOidcClientProperties client = new Pac4jOidcClientProperties();
client.setGoogle(props);

return prefix("cas.authn.pac4j.oidc[].google.", CasCoreConfigurationUtils.asMap(props));
return prefix("cas.authn.pac4j.oidc[].google.", WAConfUtils.asMap(props));
}

@Override
Expand All @@ -241,7 +240,7 @@ public Map<String, Object> map(final AuthModuleTO authModuleTO, final KeycloakOI
Pac4jOidcClientProperties client = new Pac4jOidcClientProperties();
client.setKeycloak(props);

return prefix("cas.authn.pac4j.oidc[].keycloak.", CasCoreConfigurationUtils.asMap(props));
return prefix("cas.authn.pac4j.oidc[].keycloak.", WAConfUtils.asMap(props));
}

@Override
Expand All @@ -256,7 +255,7 @@ public Map<String, Object> map(final AuthModuleTO authModuleTO, final AppleOIDCA
Pac4jOidcClientProperties client = new Pac4jOidcClientProperties();
client.setApple(props);

return prefix("cas.authn.pac4j.oidc[].apple.", CasCoreConfigurationUtils.asMap(props));
return prefix("cas.authn.pac4j.oidc[].apple.", WAConfUtils.asMap(props));
}

@Override
Expand Down Expand Up @@ -286,7 +285,7 @@ public Map<String, Object> map(final AuthModuleTO authModuleTO, final SAML2IdPAu
? TriStateBoolean.UNDEFINED
: TriStateBoolean.valueOf(conf.getNameIdPolicyAllowCreate().toUpperCase()));

return prefix("cas.authn.pac4j.saml[].", CasCoreConfigurationUtils.asMap(props));
return prefix("cas.authn.pac4j.saml[].", WAConfUtils.asMap(props));
}

@Override
Expand All @@ -305,7 +304,7 @@ public Map<String, Object> map(final AuthModuleTO authModuleTO, final SyncopeAut
props.setAttributeMappings(authModuleTO.getItems().stream().
collect(Collectors.toMap(Item::getIntAttrName, Item::getExtAttrName)));

return prefix("cas.authn.syncope.", CasCoreConfigurationUtils.asMap(props));
return prefix("cas.authn.syncope.", WAConfUtils.asMap(props));
}

@Override
Expand All @@ -326,7 +325,7 @@ public Map<String, Object> map(final AuthModuleTO authModuleTO, final GoogleMfaA
props.setLdap(ldapProps);
}

return prefix("cas.authn.mfa.gauth.", CasCoreConfigurationUtils.asMap(props));
return prefix("cas.authn.mfa.gauth.", WAConfUtils.asMap(props));
}

@Override
Expand All @@ -338,7 +337,7 @@ public Map<String, Object> map(final AuthModuleTO authModuleTO, final DuoMfaAuth
props.setDuoIntegrationKey(conf.getIntegrationKey());
props.setDuoSecretKey(conf.getSecretKey());

return prefix("cas.authn.mfa.duo.", CasCoreConfigurationUtils.asMap(props));
return prefix("cas.authn.mfa.duo.", WAConfUtils.asMap(props));
}

@Override
Expand All @@ -364,6 +363,6 @@ public Map<String, Object> map(final AuthModuleTO authModuleTO, final SimpleMfaA
}
}

return prefix("cas.authn.mfa.simple.", CasCoreConfigurationUtils.asMap(props));
return prefix("cas.authn.mfa.simple.", WAConfUtils.asMap(props));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.syncope.wa.bootstrap.mapping;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
import java.io.IOException;
import java.io.Serializable;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import org.apereo.cas.configuration.CasCoreConfigurationUtils;
import org.apereo.cas.util.function.FunctionUtils;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.util.ResourceUtils;

public final class WAConfUtils {

private static class ResourceSerializer extends StdSerializer<Resource> {

private static final long serialVersionUID = 7971411664567411958L;

ResourceSerializer() {
this(null);
}

ResourceSerializer(final Class<Resource> t) {
super(t);
}

@Override
public void serialize(final Resource value,
final JsonGenerator jgen,
final SerializerProvider provider) throws IOException {

if (value instanceof ClassPathResource) {
jgen.writeString(ResourceUtils.CLASSPATH_URL_PREFIX + value.getFilename());
} else {
jgen.writeString(value.getURI().toString());
}
}
}

private static final YAMLMapper YAML_MAPPER;

static {
YAML_MAPPER = new YAMLMapper();
YAML_MAPPER.setFilterProvider(new SimpleFilterProvider().setFailOnUnknownId(false));
YAML_MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
YAML_MAPPER.setPropertyNamingStrategy(PropertyNamingStrategies.KEBAB_CASE);

SimpleModule module = new SimpleModule();
module.addSerializer(Resource.class, new ResourceSerializer());
YAML_MAPPER.registerModule(module);
}

public static Map<String, Object> asMap(final Serializable properties) {
return FunctionUtils.doUnchecked(() -> {
try (StringWriter writer = new StringWriter()) {
YAML_MAPPER.writeValue(writer, properties);
ByteArrayResource resource = new ByteArrayResource(writer.toString().getBytes(StandardCharsets.UTF_8));
return CasCoreConfigurationUtils.loadYamlProperties(resource);
}
});
}

private WAConfUtils() {
// private construcator for static utility class
}
}

0 comments on commit 534c75c

Please sign in to comment.