Skip to content

Commit 5436fd5

Browse files
committed
Remove Unecessary Code
1 parent 4cdc6da commit 5436fd5

File tree

3 files changed

+31
-19
lines changed

3 files changed

+31
-19
lines changed

saml2/saml2-service-provider/src/main/java/org/springframework/security/saml2/provider/service/web/Saml2AuthenticationTokenConverter.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

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

19-
import java.util.function.Function;
20-
2119
import jakarta.servlet.http.HttpServletRequest;
2220

2321
import org.springframework.http.HttpMethod;
@@ -43,7 +41,7 @@ public final class Saml2AuthenticationTokenConverter implements AuthenticationCo
4341

4442
private final RelyingPartyRegistrationResolver relyingPartyRegistrationResolver;
4543

46-
private Function<HttpServletRequest, AbstractSaml2AuthenticationRequest> loader;
44+
private Saml2AuthenticationRequestRepository<AbstractSaml2AuthenticationRequest> authenticationRequestRepository;
4745

4846
/**
4947
* Constructs a {@link Saml2AuthenticationTokenConverter} given a strategy for
@@ -54,12 +52,13 @@ public final class Saml2AuthenticationTokenConverter implements AuthenticationCo
5452
public Saml2AuthenticationTokenConverter(RelyingPartyRegistrationResolver relyingPartyRegistrationResolver) {
5553
Assert.notNull(relyingPartyRegistrationResolver, "relyingPartyRegistrationResolver cannot be null");
5654
this.relyingPartyRegistrationResolver = relyingPartyRegistrationResolver;
57-
this.loader = new HttpSessionSaml2AuthenticationRequestRepository()::loadAuthenticationRequest;
55+
this.authenticationRequestRepository = new HttpSessionSaml2AuthenticationRequestRepository();
5856
}
5957

6058
@Override
6159
public Saml2AuthenticationToken convert(HttpServletRequest request) {
62-
AbstractSaml2AuthenticationRequest authenticationRequest = loadAuthenticationRequest(request);
60+
AbstractSaml2AuthenticationRequest authenticationRequest = this.authenticationRequestRepository
61+
.loadAuthenticationRequest(request);
6362
String relyingPartyRegistrationId = (authenticationRequest != null)
6463
? authenticationRequest.getRelyingPartyRegistrationId() : null;
6564
RelyingPartyRegistration relyingPartyRegistration = this.relyingPartyRegistrationResolver.resolve(request,
@@ -84,11 +83,7 @@ public Saml2AuthenticationToken convert(HttpServletRequest request) {
8483
public void setAuthenticationRequestRepository(
8584
Saml2AuthenticationRequestRepository<AbstractSaml2AuthenticationRequest> authenticationRequestRepository) {
8685
Assert.notNull(authenticationRequestRepository, "authenticationRequestRepository cannot be null");
87-
this.loader = authenticationRequestRepository::loadAuthenticationRequest;
88-
}
89-
90-
private AbstractSaml2AuthenticationRequest loadAuthenticationRequest(HttpServletRequest request) {
91-
return this.loader.apply(request);
86+
this.authenticationRequestRepository = authenticationRequestRepository;
9287
}
9388

9489
private String decode(HttpServletRequest request) {

saml2/saml2-service-provider/src/main/java/org/springframework/security/saml2/provider/service/web/authentication/Saml2WebSsoAuthenticationFilter.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistrationRepository;
3030
import org.springframework.security.saml2.provider.service.web.DefaultRelyingPartyRegistrationResolver;
3131
import org.springframework.security.saml2.provider.service.web.HttpSessionSaml2AuthenticationRequestRepository;
32-
import org.springframework.security.saml2.provider.service.web.RelyingPartyRegistrationResolver;
3332
import org.springframework.security.saml2.provider.service.web.Saml2AuthenticationRequestRepository;
3433
import org.springframework.security.saml2.provider.service.web.Saml2AuthenticationTokenConverter;
3534
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
@@ -77,9 +76,7 @@ public Saml2WebSsoAuthenticationFilter(RelyingPartyRegistrationRepository relyin
7776
public Saml2WebSsoAuthenticationFilter(RelyingPartyRegistrationRepository relyingPartyRegistrationRepository,
7877
String filterProcessesUrl) {
7978
this(new Saml2AuthenticationTokenConverter(
80-
(RelyingPartyRegistrationResolver) new DefaultRelyingPartyRegistrationResolver(
81-
relyingPartyRegistrationRepository)),
82-
filterProcessesUrl);
79+
new DefaultRelyingPartyRegistrationResolver(relyingPartyRegistrationRepository)), filterProcessesUrl);
8380
Assert.isTrue(filterProcessesUrl.contains("{registrationId}"),
8481
"filterProcessesUrl must contain a {registrationId} match variable");
8582
}
@@ -159,9 +156,9 @@ private void setAuthenticationRequestRepositoryIntoAuthenticationConverter(
159156
}
160157

161158
private void setDetails(HttpServletRequest request, Authentication authentication) {
162-
if (AbstractAuthenticationToken.class.isAssignableFrom(authentication.getClass())) {
159+
if (authentication instanceof AbstractAuthenticationToken token) {
163160
Object details = this.authenticationDetailsSource.buildDetails(request);
164-
((AbstractAuthenticationToken) authentication).setDetails(details);
161+
token.setDetails(details);
165162
}
166163
}
167164

saml2/saml2-service-provider/src/test/java/org/springframework/security/saml2/provider/service/web/authentication/Saml2WebSsoAuthenticationFilterTests.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.security.saml2.provider.service.web.authentication;
1818

19+
import jakarta.servlet.http.HttpServletRequest;
1920
import jakarta.servlet.http.HttpServletResponse;
2021
import org.junit.jupiter.api.BeforeEach;
2122
import org.junit.jupiter.api.Test;
@@ -94,16 +95,18 @@ public void constructingFilterWithMissingRegistrationIdVariableAndCustomAuthenti
9495

9596
@Test
9697
public void requiresAuthenticationWhenHappyPathThenReturnsTrue() {
97-
assertThat(this.filter.requiresAuthentication(this.request, this.response)).isTrue();
98+
RequiresAuthenticationExposingFilter filter = new RequiresAuthenticationExposingFilter(this.repository);
99+
assertThat(filter.requiresAuthentication(this.request, this.response)).isTrue();
98100
}
99101

100102
@Test
101103
public void requiresAuthenticationWhenCustomProcessingUrlThenReturnsTrue() {
102-
this.filter = new Saml2WebSsoAuthenticationFilter(this.repository, "/some/other/path/{registrationId}");
104+
RequiresAuthenticationExposingFilter filter = new RequiresAuthenticationExposingFilter(this.repository,
105+
"/some/other/path/{registrationId}");
103106
this.request.setRequestURI("/some/other/path/idp-registration-id");
104107
this.request.setPathInfo("/some/other/path/idp-registration-id");
105108
this.request.setParameter(Saml2ParameterNames.SAML_RESPONSE, "xml-data-goes-here");
106-
assertThat(this.filter.requiresAuthentication(this.request, this.response)).isTrue();
109+
assertThat(filter.requiresAuthentication(this.request, this.response)).isTrue();
107110
}
108111

109112
@Test
@@ -212,4 +215,21 @@ public void doFilterWhenPathStartsWithRegistrationIdThenAuthenticates() throws E
212215
verify(this.repository).findByRegistrationId("registration-id");
213216
}
214217

218+
static final class RequiresAuthenticationExposingFilter extends Saml2WebSsoAuthenticationFilter {
219+
220+
RequiresAuthenticationExposingFilter(RelyingPartyRegistrationRepository relyingPartyRegistrationRepository) {
221+
super(relyingPartyRegistrationRepository);
222+
}
223+
224+
RequiresAuthenticationExposingFilter(RelyingPartyRegistrationRepository registrations, String url) {
225+
super(registrations, url);
226+
}
227+
228+
@Override
229+
protected boolean requiresAuthentication(HttpServletRequest request, HttpServletResponse response) {
230+
return super.requiresAuthentication(request, response);
231+
}
232+
233+
}
234+
215235
}

0 commit comments

Comments
 (0)