Skip to content

Commit c968d86

Browse files
chao.wangjzheaux
authored andcommitted
Add metadata_uri column to saml2_asserting_party_metadata table
1 parent dcfe998 commit c968d86

File tree

6 files changed

+61
-261
lines changed

6 files changed

+61
-261
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: 28 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
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;
28+
2929
import org.springframework.core.log.LogMessage;
3030
import org.springframework.core.serializer.DefaultDeserializer;
3131
import org.springframework.core.serializer.Deserializer;
@@ -37,7 +37,6 @@
3737
import org.springframework.security.saml2.core.Saml2X509Credential;
3838
import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration.AssertingPartyDetails;
3939
import org.springframework.util.Assert;
40-
import org.springframework.util.StringUtils;
4140

4241
/**
4342
* A JDBC implementation of {@link AssertingPartyMetadataRepository}.
@@ -49,12 +48,11 @@ public final class JdbcAssertingPartyMetadataRepository implements AssertingPart
4948

5049
private final JdbcOperations jdbcOperations;
5150

52-
private RowMapper<AssertingPartyMetadata> assertingPartyMetadataRowMapper =
53-
new AssertingPartyMetadataRowMapper(ResultSet::getBytes);
51+
private RowMapper<AssertingPartyMetadata> assertingPartyMetadataRowMapper = new AssertingPartyMetadataRowMapper(
52+
ResultSet::getBytes);
5453

5554
// @formatter:off
5655
static final String COLUMN_NAMES = "entity_id, "
57-
+ "metadata_uri, "
5856
+ "singlesignon_url, "
5957
+ "singlesignon_binding, "
6058
+ "singlesignon_sign_request, "
@@ -82,7 +80,6 @@ public final class JdbcAssertingPartyMetadataRepository implements AssertingPart
8280
/**
8381
* Constructs a {@code JdbcRelyingPartyRegistrationRepository} using the provided
8482
* parameters.
85-
*
8683
* @param jdbcOperations the JDBC operations
8784
*/
8885
public JdbcAssertingPartyMetadataRepository(JdbcOperations jdbcOperations) {
@@ -94,21 +91,18 @@ public JdbcAssertingPartyMetadataRepository(JdbcOperations jdbcOperations) {
9491
* Sets the {@link RowMapper} used for mapping the current row in
9592
* {@code java.sql.ResultSet} to {@link AssertingPartyMetadata}. The default is
9693
* {@link AssertingPartyMetadataRowMapper}.
97-
*
9894
* @param assertingPartyMetadataRowMapper the {@link RowMapper} used for mapping the
99-
* current row in {@code java.sql.ResultSet} to {@link AssertingPartyMetadata}
95+
* current row in {@code java.sql.ResultSet} to {@link AssertingPartyMetadata}
10096
*/
101-
public void setAssertingPartyMetadataRowMapper(
102-
RowMapper<AssertingPartyMetadata> assertingPartyMetadataRowMapper) {
97+
public void setAssertingPartyMetadataRowMapper(RowMapper<AssertingPartyMetadata> assertingPartyMetadataRowMapper) {
10398
Assert.notNull(assertingPartyMetadataRowMapper, "assertingPartyMetadataRowMapper cannot be null");
10499
this.assertingPartyMetadataRowMapper = assertingPartyMetadataRowMapper;
105100
}
106101

107102
@Override
108103
public AssertingPartyMetadata findByEntityId(String entityId) {
109104
Assert.hasText(entityId, "entityId cannot be empty");
110-
SqlParameterValue[] parameters = new SqlParameterValue[]{
111-
new SqlParameterValue(Types.VARCHAR, entityId)};
105+
SqlParameterValue[] parameters = new SqlParameterValue[] { new SqlParameterValue(Types.VARCHAR, entityId) };
112106
PreparedStatementSetter pss = new ArgumentPreparedStatementSetter(parameters);
113107
List<AssertingPartyMetadata> result = this.jdbcOperations.query(LOAD_BY_ID_SQL, pss,
114108
this.assertingPartyMetadataRowMapper);
@@ -126,7 +120,7 @@ public Iterator<AssertingPartyMetadata> iterator() {
126120
* The default {@link RowMapper} that maps the current row in
127121
* {@code java.sql.ResultSet} to {@link AssertingPartyMetadata}.
128122
*/
129-
private final static class AssertingPartyMetadataRowMapper implements RowMapper<AssertingPartyMetadata> {
123+
private static final class AssertingPartyMetadataRowMapper implements RowMapper<AssertingPartyMetadata> {
130124

131125
private final Log logger = LogFactory.getLog(AssertingPartyMetadataRowMapper.class);
132126

@@ -141,7 +135,6 @@ private final static class AssertingPartyMetadataRowMapper implements RowMapper<
141135
@Override
142136
public AssertingPartyMetadata mapRow(ResultSet rs, int rowNum) throws SQLException {
143137
String entityId = rs.getString("entity_id");
144-
String metadataUri = rs.getString("metadata_uri");
145138
String singleSignOnUrl = rs.getString("singlesignon_url");
146139
Saml2MessageBinding singleSignOnBinding = Saml2MessageBinding.from(rs.getString("singlesignon_binding"));
147140
boolean singleSignOnSignRequest = rs.getBoolean("singlesignon_sign_request");
@@ -152,57 +145,40 @@ public AssertingPartyMetadata mapRow(ResultSet rs, int rowNum) throws SQLExcepti
152145
byte[] verificationCredentialsBytes = this.getBytes.getBytes(rs, "verification_credentials");
153146
byte[] encryptionCredentialsBytes = this.getBytes.getBytes(rs, "encryption_credentials");
154147

155-
boolean usingMetadata = StringUtils.hasText(metadataUri);
156-
AssertingPartyMetadata.Builder<?> builder = (!usingMetadata) ? new AssertingPartyDetails.Builder().entityId(entityId)
157-
: createBuilderUsingMetadata(entityId, metadataUri);
148+
AssertingPartyMetadata.Builder<?> builder = new AssertingPartyDetails.Builder();
158149
try {
159150
if (signingAlgorithmsBytes != null) {
160-
List<String> signingAlgorithms = (List<String>) deserializer.deserializeFromByteArray(signingAlgorithmsBytes);
161-
builder.signingAlgorithms(algorithms -> algorithms.addAll(signingAlgorithms));
151+
List<String> signingAlgorithms = (List<String>) this.deserializer
152+
.deserializeFromByteArray(signingAlgorithmsBytes);
153+
builder.signingAlgorithms((algorithms) -> algorithms.addAll(signingAlgorithms));
162154
}
163155
if (verificationCredentialsBytes != null) {
164-
Collection<Saml2X509Credential> verificationCredentials = (Collection<Saml2X509Credential>) deserializer.deserializeFromByteArray(verificationCredentialsBytes);
165-
builder.verificationX509Credentials(credentials -> credentials.addAll(verificationCredentials));
156+
Collection<Saml2X509Credential> verificationCredentials = (Collection<Saml2X509Credential>) this.deserializer
157+
.deserializeFromByteArray(verificationCredentialsBytes);
158+
builder.verificationX509Credentials((credentials) -> credentials.addAll(verificationCredentials));
166159
}
167160
if (encryptionCredentialsBytes != null) {
168-
Collection<Saml2X509Credential> encryptionCredentials = (Collection<Saml2X509Credential>) deserializer.deserializeFromByteArray(encryptionCredentialsBytes);
169-
builder.encryptionX509Credentials(credentials -> credentials.addAll(encryptionCredentials));
161+
Collection<Saml2X509Credential> encryptionCredentials = (Collection<Saml2X509Credential>) this.deserializer
162+
.deserializeFromByteArray(encryptionCredentialsBytes);
163+
builder.encryptionX509Credentials((credentials) -> credentials.addAll(encryptionCredentials));
170164
}
171-
} catch (Exception ex) {
172-
this.logger.debug(
173-
LogMessage.format("Parsing serialized credentials for entity %s failed", entityId), ex);
165+
}
166+
catch (Exception ex) {
167+
this.logger.debug(LogMessage.format("Parsing serialized credentials for entity %s failed", entityId),
168+
ex);
174169
return null;
175170
}
176171

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);
172+
builder.entityId(entityId)
173+
.wantAuthnRequestsSigned(singleSignOnSignRequest)
174+
.singleSignOnServiceLocation(singleSignOnUrl)
175+
.singleSignOnServiceBinding(singleSignOnBinding)
176+
.singleLogoutServiceLocation(singleLogoutUrl)
177+
.singleLogoutServiceBinding(singleLogoutBinding)
178+
.singleLogoutServiceResponseLocation(singleLogoutResponseUrl);
183179
return builder.build();
184180
}
185181

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-
}
206182
}
207183

208184
private interface GetBytes {
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
CREATE TABLE saml2_asserting_party_metadata
22
(
33
entity_id VARCHAR(1000) NOT NULL,
4-
singlesignon_url VARCHAR(1000),
5-
singlesignon_binding VARCHAR(200),
6-
singlesignon_sign_request VARCHAR(1000),
4+
singlesignon_url VARCHAR(1000) NOT NULL,
5+
singlesignon_binding VARCHAR(100),
6+
singlesignon_sign_request boolean,
77
signing_algorithms BYTEA,
8-
verification_credentials BYTEA,
8+
verification_credentials BYTEA NOT NULL,
99
encryption_credentials BYTEA,
1010
singlelogout_url VARCHAR(1000),
1111
singlelogout_response_url VARCHAR(1000),
12-
singlelogout_binding VARCHAR(200),
12+
singlelogout_binding VARCHAR(100),
1313
PRIMARY KEY (entity_id)
1414
);
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)