|
28 | 28 | import org.springframework.beans.factory.annotation.Autowired; |
29 | 29 | import org.springframework.context.annotation.Bean; |
30 | 30 | import org.springframework.context.annotation.Configuration; |
| 31 | +import org.springframework.security.access.prepost.PreAuthorize; |
31 | 32 | import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
| 33 | +import org.springframework.security.authorization.AuthenticatedAuthorizationManager; |
32 | 34 | import org.springframework.security.authorization.AuthorityAuthorizationDecision; |
| 35 | +import org.springframework.security.authorization.AuthorityAuthorizationManager; |
| 36 | +import org.springframework.security.authorization.AuthorizationDecision; |
33 | 37 | import org.springframework.security.authorization.AuthorizationManager; |
| 38 | +import org.springframework.security.authorization.AuthorizationManagers; |
34 | 39 | import org.springframework.security.authorization.AuthorizationResult; |
35 | 40 | import org.springframework.security.config.Customizer; |
36 | 41 | import org.springframework.security.config.ObjectPostProcessor; |
37 | 42 | import org.springframework.security.config.annotation.SecurityContextChangedListenerConfig; |
| 43 | +import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity; |
38 | 44 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
39 | 45 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
40 | 46 | import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer; |
|
47 | 53 | import org.springframework.security.core.context.SecurityContextChangedListener; |
48 | 54 | import org.springframework.security.core.context.SecurityContextHolderStrategy; |
49 | 55 | import org.springframework.security.core.userdetails.PasswordEncodedUser; |
| 56 | +import org.springframework.security.core.userdetails.User; |
50 | 57 | import org.springframework.security.core.userdetails.UserDetails; |
51 | 58 | import org.springframework.security.core.userdetails.UserDetailsService; |
52 | | -import org.springframework.security.crypto.password.NoOpPasswordEncoder; |
53 | | -import org.springframework.security.crypto.password.PasswordEncoder; |
54 | 59 | import org.springframework.security.provisioning.InMemoryUserDetailsManager; |
55 | 60 | import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders; |
56 | 61 | import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors; |
|
65 | 70 | import org.springframework.security.web.authentication.ott.RedirectOneTimeTokenGenerationSuccessHandler; |
66 | 71 | import org.springframework.security.web.savedrequest.RequestCache; |
67 | 72 | import org.springframework.test.web.servlet.MockMvc; |
| 73 | +import org.springframework.web.bind.annotation.GetMapping; |
| 74 | +import org.springframework.web.bind.annotation.RestController; |
68 | 75 | import org.springframework.web.servlet.config.annotation.EnableWebMvc; |
69 | 76 |
|
70 | 77 | import static org.hamcrest.Matchers.containsString; |
|
78 | 85 | import static org.springframework.security.config.annotation.SecurityContextChangedListenerArgumentMatchers.setAuthentication; |
79 | 86 | import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.formLogin; |
80 | 87 | import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.logout; |
| 88 | +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; |
81 | 89 | import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.authenticated; |
82 | 90 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
83 | 91 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
@@ -409,57 +417,58 @@ public void configureWhenPortResolverBeanThenPortResolverUsed() throws Exception |
409 | 417 |
|
410 | 418 | @Test |
411 | 419 | void requestWhenUnauthenticatedThenRequiresTwoSteps() throws Exception { |
412 | | - this.spring.register(MfaDslConfig.class).autowire(); |
| 420 | + this.spring.register(MfaDslConfig.class, UserConfig.class).autowire(); |
413 | 421 | UserDetails user = PasswordEncodedUser.user(); |
414 | | - this.mockMvc.perform(get("/profile").with(SecurityMockMvcRequestPostProcessors.user(user))) |
| 422 | + this.mockMvc.perform(get("/profile").with(user(user))) |
415 | 423 | .andExpect(status().is3xxRedirection()) |
416 | 424 | .andExpect(redirectedUrl("http://localhost/login")); |
417 | 425 | this.mockMvc |
418 | | - .perform(post("/ott/generate").param("username", "user") |
419 | | - .with(SecurityMockMvcRequestPostProcessors.user(user)) |
| 426 | + .perform(post("/ott/generate").param("username", "rod") |
| 427 | + .with(user(user)) |
420 | 428 | .with(SecurityMockMvcRequestPostProcessors.csrf())) |
421 | 429 | .andExpect(status().is3xxRedirection()) |
422 | 430 | .andExpect(redirectedUrl("/ott/sent")); |
423 | 431 | this.mockMvc |
424 | | - .perform(post("/login").param("username", user.getUsername()) |
425 | | - .param("password", user.getPassword()) |
| 432 | + .perform(post("/login").param("username", "rod") |
| 433 | + .param("password", "password") |
426 | 434 | .with(SecurityMockMvcRequestPostProcessors.csrf())) |
427 | 435 | .andExpect(status().is3xxRedirection()) |
428 | 436 | .andExpect(redirectedUrl("/")); |
429 | 437 | user = PasswordEncodedUser.withUserDetails(user).authorities("profile:read", "FACTOR_OTT").build(); |
430 | | - this.mockMvc.perform(get("/profile").with(SecurityMockMvcRequestPostProcessors.user(user))) |
| 438 | + this.mockMvc.perform(get("/profile").with(user(user))) |
431 | 439 | .andExpect(status().is3xxRedirection()) |
432 | 440 | .andExpect(redirectedUrl("http://localhost/login")); |
433 | 441 | user = PasswordEncodedUser.withUserDetails(user).authorities("profile:read", "FACTOR_PASSWORD").build(); |
434 | | - this.mockMvc.perform(get("/profile").with(SecurityMockMvcRequestPostProcessors.user(user))) |
| 442 | + this.mockMvc.perform(get("/profile").with(user(user))) |
435 | 443 | .andExpect(status().isOk()) |
436 | 444 | .andExpect(content().string(containsString("/ott/generate"))); |
437 | 445 | user = PasswordEncodedUser.withUserDetails(user) |
438 | 446 | .authorities("profile:read", "FACTOR_PASSWORD", "FACTOR_OTT") |
439 | 447 | .build(); |
440 | | - this.mockMvc.perform(get("/profile").with(SecurityMockMvcRequestPostProcessors.user(user))) |
441 | | - .andExpect(status().isNotFound()); |
| 448 | + this.mockMvc.perform(get("/profile").with(user(user))).andExpect(status().isNotFound()); |
442 | 449 | } |
443 | 450 |
|
444 | 451 | @Test |
445 | 452 | void requestWhenUnauthenticatedX509ThenRequiresTwoSteps() throws Exception { |
446 | | - this.spring.register(MfaDslX509Config.class).autowire(); |
447 | | - this.mockMvc.perform(get("/")).andExpect(status().isForbidden()); |
| 453 | + this.spring.register(MfaDslX509Config.class, UserConfig.class, BasicController.class).autowire(); |
| 454 | + this.mockMvc.perform(get("/profile")).andExpect(status().isForbidden()); |
| 455 | + this.mockMvc.perform(get("/profile").with(user(User.withUsername("rod").authorities("profile:read").build()))) |
| 456 | + .andExpect(status().isForbidden()); |
448 | 457 | this.mockMvc.perform(get("/login")).andExpect(status().isOk()); |
449 | | - this.mockMvc.perform(get("/").with(SecurityMockMvcRequestPostProcessors.x509("rod.cer"))) |
| 458 | + this.mockMvc.perform(get("/profile").with(SecurityMockMvcRequestPostProcessors.x509("rod.cer"))) |
450 | 459 | .andExpect(status().is3xxRedirection()) |
451 | 460 | .andExpect(redirectedUrl("http://localhost/login")); |
452 | | - UserDetails user = PasswordEncodedUser.withUsername("rod") |
453 | | - .password("password") |
454 | | - .authorities("AUTHN_FORM") |
455 | | - .build(); |
456 | 461 | this.mockMvc |
457 | | - .perform(post("/login").param("username", user.getUsername()) |
458 | | - .param("password", user.getPassword()) |
| 462 | + .perform(post("/login").param("username", "rod") |
| 463 | + .param("password", "password") |
459 | 464 | .with(SecurityMockMvcRequestPostProcessors.x509("rod.cer")) |
460 | 465 | .with(SecurityMockMvcRequestPostProcessors.csrf())) |
461 | 466 | .andExpect(status().is3xxRedirection()) |
462 | 467 | .andExpect(redirectedUrl("/")); |
| 468 | + UserDetails authorized = PasswordEncodedUser.withUsername("rod") |
| 469 | + .authorities("profile:read", "FACTOR_X509", "FACTOR_PASSWORD") |
| 470 | + .build(); |
| 471 | + this.mockMvc.perform(get("/profile").with(user(authorized))).andExpect(status().isOk()); |
463 | 472 | } |
464 | 473 |
|
465 | 474 | @Configuration |
@@ -832,75 +841,102 @@ public <O> O postProcess(O object) { |
832 | 841 | static class MfaDslConfig { |
833 | 842 |
|
834 | 843 | @Bean |
835 | | - SecurityFilterChain filterChain(HttpSecurity http) throws Exception { |
| 844 | + SecurityFilterChain filterChain(HttpSecurity http, AuthorizationManagerFactory authz) throws Exception { |
836 | 845 | // @formatter:off |
837 | 846 | http |
838 | 847 | .formLogin(Customizer.withDefaults()) |
839 | 848 | .oneTimeTokenLogin(Customizer.withDefaults()) |
840 | 849 | .authorizeHttpRequests((authorize) -> authorize |
841 | | - .requestMatchers("/profile").access( |
842 | | - new HasAllAuthoritiesAuthorizationManager<>("profile:read", "FACTOR_PASSWORD", "FACTOR_OTT") |
843 | | - ) |
844 | | - .anyRequest().access(new HasAllAuthoritiesAuthorizationManager<>("FACTOR_PASSWORD", "FACTOR_OTT")) |
| 850 | + .requestMatchers("/profile").access(authz.hasAuthority("profile:read")) |
| 851 | + .anyRequest().access(authz.authenticated()) |
845 | 852 | ); |
846 | 853 | return http.build(); |
847 | 854 | // @formatter:on |
848 | 855 | } |
849 | 856 |
|
850 | 857 | @Bean |
851 | | - UserDetailsService users() { |
852 | | - return new InMemoryUserDetailsManager(PasswordEncodedUser.user()); |
| 858 | + OneTimeTokenGenerationSuccessHandler tokenGenerationSuccessHandler() { |
| 859 | + return new RedirectOneTimeTokenGenerationSuccessHandler("/ott/sent"); |
853 | 860 | } |
854 | 861 |
|
855 | 862 | @Bean |
856 | | - PasswordEncoder encoder() { |
857 | | - return NoOpPasswordEncoder.getInstance(); |
858 | | - } |
859 | | - |
860 | | - @Bean |
861 | | - OneTimeTokenGenerationSuccessHandler tokenGenerationSuccessHandler() { |
862 | | - return new RedirectOneTimeTokenGenerationSuccessHandler("/ott/sent"); |
| 863 | + AuthorizationManagerFactory authz() { |
| 864 | + return new AuthorizationManagerFactory("FACTOR_PASSWORD", "FACTOR_OTT"); |
863 | 865 | } |
864 | 866 |
|
865 | 867 | } |
866 | 868 |
|
867 | 869 | @Configuration |
868 | 870 | @EnableWebSecurity |
| 871 | + @EnableMethodSecurity |
869 | 872 | static class MfaDslX509Config { |
870 | 873 |
|
871 | 874 | @Bean |
872 | | - SecurityFilterChain filterChain(HttpSecurity http) throws Exception { |
| 875 | + SecurityFilterChain filterChain(HttpSecurity http, AuthorizationManagerFactory authz) throws Exception { |
873 | 876 | // @formatter:off |
874 | 877 | http |
875 | | - .formLogin(Customizer.withDefaults()) |
876 | 878 | .x509(Customizer.withDefaults()) |
| 879 | + .formLogin(Customizer.withDefaults()) |
877 | 880 | .authorizeHttpRequests((authorize) -> authorize |
878 | | - .anyRequest().access( |
879 | | - new HasAllAuthoritiesAuthorizationManager<>("FACTOR_X509", "FACTOR_PASSWORD") |
880 | | - ) |
| 881 | + .anyRequest().access(authz.authenticated()) |
881 | 882 | ); |
882 | 883 | return http.build(); |
883 | 884 | // @formatter:on |
884 | 885 | } |
885 | 886 |
|
886 | 887 | @Bean |
887 | | - UserDetailsService users() { |
888 | | - return new InMemoryUserDetailsManager( |
889 | | - PasswordEncodedUser.withUsername("rod").password("{noop}password").build()); |
| 888 | + AuthorizationManagerFactory authz() { |
| 889 | + return new AuthorizationManagerFactory("FACTOR_X509", "FACTOR_PASSWORD"); |
| 890 | + } |
| 891 | + |
| 892 | + } |
| 893 | + |
| 894 | + @Configuration |
| 895 | + static class UserConfig { |
| 896 | + |
| 897 | + @Bean |
| 898 | + UserDetails rod() { |
| 899 | + return PasswordEncodedUser.withUsername("rod").password("password").build(); |
| 900 | + } |
| 901 | + |
| 902 | + @Bean |
| 903 | + UserDetailsService users(UserDetails user) { |
| 904 | + return new InMemoryUserDetailsManager(user); |
890 | 905 | } |
891 | 906 |
|
892 | 907 | } |
893 | 908 |
|
894 | | - private static final class HasAllAuthoritiesAuthorizationManager<C> implements AuthorizationManager<C> { |
| 909 | + @RestController |
| 910 | + static class BasicController { |
| 911 | + |
| 912 | + @GetMapping("/profile") |
| 913 | + @PreAuthorize("@authz.hasAuthority('profile:read')") |
| 914 | + String profile() { |
| 915 | + return "profile"; |
| 916 | + } |
| 917 | + |
| 918 | + } |
| 919 | + |
| 920 | + public static class AuthorizationManagerFactory { |
895 | 921 |
|
896 | 922 | private final Collection<String> authorities; |
897 | 923 |
|
898 | | - private HasAllAuthoritiesAuthorizationManager(String... authorities) { |
| 924 | + AuthorizationManagerFactory(String... authorities) { |
899 | 925 | this.authorities = List.of(authorities); |
900 | 926 | } |
901 | 927 |
|
902 | | - @Override |
903 | | - public @Nullable AuthorizationResult authorize(Supplier<Authentication> authentication, C object) { |
| 928 | + public <T> AuthorizationManager<T> authenticated() { |
| 929 | + AuthenticatedAuthorizationManager<T> authenticated = AuthenticatedAuthorizationManager.authenticated(); |
| 930 | + return AuthorizationManagers.allOf(new AuthorizationDecision(false), this::factors, authenticated); |
| 931 | + } |
| 932 | + |
| 933 | + public <T> AuthorizationManager<T> hasAuthority(String authority) { |
| 934 | + AuthorityAuthorizationManager<T> authorized = AuthorityAuthorizationManager.hasAuthority(authority); |
| 935 | + return AuthorizationManagers.allOf(new AuthorizationDecision(false), this::factors, authorized); |
| 936 | + } |
| 937 | + |
| 938 | + private AuthorizationResult factors(Supplier<? extends @Nullable Authentication> authentication, |
| 939 | + Object context) { |
904 | 940 | List<String> authorities = authentication.get() |
905 | 941 | .getAuthorities() |
906 | 942 | .stream() |
|
0 commit comments