15
15
*/
16
16
package de .sephirothj .spring .security .ltpa2 ;
17
17
18
+ import java .security .PublicKey ;
19
+ import javax .crypto .SecretKey ;
18
20
import org .junit .jupiter .api .Test ;
19
- import org .mockito .ArgumentMatchers ;
21
+ import org .mockito .ArgumentCaptor ;
20
22
import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
21
23
import org .springframework .security .core .userdetails .UserDetailsService ;
24
+ import org .springframework .security .web .authentication .AuthenticationEntryPointFailureHandler ;
25
+ import org .springframework .security .web .authentication .AuthenticationFailureHandler ;
26
+ import org .springframework .security .web .authentication .Http403ForbiddenEntryPoint ;
22
27
import org .springframework .security .web .authentication .preauth .AbstractPreAuthenticatedProcessingFilter ;
23
28
29
+ import static org .assertj .core .api .Assertions .assertThat ;
24
30
import static org .mockito .ArgumentMatchers .eq ;
25
31
import static org .mockito .BDDMockito .given ;
26
32
import static org .mockito .Mockito .mock ;
@@ -37,15 +43,57 @@ void testConfigure() throws Exception
37
43
{
38
44
HttpSecurity httpSecurity = mock (HttpSecurity .class );
39
45
given (httpSecurity .getSharedObject (UserDetailsService .class )).will (invocation -> mock (invocation .getArgument (0 )));
46
+ final String headerName = "header" ;
47
+ final String cookieName = "cookie" ;
48
+ final SecretKey sharedKey = LtpaKeyUtils .decryptSharedKey (Constants .ENCRYPTED_SHARED_KEY , Constants .ENCRYPTION_PASSWORD );
49
+ final PublicKey publicKey = LtpaKeyUtils .decodePublicKey (Constants .ENCODED_PUBLIC_KEY );
40
50
41
51
new Ltpa2Configurer ()
42
- .headerName ("header" )
43
- .cookieName ("cookie" )
52
+ .headerName (headerName )
53
+ .cookieName (cookieName )
44
54
.allowExpiredToken (true )
45
- .sharedKey (LtpaKeyUtils . decryptSharedKey ( Constants . ENCRYPTED_SHARED_KEY , Constants . ENCRYPTION_PASSWORD ) )
46
- .signerKey (LtpaKeyUtils . decodePublicKey ( Constants . ENCODED_PUBLIC_KEY ) )
55
+ .sharedKey (sharedKey )
56
+ .signerKey (publicKey )
47
57
.configure (httpSecurity );
48
58
49
- verify (httpSecurity ).addFilterAt (ArgumentMatchers .isA (Ltpa2Filter .class ), eq (AbstractPreAuthenticatedProcessingFilter .class ));
59
+ ArgumentCaptor <Ltpa2Filter > configuredFilter = ArgumentCaptor .forClass (Ltpa2Filter .class );
60
+ verify (httpSecurity ).addFilterAt (configuredFilter .capture (), eq (AbstractPreAuthenticatedProcessingFilter .class ));
61
+ assertThat (configuredFilter .getValue ())
62
+ .hasFieldOrPropertyWithValue ("headerName" , headerName )
63
+ .hasFieldOrPropertyWithValue ("headerValueIdentifier" , "LtpaToken2 " )
64
+ .hasFieldOrPropertyWithValue ("cookieName" , cookieName )
65
+ .hasFieldOrPropertyWithValue ("allowExpiredToken" , true )
66
+ .hasFieldOrPropertyWithValue ("sharedKey" , sharedKey )
67
+ .hasFieldOrPropertyWithValue ("signerKey" , publicKey )
68
+ .extracting ("authFailureHandler" ).isNotNull ()
69
+ ;
70
+ }
71
+
72
+ @ Test
73
+ void testConfigureWithAuthFaulureHandler () throws Exception
74
+ {
75
+ HttpSecurity httpSecurity = mock (HttpSecurity .class );
76
+ given (httpSecurity .getSharedObject (UserDetailsService .class )).will (invocation -> mock (invocation .getArgument (0 )));
77
+ final SecretKey sharedKey = LtpaKeyUtils .decryptSharedKey (Constants .ENCRYPTED_SHARED_KEY , Constants .ENCRYPTION_PASSWORD );
78
+ final PublicKey publicKey = LtpaKeyUtils .decodePublicKey (Constants .ENCODED_PUBLIC_KEY );
79
+ final AuthenticationFailureHandler failureHandler = new AuthenticationEntryPointFailureHandler (new Http403ForbiddenEntryPoint ());
80
+
81
+ new Ltpa2Configurer ()
82
+ .sharedKey (sharedKey )
83
+ .signerKey (publicKey )
84
+ .authFailureHandler (failureHandler )
85
+ .configure (httpSecurity );
86
+
87
+ ArgumentCaptor <Ltpa2Filter > configuredFilter = ArgumentCaptor .forClass (Ltpa2Filter .class );
88
+ verify (httpSecurity ).addFilterAt (configuredFilter .capture (), eq (AbstractPreAuthenticatedProcessingFilter .class ));
89
+ assertThat (configuredFilter .getValue ())
90
+ .hasFieldOrPropertyWithValue ("headerName" , "Authorization" )
91
+ .hasFieldOrPropertyWithValue ("headerValueIdentifier" , "LtpaToken2 " )
92
+ .hasFieldOrPropertyWithValue ("cookieName" , "LtpaToken2" )
93
+ .hasFieldOrPropertyWithValue ("allowExpiredToken" , false )
94
+ .hasFieldOrPropertyWithValue ("sharedKey" , sharedKey )
95
+ .hasFieldOrPropertyWithValue ("signerKey" , publicKey )
96
+ .hasFieldOrPropertyWithValue ("authFailureHandler" , failureHandler )
97
+ ;
50
98
}
51
99
}
0 commit comments