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;
20  
21  import java.io.InputStream;
22  import java.lang.reflect.Method;
23  import java.util.List;
24  import java.util.stream.Collectors;
25  import org.apache.commons.lang3.ArrayUtils;
26  import org.apache.syncope.common.lib.SyncopeClientException;
27  import org.apache.syncope.common.lib.to.SAML2SP4UIIdPTO;
28  import org.apache.syncope.common.lib.types.ClientExceptionType;
29  import org.apache.syncope.common.lib.types.SAML2SP4UIEntitlement;
30  import org.apache.syncope.core.logic.saml2.SAML2ClientCache;
31  import org.apache.syncope.core.persistence.api.dao.NotFoundException;
32  import org.apache.syncope.core.persistence.api.dao.SAML2SP4UIIdPDAO;
33  import org.apache.syncope.core.persistence.api.entity.SAML2SP4UIIdP;
34  import org.apache.syncope.core.provisioning.api.data.SAML2SP4UIIdPDataBinder;
35  import org.springframework.core.io.support.ResourcePatternResolver;
36  import org.springframework.security.access.prepost.PreAuthorize;
37  import org.springframework.transaction.annotation.Transactional;
38  
39  public class SAML2SP4UIIdPLogic extends AbstractSAML2SP4UILogic {
40  
41      protected final SAML2ClientCache saml2ClientCacheLogin;
42  
43      protected final SAML2ClientCache saml2ClientCacheLogout;
44  
45      protected final SAML2SP4UIIdPDataBinder binder;
46  
47      protected final SAML2SP4UIIdPDAO idpDAO;
48  
49      public SAML2SP4UIIdPLogic(
50              final SAML2SP4UIProperties props,
51              final ResourcePatternResolver resourceResolver,
52              final SAML2ClientCache saml2ClientCacheLogin,
53              final SAML2ClientCache saml2ClientCacheLogout,
54              final SAML2SP4UIIdPDataBinder binder,
55              final SAML2SP4UIIdPDAO idpDAO) {
56  
57          super(props, resourceResolver);
58          this.saml2ClientCacheLogin = saml2ClientCacheLogin;
59          this.saml2ClientCacheLogout = saml2ClientCacheLogout;
60          this.binder = binder;
61          this.idpDAO = idpDAO;
62      }
63  
64      @PreAuthorize("isAuthenticated()")
65      @Transactional(readOnly = true)
66      public List<SAML2SP4UIIdPTO> list() {
67          return idpDAO.findAll().stream().map(binder::getIdPTO).collect(Collectors.toList());
68      }
69  
70      @PreAuthorize("hasRole('" + SAML2SP4UIEntitlement.IDP_READ + "')")
71      @Transactional(readOnly = true)
72      public SAML2SP4UIIdPTO read(final String key) {
73          SAML2SP4UIIdP idp = idpDAO.find(key);
74          if (idp == null) {
75              throw new NotFoundException("SAML 2.0 IdP '" + key + '\'');
76          }
77  
78          return binder.getIdPTO(idp);
79      }
80  
81      @PreAuthorize("hasRole('" + SAML2SP4UIEntitlement.IDP_IMPORT + "')")
82      public String importFromMetadata(final InputStream input) {
83          try {
84              SAML2SP4UIIdPTO idpTO = SAML2ClientCache.importMetadata(input, newSAML2Configuration());
85              SAML2SP4UIIdP idp = binder.create(idpTO);
86  
87              return idp.getKey();
88          } catch (SyncopeClientException e) {
89              throw e;
90          } catch (Exception e) {
91              LOG.error("Unexpected error while importing IdP metadata", e);
92              SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InvalidEntity);
93              sce.getElements().add(e.getMessage());
94              throw sce;
95          }
96      }
97  
98      @PreAuthorize("hasRole('" + SAML2SP4UIEntitlement.IDP_UPDATE + "')")
99      public void update(final SAML2SP4UIIdPTO saml2IdpTO) {
100         SAML2SP4UIIdP idp = idpDAO.find(saml2IdpTO.getKey());
101         if (idp == null) {
102             throw new NotFoundException("SAML 2.0 IdP '" + saml2IdpTO.getKey() + '\'');
103         }
104 
105         idp = binder.update(idp, saml2IdpTO);
106         saml2ClientCacheLogin.removeAll(idp.getEntityID());
107         saml2ClientCacheLogout.removeAll(idp.getEntityID());
108     }
109 
110     @PreAuthorize("hasRole('" + SAML2SP4UIEntitlement.IDP_DELETE + "')")
111     public void delete(final String key) {
112         SAML2SP4UIIdP idp = idpDAO.find(key);
113         if (idp == null) {
114             throw new NotFoundException("SAML 2.0 IdP '" + key + '\'');
115         }
116 
117         idpDAO.delete(key);
118         saml2ClientCacheLogin.removeAll(idp.getEntityID());
119         saml2ClientCacheLogout.removeAll(idp.getEntityID());
120     }
121 
122     @Override
123     protected SAML2SP4UIIdPTO resolveReference(final Method method, final Object... args)
124             throws UnresolvedReferenceException {
125 
126         String key = null;
127 
128         if (ArrayUtils.isNotEmpty(args)) {
129             for (int i = 0; key == null && i < args.length; i++) {
130                 if (args[i] instanceof String) {
131                     key = (String) args[i];
132                 } else if (args[i] instanceof SAML2SP4UIIdPTO) {
133                     key = ((SAML2SP4UIIdPTO) args[i]).getKey();
134                 }
135             }
136         }
137 
138         if (key != null) {
139             try {
140                 SAML2SP4UIIdP idp = idpDAO.find(key);
141                 return binder.getIdPTO(idp);
142             } catch (Throwable ignore) {
143                 LOG.debug("Unresolved reference", ignore);
144                 throw new UnresolvedReferenceException(ignore);
145             }
146         }
147 
148         throw new UnresolvedReferenceException();
149     }
150 }