Browse Source

./gradlew format

Rob Winch 1 month ago
parent
commit
652a06660c
14 changed files with 114 additions and 117 deletions
  1. 11 9
      servlet/spring-boot/java/authentication/mfa/formLogin+ott/src/main/java/example/FormLoginConfig.java
  2. 10 7
      servlet/spring-boot/java/authentication/mfa/formLogin+ott/src/main/java/example/FormLoginOttMfaApplication.java
  3. 10 9
      servlet/spring-boot/java/authentication/mfa/formLogin+ott/src/main/java/example/OttLoginConfig.java
  4. 49 52
      servlet/spring-boot/java/authentication/mfa/formLogin+ott/src/main/java/example/SecurityDefaultsConfig.java
  5. 2 0
      servlet/spring-boot/java/authentication/mfa/formLogin+ott/src/test/java/example/CustomPagesConfigTests.java
  6. 2 0
      servlet/spring-boot/java/authentication/mfa/formLogin+ott/src/test/java/example/DefaultConfigTests.java
  7. 6 8
      servlet/spring-boot/java/authentication/mfa/formLogin+ott/src/test/java/example/ElevatedSecurityPageConfigTests.java
  8. 3 0
      servlet/spring-boot/java/authentication/mfa/oauth2/src/main/java/example/FormLoginOAuth2Application.java
  9. 9 5
      servlet/spring-boot/java/authentication/mfa/oauth2/src/main/java/example/SecurityConfig.java
  10. 4 5
      servlet/spring-boot/java/authentication/mfa/oauth2/src/test/java/example/FormLoginOAuth2ApplicationTests.java
  11. 1 6
      servlet/spring-boot/java/authentication/mfa/x509+formLogin/src/main/java/example/SecurityConfig.java
  12. 2 5
      servlet/spring-boot/java/authentication/mfa/x509+formLogin/src/test/java/example/MfaApplicationTests.java
  13. 2 7
      servlet/spring-boot/java/authentication/mfa/x509+webauthn/src/main/java/example/SecurityConfig.java
  14. 3 4
      servlet/spring-boot/java/authentication/mfa/x509+webauthn/src/test/java/example/X509WebAuthnMfaApplicationTests.java

+ 11 - 9
servlet/spring-boot/java/authentication/mfa/formLogin+ott/src/main/java/example/FormLoginConfig.java

@@ -12,19 +12,21 @@ import org.springframework.web.bind.annotation.GetMapping;
 @Controller
 @Profile("custom-pages")
 class FormLoginConfig {
-    static final String PATH = "/auth/password";
 
-    @GetMapping(PATH)
-    String auth() {
-        return "password";
-    }
+	static final String PATH = "/auth/password";
 
-    @Bean
-    Customizer<HttpSecurity> formLogin() {
-        // @formatter:off
+	@GetMapping(PATH)
+	String auth() {
+		return "password";
+	}
+
+	@Bean
+	Customizer<HttpSecurity> formLogin() {
+		// @formatter:off
         return (http) -> http
             .authorizeHttpRequests((authz) -> authz.requestMatchers(PATH).permitAll())
             .formLogin((form) -> form.loginPage(PATH));
         // @formatter:on
-    }
+	}
+
 }

+ 10 - 7
servlet/spring-boot/java/authentication/mfa/formLogin+ott/src/main/java/example/FormLoginOttMfaApplication.java

@@ -28,11 +28,14 @@ public class FormLoginOttMfaApplication {
 		SpringApplication.run(FormLoginOttMfaApplication.class, args);
 	}
 
-    @Controller
-    static class AppController {
-        @GetMapping("/profile")
-        String profile() {
-            return "profile";
-        }
-    }
+	@Controller
+	static class AppController {
+
+		@GetMapping("/profile")
+		String profile() {
+			return "profile";
+		}
+
+	}
+
 }

+ 10 - 9
servlet/spring-boot/java/authentication/mfa/formLogin+ott/src/main/java/example/OttLoginConfig.java

