32
32
import java .nio .file .Paths ;
33
33
import java .time .Instant ;
34
34
import java .util .ArrayList ;
35
+ import java .util .Collection ;
35
36
import java .util .Date ;
36
37
import java .util .HashMap ;
37
38
import java .util .List ;
54
55
import org .springframework .context .annotation .ClassPathScanningCandidateComponentProvider ;
55
56
import org .springframework .core .type .filter .AssignableTypeFilter ;
56
57
import org .springframework .security .access .intercept .RunAsUserToken ;
58
+ import org .springframework .security .authentication .AbstractAuthenticationToken ;
57
59
import org .springframework .security .authentication .AnonymousAuthenticationToken ;
58
60
import org .springframework .security .authentication .RememberMeAuthenticationToken ;
59
61
import org .springframework .security .authentication .TestAuthentication ;
62
+ import org .springframework .security .authentication .TestingAuthenticationToken ;
60
63
import org .springframework .security .authentication .UsernamePasswordAuthenticationToken ;
61
64
import org .springframework .security .authentication .jaas .JaasAuthenticationToken ;
65
+ import org .springframework .security .authentication .ott .OneTimeTokenAuthenticationToken ;
62
66
import org .springframework .security .cas .authentication .CasAssertionAuthenticationToken ;
63
67
import org .springframework .security .cas .authentication .CasAuthenticationToken ;
64
68
import org .springframework .security .cas .authentication .CasServiceTicketAuthenticationToken ;
69
+ import org .springframework .security .core .GrantedAuthority ;
65
70
import org .springframework .security .core .SpringSecurityCoreVersion ;
71
+ import org .springframework .security .core .authority .AuthorityUtils ;
66
72
import org .springframework .security .core .session .ReactiveSessionInformation ;
67
73
import org .springframework .security .core .session .SessionInformation ;
68
74
import org .springframework .security .core .userdetails .UserDetails ;
75
+ import org .springframework .security .ldap .userdetails .LdapAuthority ;
69
76
import org .springframework .security .oauth2 .client .OAuth2AuthorizedClient ;
70
77
import org .springframework .security .oauth2 .client .authentication .OAuth2AuthenticationToken ;
71
78
import org .springframework .security .oauth2 .client .authentication .OAuth2AuthorizationCodeAuthenticationToken ;
72
79
import org .springframework .security .oauth2 .client .authentication .OAuth2LoginAuthenticationToken ;
73
80
import org .springframework .security .oauth2 .client .authentication .TestOAuth2AuthenticationTokens ;
74
81
import org .springframework .security .oauth2 .client .authentication .TestOAuth2AuthorizationCodeAuthenticationTokens ;
82
+ import org .springframework .security .oauth2 .client .oidc .authentication .logout .OidcLogoutToken ;
83
+ import org .springframework .security .oauth2 .client .oidc .authentication .logout .TestOidcLogoutTokens ;
84
+ import org .springframework .security .oauth2 .client .oidc .session .OidcSessionInformation ;
85
+ import org .springframework .security .oauth2 .client .oidc .session .TestOidcSessionInformations ;
75
86
import org .springframework .security .oauth2 .client .registration .ClientRegistration ;
76
87
import org .springframework .security .oauth2 .client .registration .TestClientRegistrations ;
88
+ import org .springframework .security .oauth2 .core .DefaultOAuth2AuthenticatedPrincipal ;
89
+ import org .springframework .security .oauth2 .core .OAuth2AccessToken ;
90
+ import org .springframework .security .oauth2 .core .OAuth2AuthenticatedPrincipal ;
91
+ import org .springframework .security .oauth2 .core .OAuth2DeviceCode ;
92
+ import org .springframework .security .oauth2 .core .OAuth2RefreshToken ;
93
+ import org .springframework .security .oauth2 .core .OAuth2UserCode ;
77
94
import org .springframework .security .oauth2 .core .TestOAuth2AccessTokens ;
78
95
import org .springframework .security .oauth2 .core .TestOAuth2AuthenticatedPrincipals ;
79
96
import org .springframework .security .oauth2 .core .endpoint .OAuth2AuthorizationExchange ;
82
99
import org .springframework .security .oauth2 .core .endpoint .TestOAuth2AuthorizationExchanges ;
83
100
import org .springframework .security .oauth2 .core .endpoint .TestOAuth2AuthorizationRequests ;
84
101
import org .springframework .security .oauth2 .core .endpoint .TestOAuth2AuthorizationResponses ;
102
+ import org .springframework .security .oauth2 .core .oidc .OidcIdToken ;
85
103
import org .springframework .security .oauth2 .core .oidc .OidcUserInfo ;
104
+ import org .springframework .security .oauth2 .core .oidc .TestOidcIdTokens ;
105
+ import org .springframework .security .oauth2 .core .oidc .user .DefaultOidcUser ;
106
+ import org .springframework .security .oauth2 .core .oidc .user .OidcUserAuthority ;
107
+ import org .springframework .security .oauth2 .core .oidc .user .TestOidcUsers ;
86
108
import org .springframework .security .oauth2 .core .user .DefaultOAuth2User ;
87
109
import org .springframework .security .oauth2 .core .user .OAuth2UserAuthority ;
88
110
import org .springframework .security .oauth2 .core .user .TestOAuth2Users ;
111
+ import org .springframework .security .oauth2 .jwt .Jwt ;
89
112
import org .springframework .security .oauth2 .jwt .TestJwts ;
113
+ import org .springframework .security .oauth2 .server .resource .BearerTokenError ;
114
+ import org .springframework .security .oauth2 .server .resource .BearerTokenErrors ;
90
115
import org .springframework .security .oauth2 .server .resource .authentication .BearerTokenAuthentication ;
91
116
import org .springframework .security .oauth2 .server .resource .authentication .BearerTokenAuthenticationToken ;
92
117
import org .springframework .security .oauth2 .server .resource .authentication .JwtAuthenticationToken ;
118
+ import org .springframework .security .oauth2 .server .resource .introspection .OAuth2IntrospectionAuthenticatedPrincipal ;
93
119
import org .springframework .security .saml2 .provider .service .authentication .DefaultSaml2AuthenticatedPrincipal ;
120
+ import org .springframework .security .saml2 .provider .service .authentication .Saml2Authentication ;
121
+ import org .springframework .security .saml2 .provider .service .authentication .Saml2PostAuthenticationRequest ;
122
+ import org .springframework .security .saml2 .provider .service .authentication .Saml2RedirectAuthenticationRequest ;
94
123
import org .springframework .security .saml2 .provider .service .authentication .TestSaml2Authentications ;
124
+ import org .springframework .security .saml2 .provider .service .authentication .TestSaml2PostAuthenticationRequests ;
125
+ import org .springframework .security .saml2 .provider .service .authentication .TestSaml2RedirectAuthenticationRequests ;
95
126
import org .springframework .security .web .authentication .WebAuthenticationDetails ;
96
127
import org .springframework .security .web .authentication .preauth .PreAuthenticatedAuthenticationToken ;
97
128
@@ -138,6 +169,17 @@ class SpringSecurityCoreVersionSerializableTests {
138
169
(r ) -> new SessionInformation (user , r .alphanumeric (4 ), new Date (1704378933936L )));
139
170
generatorByClassName .put (ReactiveSessionInformation .class ,
140
171
(r ) -> new ReactiveSessionInformation (user , r .alphanumeric (4 ), Instant .ofEpochMilli (1704378933936L )));
172
+ generatorByClassName .put (OAuth2AccessToken .class , (r ) -> TestOAuth2AccessTokens .scopes ("scope" ));
173
+ generatorByClassName .put (OAuth2DeviceCode .class ,
174
+ (r ) -> new OAuth2DeviceCode ("token" , Instant .now (), Instant .now ()));
175
+ generatorByClassName .put (OAuth2RefreshToken .class ,
176
+ (r ) -> new OAuth2RefreshToken ("refreshToken" , Instant .now (), Instant .now ()));
177
+ generatorByClassName .put (OAuth2UserCode .class ,
178
+ (r ) -> new OAuth2UserCode ("token" , Instant .now (), Instant .now ()));
179
+ generatorByClassName .put (DefaultOidcUser .class , (r ) -> TestOidcUsers .create ());
180
+ generatorByClassName .put (OidcUserAuthority .class ,
181
+ (r ) -> new OidcUserAuthority (TestOidcIdTokens .idToken ().build (),
182
+ new OidcUserInfo (Map .of ("claim" , "value" )), "claim" ));
141
183
142
184
// oauth2-client
143
185
ClientRegistration .Builder clientRegistrationBuilder = TestClientRegistrations .clientRegistration ();
@@ -167,6 +209,18 @@ class SpringSecurityCoreVersionSerializableTests {
167
209
token .setDetails (details );
168
210
return token ;
169
211
});
212
+ generatorByClassName .put (OidcIdToken .class , (r ) -> TestOidcIdTokens .idToken ().build ());
213
+ generatorByClassName .put (OidcLogoutToken .class ,
214
+ (r ) -> TestOidcLogoutTokens .withSessionId ("issuer" , "sessionId" ).issuedAt (Instant .now ()).build ());
215
+ generatorByClassName .put (OidcSessionInformation .class , (r ) -> TestOidcSessionInformations .create ());
216
+ generatorByClassName .put (DefaultOAuth2AuthenticatedPrincipal .class , (r ) -> {
217
+ OAuth2AuthenticatedPrincipal principal = TestOAuth2AuthenticatedPrincipals .active ();
218
+ return new DefaultOAuth2AuthenticatedPrincipal (principal .getName (), principal .getAttributes (),
219
+ (Collection <GrantedAuthority >) principal .getAuthorities ());
220
+ });
221
+
222
+ // oauth2-jwt
223
+ generatorByClassName .put (Jwt .class , (r ) -> TestJwts .user ());
170
224
171
225
// oauth2-resource-server
172
226
generatorByClassName
@@ -192,6 +246,9 @@ class SpringSecurityCoreVersionSerializableTests {
192
246
token .setDetails (details );
193
247
return token ;
194
248
});
249
+ generatorByClassName .put (BearerTokenError .class , (r ) -> BearerTokenErrors .invalidToken ("invalid token" ));
250
+ generatorByClassName .put (OAuth2IntrospectionAuthenticatedPrincipal .class ,
251
+ (r ) -> TestOAuth2AuthenticatedPrincipals .active ());
195
252
196
253
// core
197
254
generatorByClassName .put (RunAsUserToken .class , (r ) -> {
@@ -215,6 +272,11 @@ class SpringSecurityCoreVersionSerializableTests {
215
272
token .setDetails (details );
216
273
return token ;
217
274
});
275
+ generatorByClassName .put (OneTimeTokenAuthenticationToken .class ,
276
+ (r ) -> applyDetails (new OneTimeTokenAuthenticationToken ("username" , "token" )));
277
+
278
+ generatorByClassName .put (TestingAuthenticationToken .class ,
279
+ (r ) -> applyDetails (new TestingAuthenticationToken ("username" , "password" )));
218
280
219
281
// cas
220
282
generatorByClassName .put (CasServiceTicketAuthenticationToken .class , (r ) -> {
@@ -234,11 +296,25 @@ class SpringSecurityCoreVersionSerializableTests {
234
296
return token ;
235
297
});
236
298
299
+ // ldap
300
+ generatorByClassName .put (LdapAuthority .class ,
301
+ (r ) -> new LdapAuthority ("USER" , "username" , Map .of ("attribute" , List .of ("value1" , "value2" ))));
302
+
237
303
// saml2-service-provider
238
304
generatorByClassName .put (DefaultSaml2AuthenticatedPrincipal .class ,
239
305
(r ) -> TestSaml2Authentications .authentication ().getPrincipal ());
306
+ generatorByClassName .put (Saml2Authentication .class ,
307
+ (r ) -> applyDetails (TestSaml2Authentications .authentication ()));
308
+ generatorByClassName .put (Saml2PostAuthenticationRequest .class ,
309
+ (r ) -> TestSaml2PostAuthenticationRequests .create ());
310
+ generatorByClassName .put (Saml2RedirectAuthenticationRequest .class ,
311
+ (r ) -> TestSaml2RedirectAuthenticationRequests .create ());
240
312
241
313
// web
314
+ generatorByClassName .put (AnonymousAuthenticationToken .class , (r ) -> {
315
+ Collection <GrantedAuthority > authorities = AuthorityUtils .createAuthorityList ("ROLE_USER" );
316
+ return applyDetails (new AnonymousAuthenticationToken ("key" , "username" , authorities ));
317
+ });
242
318
generatorByClassName .put (PreAuthenticatedAuthenticationToken .class , (r ) -> {
243
319
PreAuthenticatedAuthenticationToken token = new PreAuthenticatedAuthenticationToken (user , "creds" ,
244
320
user .getAuthorities ());
@@ -361,6 +437,12 @@ private static InstancioApi<?> instancioWithDefaults(Class<?> clazz) {
361
437
return instancio ;
362
438
}
363
439
440
+ private static <T extends AbstractAuthenticationToken > T applyDetails (T authentication ) {
441
+ WebAuthenticationDetails details = new WebAuthenticationDetails ("remote" , "sessionId" );
442
+ authentication .setDetails (details );
443
+ return authentication ;
444
+ }
445
+
364
446
private static String getCurrentVersion () {
365
447
String version = System .getProperty ("springSecurityVersion" );
366
448
String [] parts = version .split ("\\ ." );
0 commit comments