1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 }