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.saml.idp.metadata;
20  
21  import java.nio.charset.StandardCharsets;
22  import java.util.Base64;
23  import java.util.Optional;
24  import org.apache.commons.lang3.tuple.Pair;
25  import org.apache.syncope.common.lib.to.SAML2IdPEntityTO;
26  import org.apache.syncope.common.rest.api.service.SAML2IdPEntityService;
27  import org.apache.syncope.wa.bootstrap.WARestClient;
28  import org.apereo.cas.support.saml.idp.metadata.generator.BaseSamlIdPMetadataGenerator;
29  import org.apereo.cas.support.saml.idp.metadata.generator.SamlIdPMetadataGeneratorConfigurationContext;
30  import org.apereo.cas.support.saml.services.SamlRegisteredService;
31  import org.apereo.cas.support.saml.services.idp.metadata.SamlIdPMetadataDocument;
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  
35  public class WASamlIdPMetadataGenerator extends BaseSamlIdPMetadataGenerator {
36  
37      protected static final Logger LOG = LoggerFactory.getLogger(WASamlIdPMetadataGenerator.class);
38  
39      protected final WARestClient waRestClient;
40  
41      public WASamlIdPMetadataGenerator(
42              final SamlIdPMetadataGeneratorConfigurationContext samlIdPMetadataGeneratorConfigurationContext,
43              final WARestClient waRestClient) {
44  
45          super(samlIdPMetadataGeneratorConfigurationContext);
46          this.waRestClient = waRestClient;
47      }
48  
49      @Override
50      protected SamlIdPMetadataDocument finalizeMetadataDocument(
51              final SamlIdPMetadataDocument doc,
52              final Optional<SamlRegisteredService> registeredService) throws Exception {
53  
54          doc.setAppliesTo(registeredService.
55                  map(SamlRegisteredService::getName).
56                  orElse(SAML2IdPEntityService.DEFAULT_OWNER));
57  
58          LOG.info("Setting new SAML2 IdP metadata document for {}", doc.getAppliesTo());
59  
60          SAML2IdPEntityTO entityTO = new SAML2IdPEntityTO.Builder().
61                  key(doc.getAppliesTo()).
62                  metadata(Base64.getEncoder().encodeToString(doc.getMetadata().getBytes(StandardCharsets.UTF_8))).
63                  build();
64          if (doc.getSigningKey() != null) {
65              entityTO.setSigningKey(Base64.getEncoder().encodeToString(
66                      doc.getSigningKey().getBytes(StandardCharsets.UTF_8)));
67          }
68          if (doc.getSigningCertificate() != null) {
69              entityTO.setSigningCertificate(Base64.getEncoder().encodeToString(
70                      doc.getSigningCertificate().getBytes(StandardCharsets.UTF_8)));
71          }
72          if (doc.getEncryptionKey() != null) {
73              entityTO.setEncryptionKey(Base64.getEncoder().encodeToString(
74                      doc.getEncryptionKey().getBytes(StandardCharsets.UTF_8)));
75          }
76          if (doc.getEncryptionCertificate() != null) {
77              entityTO.setEncryptionCertificate(Base64.getEncoder().encodeToString(
78                      doc.getEncryptionCertificate().getBytes(StandardCharsets.UTF_8)));
79          }
80  
81          waRestClient.getService(SAML2IdPEntityService.class).set(entityTO);
82  
83          return doc;
84      }
85  
86      @Override
87      public Pair<String, String> buildSelfSignedEncryptionCert(final Optional<SamlRegisteredService> registeredService)
88              throws Exception {
89  
90          return generateCertificateAndKey();
91      }
92  
93      @Override
94      public Pair<String, String> buildSelfSignedSigningCert(final Optional<SamlRegisteredService> registeredService)
95              throws Exception {
96  
97          return generateCertificateAndKey();
98      }
99  }