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