Skip to content

Commit b98c72e

Browse files
Added nullable
1 parent 1455798 commit b98c72e

5 files changed

Lines changed: 58 additions & 46 deletions

File tree

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

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,10 @@
1616

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

19-
import java.util.Objects;
20-
21-
import jakarta.servlet.http.HttpServletRequest;
22-
import org.jspecify.annotations.Nullable;
23-
import org.opensaml.core.xml.schema.XSString;
24-
import org.opensaml.saml.saml2.core.Issuer;
2519
import org.opensaml.saml.saml2.core.Response;
26-
2720
import org.springframework.http.HttpMethod;
21+
22+
import org.jspecify.annotations.Nullable;
2823
import org.springframework.security.saml2.core.OpenSamlInitializationService;
2924
import org.springframework.security.saml2.core.Saml2Error;
3025
import org.springframework.security.saml2.core.Saml2ParameterNames;
@@ -35,11 +30,12 @@
3530
import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistrationRepository;
3631
import org.springframework.security.saml2.provider.service.web.RelyingPartyRegistrationPlaceholderResolvers.UriResolver;
3732
import org.springframework.security.web.authentication.AuthenticationConverter;
33+
import static org.springframework.security.web.servlet.util.matcher.PathPatternRequestMatcher.pathPattern;
3834
import org.springframework.security.web.util.matcher.OrRequestMatcher;
3935
import org.springframework.security.web.util.matcher.RequestMatcher;
4036
import org.springframework.util.Assert;
4137

42-
import static org.springframework.security.web.servlet.util.matcher.PathPatternRequestMatcher.pathPattern;
38+
import jakarta.servlet.http.HttpServletRequest;
4339

