View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.wa.bootstrap.mapping;
20  
21  import java.util.Map;
22  import java.util.stream.Collectors;
23  import org.apache.commons.lang3.StringUtils;
24  import org.apache.syncope.client.lib.SyncopeClient;
25  import org.apache.syncope.common.lib.attr.AttrRepoConf;
26  import org.apache.syncope.common.lib.attr.JDBCAttrRepoConf;
27  import org.apache.syncope.common.lib.attr.LDAPAttrRepoConf;
28  import org.apache.syncope.common.lib.attr.StubAttrRepoConf;
29  import org.apache.syncope.common.lib.attr.SyncopeAttrRepoConf;
30  import org.apache.syncope.common.lib.to.AttrRepoTO;
31  import org.apache.syncope.common.lib.to.Item;
32  import org.apache.syncope.wa.bootstrap.WARestClient;
33  import org.apereo.cas.configuration.CasCoreConfigurationUtils;
34  import org.apereo.cas.configuration.model.core.authentication.AttributeRepositoryStates;
35  import org.apereo.cas.configuration.model.core.authentication.StubPrincipalAttributesProperties;
36  import org.apereo.cas.configuration.model.support.jdbc.JdbcPrincipalAttributesProperties;
37  import org.apereo.cas.configuration.model.support.ldap.LdapPrincipalAttributesProperties;
38  import org.apereo.cas.configuration.model.support.syncope.SyncopePrincipalAttributesProperties;
39  
40  public class AttrRepoPropertySourceMapper extends PropertySourceMapper implements AttrRepoConf.Mapper {
41  
42      protected final WARestClient waRestClient;
43  
44      public AttrRepoPropertySourceMapper(final WARestClient waRestClient) {
45          this.waRestClient = waRestClient;
46      }
47  
48      @Override
49      public Map<String, Object> map(final AttrRepoTO attrRepoTO, final StubAttrRepoConf conf) {
50          StubPrincipalAttributesProperties props = new StubPrincipalAttributesProperties();
51          props.setId(attrRepoTO.getKey());
52          props.setState(AttributeRepositoryStates.valueOf(attrRepoTO.getState().name()));
53          props.setOrder(attrRepoTO.getOrder());
54          props.setAttributes(conf.getAttributes());
55  
56          return prefix("cas.authn.attribute-repository.stub.", CasCoreConfigurationUtils.asMap(props));
57      }
58  
59      @Override
60      public Map<String, Object> map(final AttrRepoTO attrRepoTO, final LDAPAttrRepoConf conf) {
61          LdapPrincipalAttributesProperties props = new LdapPrincipalAttributesProperties();
62          props.setId(attrRepoTO.getKey());
63          props.setState(AttributeRepositoryStates.valueOf(attrRepoTO.getState().name()));
64          props.setOrder(attrRepoTO.getOrder());
65          props.setUseAllQueryAttributes(conf.isUseAllQueryAttributes());
66          props.setQueryAttributes(conf.getQueryAttributes());
67          props.setAttributes(attrRepoTO.getItems().stream().
68                  collect(Collectors.toMap(Item::getIntAttrName, Item::getExtAttrName)));
69          fill(props, conf);
70  
71          return prefix("cas.authn.attribute-repository.ldap[].", CasCoreConfigurationUtils.asMap(props));
72      }
73  
74      @Override
75      public Map<String, Object> map(final AttrRepoTO attrRepoTO, final JDBCAttrRepoConf conf) {
76          JdbcPrincipalAttributesProperties props = new JdbcPrincipalAttributesProperties();
77          props.setId(attrRepoTO.getKey());
78          props.setState(AttributeRepositoryStates.valueOf(attrRepoTO.getState().name()));
79          props.setOrder(attrRepoTO.getOrder());
80          props.setSql(conf.getSql());
81          props.setSingleRow(conf.isSingleRow());
82          props.setRequireAllAttributes(conf.isRequireAllAttributes());
83          props.setCaseCanonicalization(conf.getCaseCanonicalization().name());
84          props.setQueryType(conf.getQueryType().name());
85          props.setColumnMappings(conf.getColumnMappings());
86          props.setUsername(conf.getUsername());
87          props.setCaseInsensitiveQueryAttributes(conf.getCaseInsensitiveQueryAttributes());
88          props.setQueryAttributes(conf.getQueryAttributes());
89          props.setAttributes(attrRepoTO.getItems().stream().
90                  collect(Collectors.toMap(Item::getIntAttrName, Item::getExtAttrName)));
91          fill(props, conf);
92  
93          return prefix("cas.authn.attribute-repository.jdbc[].", CasCoreConfigurationUtils.asMap(props));
94      }
95  
96      @Override
97      public Map<String, Object> map(final AttrRepoTO attrRepoTO, final SyncopeAttrRepoConf conf) {
98          SyncopeClient syncopeClient = waRestClient.getSyncopeClient();
99          if (syncopeClient == null) {
100             LOG.warn("Application context is not ready to bootstrap WA configuration");
101             return Map.of();
102         }
103 
104         SyncopePrincipalAttributesProperties props = new SyncopePrincipalAttributesProperties();
105         props.setId(attrRepoTO.getKey());
106         props.setState(AttributeRepositoryStates.valueOf(attrRepoTO.getState().name()));
107         props.setOrder(attrRepoTO.getOrder());
108         props.setDomain(conf.getDomain());
109         props.setUrl(StringUtils.substringBefore(syncopeClient.getAddress(), "/rest"));
110         props.setSearchFilter(conf.getSearchFilter());
111         props.setBasicAuthUsername(conf.getBasicAuthUsername());
112         props.setBasicAuthPassword(conf.getBasicAuthPassword());
113         props.setHeaders(props.getHeaders());
114         props.setAttributeMappings(attrRepoTO.getItems().
115                 stream().collect(Collectors.toMap(Item::getIntAttrName, Item::getExtAttrName)));
116 
117         return prefix("cas.authn.attribute-repository.syncope.", CasCoreConfigurationUtils.asMap(props));
118     }
119 }