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.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  }