1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.syncope.sra.security.saml2;
20
21 import com.google.common.net.HttpHeaders;
22 import org.apache.commons.lang3.StringUtils;
23 import org.apache.syncope.sra.SRAProperties;
24 import org.opensaml.saml.common.xml.SAMLConstants;
25 import org.opensaml.saml.saml2.metadata.EntityDescriptor;
26 import org.pac4j.saml.client.SAML2Client;
27 import org.pac4j.saml.exceptions.SAMLException;
28 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
29 import org.springframework.http.MediaType;
30 import org.springframework.http.ResponseEntity;
31 import org.springframework.web.bind.annotation.GetMapping;
32 import org.springframework.web.bind.annotation.RequestMapping;
33 import org.springframework.web.bind.annotation.ResponseBody;
34 import org.springframework.web.bind.annotation.RestController;
35 import reactor.core.publisher.Mono;
36
37 @RestController
38 @RequestMapping(SAML2MetadataEndpoint.METADATA_URL)
39 @ConditionalOnProperty(prefix = SRAProperties.PREFIX, name = SRAProperties.AM_TYPE, havingValue = "SAML2")
40 public class SAML2MetadataEndpoint {
41
42 public static final String METADATA_URL = "/saml2/metadata";
43
44 private final String metadata;
45
46 public SAML2MetadataEndpoint(final SAML2Client saml2Client) {
47 EntityDescriptor entityDescriptor = (EntityDescriptor) saml2Client.getServiceProviderMetadataResolver().
48 getEntityDescriptorElement();
49 entityDescriptor.getSPSSODescriptor(SAMLConstants.SAML20P_NS).getSingleLogoutServices().
50 removeIf(slo -> !saml2Client.getConfiguration().
51 getSpLogoutResponseBindingType().equals(slo.getBinding()));
52 entityDescriptor.getSPSSODescriptor(SAMLConstants.SAML20P_NS).getSingleLogoutServices().
53 forEach(slo -> slo.setLocation(
54 StringUtils.substringBefore(slo.getLocation(), "?").replace("login", "logout")));
55
56 try {
57 this.metadata = saml2Client.getConfiguration().toMetadataGenerator().getMetadata(entityDescriptor);
58 } catch (Exception e) {
59 throw new SAMLException("Unable to fetch metadata", e);
60 }
61 }
62
63 @GetMapping(produces = { MediaType.APPLICATION_XML_VALUE })
64 @ResponseBody
65 public Mono<ResponseEntity<String>> metadata() {
66 return Mono.just(ResponseEntity.ok().
67 header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE).
68 body(metadata));
69 }
70 }