22
22
23
23
import org .hamcrest .BaseMatcher ;
24
24
import org .hamcrest .Description ;
25
+ import org .hamcrest .Matcher ;
25
26
import org .joda .time .DateTime ;
26
27
import org .joda .time .Duration ;
27
28
import org .junit .Before ;
48
49
import static org .springframework .test .util .AssertionErrors .assertTrue ;
49
50
import static org .springframework .util .StringUtils .hasText ;
50
51
52
+ /**
53
+ * Tests for {@link OpenSamlAuthenticationProvider}
54
+ *
55
+ * @author Filip Hanik
56
+ * @author Josh Cummings
57
+ */
51
58
public class OpenSamlAuthenticationProviderTests {
52
59
53
60
private static String username =
"[email protected] " ;
@@ -64,9 +71,9 @@ public class OpenSamlAuthenticationProviderTests {
64
71
65
72
@ Before
66
73
public void setup () {
67
- saml = OpenSamlImplementation .getInstance ();
68
- provider = new OpenSamlAuthenticationProvider ();
69
- token = new Saml2AuthenticationToken (
74
+ this . saml = OpenSamlImplementation .getInstance ();
75
+ this . provider = new OpenSamlAuthenticationProvider ();
76
+ this . token = new Saml2AuthenticationToken (
70
77
"responseXml" ,
71
78
recipientUri ,
72
79
idpEntityId ,
@@ -79,73 +86,66 @@ public void setup() {
79
86
public void supportsWhenSaml2AuthenticationTokenThenReturnTrue () {
80
87
81
88
assertTrue (
82
- OpenSamlAuthenticationProvider .class + "should support " + token .getClass (),
83
- provider .supports (token .getClass ())
89
+ OpenSamlAuthenticationProvider .class + "should support " + this . token .getClass (),
90
+ this . provider .supports (this . token .getClass ())
84
91
);
85
92
}
86
93
87
94
@ Test
88
95
public void supportsWhenNotSaml2AuthenticationTokenThenReturnFalse () {
89
96
assertTrue (
90
97
OpenSamlAuthenticationProvider .class + "should not support " + Authentication .class ,
91
- !provider .supports (Authentication .class )
98
+ !this . provider .supports (Authentication .class )
92
99
);
93
100
}
94
101
95
102
@ Test
96
103
public void authenticateWhenUnknownDataClassThenThrowAuthenticationException () {
97
104
Assertion assertion = defaultAssertion ();
98
- token = responseXml (assertion );
99
- exception .expect (authenticationMatcher (Saml2ErrorCodes .UNKNOWN_RESPONSE_CLASS ));
100
- provider .authenticate (token );
105
+ this . token = responseXml (assertion );
106
+ this . exception .expect (authenticationMatcher (Saml2ErrorCodes .UNKNOWN_RESPONSE_CLASS ));
107
+ this . provider .authenticate (this . token );
101
108
}
102
109
103
110
@ Test
104
111
public void authenticateWhenXmlErrorThenThrowAuthenticationException () {
105
- token = new Saml2AuthenticationToken (
112
+ this . token = new Saml2AuthenticationToken (
106
113
"invalid xml string" ,
107
114
recipientUri ,
108
115
idpEntityId ,
109
116
recipientEntityId ,
110
117
relyingPartyCredentials ()
111
118
);
112
- exception .expect (authenticationMatcher (Saml2ErrorCodes .MALFORMED_RESPONSE_DATA ));
113
- provider .authenticate (token );
119
+ this . exception .expect (authenticationMatcher (Saml2ErrorCodes .MALFORMED_RESPONSE_DATA ));
120
+ this . provider .authenticate (this . token );
114
121
}
115
122
116
123
@ Test
117
124
public void authenticateWhenInvalidDestinationThenThrowAuthenticationException () {
118
125
Response response = response (recipientUri + "invalid" , idpEntityId );
119
- token = responseXml (response );
120
- exception .expect (authenticationMatcher (Saml2ErrorCodes .INVALID_DESTINATION ));
121
- provider .authenticate (token );
126
+ this . token = responseXml (response );
127
+ this . exception .expect (authenticationMatcher (Saml2ErrorCodes .INVALID_DESTINATION ));
128
+ this . provider .authenticate (this . token );
122
129
}
123
130
124
131
@ Test
125
132
public void authenticateWhenNoAssertionsPresentThenThrowAuthenticationException () {
126
133
Response response = response (recipientUri , idpEntityId );
127
- token = responseXml (response );
128
- exception .expect (
129
- authenticationMatcher (
130
- Saml2ErrorCodes .MALFORMED_RESPONSE_DATA ,
131
- "No assertions found in response."
132
- )
134
+ this .token = responseXml (response );
135
+ this .exception .expect (
136
+ authenticationMatcher (Saml2ErrorCodes .MALFORMED_RESPONSE_DATA , "No assertions found in response." )
133
137
);
134
- provider .authenticate (token );
138
+ this . provider .authenticate (this . token );
135
139
}
136
140
137
141
@ Test
138
142
public void authenticateWhenInvalidSignatureOnAssertionThenThrowAuthenticationException () {
139
143
Response response = response (recipientUri , idpEntityId );
140
144
Assertion assertion = defaultAssertion ();
141
145
response .getAssertions ().add (assertion );
142
- token = responseXml (response );
143
- exception .expect (
144
- authenticationMatcher (
145
- Saml2ErrorCodes .INVALID_SIGNATURE
146
- )
147
- );
148
- provider .authenticate (token );
146
+ this .token = responseXml (response );
147
+ this .exception .expect (authenticationMatcher (Saml2ErrorCodes .INVALID_SIGNATURE ));
148
+ this .provider .authenticate (this .token );
149
149
}
150
150
151
151
@ Test
@@ -164,14 +164,10 @@ public void authenticateWhenOpenSAMLValidationErrorThenThrowAuthenticationExcept
164
164
recipientEntityId
165
165
);
166
166
response .getAssertions ().add (assertion );
167
- token = responseXml (response );
167
+ this . token = responseXml (response );
168
168
169
- exception .expect (
170
- authenticationMatcher (
171
- Saml2ErrorCodes .INVALID_ASSERTION
172
- )
173
- );
174
- provider .authenticate (token );
169
+ this .exception .expect (authenticationMatcher (Saml2ErrorCodes .INVALID_ASSERTION ));
170
+ this .provider .authenticate (this .token );
175
171
}
176
172
177
173
@ Test
@@ -185,14 +181,10 @@ public void authenticateWhenMissingSubjectThenThrowAuthenticationException() {
185
181
recipientEntityId
186
182
);
187
183
response .getAssertions ().add (assertion );
188
- token = responseXml (response );
184
+ this . token = responseXml (response );
189
185
190
- exception .expect (
191
- authenticationMatcher (
192
- Saml2ErrorCodes .SUBJECT_NOT_FOUND
193
- )
194
- );
195
- provider .authenticate (token );
186
+ this .exception .expect (authenticationMatcher (Saml2ErrorCodes .SUBJECT_NOT_FOUND ));
187
+ this .provider .authenticate (token );
196
188
}
197
189
198
190
@ Test
@@ -209,14 +201,10 @@ public void authenticateWhenUsernameMissingThenThrowAuthenticationException() th
209
201
recipientEntityId
210
202
);
211
203
response .getAssertions ().add (assertion );
212
- token = responseXml (response );
204
+ this . token = responseXml (response );
213
205
214
- exception .expect (
215
- authenticationMatcher (
216
- Saml2ErrorCodes .USERNAME_NOT_FOUND
217
- )
218
- );
219
- provider .authenticate (token );
206
+ this .exception .expect (authenticationMatcher (Saml2ErrorCodes .USERNAME_NOT_FOUND ));
207
+ this .provider .authenticate (this .token );
220
208
}
221
209
222
210
@ Test
@@ -232,8 +220,8 @@ public void authenticateWhenAssertionContainsValidationAddressThenItSucceeds() t
232
220
recipientEntityId
233
221
);
234
222
response .getAssertions ().add (assertion );
235
- token = responseXml (response );
236
- provider .authenticate (token );
223
+ this . token = responseXml (response );
224
+ this . provider .authenticate (this . token );
237
225
}
238
226
239
227
@ Test
@@ -242,13 +230,9 @@ public void authenticateWhenEncryptedAssertionWithoutSignatureThenItFails() thro
242
230
Assertion assertion = defaultAssertion ();
243
231
EncryptedAssertion encryptedAssertion = encryptAssertion (assertion , assertingPartyCredentials ());
244
232
response .getEncryptedAssertions ().add (encryptedAssertion );
245
- token = responseXml (response );
246
- exception .expect (
247
- authenticationMatcher (
248
- Saml2ErrorCodes .INVALID_SIGNATURE
249
- )
250
- );
251
- provider .authenticate (token );
233
+ this .token = responseXml (response );
234
+ this .exception .expect (authenticationMatcher (Saml2ErrorCodes .INVALID_SIGNATURE ));
235
+ this .provider .authenticate (this .token );
252
236
}
253
237
254
238
@ Test
@@ -262,8 +246,8 @@ public void authenticateWhenEncryptedAssertionWithSignatureThenItSucceeds() thro
262
246
);
263
247
EncryptedAssertion encryptedAssertion = encryptAssertion (assertion , assertingPartyCredentials ());
264
248
response .getEncryptedAssertions ().add (encryptedAssertion );
265
- token = responseXml (response );
266
- provider .authenticate (token );
249
+ this . token = responseXml (response );
250
+ this . provider .authenticate (this . token );
267
251
}
268
252
269
253
@ Test
@@ -277,8 +261,8 @@ public void authenticateWhenEncryptedAssertionWithResponseSignatureThenItSucceed
277
261
assertingPartyCredentials (),
278
262
recipientEntityId
279
263
);
280
- token = responseXml (response );
281
- provider .authenticate (token );
264
+ this . token = responseXml (response );
265
+ provider .authenticate (this . token );
282
266
}
283
267
284
268
@ Test
@@ -295,8 +279,8 @@ public void authenticateWhenEncryptedNameIdWithSignatureThenItSucceeds() throws
295
279
recipientEntityId
296
280
);
297
281
response .getAssertions ().add (assertion );
298
- token = responseXml (response );
299
- provider .authenticate (token );
282
+ this . token = responseXml (response );
283
+ this . provider .authenticate (this . token );
300
284
}
301
285
302
286
@@ -306,23 +290,20 @@ public void authenticateWhenDecryptionKeysAreMissingThenThrowAuthenticationExcep
306
290
Assertion assertion = defaultAssertion ();
307
291
EncryptedAssertion encryptedAssertion = encryptAssertion (assertion , assertingPartyCredentials ());
308
292
response .getEncryptedAssertions ().add (encryptedAssertion );
309
- token = responseXml (response );
293
+ this . token = responseXml (response );
310
294
311
- token = new Saml2AuthenticationToken (
312
- token .getSaml2Response (),
295
+ this . token = new Saml2AuthenticationToken (
296
+ this . token .getSaml2Response (),
313
297
recipientUri ,
314
298
idpEntityId ,
315
299
recipientEntityId ,
316
300
emptyList ()
317
301
);
318
302
319
- exception .expect (
320
- authenticationMatcher (
321
- Saml2ErrorCodes .DECRYPTION_ERROR ,
322
- "No valid decryption credentials found."
323
- )
303
+ this .exception .expect (
304
+ authenticationMatcher (Saml2ErrorCodes .DECRYPTION_ERROR , "No valid decryption credentials found." )
324
305
);
325
- provider .authenticate (token );
306
+ this . provider .authenticate (this . token );
326
307
}
327
308
328
309
@ Test
@@ -331,23 +312,20 @@ public void authenticateWhenDecryptionKeysAreWrongThenThrowAuthenticationExcepti
331
312
Assertion assertion = defaultAssertion ();
332
313
EncryptedAssertion encryptedAssertion = encryptAssertion (assertion , assertingPartyCredentials ());
333
314
response .getEncryptedAssertions ().add (encryptedAssertion );
334
- token = responseXml (response );
315
+ this . token = responseXml (response );
335
316
336
- token = new Saml2AuthenticationToken (
337
- token .getSaml2Response (),
317
+ this . token = new Saml2AuthenticationToken (
318
+ this . token .getSaml2Response (),
338
319
recipientUri ,
339
320
idpEntityId ,
340
321
recipientEntityId ,
341
322
assertingPartyCredentials ()
342
323
);
343
324
344
- exception .expect (
345
- authenticationMatcher (
346
- Saml2ErrorCodes .DECRYPTION_ERROR ,
347
- "Failed to decrypt EncryptedData"
348
- )
325
+ this .exception .expect (
326
+ authenticationMatcher (Saml2ErrorCodes .DECRYPTION_ERROR , "Failed to decrypt EncryptedData" )
349
327
);
350
- provider .authenticate (token );
328
+ this . provider .authenticate (this . token );
351
329
}
352
330
353
331
@ Test
@@ -361,9 +339,9 @@ public void writeObjectWhenTypeIsSaml2AuthenticationThenNoException() throws IOE
361
339
);
362
340
EncryptedAssertion encryptedAssertion = encryptAssertion (assertion , assertingPartyCredentials ());
363
341
response .getEncryptedAssertions ().add (encryptedAssertion );
364
- token = responseXml (response );
342
+ this . token = responseXml (response );
365
343
366
- Saml2Authentication authentication = (Saml2Authentication ) provider .authenticate (token );
344
+ Saml2Authentication authentication = (Saml2Authentication ) this . provider .authenticate (this . token );
367
345
368
346
// the following code will throw an exception if authentication isn't serializable
369
347
ByteArrayOutputStream byteStream = new ByteArrayOutputStream (1024 );
@@ -382,7 +360,7 @@ private Assertion defaultAssertion() {
382
360
}
383
361
384
362
private Saml2AuthenticationToken responseXml (XMLObject assertion ) {
385
- String xml = saml .serialize (assertion );
363
+ String xml = this . saml .serialize (assertion );
386
364
return new Saml2AuthenticationToken (
387
365
xml ,
388
366
recipientUri ,
@@ -393,28 +371,23 @@ private Saml2AuthenticationToken responseXml(XMLObject assertion) {
393
371
394
372
}
395
373
396
- private BaseMatcher <Saml2AuthenticationException > authenticationMatcher (String code ) {
374
+ private Matcher <Saml2AuthenticationException > authenticationMatcher (String code ) {
397
375
return authenticationMatcher (code , null );
398
376
}
399
377
400
- private BaseMatcher <Saml2AuthenticationException > authenticationMatcher (String code , String description ) {
378
+ private Matcher <Saml2AuthenticationException > authenticationMatcher (String code , String description ) {
401
379
return new BaseMatcher <Saml2AuthenticationException >() {
402
- private Object value = null ;
403
-
404
380
@ Override
405
381
public boolean matches (Object item ) {
406
382
if (!(item instanceof Saml2AuthenticationException )) {
407
- value = item ;
408
383
return false ;
409
384
}
410
385
Saml2AuthenticationException ex = (Saml2AuthenticationException ) item ;
411
386
if (!code .equals (ex .getError ().getErrorCode ())) {
412
- value = item ;
413
387
return false ;
414
388
}
415
389
if (hasText (description )) {
416
390
if (!description .equals (ex .getError ().getDescription ())) {
417
- value = item ;
418
391
return false ;
419
392
}
420
393
}
0 commit comments