Skip to content

Commit 0025fe9

Browse files
author
chao.wang
committed
Refactor JdbcAssertingPartyMetadataRepository to remove metadata_uri handling and clean up unused code
1 parent d58fcb9 commit 0025fe9

File tree

5 files changed

+40
-210
lines changed

5 files changed

+40
-210
lines changed

saml2/saml2-service-provider/src/main/java/org/springframework/security/saml2/provider/service/registration/AssertingPartyMetadata.java

Lines changed: 0 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,11 @@
1616

1717
package org.springframework.security.saml2.provider.service.registration;
1818

19-
import java.io.IOException;
20-
import java.io.InputStream;
2119
import java.io.Serializable;
22-
import java.util.ArrayList;
2320
import java.util.Collection;
2421
import java.util.List;
2522
import java.util.function.Consumer;
2623

27-
import org.opensaml.saml.common.xml.SAMLConstants;
28-
import org.opensaml.saml.saml2.metadata.EntityDescriptor;
29-
import org.springframework.core.io.DefaultResourceLoader;
30-
import org.springframework.core.io.ResourceLoader;
31-
import org.springframework.security.saml2.Saml2Exception;
3224
import org.springframework.security.saml2.core.Saml2X509Credential;
3325

3426
/**
@@ -39,8 +31,6 @@
3931
*/
4032
public interface AssertingPartyMetadata extends Serializable {
4133

42-
ResourceLoader resourceLoader = new DefaultResourceLoader();
43-
4434
/**
4535
* Get the asserting party's <a href=
4636
* "https://www.oasis-open.org/committees/download.php/51890/SAML%20MD%20simplified%20overview.pdf#2.9%20EntityDescriptor">EntityID</a>.
@@ -284,70 +274,4 @@ interface Builder<B extends Builder<B>> {
284274

285275
}
286276

287-
/**
288-
* Return a {@link Collection} of {@link Builder}s based off
289-
* of the given SAML 2.0 Asserting Party (IDP) metadata location.
290-
*
291-
* Valid locations can be classpath- or file-based or they can be HTTPS endpoints.
292-
* Some valid endpoints might include:
293-
*
294-
* <pre>
295-
* metadataLocation = "classpath:asserting-party-metadata.xml";
296-
* metadataLocation = "file:asserting-party-metadata.xml";
297-
* metadataLocation = "https://ap.example.org/metadata";
298-
* </pre>
299-
*
300-
* @param location The classpath- or file-based locations or HTTPS endpoints of the
301-
* asserting party metadata file
302-
* @return the {@link Collection} of {@link Builder}s for
303-
* further configuration
304-
* @since 7.0
305-
*/
306-
static Collection<Builder<?>> collectionFromMetadataLocation(String location) {
307-
try (InputStream source = resourceLoader.getResource(location).getInputStream()) {
308-
return collectionFromMetadata(source);
309-
}
310-
catch (IOException ex) {
311-
if (ex.getCause() instanceof Saml2Exception) {
312-
throw (Saml2Exception) ex.getCause();
313-
}
314-
throw new Saml2Exception(ex);
315-
}
316-
}
317-
318-
/**
319-
* Return a {@link Collection} of {@link Builder}s based off
320-
* of the given SAML 2.0 Asserting Party (IDP) metadata.
321-
*
322-
* <p>
323-
* This method is intended for scenarios when the metadata is looked up by a separate
324-
* mechanism. One such example is when the metadata is stored in a database.
325-
* </p>
326-
*
327-
* <p>
328-
* <strong>The callers of this method are accountable for closing the
329-
* {@code InputStream} source.</strong>
330-
* </p>
331-
*
332-
* @param source the {@link InputStream} source containing the asserting party
333-
* metadata
334-
* @return the {@link Collection} of {@link Builder}s for
335-
* further configuration
336-
* @since 7.0
337-
*/
338-
static Collection<Builder<?>> collectionFromMetadata(InputStream source) {
339-
Collection<Builder<?>> builders = new ArrayList<>();
340-
for (EntityDescriptor descriptor : OpenSamlMetadataUtils.descriptors(source)) {
341-
if (descriptor.getIDPSSODescriptor(SAMLConstants.SAML20P_NS) != null) {
342-
OpenSamlAssertingPartyDetails.Builder builder = OpenSamlAssertingPartyDetails
343-
.withEntityDescriptor(descriptor);
344-
builders.add(builder);
345-
}
346-
}
347-
if (builders.isEmpty()) {
348-
throw new Saml2Exception("Metadata response is missing the necessary IDPSSODescriptor element");
349-
}
350-
return builders;
351-
}
352-
353277
}

