35
35
import org .springframework .security .web .util .matcher .RequestMatcher ;
36
36
import org .springframework .security .web .util .matcher .RequestMatcher .MatchResult ;
37
37
import org .springframework .util .Assert ;
38
+ import org .springframework .util .StringUtils ;
38
39
import org .springframework .web .filter .OncePerRequestFilter ;
39
40
import org .springframework .web .util .HtmlUtils ;
40
41
import org .springframework .web .util .UriComponentsBuilder ;
41
42
import org .springframework .web .util .UriUtils ;
42
43
43
- import static java .lang .String .format ;
44
44
import static java .nio .charset .StandardCharsets .ISO_8859_1 ;
45
- import static org .springframework .util .StringUtils .hasText ;
46
45
47
46
/**
48
47
* This {@code Filter} formulates a
@@ -128,72 +127,36 @@ public void setRedirectMatcher(RequestMatcher redirectMatcher) {
128
127
@ Override
129
128
protected void doFilterInternal (HttpServletRequest request , HttpServletResponse response , FilterChain filterChain )
130
129
throws ServletException , IOException {
130
+
131
131
MatchResult matcher = this .redirectMatcher .matcher (request );
132
132
if (!matcher .isMatch ()) {
133
133
filterChain .doFilter (request , response );
134
134
return ;
135
135
}
136
136
137
137
String registrationId = matcher .getVariables ().get ("registrationId" );
138
- RelyingPartyRegistration relyingParty = this .relyingPartyRegistrationRepository .findByRegistrationId (registrationId );
138
+ RelyingPartyRegistration relyingParty =
139
+ this .relyingPartyRegistrationRepository .findByRegistrationId (registrationId );
139
140
if (relyingParty == null ) {
140
141
response .sendError (HttpServletResponse .SC_UNAUTHORIZED );
141
142
return ;
142
143
}
143
144
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 () + "]" );
145
147
}
146
- Saml2AuthenticationRequestContext authnRequestCtx = createRedirectAuthenticationRequestContext (relyingParty , request );
148
+ Saml2AuthenticationRequestContext context = createRedirectAuthenticationRequestContext (request , relyingParty );
147
149
if (relyingParty .getProviderDetails ().getBinding () == Saml2MessageBinding .REDIRECT ) {
148
- sendRedirect (response , authnRequestCtx );
150
+ sendRedirect (response , context );
149
151
}
150
152
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 );
191
154
}
192
155
}
193
156
194
157
private Saml2AuthenticationRequestContext createRedirectAuthenticationRequestContext (
195
- RelyingPartyRegistration relyingParty ,
196
- HttpServletRequest request ) {
158
+ HttpServletRequest request , RelyingPartyRegistration relyingParty ) {
159
+
197
160
String applicationUri = Saml2ServletUtils .getApplicationUri (request );
198
161
Function <String , String > resolver = templateResolver (applicationUri , relyingParty );
199
162
String localSpEntityId = resolver .apply (relyingParty .getLocalEntityIdTemplate ());
@@ -210,17 +173,45 @@ private Function<String, String> templateResolver(String applicationUri, Relying
210
173
return template -> Saml2ServletUtils .resolveUrlTemplate (template , applicationUri , relyingParty );
211
174
}
212
175
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
+ );
216
199
}
217
- return value ;
218
200
}
219
201
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 ();
224
215
StringBuilder postHtml = new StringBuilder ()
225
216
.append ("<!DOCTYPE html>\n " )
226
217
.append ("<html>\n " )
@@ -235,16 +226,15 @@ private String createSamlPostRequestFormData(Saml2PostAuthenticationRequest requ
235
226
.append (" </p>\n " )
236
227
.append (" </noscript>\n " )
237
228
.append (" \n " )
238
- .append (" <form action=\" " ).append (destination ).append ("\" method=\" post\" >\n " )
229
+ .append (" <form action=\" " ).append (authenticationRequestUri ).append ("\" method=\" post\" >\n " )
239
230
.append (" <div>\n " )
240
231
.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 )) {
245
235
postHtml
246
236
.append (" <input type=\" hidden\" name=\" RelayState\" value=\" " )
247
- .append (relayState )
237
+ .append (HtmlUtils . htmlEscape ( relayState ) )
248
238
.append ("\" />\n " );
249
239
}
250
240
postHtml
@@ -257,8 +247,7 @@ private String createSamlPostRequestFormData(Saml2PostAuthenticationRequest requ
257
247
.append (" </form>\n " )
258
248
.append (" \n " )
259
249
.append (" </body>\n " )
260
- .append ("</html>" )
261
- ;
250
+ .append ("</html>" );
262
251
return postHtml .toString ();
263
252
}
264
253
}
0 commit comments