@@ -12,20 +12,21 @@ import org.springframework.web.bind.annotation.GetMapping;
 @Controller
 @Profile("custom-pages")
 class OttLoginConfig {
-    static final String PATH = "/auth/ott";
 
-    @GetMapping(PATH)
-    String auth() {
-        return "ott";
-    }
+	static final String PATH = "/auth/ott";
 
-    @Bean
-    Customizer<HttpSecurity> ottLogin() {
-        // @formatter:off
+	@GetMapping(PATH)
+	String auth() {
+		return "ott";
+	}
+
+	@Bean
+	Customizer<HttpSecurity> ottLogin() {
+		// @formatter:off
         return (http) -> http
             .authorizeHttpRequests((authz) -> authz.requestMatchers(PATH).permitAll())
             .oneTimeTokenLogin((ott) -> ott.loginPage(PATH));
         // @formatter:on
-    }
+	}
 
 }

+ 49 - 52
servlet/spring-boot/java/authentication/mfa/formLogin+ott/src/main/java/example/SecurityDefaultsConfig.java

@@ -28,57 +28,54 @@ import static org.springframework.security.core.authority.FactorGrantedAuthority
 @Configuration(proxyBeanMethods = false)
 @EnableGlobalMultiFactorAuthentication(authorities = { PASSWORD_AUTHORITY, OTT_AUTHORITY })
 class SecurityDefaultsConfig {
-    @Bean
-    SecurityFilterChain app(HttpSecurity http, AuthorizationManager<Object> passwordIn5m) {
-        http
-            .authorizeHttpRequests((authz) -> authz
-                .requestMatchers("/profile").access(passwordIn5m)
-                .anyRequest().authenticated()
-            )
-            .formLogin(Customizer.withDefaults())
-            .oneTimeTokenLogin(Customizer.withDefaults());
-        return http.build();
-    }
-
-    @Bean
-    AuthorizationManager<Object> passwordIn5m() {
-        return AuthorizationManagerFactories.multiFactor()
-                .requireFactor((f) -> f.passwordAuthority().validDuration(Duration.ofMinutes(5)))
-                .requireFactor((f) -> f.ottAuthority()).build().authenticated();
-    }
-
-    @Bean
-    UserDetailsService users() {
-        return new InMemoryUserDetailsManager(User.withDefaultPasswordEncoder()
-                .username("user")
-                .password("password")
-                .authorities("app")
-                .build());
-    }
-
-    @Bean
-    OneTimeTokenGenerationSuccessHandler ottSuccessHandler() {
-        return new LoggingOneTimeTokenGenerationSuccessHandler();
-    }
-
-    static final class LoggingOneTimeTokenGenerationSuccessHandler implements OneTimeTokenGenerationSuccessHandler {
-
-        private static final String TOKEN_TEMPLATE = """
-		********************************************************
-		
-		Use this one-time token: %s
-		
-		********************************************************""";
-
-        private final Log logger = LogFactory.getLog(this.getClass());
-
-        @Override
-        public void handle(HttpServletRequest request, HttpServletResponse response, OneTimeToken oneTimeToken)
-                throws IOException {
-            this.logger.info(String.format(TOKEN_TEMPLATE, oneTimeToken.getTokenValue()));
-            response.sendRedirect("/login/ott");
-        }
-
-    }
+
+	@Bean
+	SecurityFilterChain app(HttpSecurity http, AuthorizationManager<Object> passwordIn5m) {
+		http.authorizeHttpRequests(
+				(authz) -> authz.requestMatchers("/profile").access(passwordIn5m).anyRequest().authenticated())
+			.formLogin(Customizer.withDefaults())
+			.oneTimeTokenLogin(Customizer.withDefaults());
+		return http.build();
+	}
+
+	@Bean
+	AuthorizationManager<Object> passwordIn5m() {
+		return AuthorizationManagerFactories.multiFactor()
+			.requireFactor((f) -> f.passwordAuthority().validDuration(Duration.ofMinutes(5)))
+			.requireFactor((f) -> f.ottAuthority())
+			.build()
+			.authenticated();
+	}
+
+	@Bean
+	UserDetailsService users() {
+		return new InMemoryUserDetailsManager(
+				User.withDefaultPasswordEncoder().username("user").password("password").authorities("app").build());
+	}
+
+	@Bean
+	OneTimeTokenGenerationSuccessHandler ottSuccessHandler() {
+		return new LoggingOneTimeTokenGenerationSuccessHandler();
+	}
+
+	static final class LoggingOneTimeTokenGenerationSuccessHandler implements OneTimeTokenGenerationSuccessHandler {
+
+		private static final String TOKEN_TEMPLATE = """
+				********************************************************
+
+				Use this one-time token: %s
+
+				********************************************************""";
+
+		private final Log logger = LogFactory.getLog(this.getClass());
+
+		@Override
+		public void handle(HttpServletRequest request, HttpServletResponse response, OneTimeToken oneTimeToken)
+				throws IOException {
+			this.logger.info(String.format(TOKEN_TEMPLATE, oneTimeToken.getTokenValue()));
+			response.sendRedirect("/login/ott");
+		}
+
+	}
 
 }

