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 static org.junit.jupiter.api.Assertions.assertNotNull;
22  import static org.mockito.ArgumentMatchers.any;
23  import static org.mockito.ArgumentMatchers.anyString;
24  import static org.mockito.Mockito.doNothing;
25  import static org.mockito.Mockito.mock;
26  import static org.mockito.Mockito.when;
27  
28  import java.io.File;
29  import java.io.IOException;
30  import java.nio.charset.StandardCharsets;
31  import org.apache.commons.io.IOUtils;
32  import org.apache.syncope.common.lib.to.SAML2SPEntityTO;
33  import org.apache.syncope.common.rest.api.service.SAML2SPEntityService;
34  import org.apache.syncope.wa.bootstrap.WARestClient;
35  import org.junit.jupiter.api.Test;
36  import org.opensaml.saml.saml2.metadata.EntityDescriptor;
37  import org.pac4j.saml.client.SAML2Client;
38  import org.pac4j.saml.metadata.SAML2MetadataGenerator;
39  import org.springframework.core.io.ClassPathResource;
40  
41  public class WASAML2ClientMetadataGeneratorTest extends BaseWASAML2ClientTest {
42  
43      private static WARestClient getWaRestClient() throws IOException {
44          SAML2SPEntityTO metadataTO = new SAML2SPEntityTO.Builder()
45                  .key("Syncope")
46                  .metadata(IOUtils.toString(new ClassPathResource("sp-metadata.xml").getInputStream(),
47                          StandardCharsets.UTF_8))
48                  .build();
49  
50          SAML2SPEntityService saml2SPMetadataService = mock(SAML2SPEntityService.class);
51          when(saml2SPMetadataService.get(anyString())).thenReturn(metadataTO);
52          doNothing().when(saml2SPMetadataService).set(any(SAML2SPEntityTO.class));
53  
54          WARestClient waRestClient = mock(WARestClient.class);
55          when(waRestClient.getService(SAML2SPEntityService.class)).thenReturn(saml2SPMetadataService);
56          return waRestClient;
57      }
58  
59      @Test
60      public void storeMetadata() throws Exception {
61          SAML2Client client = getSAML2Client();
62          String keystoreFile = File.createTempFile("keystore", "jks").getCanonicalPath();
63          client.getConfiguration().setKeystoreResourceFilepath(keystoreFile);
64  
65          SAML2MetadataGenerator generator = new WASAML2ClientMetadataGenerator(getWaRestClient(), client);
66          EntityDescriptor entityDescriptor = generator.buildEntityDescriptor();
67          String metadata = generator.getMetadata(entityDescriptor);
68          assertNotNull(generator.storeMetadata(metadata, null, false));
69      }
70  }