Skip to content

Commit 95f0d02

Browse files
committed
Polish Saml2WebSsoAuthenticationRequestFilter
- Updated formatting - Reordered methods - Removed a method These changes will hopefully simplify future contribution. Issue gh-6019
1 parent 711954e commit 95f0d02

File tree

1 file changed

+53
-64
lines changed

1 file changed

+53
-64
lines changed

saml2/saml2-service-provider/src/main/java/org/springframework/security/saml2/provider/service/servlet/filter/Saml2WebSsoAuthenticationRequestFilter.java

Lines changed: 53 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,13 @@
3535
import org.springframework.security.web.util.matcher.RequestMatcher;
3636
import org.springframework.security.web.util.matcher.RequestMatcher.MatchResult;
3737
import org.springframework.util.Assert;
38+
import org.springframework.util.StringUtils;
3839
import org.springframework.web.filter.OncePerRequestFilter;
3940
import org.springframework.web.util.HtmlUtils;
4041
import org.springframework.web.util.UriComponentsBuilder;
4142
import org.springframework.web.util.UriUtils;
4243

43-
import static java.lang.String.format;
4444
import static java.nio.charset.StandardCharsets.ISO_8859_1;
45-
import static org.springframework.util.StringUtils.hasText;
4645

4746
/**
4847
* This {@code Filter} formulates a
@@ -128,72 +127,36 @@ public void setRedirectMatcher(RequestMatcher redirectMatcher) {
128127
@Override
129128
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
130129
throws ServletException, IOException {
130+
131131
MatchResult matcher = this.redirectMatcher.matcher(request);
132132
if (!matcher.isMatch()) {
133133
filterChain.doFilter(request, response);
134134
return;
135135
}
136136

137137
String registrationId = matcher.getVariables().get("registrationId");
138-
RelyingPartyRegistration relyingParty = this.relyingPartyRegistrationRepository.findByRegistrationId(registrationId);
138+
RelyingPartyRegistration relyingParty =
139+
this.relyingPartyRegistrationRepository.findByRegistrationId(registrationId);
139140
if (relyingParty == null) {
140141
response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
141142
return;
142143
}
143144
if (this.logger.isDebugEnabled()) {
144-
this.logger.debug(format("Creating SAML2 SP Authentication Request for IDP[%s]", relyingParty.getRegistrationId()));
145+
this.logger.debug("Creating SAML 2.0 Authentication Request for Asserting Party [" +
146+
relyingParty.getRegistrationId() + "]");
145147
}
146-
Saml2AuthenticationRequestContext authnRequestCtx = createRedirectAuthenticationRequestContext(relyingParty, request);
148+
Saml2AuthenticationRequestContext context = createRedirectAuthenticationRequestContext(request, relyingParty);
147149
if (relyingParty.getProviderDetails().getBinding() == Saml2MessageBinding.REDIRECT) {
148-
sendRedirect(response, authnRequestCtx);
150+
sendRedirect(response, context);
149151
}
150152
else {
151-
sendPost(response, authnRequestCtx);
152-
}
153-
}
154-
155-
private void sendRedirect(HttpServletResponse response, Saml2AuthenticationRequestContext authnRequestCtx)
156-
throws IOException {
157-
String redirectUrl = createSamlRequestRedirectUrl(authnRequestCtx);
158-
response.sendRedirect(redirectUrl);
159-
}
160-
161-
private void sendPost(HttpServletResponse response, Saml2AuthenticationRequestContext authnRequestCtx)
162-
throws IOException {
163-
Saml2PostAuthenticationRequest authNData =
164-
this.authenticationRequestFactory.createPostAuthenticationRequest(authnRequestCtx);
165-
String html = createSamlPostRequestFormData(authNData);
166-
response.setContentType(MediaType.TEXT_HTML_VALUE);
167-
response.getWriter().write(html);
168-
}
169-
170-
private String createSamlRequestRedirectUrl(Saml2AuthenticationRequestContext authnRequestCtx) {
171-
172-
Saml2RedirectAuthenticationRequest authNData =
173-
this.authenticationRequestFactory.createRedirectAuthenticationRequest(authnRequestCtx);
174-
UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromUriString(authNData.getAuthenticationRequestUri());
175-
addParameter("SAMLRequest", authNData.getSamlRequest(), uriBuilder);
176-
addParameter("RelayState", authNData.getRelayState(), uriBuilder);
177-
addParameter("SigAlg", authNData.getSigAlg(), uriBuilder);
178-
addParameter("Signature", authNData.getSignature(), uriBuilder);
179-
return uriBuilder
180-
.build(true)
181-
.toUriString();
182-
}
183-
184-
private void addParameter(String name, String value, UriComponentsBuilder builder) {
185-
Assert.hasText(name, "name cannot be empty or null");
186-
if (hasText(value)) {
187-
builder.queryParam(
188-
UriUtils.encode(name, ISO_8859_1),
189-
UriUtils.encode(value, ISO_8859_1)
190-
);
153+
sendPost(response, context);
191154
}
192155
}
193156

194157
private Saml2AuthenticationRequestContext createRedirectAuthenticationRequestContext(
195-
RelyingPartyRegistration relyingParty,
196-
HttpServletRequest request) {
158+
HttpServletRequest request, RelyingPartyRegistration relyingParty) {
159+
197160
String applicationUri = Saml2ServletUtils.getApplicationUri(request);
198161
Function<String, String> resolver = templateResolver(applicationUri, relyingParty);
199162
String localSpEntityId = resolver.apply(relyingParty.getLocalEntityIdTemplate());
@@ -210,17 +173,45 @@ private Function<String, String> templateResolver(String applicationUri, Relying
210173
return template -> Saml2ServletUtils.resolveUrlTemplate(template, applicationUri, relyingParty);
211174
}
212175

213-
private String htmlEscape(String value) {
214-
if (hasText(value)) {
215-
return HtmlUtils.htmlEscape(value);
176+
private void sendRedirect(HttpServletResponse response, Saml2AuthenticationRequestContext context)
177+
throws IOException {
178+
Saml2RedirectAuthenticationRequest authenticationRequest =
179+
this.authenticationRequestFactory.createRedirectAuthenticationRequest(context);
180+
UriComponentsBuilder uriBuilder = UriComponentsBuilder
181+
.fromUriString(authenticationRequest.getAuthenticationRequestUri());
182+
addParameter("SAMLRequest", authenticationRequest.getSamlRequest(), uriBuilder);
183+
addParameter("RelayState", authenticationRequest.getRelayState(), uriBuilder);
184+
addParameter("SigAlg", authenticationRequest.getSigAlg(), uriBuilder);
185+
addParameter("Signature", authenticationRequest.getSignature(), uriBuilder);
186+
String redirectUrl = uriBuilder
187+
.build(true)
188+
.toUriString();
189+
response.sendRedirect(redirectUrl);
190+
}
191+
192+
private void addParameter(String name, String value, UriComponentsBuilder builder) {
193+
Assert.hasText(name, "name cannot be empty or null");
194+
if (StringUtils.hasText(value)) {
195+
builder.queryParam(
196+
UriUtils.encode(name, ISO_8859_1),
197+
UriUtils.encode(value, ISO_8859_1)
198+
);
216199
}
217-
return value;
218200
}
219201

220-
private String createSamlPostRequestFormData(Saml2PostAuthenticationRequest request) {
221-
String destination = request.getAuthenticationRequestUri();
222-
String relayState = htmlEscape(request.getRelayState());
223-
String samlRequest = htmlEscape(request.getSamlRequest());
202+
private void sendPost(HttpServletResponse response, Saml2AuthenticationRequestContext context)
203+
throws IOException {
204+
Saml2PostAuthenticationRequest authenticationRequest =
205+
this.authenticationRequestFactory.createPostAuthenticationRequest(context);
206+
String html = createSamlPostRequestFormData(authenticationRequest);
207+
response.setContentType(MediaType.TEXT_HTML_VALUE);
208+
response.getWriter().write(html);
209+
}
210+
211+
private String createSamlPostRequestFormData(Saml2PostAuthenticationRequest authenticationRequest) {
212+
String authenticationRequestUri = authenticationRequest.getAuthenticationRequestUri();
213+
String relayState = authenticationRequest.getRelayState();
214+
String samlRequest = authenticationRequest.getSamlRequest();
224215
StringBuilder postHtml = new StringBuilder()
225216
.append("<!DOCTYPE html>\n")
226217
.append("<html>\n")
@@ -235,16 +226,15 @@ private String createSamlPostRequestFormData(Saml2PostAuthenticationRequest requ
235226
.append(" </p>\n")
236227
.append(" </noscript>\n")
237228
.append(" \n")
238-
.append(" <form action=\"").append(destination).append("\" method=\"post\">\n")
229+
.append(" <form action=\"").append(authenticationRequestUri).append("\" method=\"post\">\n")
239230
.append(" <div>\n")
240231
.append(" <input type=\"hidden\" name=\"SAMLRequest\" value=\"")
241-
.append(samlRequest)
242-
.append("\"/>\n")
243-
;
244-
if (hasText(relayState)) {
232+
.append(HtmlUtils.htmlEscape(samlRequest))
233+
.append("\"/>\n");
234+
if (StringUtils.hasText(relayState)) {
245235
postHtml
246236
.append(" <input type=\"hidden\" name=\"RelayState\" value=\"")
247-
.append(relayState)
237+
.append(HtmlUtils.htmlEscape(relayState))
248238
.append("\"/>\n");
249239
}
250240
postHtml
@@ -257,8 +247,7 @@ private String createSamlPostRequestFormData(Saml2PostAuthenticationRequest requ
257247
.append(" </form>\n")
258248
.append(" \n")
259249
.append(" </body>\n")
260-
.append("</html>")
261-
;
250+
.append("</html>");
262251
return postHtml.toString();
263252
}
264253
}

0 commit comments

Comments
 (0)