16
16
17
17
package org .springframework .security .saml2 .provider .service .registration ;
18
18
19
- import java .io .IOException ;
20
- import java .io .InputStream ;
21
- import java .security .cert .CertificateFactory ;
22
- import java .security .cert .X509Certificate ;
23
- import java .util .Collection ;
24
19
import java .util .Iterator ;
25
- import java .util .List ;
26
20
27
21
import org .junit .jupiter .api .AfterEach ;
28
22
import org .junit .jupiter .api .BeforeEach ;
29
23
import org .junit .jupiter .api .Test ;
30
24
31
- import org .springframework .core .io .ClassPathResource ;
32
- import org .springframework .core .serializer .DefaultSerializer ;
33
- import org .springframework .core .serializer .Serializer ;
34
25
import org .springframework .jdbc .core .JdbcOperations ;
35
26
import org .springframework .jdbc .core .JdbcTemplate ;
36
27
import org .springframework .jdbc .datasource .embedded .EmbeddedDatabase ;
37
28
import org .springframework .jdbc .datasource .embedded .EmbeddedDatabaseBuilder ;
38
29
import org .springframework .jdbc .datasource .embedded .EmbeddedDatabaseType ;
39
- import org .springframework .security .saml2 .core .Saml2X509Credential ;
40
30
41
31
import static org .assertj .core .api .Assertions .assertThat ;
42
32
import static org .assertj .core .api .Assertions .assertThatIllegalArgumentException ;
@@ -48,41 +38,21 @@ class JdbcAssertingPartyMetadataRepositoryTests {
48
38
49
39
private static final String SCHEMA_SQL_RESOURCE = "org/springframework/security/saml2/saml2-asserting-party-metadata-schema.sql" ;
50
40
51
- private static final String SAVE_SQL = "INSERT INTO saml2_asserting_party_metadata ("
52
- + JdbcAssertingPartyMetadataRepository .COLUMN_NAMES + ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" ;
53
-
54
- private static final String ENTITY_ID = "https://localhost/simplesaml/saml2/idp/metadata.php" ;
55
-
56
- private static final String SINGLE_SIGNON_URL = "https://localhost/SSO" ;
57
-
58
- private static final String SINGLE_SIGNON_BINDING = Saml2MessageBinding .REDIRECT .getUrn ();
59
-
60
- private static final boolean SINGLE_SIGNON_SIGN_REQUEST = false ;
61
-
62
- private static final String SINGLE_LOGOUT_URL = "https://localhost/SLO" ;
63
-
64
- private static final String SINGLE_LOGOUT_RESPONSE_URL = "https://localhost/SLO/response" ;
65
-
66
- private static final String SINGLE_LOGOUT_BINDING = Saml2MessageBinding .REDIRECT .getUrn ();
67
-
68
- private static final List <String > SIGNING_ALGORITHMS = List .of ("http://www.w3.org/2001/04/xmldsig-more#rsa-sha512" );
69
-
70
- private X509Certificate certificate ;
71
-
72
41
private EmbeddedDatabase db ;
73
42
74
43
private JdbcAssertingPartyMetadataRepository repository ;
75
44
76
45
private JdbcOperations jdbcOperations ;
77
46
78
- private final Serializer <Object > serializer = new DefaultSerializer ();
47
+ private final AssertingPartyMetadata metadata = TestRelyingPartyRegistrations .full ()
48
+ .build ()
49
+ .getAssertingPartyMetadata ();
79
50
80
51
@ BeforeEach
81
52
void setUp () {
82
53
this .db = createDb ();
83
54
this .jdbcOperations = new JdbcTemplate (this .db );
84
55
this .repository = new JdbcAssertingPartyMetadataRepository (this .jdbcOperations );
85
- this .certificate = loadCertificate ("rsa.crt" );
86
56
}
87
57
88
58
@ AfterEach
@@ -109,26 +79,12 @@ void findByEntityIdWhenEntityIdIsNullThenThrowIllegalArgumentException() {
109
79
}
110
80
111
81
@ Test
112
- void findByEntityId () throws IOException {
113
- this .jdbcOperations .update (SAVE_SQL , ENTITY_ID , SINGLE_SIGNON_URL , SINGLE_SIGNON_BINDING ,
114
- SINGLE_SIGNON_SIGN_REQUEST , this .serializer .serializeToByteArray (SIGNING_ALGORITHMS ),
115
- this .serializer .serializeToByteArray (asCredentials (this .certificate )),
116
- this .serializer .serializeToByteArray (asCredentials (this .certificate )), SINGLE_LOGOUT_URL ,
117
- SINGLE_LOGOUT_RESPONSE_URL , SINGLE_LOGOUT_BINDING );
82
+ void findByEntityId () {
83
+ this .repository .save (this .metadata );
118
84
119
- AssertingPartyMetadata found = this .repository .findByEntityId (ENTITY_ID );
85
+ AssertingPartyMetadata found = this .repository .findByEntityId (this . metadata . getEntityId () );
120
86
121
- assertThat (found ).isNotNull ();
122
- assertThat (found .getEntityId ()).isEqualTo (ENTITY_ID );
123
- assertThat (found .getSingleSignOnServiceLocation ()).isEqualTo (SINGLE_SIGNON_URL );
124
- assertThat (found .getSingleSignOnServiceBinding ().getUrn ()).isEqualTo (SINGLE_SIGNON_BINDING );
125
- assertThat (found .getWantAuthnRequestsSigned ()).isEqualTo (SINGLE_SIGNON_SIGN_REQUEST );
126
- assertThat (found .getSingleLogoutServiceLocation ()).isEqualTo (SINGLE_LOGOUT_URL );
127
- assertThat (found .getSingleLogoutServiceResponseLocation ()).isEqualTo (SINGLE_LOGOUT_RESPONSE_URL );
128
- assertThat (found .getSingleLogoutServiceBinding ().getUrn ()).isEqualTo (SINGLE_LOGOUT_BINDING );
129
- assertThat (found .getSigningAlgorithms ()).contains (SIGNING_ALGORITHMS .get (0 ));
130
- assertThat (found .getVerificationX509Credentials ()).hasSize (1 );
131
- assertThat (found .getEncryptionX509Credentials ()).hasSize (1 );
87
+ assertAssertingPartyEquals (found , this .metadata );
132
88
}
133
89
134
90
@ Test
@@ -138,28 +94,30 @@ void findByEntityIdWhenNotExists() {
138
94
}
139
95
140
96
@ Test
141
- void iterator () throws IOException {
142
- this .jdbcOperations .update (SAVE_SQL , ENTITY_ID , SINGLE_SIGNON_URL , SINGLE_SIGNON_BINDING ,
143
- SINGLE_SIGNON_SIGN_REQUEST , this .serializer .serializeToByteArray (SIGNING_ALGORITHMS ),
144
- this .serializer .serializeToByteArray (asCredentials (this .certificate )),
145
- this .serializer .serializeToByteArray (asCredentials (this .certificate )), SINGLE_LOGOUT_URL ,
146
- SINGLE_LOGOUT_RESPONSE_URL , SINGLE_LOGOUT_BINDING );
147
-
148
- this .jdbcOperations .update (SAVE_SQL , "https://localhost/simplesaml2/saml2/idp/metadata.php" , SINGLE_SIGNON_URL ,
149
- SINGLE_SIGNON_BINDING , SINGLE_SIGNON_SIGN_REQUEST ,
150
- this .serializer .serializeToByteArray (SIGNING_ALGORITHMS ),
151
- this .serializer .serializeToByteArray (asCredentials (this .certificate )),
152
- this .serializer .serializeToByteArray (asCredentials (this .certificate )), SINGLE_LOGOUT_URL ,
153
- SINGLE_LOGOUT_RESPONSE_URL , SINGLE_LOGOUT_BINDING );
97
+ void iterator () {
98
+ AssertingPartyMetadata second = RelyingPartyRegistration .withAssertingPartyMetadata (this .metadata )
99
+ .assertingPartyMetadata ((a ) -> a .entityId ("https://example.org/idp" ))
100
+ .build ()
101
+ .getAssertingPartyMetadata ();
102
+ this .repository .save (this .metadata );
103
+ this .repository .save (second );
154
104
155
105
Iterator <AssertingPartyMetadata > iterator = this .repository .iterator ();
156
- AssertingPartyMetadata first = iterator .next ();
157
- assertThat (first ).isNotNull ();
158
- AssertingPartyMetadata second = iterator .next ();
159
- assertThat (second ).isNotNull ();
106
+
107
+ assertAssertingPartyEquals (iterator .next (), this .metadata );
108
+ assertAssertingPartyEquals (iterator .next (), second );
160
109
assertThat (iterator .hasNext ()).isFalse ();
161
110
}
162
111
112
+ @ Test
113
+ void saveWhenExistingThenUpdates () {
114
+ this .repository .save (this .metadata );
115
+ boolean existing = this .metadata .getWantAuthnRequestsSigned ();
116
+ this .repository .save (this .metadata .mutate ().wantAuthnRequestsSigned (!existing ).build ());
117
+ boolean updated = this .repository .findByEntityId (this .metadata .getEntityId ()).getWantAuthnRequestsSigned ();
118
+ assertThat (existing ).isNotEqualTo (updated );
119
+ }
120
+
163
121
private static EmbeddedDatabase createDb () {
164
122
return createDb (SCHEMA_SQL_RESOURCE );
165
123
}
@@ -175,19 +133,19 @@ private static EmbeddedDatabase createDb(String schema) {
175
133
// @formatter:on
176
134
}
177
135
178
- private X509Certificate loadCertificate ( String path ) {
179
- try ( InputStream is = new ClassPathResource ( path ). getInputStream ()) {
180
- CertificateFactory factory = CertificateFactory . getInstance ( "X.509" );
181
- return ( X509Certificate ) factory . generateCertificate ( is );
182
- }
183
- catch ( Exception ex ) {
184
- throw new RuntimeException ( "Error loading certificate from " + path , ex );
185
- }
186
- }
187
-
188
- private Collection < Saml2X509Credential > asCredentials ( X509Certificate certificate ) {
189
- return List . of ( new Saml2X509Credential ( certificate , Saml2X509Credential . Saml2X509CredentialType . ENCRYPTION ,
190
- Saml2X509Credential . Saml2X509CredentialType . VERIFICATION ));
136
+ private void assertAssertingPartyEquals ( AssertingPartyMetadata found , AssertingPartyMetadata expected ) {
137
+ assertThat ( found ). isNotNull ();
138
+ assertThat ( found . getEntityId ()). isEqualTo ( expected . getEntityId () );
139
+ assertThat ( found . getSingleSignOnServiceLocation ()). isEqualTo ( expected . getSingleSignOnServiceLocation () );
140
+ assertThat ( found . getSingleSignOnServiceBinding ()). isEqualTo ( expected . getSingleSignOnServiceBinding ());
141
+ assertThat ( found . getWantAuthnRequestsSigned ()). isEqualTo ( expected . getWantAuthnRequestsSigned ());
142
+ assertThat ( found . getSingleLogoutServiceLocation ()). isEqualTo ( expected . getSingleLogoutServiceLocation () );
143
+ assertThat ( found . getSingleLogoutServiceResponseLocation ())
144
+ . isEqualTo ( expected . getSingleLogoutServiceResponseLocation ());
145
+ assertThat ( found . getSingleLogoutServiceBinding ()). isEqualTo ( expected . getSingleLogoutServiceBinding ());
146
+ assertThat ( found . getSigningAlgorithms ()). containsAll ( expected . getSigningAlgorithms ());
147
+ assertThat ( found . getVerificationX509Credentials ()). containsAll ( expected . getVerificationX509Credentials ());
148
+ assertThat ( found . getEncryptionX509Credentials ()). containsAll ( expected . getEncryptionX509Credentials ( ));
191
149
}
192
150
193
151
}
0 commit comments