4440
final class BaseOpenSamlAuthenticationTokenConverter implements AuthenticationConverter {
4541

@@ -96,8 +92,14 @@ final class BaseOpenSamlAuthenticationTokenConverter implements AuthenticationCo
9692
* @throws Saml2AuthenticationException if the {@link RequestMatcher} specifies a
9793
* non-existent {@code registrationId}
9894
*/
95+
9996
@Override
97+
<<<<<<< HEAD
10098
public @Nullable Saml2AuthenticationToken convert(HttpServletRequest request) {
99+
=======
100+
@Nullable
101+
public Saml2AuthenticationToken convert(HttpServletRequest request) {
102+
>>>>>>> 46ffe65384 (Added nullable)
101103
String serialized = request.getParameter(Saml2ParameterNames.SAML_RESPONSE);
102104
if (serialized == null) {
103105
return null;
@@ -115,8 +117,9 @@ final class BaseOpenSamlAuthenticationTokenConverter implements AuthenticationCo
115117
}
116118
return token;
117119
}
118-
119-
private @Nullable Saml2AuthenticationToken tokenByAuthenticationRequest(HttpServletRequest request) {
120+
121+
@Nullable
122+
private Saml2AuthenticationToken tokenByAuthenticationRequest(HttpServletRequest request) {
120123
AbstractSaml2AuthenticationRequest authenticationRequest = this.authenticationRequests
121124
.loadAuthenticationRequest(request);
122125
if (authenticationRequest == null) {
@@ -129,8 +132,9 @@ final class BaseOpenSamlAuthenticationTokenConverter implements AuthenticationCo
129132
RelyingPartyRegistration registration = this.registrations.findByRegistrationId(registrationId);
130133
return tokenByRegistration(request, registration, authenticationRequest);
131134
}
132-
133-
private @Nullable Saml2AuthenticationToken tokenByRegistrationId(HttpServletRequest request,
135+
136+
@Nullable
137+
private Saml2AuthenticationToken tokenByRegistrationId(HttpServletRequest request,
134138
RequestMatcher.MatchResult result) {
135139
String registrationId = result.getVariables().get("registrationId");
136140
if (registrationId == null) {
@@ -140,21 +144,18 @@ final class BaseOpenSamlAuthenticationTokenConverter implements AuthenticationCo
140144
return tokenByRegistration(request, registration, null);
141145
}
142146

143-
private @Nullable Saml2AuthenticationToken tokenByEntityId(HttpServletRequest request) {
144-
String decoded = decode(request);
145-
if (decoded == null) {
146-
return null;
147-
}
148-
Response response = this.saml.deserialize(decoded);
149-
Issuer issuer = response.getIssuer();
150-
Assert.notNull(issuer, "Response#Issuer cannot be null");
151-
RelyingPartyRegistration registration = this.registrations.findUniqueByAssertingPartyEntityId(getValue(issuer));
147+
@Nullable
148+
private Saml2AuthenticationToken tokenByEntityId(HttpServletRequest request) {
149+
Response response = this.saml.deserialize(decode(request));
150+
String issuer = response.getIssuer().getValue();
151+
RelyingPartyRegistration registration = this.registrations.findUniqueByAssertingPartyEntityId(issuer);
152152
return tokenByRegistration(request, registration, null);
153153
}
154154

155-
private @Nullable Saml2AuthenticationToken tokenByRegistration(HttpServletRequest request,
156-
@Nullable RelyingPartyRegistration registration,
157-
@Nullable AbstractSaml2AuthenticationRequest authenticationRequest) {
155+
@Nullable
156+
private Saml2AuthenticationToken tokenByRegistration(HttpServletRequest request,
157+
@Nullable RelyingPartyRegistration registration,
158+
@Nulable AbstractSaml2AuthenticationRequest authenticationRequest) {
158159
if (registration == null) {
159160
return null;
160161
}
@@ -205,7 +206,8 @@ void setShouldConvertGetRequests(boolean shouldConvertGetRequests) {
205206
this.shouldConvertGetRequests = shouldConvertGetRequests;
206207
}
207208

208-
private @Nullable String decode(HttpServletRequest request) {
209+
@Nullable
210+
private String decode(HttpServletRequest request) {
209211
String encoded = request.getParameter(Saml2ParameterNames.SAML_RESPONSE);
210212
boolean isGet = HttpMethod.GET.matches(request.getMethod());
211213
if (!this.shouldConvertGetRequests && isGet) {

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

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

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

19-
import jakarta.servlet.http.HttpServletRequest;
20-
import jakarta.servlet.http.HttpServletResponse;
2119
import org.jspecify.annotations.Nullable;
22-
2320
import org.springframework.cache.Cache;
2421
import org.springframework.cache.concurrent.ConcurrentMapCache;
2522
import org.springframework.security.saml2.core.Saml2ParameterNames;
2623
import org.springframework.security.saml2.provider.service.authentication.AbstractSaml2AuthenticationRequest;
2724
import org.springframework.util.Assert;
2825

26+
import jakarta.servlet.http.HttpServletRequest;
27+
import jakarta.servlet.http.HttpServletResponse;
28+
2929
/**
3030
* A cache-based {@link Saml2AuthenticationRequestRepository}. This can be handy when you
3131
* are dropping requests due to using SameSite=Strict and the previous session is lost.
@@ -44,7 +44,8 @@ public final class CacheSaml2AuthenticationRequestRepository
4444
private Cache cache = new ConcurrentMapCache("authentication-requests");
4545

4646
@Override
47-
public @Nullable AbstractSaml2AuthenticationRequest loadAuthenticationRequest(HttpServletRequest request) {
47+
@Nullable
48+
public AbstractSaml2AuthenticationRequest loadAuthenticationRequest(HttpServletRequest request) {
4849
String relayState = request.getParameter(Saml2ParameterNames.RELAY_STATE);
4950
Assert.notNull(relayState, "relayState must not be null");
5051
return this.cache.get(relayState, AbstractSaml2AuthenticationRequest.class);
@@ -60,7 +61,8 @@ public void saveAuthenticationRequest(AbstractSaml2AuthenticationRequest authent
6061
}
6162

6263
@Override
63-
public @Nullable AbstractSaml2AuthenticationRequest removeAuthenticationRequest(HttpServletRequest request,
64+
@Nullable
65+
public AbstractSaml2AuthenticationRequest removeAuthenticationRequest(HttpServletRequest request,
6466
HttpServletResponse response) {
6567
String relayState = request.getParameter(Saml2ParameterNames.RELAY_STATE);
6668
Assert.notNull(relayState, "relayState must not be null");

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@
1717
package org.springframework.security.saml2.provider.service.web;
1818

1919
import java.util.Map;
20-
import java.util.Objects;
20+
import java.util.regex.MatchResult;
2121

22-
import jakarta.servlet.http.HttpServletRequest;
22+
import org.jspecify.annotations.Nullable;
2323
import org.apache.commons.logging.Log;
2424
import org.apache.commons.logging.LogFactory;
25-
import org.jspecify.annotations.Nullable;
26-
2725
import org.springframework.core.convert.converter.Converter;
2826
import org.springframework.http.server.PathContainer;
2927
import org.springframework.http.server.RequestPath;
@@ -33,6 +31,8 @@
3331
import org.springframework.security.web.util.matcher.RequestMatcher;
3432
import org.springframework.util.Assert;
3533

34+
import jakarta.servlet.http.HttpServletRequest;
35+
3636
/**
3737
* A {@link Converter} that resolves a {@link RelyingPartyRegistration} by extracting the
3838
* registration id from the request, querying a
@@ -78,15 +78,17 @@ public DefaultRelyingPartyRegistrationResolver(
7878
* {@inheritDoc}
7979
*/
8080
@Override
81-
public @Nullable RelyingPartyRegistration convert(HttpServletRequest request) {
81+
@Nullable
82+
public RelyingPartyRegistration convert(HttpServletRequest request) {
8283
return resolve(request, null);
8384
}
8485

8586
/**
8687
* {@inheritDoc}
8788
*/
8889
@Override
89-
public @Nullable RelyingPartyRegistration resolve(HttpServletRequest request,
90+
@Nullable
91+
public RelyingPartyRegistration resolve(HttpServletRequest request,
9092
@Nullable String relyingPartyRegistrationId) {
9193
if (relyingPartyRegistrationId == null) {
9294
if (this.logger.isTraceEnabled()) {

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

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

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

19+
import org.jspecify.annotations.Nullable;
20+
import org.springframework.security.saml2.provider.service.authentication.AbstractSaml2AuthenticationRequest;
21+
1922
import jakarta.servlet.http.HttpServletRequest;
2023
import jakarta.servlet.http.HttpServletResponse;
2124
import jakarta.servlet.http.HttpSession;
2225
import org.jspecify.annotations.Nullable;
2326

24-
import org.springframework.security.saml2.provider.service.authentication.AbstractSaml2AuthenticationRequest;
25-
2627
/**
2728
* A {@link Saml2AuthenticationRequestRepository} implementation that uses
2829
* {@link HttpSession} to store and retrieve the
@@ -41,7 +42,8 @@ public class HttpSessionSaml2AuthenticationRequestRepository
4142
private String saml2AuthnRequestAttributeName = DEFAULT_SAML2_AUTHN_REQUEST_ATTR_NAME;
4243

4344
@Override
44-
public @Nullable AbstractSaml2AuthenticationRequest loadAuthenticationRequest(HttpServletRequest request) {
45+
@Nullable
46+
public AbstractSaml2AuthenticationRequest loadAuthenticationRequest(HttpServletRequest request) {
4547
HttpSession httpSession = request.getSession(false);
4648
if (httpSession == null) {
4749
return null;
@@ -50,7 +52,7 @@ public class HttpSessionSaml2AuthenticationRequestRepository
5052
}
5153

5254
@Override
53-
public void saveAuthenticationRequest(AbstractSaml2AuthenticationRequest authenticationRequest,
55+
public void saveAuthenticationRequest(@Nullable AbstractSaml2AuthenticationRequest authenticationRequest,
5456
HttpServletRequest request, HttpServletResponse response) {
5557
if (authenticationRequest == null) {
5658
removeAuthenticationRequest(request, response);
@@ -61,7 +63,8 @@ public void saveAuthenticationRequest(AbstractSaml2AuthenticationRequest authent
6163
}
6264

6365
@Override
64-
public @Nullable AbstractSaml2AuthenticationRequest removeAuthenticationRequest(HttpServletRequest request,
66+
@Nullable
67+
public AbstractSaml2AuthenticationRequest removeAuthenticationRequest(HttpServletRequest request,
6568
HttpServletResponse response) {
6669
AbstractSaml2AuthenticationRequest authenticationRequest = loadAuthenticationRequest(request);
6770
if (authenticationRequest == null) {

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

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

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

19+
import org.jspecify.annotations.Nullable;
20+
import org.springframework.security.saml2.provider.service.authentication.AbstractSaml2AuthenticationRequest;
21+
1922
import jakarta.servlet.http.HttpServletRequest;
2023
import jakarta.servlet.http.HttpServletResponse;
2124
import org.jspecify.annotations.Nullable;
2225

23-
import org.springframework.security.saml2.provider.service.authentication.AbstractSaml2AuthenticationRequest;
24-
2526
/**
2627
* A repository for {@link AbstractSaml2AuthenticationRequest}
2728
*
@@ -37,7 +38,8 @@ public interface Saml2AuthenticationRequestRepository<T extends AbstractSaml2Aut
3738
* @return the {@link AbstractSaml2AuthenticationRequest} or {@code null} if it is not
3839
* present
3940
*/
40-
@Nullable T loadAuthenticationRequest(HttpServletRequest request);
41+
@Nullable
42+
T loadAuthenticationRequest(HttpServletRequest request);
4143

4244
/**
4345
* Saves the current authentication request using the {@link HttpServletRequest} and
@@ -46,7 +48,7 @@ public interface Saml2AuthenticationRequestRepository<T extends AbstractSaml2Aut
4648
* @param request the current request
4749
* @param response the current response
4850
*/
49-
void saveAuthenticationRequest(T authenticationRequest, HttpServletRequest request, HttpServletResponse response);
51+
void saveAuthenticationRequest(@Nullable T authenticationRequest, HttpServletRequest request, HttpServletResponse response);
5052

5153
/**
5254
* Removes the authentication request using the {@link HttpServletRequest} and
@@ -56,6 +58,7 @@ public interface Saml2AuthenticationRequestRepository<T extends AbstractSaml2Aut
5658
* @return the removed {@link AbstractSaml2AuthenticationRequest} or {@code null} if
5759
* it is not present
5860
*/
59-
@Nullable T removeAuthenticationRequest(HttpServletRequest request, HttpServletResponse response);
61+
@Nullable
62+
T removeAuthenticationRequest(HttpServletRequest request, HttpServletResponse response);
6063

6164
}

0 commit comments

Comments
 (0)