+ 2 - 0
servlet/spring-boot/java/authentication/mfa/formLogin+ott/src/test/java/example/CustomPagesConfigTests.java

@@ -19,6 +19,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
 @AutoConfigureMockMvc
 @ActiveProfiles("custom-pages")
 class CustomPagesConfigTests {
+
 	@Autowired
 	private MockMvc mvc;
 
@@ -52,4 +53,5 @@ class CustomPagesConfigTests {
 			.andExpect(status().is3xxRedirection())
 			.andExpect(redirectedUrl("http://localhost/auth/ott?factor=ott"));
 	}
+
 }

+ 2 - 0
servlet/spring-boot/java/authentication/mfa/formLogin+ott/src/test/java/example/DefaultConfigTests.java

@@ -19,6 +19,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
 @AutoConfigureMockMvc
 @ActiveProfiles("default")
 class DefaultConfigTests {
+
 	@Autowired
 	private MockMvc mvc;
 
@@ -52,4 +53,5 @@ class DefaultConfigTests {
 			.andExpect(status().is3xxRedirection())
 			.andExpect(redirectedUrl("http://localhost/login?factor=ott"));
 	}
+
 }

+ 6 - 8
servlet/spring-boot/java/authentication/mfa/formLogin+ott/src/test/java/example/ElevatedSecurityPageConfigTests.java

