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.core.provisioning.java.data.wa;
20  
21  import org.apache.syncope.common.lib.policy.AuthPolicyConf;
22  import org.apache.syncope.common.lib.policy.DefaultAuthPolicyConf;
23  import org.apache.syncope.common.lib.wa.WAClientApp;
24  import org.apache.syncope.core.persistence.api.dao.AuthModuleDAO;
25  import org.apache.syncope.core.persistence.api.entity.am.AuthModule;
26  import org.apache.syncope.core.persistence.api.entity.am.ClientApp;
27  import org.apache.syncope.core.provisioning.api.data.AuthModuleDataBinder;
28  import org.apache.syncope.core.provisioning.api.data.ClientAppDataBinder;
29  import org.apache.syncope.core.provisioning.api.data.PolicyDataBinder;
30  import org.apache.syncope.core.provisioning.api.data.wa.WAClientAppDataBinder;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  public class WAClientAppDataBinderImpl implements WAClientAppDataBinder {
35  
36      protected static final Logger LOG = LoggerFactory.getLogger(WAClientAppDataBinder.class);
37  
38      protected final ClientAppDataBinder clientAppDataBinder;
39  
40      protected final PolicyDataBinder policyDataBinder;
41  
42      protected final AuthModuleDataBinder authModuleDataBinder;
43  
44      protected final AuthModuleDAO authModuleDAO;
45  
46      public WAClientAppDataBinderImpl(
47              final ClientAppDataBinder clientAppDataBinder,
48              final PolicyDataBinder policyDataBinder,
49              final AuthModuleDataBinder authModuleDataBinder,
50              final AuthModuleDAO authModuleDAO) {
51  
52          this.clientAppDataBinder = clientAppDataBinder;
53          this.policyDataBinder = policyDataBinder;
54          this.authModuleDataBinder = authModuleDataBinder;
55          this.authModuleDAO = authModuleDAO;
56      }
57  
58      @Override
59      public WAClientApp getWAClientApp(final ClientApp clientApp) {
60          WAClientApp waClientApp = new WAClientApp();
61          waClientApp.setClientAppTO(clientAppDataBinder.getClientAppTO(clientApp));
62  
63          try {
64              AuthPolicyConf authPolicyConf = null;
65              if (clientApp.getAuthPolicy() != null) {
66                  authPolicyConf = clientApp.getAuthPolicy().getConf();
67                  waClientApp.setAuthPolicy(policyDataBinder.getPolicyTO(clientApp.getAuthPolicy()));
68              } else if (clientApp.getRealm() != null && clientApp.getRealm().getAuthPolicy() != null) {
69                  authPolicyConf = clientApp.getRealm().getAuthPolicy().getConf();
70                  waClientApp.setAuthPolicy(policyDataBinder.getPolicyTO(clientApp.getRealm().getAuthPolicy()));
71              }
72              if (authPolicyConf instanceof DefaultAuthPolicyConf) {
73                  ((DefaultAuthPolicyConf) authPolicyConf).getAuthModules().forEach(key -> {
74                      AuthModule authModule = authModuleDAO.find(key);
75                      if (authModule == null) {
76                          LOG.warn("AuthModule " + authModule + " not found");
77                      } else {
78                          waClientApp.getAuthModules().add(authModuleDataBinder.getAuthModuleTO(authModule));
79                      }
80                  });
81              }
82  
83              if (clientApp.getAccessPolicy() != null) {
84                  waClientApp.setAccessPolicy(policyDataBinder.getPolicyTO(clientApp.getAccessPolicy()));
85              } else if (clientApp.getRealm() != null && clientApp.getRealm().getAccessPolicy() != null) {
86                  waClientApp.setAccessPolicy(policyDataBinder.getPolicyTO(clientApp.getRealm().getAccessPolicy()));
87              }
88  
89              if (clientApp.getAttrReleasePolicy() != null) {
90                  waClientApp.setAttrReleasePolicy(
91                          policyDataBinder.getPolicyTO(clientApp.getAttrReleasePolicy()));
92              } else if (clientApp.getRealm() != null && clientApp.getRealm().getAttrReleasePolicy() != null) {
93                  waClientApp.setAttrReleasePolicy(
94                          policyDataBinder.getPolicyTO(clientApp.getRealm().getAttrReleasePolicy()));
95              }
96  
97              if (clientApp.getTicketExpirationPolicy() != null) {
98                  waClientApp.setTicketExpirationPolicy(
99                          policyDataBinder.getPolicyTO(clientApp.getTicketExpirationPolicy()));
100             } else if (clientApp.getRealm() != null && clientApp.getRealm().getTicketExpirationPolicy() != null) {
101                 waClientApp.setTicketExpirationPolicy(
102                         policyDataBinder.getPolicyTO(clientApp.getRealm().getTicketExpirationPolicy()));
103             }
104         } catch (Exception e) {
105             LOG.error("While building the configuration from an application's policy ", e);
106         }
107 
108         return waClientApp;
109     }
110 }