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.util.Base64;
22  import net.shibboleth.utilities.java.support.resolver.ResolverException;
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.impl.AbstractReloadingMetadataResolver;
27  import org.pac4j.saml.client.SAML2Client;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  public class WASAML2MetadataResolver extends AbstractReloadingMetadataResolver {
32  
33      protected static final Logger LOG = LoggerFactory.getLogger(WASAML2MetadataResolver.class);
34  
35      protected final WARestClient waRestClient;
36  
37      protected final SAML2Client saml2Client;
38  
39      public WASAML2MetadataResolver(final WARestClient waRestClient, final SAML2Client saml2Client) {
40          this.waRestClient = waRestClient;
41          this.saml2Client = saml2Client;
42      }
43  
44      @Override
45      protected String getMetadataIdentifier() {
46          return saml2Client.getName();
47      }
48  
49      @Override
50      protected byte[] fetchMetadata() throws ResolverException {
51          try {
52              SAML2SPEntityTO metadataTO = waRestClient.getService(SAML2SPEntityService.class).get(saml2Client.getName());
53              return Base64.getDecoder().decode(metadataTO.getMetadata());
54          } catch (Exception e) {
55              String message = "Unable to fetch SP metadata for " + saml2Client.getName();
56              LOG.error(message, e);
57              throw new ResolverException(message);
58          }
59      }
60  }