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.logic.wa;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  import java.util.stream.Collectors;
24  import java.util.stream.Stream;
25  import org.apache.syncope.common.lib.types.ClientAppType;
26  import org.apache.syncope.common.lib.types.IdRepoEntitlement;
27  import org.apache.syncope.common.lib.wa.WAClientApp;
28  import org.apache.syncope.core.persistence.api.dao.CASSPClientAppDAO;
29  import org.apache.syncope.core.persistence.api.dao.NotFoundException;
30  import org.apache.syncope.core.persistence.api.dao.OIDCRPClientAppDAO;
31  import org.apache.syncope.core.persistence.api.dao.SAML2SPClientAppDAO;
32  import org.apache.syncope.core.persistence.api.entity.am.CASSPClientApp;
33  import org.apache.syncope.core.persistence.api.entity.am.OIDCRPClientApp;
34  import org.apache.syncope.core.persistence.api.entity.am.SAML2SPClientApp;
35  import org.apache.syncope.core.provisioning.api.data.wa.WAClientAppDataBinder;
36  import org.springframework.security.access.prepost.PreAuthorize;
37  import org.springframework.transaction.annotation.Transactional;
38  
39  public class WAClientAppLogic {
40  
41      protected final WAClientAppDataBinder binder;
42  
43      protected final CASSPClientAppDAO casSPClientAppDAO;
44  
45      protected final OIDCRPClientAppDAO oidcRPClientAppDAO;
46  
47      protected final SAML2SPClientAppDAO saml2SPClientAppDAO;
48  
49      public WAClientAppLogic(
50              final WAClientAppDataBinder binder,
51              final CASSPClientAppDAO casSPClientAppDAO,
52              final OIDCRPClientAppDAO oidcRPClientAppDAO,
53              final SAML2SPClientAppDAO saml2SPClientAppDAO) {
54  
55          this.binder = binder;
56          this.casSPClientAppDAO = casSPClientAppDAO;
57          this.oidcRPClientAppDAO = oidcRPClientAppDAO;
58          this.saml2SPClientAppDAO = saml2SPClientAppDAO;
59      }
60  
61      @PreAuthorize("hasRole('" + IdRepoEntitlement.ANONYMOUS + "')")
62      @Transactional(readOnly = true)
63      public List<WAClientApp> list() {
64          List<WAClientApp> clientApps = new ArrayList<>();
65  
66          Stream.of(ClientAppType.values()).forEach(type -> {
67              switch (type) {
68                  case OIDCRP:
69                      clientApps.addAll(oidcRPClientAppDAO.findAll().stream().
70                              map(binder::getWAClientApp).collect(Collectors.toList()));
71                      break;
72  
73                  case SAML2SP:
74                      clientApps.addAll(saml2SPClientAppDAO.findAll().stream().
75                              map(binder::getWAClientApp).collect(Collectors.toList()));
76                      break;
77  
78                  case CASSP:
79                  default:
80                      clientApps.addAll(casSPClientAppDAO.findAll().stream().
81                              map(binder::getWAClientApp).collect(Collectors.toList()));
82              }
83          });
84  
85          return clientApps;
86      }
87  
88      protected WAClientApp doRead(final Long clientAppId, final ClientAppType type) {
89          WAClientApp clientApp = null;
90  
91          switch (type) {
92              case OIDCRP:
93                  OIDCRPClientApp oidcrp = oidcRPClientAppDAO.findByClientAppId(clientAppId);
94                  if (oidcrp != null) {
95                      clientApp = binder.getWAClientApp(oidcrp);
96                  }
97                  break;
98  
99              case SAML2SP:
100                 SAML2SPClientApp saml2sp = saml2SPClientAppDAO.findByClientAppId(clientAppId);
101                 if (saml2sp != null) {
102                     clientApp = binder.getWAClientApp(saml2sp);
103                 }
104                 break;
105 
106             case CASSP:
107                 CASSPClientApp cassp = casSPClientAppDAO.findByClientAppId(clientAppId);
108                 if (cassp != null) {
109                     clientApp = binder.getWAClientApp(cassp);
110                 }
111                 break;
112 
113             default:
114         }
115 
116         return clientApp;
117     }
118 
119     @PreAuthorize("hasRole('" + IdRepoEntitlement.ANONYMOUS + "')")
120     @Transactional(readOnly = true)
121     public WAClientApp read(final Long clientAppId, final ClientAppType type) {
122         WAClientApp clientApp = null;
123         if (type == null) {
124             for (int i = 0; i < ClientAppType.values().length && clientApp == null; i++) {
125                 clientApp = doRead(clientAppId, ClientAppType.values()[i]);
126             }
127         } else {
128             clientApp = doRead(clientAppId, type);
129         }
130 
131         if (clientApp == null) {
132             throw new NotFoundException(
133                     "Client app with clientApp ID " + clientAppId + " and type " + type + " not found");
134         }
135         return clientApp;
136     }
137 
138     protected WAClientApp doRead(final String name, final ClientAppType type) {
139         WAClientApp clientApp = null;
140 
141         switch (type) {
142             case OIDCRP:
143                 OIDCRPClientApp oidcrp = oidcRPClientAppDAO.findByName(name);
144                 if (oidcrp != null) {
145                     clientApp = binder.getWAClientApp(oidcrp);
146                 }
147                 break;
148 
149             case SAML2SP:
150                 SAML2SPClientApp saml2sp = saml2SPClientAppDAO.findByName(name);
151                 if (saml2sp != null) {
152                     clientApp = binder.getWAClientApp(saml2sp);
153                 }
154                 break;
155 
156             case CASSP:
157                 CASSPClientApp cassp = casSPClientAppDAO.findByName(name);
158                 if (cassp != null) {
159                     clientApp = binder.getWAClientApp(cassp);
160                 }
161                 break;
162 
163             default:
164         }
165 
166         return clientApp;
167     }
168 
169     @PreAuthorize("hasRole('" + IdRepoEntitlement.ANONYMOUS + "')")
170     @Transactional(readOnly = true)
171     public WAClientApp read(final String name, final ClientAppType type) {
172         WAClientApp clientApp = null;
173         if (type == null) {
174             for (int i = 0; i < ClientAppType.values().length && clientApp == null; i++) {
175                 clientApp = doRead(name, ClientAppType.values()[i]);
176             }
177         } else {
178             clientApp = doRead(name, type);
179         }
180 
181         if (clientApp == null) {
182             throw new NotFoundException("Client app with name " + name + " with type " + type + " not found");
183         }
184         return clientApp;
185     }
186 }