Skip to content

Commit 3a41567

Browse files
committed
Add OpenSamlSigningUtilsTests
Issue gh-11354
1 parent 8998972 commit 3a41567

File tree

1 file changed

+89
-0
lines changed
  • saml2/saml2-service-provider/src/test/java/org/springframework/security/saml2/provider/service/web/authentication/logout

1 file changed

+89
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright 2002-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.saml2.provider.service.web.authentication.logout;
18+
19+
import java.util.UUID;
20+
21+
import javax.xml.namespace.QName;
22+
23+
import org.junit.jupiter.api.BeforeEach;
24+
import org.junit.jupiter.api.Test;
25+
import org.opensaml.core.xml.XMLObject;
26+
import org.opensaml.core.xml.config.XMLObjectProviderRegistrySupport;
27+
import org.opensaml.saml.common.SAMLVersion;
28+
import org.opensaml.saml.saml2.core.Issuer;
29+
import org.opensaml.saml.saml2.core.Response;
30+
import org.opensaml.xmlsec.signature.Signature;
31+
32+
import org.springframework.security.saml2.core.OpenSamlInitializationService;
33+
import org.springframework.security.saml2.core.TestSaml2X509Credentials;
34+
import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration;
35+
36+
import static org.assertj.core.api.Assertions.assertThat;
37+
38+
/**
39+
* Test open SAML signatures
40+
*/
41+
public class OpenSamlSigningUtilsTests {
42+
43+
static {
44+
OpenSamlInitializationService.initialize();
45+
}
46+
47+
private RelyingPartyRegistration registration;
48+
49+
@BeforeEach
50+
public void setup() {
51+
this.registration = RelyingPartyRegistration.withRegistrationId("saml-idp")
52+
.entityId("https://some.idp.example.com/entity-id").signingX509Credentials((c) -> {
53+
c.add(TestSaml2X509Credentials.relyingPartySigningCredential());
54+
c.add(TestSaml2X509Credentials.assertingPartySigningCredential());
55+
}).assertingPartyDetails((c) -> c.entityId("https://some.idp.example.com/entity-id")
56+
.singleSignOnServiceLocation("https://some.idp.example.com/service-location"))
57+
.build();
58+
}
59+
60+
@Test
61+
public void whenSigningAnObjectThenKeyInfoIsPartOfTheSignature() {
62+
Response response = response("destination", "issuer");
63+
OpenSamlSigningUtils.sign(response, this.registration);
64+
Signature signature = response.getSignature();
65+
assertThat(signature).isNotNull();
66+
assertThat(signature.getKeyInfo()).isNotNull();
67+
}
68+
69+
Response response(String destination, String issuerEntityId) {
70+
Response response = build(Response.DEFAULT_ELEMENT_NAME);
71+
response.setID("R" + UUID.randomUUID());
72+
response.setVersion(SAMLVersion.VERSION_20);
73+
response.setID("_" + UUID.randomUUID());
74+
response.setDestination(destination);
75+
response.setIssuer(issuer(issuerEntityId));
76+
return response;
77+
}
78+
79+
Issuer issuer(String entityId) {
80+
Issuer issuer = build(Issuer.DEFAULT_ELEMENT_NAME);
81+
issuer.setValue(entityId);
82+
return issuer;
83+
}
84+
85+
<T extends XMLObject> T build(QName qName) {
86+
return (T) XMLObjectProviderRegistrySupport.getBuilderFactory().getBuilder(qName).buildObject(qName);
87+
}
88+
89+
}

0 commit comments

Comments
 (0)