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.starter.mapping;
20  
21  import java.util.Optional;
22  import org.apache.syncope.common.lib.clientapps.AnonymousUsernameAttributeProviderConf;
23  import org.apache.syncope.common.lib.clientapps.DefaultUsernameAttributeProviderConf;
24  import org.apache.syncope.common.lib.clientapps.GroovyUsernameAttributeProviderConf;
25  import org.apache.syncope.common.lib.clientapps.PairwiseOidcUsernameAttributeProviderConf;
26  import org.apache.syncope.common.lib.clientapps.PrincipalAttributeUsernameAttributeProviderConf;
27  import org.apache.syncope.common.lib.clientapps.UsernameAttributeProviderConf;
28  import org.apache.syncope.common.lib.types.PersistentIdGenerator;
29  import org.apereo.cas.authentication.principal.OidcPairwisePersistentIdGenerator;
30  import org.apereo.cas.authentication.principal.ShibbolethCompatiblePersistentIdGenerator;
31  import org.apereo.cas.services.AnonymousRegisteredServiceUsernameAttributeProvider;
32  import org.apereo.cas.services.BaseWebBasedRegisteredService;
33  import org.apereo.cas.services.DefaultRegisteredServiceUsernameProvider;
34  import org.apereo.cas.services.GroovyRegisteredServiceUsernameProvider;
35  import org.apereo.cas.services.PairwiseOidcRegisteredServiceUsernameAttributeProvider;
36  import org.apereo.cas.services.PrincipalAttributeRegisteredServiceUsernameProvider;
37  import org.apereo.cas.util.RandomUtils;
38  
39  public class DefaultUsernameAttributeProviderConfMapper implements UsernameAttributeProviderConf.Mapper {
40  
41      protected static Optional<org.apereo.cas.authentication.principal.PersistentIdGenerator> toPersistentIdGenerator(
42              final PersistentIdGenerator persistentIdGenerator) {
43  
44          if (persistentIdGenerator == null) {
45              return Optional.empty();
46          }
47  
48          org.apereo.cas.authentication.principal.PersistentIdGenerator result = null;
49          switch (persistentIdGenerator) {
50              case SHIBBOLETH:
51                  result = new ShibbolethCompatiblePersistentIdGenerator(RandomUtils.randomAlphanumeric(16));
52                  break;
53  
54              case OIDC:
55                  result = new OidcPairwisePersistentIdGenerator();
56                  break;
57  
58              default:
59          }
60  
61          return Optional.ofNullable(result);
62      }
63  
64      protected final BaseWebBasedRegisteredService service;
65  
66      public DefaultUsernameAttributeProviderConfMapper(final BaseWebBasedRegisteredService service) {
67          this.service = service;
68      }
69  
70      @Override
71      public void map(final AnonymousUsernameAttributeProviderConf conf) {
72          AnonymousRegisteredServiceUsernameAttributeProvider provider =
73                  new AnonymousRegisteredServiceUsernameAttributeProvider();
74          toPersistentIdGenerator(conf.getPersistentIdGenerator()).ifPresent(provider::setPersistentIdGenerator);
75          provider.setCanonicalizationMode(conf.getCaseCanonicalizationMode().name());
76          service.setUsernameAttributeProvider(provider);
77      }
78  
79      @Override
80      public void map(final DefaultUsernameAttributeProviderConf conf) {
81          service.setUsernameAttributeProvider(
82                  new DefaultRegisteredServiceUsernameProvider(conf.getCaseCanonicalizationMode().name()));
83      }
84  
85      @Override
86      public void map(final GroovyUsernameAttributeProviderConf conf) {
87          GroovyRegisteredServiceUsernameProvider provider =
88                  new GroovyRegisteredServiceUsernameProvider(conf.getGroovyScript());
89          provider.setCanonicalizationMode(conf.getCaseCanonicalizationMode().name());
90          service.setUsernameAttributeProvider(provider);
91      }
92  
93      @Override
94      public void map(final PairwiseOidcUsernameAttributeProviderConf conf) {
95          PairwiseOidcRegisteredServiceUsernameAttributeProvider provider =
96                  new PairwiseOidcRegisteredServiceUsernameAttributeProvider();
97          toPersistentIdGenerator(conf.getPersistentIdGenerator()).ifPresent(provider::setPersistentIdGenerator);
98          provider.setCanonicalizationMode(conf.getCaseCanonicalizationMode().name());
99          service.setUsernameAttributeProvider(provider);
100     }
101 
102     @Override
103     public void map(final PrincipalAttributeUsernameAttributeProviderConf conf) {
104         service.setUsernameAttributeProvider(
105                 new PrincipalAttributeRegisteredServiceUsernameProvider(
106                         conf.getUsernameAttribute(), conf.getCaseCanonicalizationMode().name()));
107     }
108 }