saml2/saml2-service-provider/src/main/java/org/springframework/security/saml2/provider/service/registration/JdbcAssertingPartyMetadataRepository.java

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.util.Collection;
2323
import java.util.Iterator;
2424
import java.util.List;
25-
import java.util.function.Consumer;
2625

2726
import org.apache.commons.logging.Log;
2827
import org.apache.commons.logging.LogFactory;
@@ -37,7 +36,6 @@
3736
import org.springframework.security.saml2.core.Saml2X509Credential;
3837
import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration.AssertingPartyDetails;
3938
import org.springframework.util.Assert;
40-
import org.springframework.util.StringUtils;
4139

4240
/**
4341
* A JDBC implementation of {@link AssertingPartyMetadataRepository}.
@@ -54,7 +52,6 @@ public final class JdbcAssertingPartyMetadataRepository implements AssertingPart
5452

5553
// @formatter:off
5654
static final String COLUMN_NAMES = "entity_id, "
57-
+ "metadata_uri, "
5855
+ "singlesignon_url, "
5956
+ "singlesignon_binding, "
6057
+ "singlesignon_sign_request, "
@@ -141,7 +138,6 @@ private final static class AssertingPartyMetadataRowMapper implements RowMapper<
141138
@Override
142139
public AssertingPartyMetadata mapRow(ResultSet rs, int rowNum) throws SQLException {
143140
String entityId = rs.getString("entity_id");
144-
String metadataUri = rs.getString("metadata_uri");
145141
String singleSignOnUrl = rs.getString("singlesignon_url");
146142
Saml2MessageBinding singleSignOnBinding = Saml2MessageBinding.from(rs.getString("singlesignon_binding"));
147143
boolean singleSignOnSignRequest = rs.getBoolean("singlesignon_sign_request");
@@ -152,57 +148,41 @@ public AssertingPartyMetadata mapRow(ResultSet rs, int rowNum) throws SQLExcepti
152148
byte[] verificationCredentialsBytes = this.getBytes.getBytes(rs, "verification_credentials");
153149
byte[] encryptionCredentialsBytes = this.getBytes.getBytes(rs, "encryption_credentials");
154150

155-
boolean usingMetadata = StringUtils.hasText(metadataUri);
156-
AssertingPartyMetadata.Builder<?> builder = (!usingMetadata) ? new AssertingPartyDetails.Builder().entityId(entityId)
157-
: createBuilderUsingMetadata(entityId, metadataUri);
151+
AssertingPartyMetadata.Builder<?> builder = new AssertingPartyDetails.Builder();
158152
try {
159153
if (signingAlgorithmsBytes != null) {
160-
List<String> signingAlgorithms = (List<String>) deserializer.deserializeFromByteArray(signingAlgorithmsBytes);
154+
List<String> signingAlgorithms = (List<String>)
155+
this.deserializer.deserializeFromByteArray(signingAlgorithmsBytes);
161156
builder.signingAlgorithms(algorithms -> algorithms.addAll(signingAlgorithms));
162157
}
163158
if (verificationCredentialsBytes != null) {
164-
Collection<Saml2X509Credential> verificationCredentials = (Collection<Saml2X509Credential>) deserializer.deserializeFromByteArray(verificationCredentialsBytes);
165-
builder.verificationX509Credentials(credentials -> credentials.addAll(verificationCredentials));
159+
Collection<Saml2X509Credential> verificationCredentials = (Collection<Saml2X509Credential>)
160+
this.deserializer.deserializeFromByteArray(verificationCredentialsBytes);
161+
builder.verificationX509Credentials(
162+
credentials -> credentials.addAll(verificationCredentials));
166163
}
167164
if (encryptionCredentialsBytes != null) {
168-
Collection<Saml2X509Credential> encryptionCredentials = (Collection<Saml2X509Credential>) deserializer.deserializeFromByteArray(encryptionCredentialsBytes);
169-
builder.encryptionX509Credentials(credentials -> credentials.addAll(encryptionCredentials));
165+
Collection<Saml2X509Credential> encryptionCredentials = (Collection<Saml2X509Credential>)
166+
this.deserializer.deserializeFromByteArray(encryptionCredentialsBytes);
167+
builder.encryptionX509Credentials(
168+
credentials -> credentials.addAll(encryptionCredentials));
170169
}
171170
} catch (Exception ex) {
172171
this.logger.debug(
173172
LogMessage.format("Parsing serialized credentials for entity %s failed", entityId), ex);
174173
return null;
175174
}
176175

177-
applyingWhenNonNull(singleSignOnUrl, builder::singleSignOnServiceLocation);
178-
applyingWhenNonNull(singleSignOnBinding, builder::singleSignOnServiceBinding);
179-
applyingWhenNonNull(singleSignOnSignRequest, builder::wantAuthnRequestsSigned);
180-
applyingWhenNonNull(singleLogoutUrl, builder::singleLogoutServiceLocation);
181-
applyingWhenNonNull(singleLogoutResponseUrl, builder::singleLogoutServiceResponseLocation);
182-
applyingWhenNonNull(singleLogoutBinding, builder::singleLogoutServiceBinding);
176+
builder
177+
.entityId(entityId)
178+
.wantAuthnRequestsSigned(singleSignOnSignRequest)
179+
.singleSignOnServiceLocation(singleSignOnUrl)
180+
.singleSignOnServiceBinding(singleSignOnBinding)
181+
.singleLogoutServiceLocation(singleLogoutUrl)
182+
.singleLogoutServiceBinding(singleLogoutBinding)
183+
.singleLogoutServiceResponseLocation(singleLogoutResponseUrl);
183184
return builder.build();
184185
}
185-
186-
private <T> void applyingWhenNonNull(T value, Consumer<T> consumer) {
187-
if (value != null) {
188-
consumer.accept(value);
189-
}
190-
}
191-
192-
private AssertingPartyMetadata.Builder<?> createBuilderUsingMetadata(String entityId, String metadataUri) {
193-
Collection<AssertingPartyMetadata.Builder<?>> candidates = AssertingPartyMetadata
194-
.collectionFromMetadataLocation(metadataUri);
195-
for (AssertingPartyMetadata.Builder<?> candidate : candidates) {
196-
if (entityId == null || entityId.equals(getEntityId(candidate))) {
197-
return candidate;
198-
}
199-
}
200-
throw new IllegalStateException("No asserting party metadata with Entity ID '" + entityId + "' found");
201-
}
202-
203-
private Object getEntityId(AssertingPartyMetadata.Builder<?> candidate) {
204-
return candidate.build().getEntityId();
205-
}
206186
}
207187

208188
private interface GetBytes {
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
CREATE TABLE saml2_asserting_party_metadata
22
(
33
entity_id VARCHAR(1000) NOT NULL,
4-
metadata_uri VARCHAR(1000),
5-
singlesignon_url VARCHAR(1000),
6-
singlesignon_binding VARCHAR(200),
7-
singlesignon_sign_request VARCHAR(1000),
4+
singlesignon_url VARCHAR(1000) NOT NULL,
5+
singlesignon_binding VARCHAR(100),
6+
singlesignon_sign_request boolean,
87
signing_algorithms BYTEA,
9-
verification_credentials BYTEA,
8+
verification_credentials BYTEA NOT NULL,
109
encryption_credentials BYTEA,
1110
singlelogout_url VARCHAR(1000),
1211
singlelogout_response_url VARCHAR(1000),
13-
singlelogout_binding VARCHAR(200),
12+
singlelogout_binding VARCHAR(100),
1413
PRIMARY KEY (entity_id)
1514
);
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
CREATE TABLE saml2_asserting_party_metadata
22
(
33
entity_id VARCHAR(1000) NOT NULL,
4-
metadata_uri VARCHAR(1000),
5-
singlesignon_url VARCHAR(1000),
6-
singlesignon_binding VARCHAR(200),
7-
singlesignon_sign_request VARCHAR(1000),
4+
singlesignon_url VARCHAR(1000) NOT NULL,
5+
singlesignon_binding VARCHAR(100),
6+
singlesignon_sign_request boolean,
87
signing_algorithms blob,
9-
verification_credentials blob,
8+
verification_credentials blob NOT NULL,
109
encryption_credentials blob,
1110
singlelogout_url VARCHAR(1000),
1211
singlelogout_response_url VARCHAR(1000),
13-
singlelogout_binding VARCHAR(200),
12+
singlelogout_binding VARCHAR(100),
1413
PRIMARY KEY (entity_id)
1514
);

0 commit comments

Comments
 (0)