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.pac4j.saml;
20  
21  import java.nio.charset.StandardCharsets;
22  import java.util.Base64;
23  import org.apache.syncope.common.lib.to.SAML2SPEntityTO;
24  import org.apache.syncope.common.rest.api.service.SAML2SPEntityService;
25  import org.apache.syncope.wa.bootstrap.WARestClient;
26  import org.opensaml.saml.metadata.resolver.MetadataResolver;
27  import org.opensaml.saml.metadata.resolver.impl.AbstractBatchMetadataResolver;
28  import org.pac4j.saml.client.SAML2Client;
29  import org.pac4j.saml.metadata.BaseSAML2MetadataGenerator;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  import org.springframework.core.io.Resource;
33  
34  public class WASAML2ClientMetadataGenerator extends BaseSAML2MetadataGenerator {
35  
36      protected static final Logger LOG = LoggerFactory.getLogger(WASAML2ClientMetadataGenerator.class);
37  
38      protected final WARestClient waRestClient;
39  
40      protected final SAML2Client saml2Client;
41  
42      public WASAML2ClientMetadataGenerator(final WARestClient waRestClient, final SAML2Client saml2Client) {
43          this.waRestClient = waRestClient;
44          this.saml2Client = saml2Client;
45      }
46  
47      @Override
48      public boolean storeMetadata(final String metadata, final Resource resource, final boolean force) throws Exception {
49          return true;
50      }
51  
52      @Override
53      protected AbstractBatchMetadataResolver createMetadataResolver(final Resource metadataResource) {
54          return new WASAML2MetadataResolver(waRestClient, saml2Client);
55      }
56  
57      @Override
58      public MetadataResolver buildMetadataResolver(final Resource metadataResource) throws Exception {
59          String encodedMetadata = Base64.getEncoder().encodeToString(
60                  getMetadata(buildEntityDescriptor()).getBytes(StandardCharsets.UTF_8));
61  
62          SAML2SPEntityTO entityTO;
63          try {
64              entityTO = waRestClient.getService(SAML2SPEntityService.class).get(saml2Client.getName());
65              entityTO.setMetadata(encodedMetadata);
66          } catch (Exception e) {
67              LOG.debug("SP Entity {} not found, creating new", saml2Client.getName(), e);
68  
69              entityTO = new SAML2SPEntityTO.Builder().
70                      key(saml2Client.getName()).
71                      metadata(encodedMetadata).
72                      build();
73          }
74  
75          LOG.debug("Storing SP Entity {}", entityTO);
76          waRestClient.getService(SAML2SPEntityService.class).set(entityTO);
77  
78          return super.buildMetadataResolver(metadataResource);
79      }
80  }