-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Add OneTimeUse support for SAML assertions #19144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
therepanic
wants to merge
1
commit into
spring-projects:main
Choose a base branch
from
therepanic:gh-19130
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,6 +42,7 @@ | |
| import org.opensaml.saml.saml2.assertion.impl.AudienceRestrictionConditionValidator; | ||
| import org.opensaml.saml.saml2.assertion.impl.BearerSubjectConfirmationValidator; | ||
| import org.opensaml.saml.saml2.assertion.impl.DelegationRestrictionConditionValidator; | ||
| import org.opensaml.saml.saml2.assertion.impl.OneTimeUseConditionValidator; | ||
| import org.opensaml.saml.saml2.assertion.impl.ProxyRestrictionConditionValidator; | ||
| import org.opensaml.saml.saml2.core.Assertion; | ||
| import org.opensaml.saml.saml2.core.Condition; | ||
|
|
@@ -57,6 +58,7 @@ | |
| import org.opensaml.xmlsec.signature.support.SignaturePrevalidator; | ||
| import org.opensaml.xmlsec.signature.support.SignatureTrustEngine; | ||
|
|
||
| import org.springframework.cache.Cache; | ||
| import org.springframework.core.convert.converter.Converter; | ||
| import org.springframework.security.authentication.AbstractAuthenticationToken; | ||
| import org.springframework.security.authentication.AuthenticationProvider; | ||
|
|
@@ -109,6 +111,7 @@ | |
| * asserting party, IDP, verification certificates. | ||
| * | ||
| * @author Josh Cummings | ||
| * @author Andrey Litvitski | ||
| * @since 5.5 | ||
| * @see <a href= | ||
| * "https://docs.oasis-open.org/security/saml/v2.0/saml-core-2.0-os.pdf#page=38">SAML 2 | ||
|
|
@@ -737,10 +740,11 @@ public static final class Builder { | |
|
|
||
| private final Map<String, Object> validationParameters = new HashMap<>(); | ||
|
|
||
| @Nullable private Cache replayCache; | ||
|
|
||
| private Builder() { | ||
| this.conditions.add(new AudienceRestrictionConditionValidator()); | ||
| this.conditions.add(new DelegationRestrictionConditionValidator()); | ||
| this.conditions.add(new ValidConditionValidator(OneTimeUse.DEFAULT_ELEMENT_NAME)); | ||
| this.conditions.add(new ProxyRestrictionConditionValidator()); | ||
| this.subjects.add(new BearerSubjectConfirmationValidator()); | ||
| this.validationParameters.put(SAML2AssertionValidationParameters.CLOCK_SKEW, Duration.ofMinutes(5)); | ||
|
|
@@ -819,11 +823,34 @@ public Builder subjectValidators(Consumer<List<SubjectConfirmationValidator>> su | |
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Use this {@link Cache} to validate {@code <saml2:OneTimeUse>} conditions in | ||
| * SAML assertions. When set, assertions with a {@code <saml2:OneTimeUse>} | ||
| * condition will be rejected if the assertion ID has already been seen and | ||
| * has not yet expired. | ||
| * <p> | ||
| * If not set, {@code <saml2:OneTimeUse>} conditions are skipped. | ||
| * @param cache the {@link Cache} to use for replay detection | ||
| * @return the {@link Builder} for further configuration | ||
| */ | ||
| public Builder replayCache(Cache cache) { | ||
| Assert.notNull(cache, "cache cannot be null"); | ||
| this.replayCache = cache; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Build the {@link AssertionValidator} | ||
| * @return the {@link AssertionValidator} | ||
| */ | ||
| public AssertionValidator build() { | ||
| if (this.replayCache != null) { | ||
| this.conditions | ||
| .add(new OneTimeUseConditionValidator(new SpringCacheReplayCache(this.replayCache), null)); | ||
| } | ||
| else { | ||
|
Comment on lines
846
to
+851
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can afford to ignore |
||
| this.conditions.add(new ValidConditionValidator(OneTimeUse.DEFAULT_ELEMENT_NAME)); | ||
| } | ||
| AssertionValidator validator = new AssertionValidator(new ValidSignatureAssertionValidator( | ||
| this.conditions, this.subjects, List.of(), null, null, null)); | ||
| validator.setValidationContextParameters((params) -> params.putAll(this.validationParameters)); | ||
|
|
||
49 changes: 49 additions & 0 deletions
49
...pringframework/security/saml2/provider/service/authentication/SpringCacheReplayCache.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| /* | ||
| * Copyright 2004-present the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.springframework.security.saml2.provider.service.authentication; | ||
|
|
||
| import java.time.Instant; | ||
|
|
||
| import org.opensaml.storage.ReplayCache; | ||
|
|
||
| import org.springframework.cache.Cache; | ||
|
|
||
| /** | ||
| * For internal use only. | ||
| */ | ||
| final class SpringCacheReplayCache implements ReplayCache { | ||
|
|
||
| private final Cache cache; | ||
|
|
||
| SpringCacheReplayCache(Cache cache) { | ||
| this.cache = cache; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean check(String context, String key, Instant expires) { | ||
| Cache.ValueWrapper existing = this.cache.get(context); | ||
| if (existing != null) { | ||
| Instant storedExpiry = (Instant) existing.get(); | ||
| if (storedExpiry != null && Instant.now().isBefore(storedExpiry)) { | ||
| return false; | ||
| } | ||
| } | ||
| this.cache.put(context, expires); | ||
| return true; | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given how validation works, I don't think it would be a bad idea to move the logic for adding
OneTimeUse.DEFAULT_ELEMENT_NAMEinto thebuildmethod itself