@@ -19,6 +19,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
 @AutoConfigureMockMvc
 @ActiveProfiles("elevated-security")
 class ElevatedSecurityPageConfigTests {
+
 	@Autowired
 	private MockMvc mvc;
 
@@ -32,22 +33,19 @@ class ElevatedSecurityPageConfigTests {
 	@Test
 	@WithMockUser
 	void indexWhenAuthenticatedButNoFactorsThenAllows() throws Exception {
-		this.mvc.perform(get("/"))
-			.andExpect(status().isOk());
+		this.mvc.perform(get("/")).andExpect(status().isOk());
 	}
 
 	@Test
 	@WithMockUser(authorities = OTT_AUTHORITY)
 	void indexWhenAuthenticatedWithOttThenAllows() throws Exception {
-		this.mvc.perform(get("/"))
-			.andExpect(status().isOk());
+		this.mvc.perform(get("/")).andExpect(status().isOk());
 	}
 
 	@Test
 	@WithMockUser(authorities = PASSWORD_AUTHORITY)
 	void indexWhenAuthenticatedWithPasswordThenAllows() throws Exception {
-		this.mvc.perform(get("/"))
-			.andExpect(status().isOk());
+		this.mvc.perform(get("/")).andExpect(status().isOk());
 	}
 
 	@Test
@@ -61,7 +59,7 @@ class ElevatedSecurityPageConfigTests {
 	@Test
 	@WithMockUser(authorities = OTT_AUTHORITY)
 	void profileWhenAuthenticatedWithOttThenAllows() throws Exception {
-		this.mvc.perform(get("/profile"))
-			.andExpect(status().isOk());
+		this.mvc.perform(get("/profile")).andExpect(status().isOk());
 	}
+
 }

+ 3 - 0
servlet/spring-boot/java/authentication/mfa/oauth2/src/main/java/example/FormLoginOAuth2Application.java

@@ -34,10 +34,12 @@ public class FormLoginOAuth2Application {
 
 	@Controller
 	static class AppController {
+
 		@GetMapping("/profile")
 		String profile() {
 			return "profile";
 		}
+
 	}
 
 	@Bean
@@ -49,4 +51,5 @@ public class FormLoginOAuth2Application {
 			.build();
 		return new InMemoryUserDetailsManager(user);
 	}
+
 }

+ 9 - 5
servlet/spring-boot/java/authentication/mfa/oauth2/src/main/java/example/SecurityConfig.java

@@ -47,7 +47,8 @@ class SecurityConfig {
 	static final String SCOPE = "https://www.googleapis.com/auth/gmail.readonly";
 
 	@Bean
-	public SecurityFilterChain securityFilterChain(HttpSecurity http, AuthenticationEntryPoint oauth2) throws Exception {
+	public SecurityFilterChain securityFilterChain(HttpSecurity http, AuthenticationEntryPoint oauth2)
+			throws Exception {
 		// @formatter:off
 		http
 			.authorizeHttpRequests((authz) -> authz
@@ -79,8 +80,7 @@ class SecurityConfig {
 
 		private final OAuth2AuthorizationRequestResolver authorizationRequestResolver;
 
-		private final AuthorizationRequestRepository<OAuth2AuthorizationRequest> authorizationRequestRepository =
-			new HttpSessionOAuth2AuthorizationRequestRepository();
+		private final AuthorizationRequestRepository<OAuth2AuthorizationRequest> authorizationRequestRepository = new HttpSessionOAuth2AuthorizationRequestRepository();
 
 		OAuth2ScopeAuthenticationEntryPoint(ClientRegistrationRepository clients) {
 			this.google = clients.findByRegistrationId("google");
@@ -88,11 +88,15 @@ class SecurityConfig {
 		}
 
 		@Override
-		public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException ex) throws IOException, ServletException {
-			OAuth2AuthorizationRequest oauth2 = this.authorizationRequestResolver.resolve(request, this.google.getRegistrationId());
+		public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException ex)
+				throws IOException, ServletException {
+			OAuth2AuthorizationRequest oauth2 = this.authorizationRequestResolver.resolve(request,
+					this.google.getRegistrationId());
 			oauth2 = OAuth2AuthorizationRequest.from(oauth2).scopes(Set.of(SCOPE)).build();
 			this.authorizationRequestRepository.saveAuthorizationRequest(oauth2, request, response);
 			response.sendRedirect(oauth2.getAuthorizationRequestUri());
 		}
+
 	}
+
 }

+ 4 - 5
servlet/spring-boot/java/authentication/mfa/oauth2/src/test/java/example/FormLoginOAuth2ApplicationTests.java

@@ -39,8 +39,7 @@ class FormLoginOAuth2ApplicationTests {
 	@Test
 	@WithMockUser
 	void indexWhenAuthenticatedThenAllows() throws Exception {
-		this.mvc.perform(get("/"))
-			.andExpect(status().isOk());
+		this.mvc.perform(get("/")).andExpect(status().isOk());
 	}
 
 	@Test
@@ -48,14 +47,14 @@ class FormLoginOAuth2ApplicationTests {
 	void profileWhenAuthenticatedThenRedirectsToAuthorizationServer() throws Exception {
 		this.mvc.perform(get("/profile"))
 			.andExpect(status().is3xxRedirection())
-			.andExpect(header().string("Location", startsWith("https://accounts.google.com/o/oauth2/v2/auth?response_type=code&client_id=id&scope=https://www.googleapis.com/auth/gmail.readonly")));
+			.andExpect(header().string("Location", startsWith(
+					"https://accounts.google.com/o/oauth2/v2/auth?response_type=code&client_id=id&scope=https://www.googleapis.com/auth/gmail.readonly")));
 	}
 
 	@Test
 	@WithMockUser(authorities = "SCOPE_" + SecurityConfig.SCOPE)
 	void profileWhenAuthenticatedWithScopeThenAllows() throws Exception {
-		this.mvc.perform(get("/profile"))
-			.andExpect(status().isOk());
+		this.mvc.perform(get("/profile")).andExpect(status().isOk());
 	}
 
 }

+ 1 - 6
servlet/spring-boot/java/authentication/mfa/x509+formLogin/src/main/java/example/SecurityConfig.java

@@ -47,12 +47,7 @@ public class SecurityConfig {
 	@Bean
 	public UserDetailsService users() {
 		return new InMemoryUserDetailsManager(
-			User.withDefaultPasswordEncoder()
-				.username("josh")
-				.password("password")
-				.authorities("app")
-				.build()
-		);
+				User.withDefaultPasswordEncoder().username("josh").password("password").authorities("app").build());
 	}
 
 }

+ 2 - 5
servlet/spring-boot/java/authentication/mfa/x509+formLogin/src/test/java/example/MfaApplicationTests.java

@@ -50,8 +50,7 @@ class MfaApplicationTests {
 	@Test
 	@WithMockUser
 	void indexWhenAuthenticatedButNoFactorsThenForbidden() throws Exception {
-		this.mvc.perform(get("/"))
-			.andExpect(status().isForbidden());
+		this.mvc.perform(get("/")).andExpect(status().isForbidden());
 	}
 
 	@Test
@@ -65,9 +64,7 @@ class MfaApplicationTests {
 	@Test
 	@WithMockUser(authorities = PASSWORD_AUTHORITY)
 	void indexWhenAuthenticatedWithPasswordThenForbidden() throws Exception {
-		this.mvc.perform(get("/"))
-			.andExpect(status().isForbidden());
+		this.mvc.perform(get("/")).andExpect(status().isForbidden());
 	}
 
-
 }

+ 2 - 7
servlet/spring-boot/java/authentication/mfa/x509+webauthn/src/main/java/example/SecurityConfig.java

@@ -30,7 +30,7 @@ import static org.springframework.security.core.authority.FactorGrantedAuthority
 import static org.springframework.security.core.authority.FactorGrantedAuthority.X509_AUTHORITY;
 
 @Configuration
-@EnableGlobalMultiFactorAuthentication(authorities = { X509_AUTHORITY, WEBAUTHN_AUTHORITY})
+@EnableGlobalMultiFactorAuthentication(authorities = { X509_AUTHORITY, WEBAUTHN_AUTHORITY })
 public class SecurityConfig {
 
 	@Bean
@@ -52,12 +52,7 @@ public class SecurityConfig {
 	@Bean
 	public UserDetailsService userDetailsService() {
 		return new InMemoryUserDetailsManager(
-			User.withDefaultPasswordEncoder()
-				.username("josh")
-				.password("password")
-				.authorities("app")
-				.build()
-		);
+				User.withDefaultPasswordEncoder().username("josh").password("password").authorities("app").build());
 	}
 
 }

+ 3 - 4
servlet/spring-boot/java/authentication/mfa/x509+webauthn/src/test/java/example/X509WebAuthnMfaApplicationTests.java

@@ -36,6 +36,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
 @SpringBootTest
 @AutoConfigureMockMvc
 public class X509WebAuthnMfaApplicationTests {
+
 	@Autowired
 	private MockMvc mvc;
 
@@ -49,15 +50,13 @@ public class X509WebAuthnMfaApplicationTests {
 	@Test
 	@WithMockUser
 	void indexWhenAuthenticatedButNoFactorsThenRedirectsToLogin() throws Exception {
-		this.mvc.perform(get("/"))
-			.andExpect(status().isForbidden());
+		this.mvc.perform(get("/")).andExpect(status().isForbidden());
 	}
 
 	@Test
 	@WithMockUser(authorities = WEBAUTHN_AUTHORITY)
 	void indexWhenAuthenticatedWithWebAuthnThenForbidden() throws Exception {
-		this.mvc.perform(get("/"))
-			.andExpect(status().isForbidden());
+		this.mvc.perform(get("/")).andExpect(status().isForbidden());
 	}
 
 	@Test