Skip to content

Commit 652a066

Browse files
committed
./gradlew format
1 parent 42831f2 commit 652a066

File tree

14 files changed

+114
-117
lines changed

14 files changed

+114
-117
lines changed

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,21 @@
1212
@Controller
1313
@Profile("custom-pages")
1414
class FormLoginConfig {
15-
static final String PATH = "/auth/password";
1615

17-
@GetMapping(PATH)
18-
String auth() {
19-
return "password";
20-
}
16+
static final String PATH = "/auth/password";
2117

22-
@Bean
23-
Customizer<HttpSecurity> formLogin() {
24-
// @formatter:off
18+
@GetMapping(PATH)
19+
String auth() {
20+
return "password";
21+
}
22+
23+
@Bean
24+
Customizer<HttpSecurity> formLogin() {
25+
// @formatter:off
2526
return (http) -> http
2627
.authorizeHttpRequests((authz) -> authz.requestMatchers(PATH).permitAll())
2728
.formLogin((form) -> form.loginPage(PATH));
2829
// @formatter:on
29-
}
30+
}
31+
3032
}

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,14 @@ public static void main(String[] args) {
2828
SpringApplication.run(FormLoginOttMfaApplication.class, args);
2929
}
3030

31-
@Controller
32-
static class AppController {
33-
@GetMapping("/profile")
34-
String profile() {
35-
return "profile";
36-
}
37-
}
31+
@Controller
32+
static class AppController {
33+
34+
@GetMapping("/profile")
35+
String profile() {
36+
return "profile";
37+
}
38+
39+
}
40+
3841
}

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,21 @@
1212
@Controller
1313
@Profile("custom-pages")
1414
class OttLoginConfig {
15-
static final String PATH = "/auth/ott";
1615

17-
@GetMapping(PATH)
18-
String auth() {
19-
return "ott";
20-
}
16+
static final String PATH = "/auth/ott";
2117

22-
@Bean
23-
Customizer<HttpSecurity> ottLogin() {
24-
// @formatter:off
18+
@GetMapping(PATH)
19+
String auth() {
20+
return "ott";
21+
}
22+
23+
@Bean
24+
Customizer<HttpSecurity> ottLogin() {
25+
// @formatter:off
2526
return (http) -> http
2627
.authorizeHttpRequests((authz) -> authz.requestMatchers(PATH).permitAll())
2728
.oneTimeTokenLogin((ott) -> ott.loginPage(PATH));
2829
// @formatter:on
29-
}
30+
}
3031

3132
}

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

Lines changed: 49 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -28,57 +28,54 @@
2828
@Configuration(proxyBeanMethods = false)
2929
@EnableGlobalMultiFactorAuthentication(authorities = { PASSWORD_AUTHORITY, OTT_AUTHORITY })
3030
class SecurityDefaultsConfig {
31-
@Bean
32-
SecurityFilterChain app(HttpSecurity http, AuthorizationManager<Object> passwordIn5m) {
33-
http
34-
.authorizeHttpRequests((authz) -> authz
35-
.requestMatchers("/profile").access(passwordIn5m)
36-
.anyRequest().authenticated()
37-
)
38-
.formLogin(Customizer.withDefaults())
39-
.oneTimeTokenLogin(Customizer.withDefaults());
40-
return http.build();
41-
}
42-
43-
@Bean
44-
AuthorizationManager<Object> passwordIn5m() {
45-
return AuthorizationManagerFactories.multiFactor()
46-
.requireFactor((f) -> f.passwordAuthority().validDuration(Duration.ofMinutes(5)))
47-
.requireFactor((f) -> f.ottAuthority()).build().authenticated();
48-
}
49-
50-
@Bean
51-
UserDetailsService users() {
52-
return new InMemoryUserDetailsManager(User.withDefaultPasswordEncoder()
53-
.username("user")
54-
.password("password")
55-
.authorities("app")
56-
.build());
57-
}
58-
59-
@Bean
60-
OneTimeTokenGenerationSuccessHandler ottSuccessHandler() {
61-
return new LoggingOneTimeTokenGenerationSuccessHandler();
62-
}
63-
64-
static final class LoggingOneTimeTokenGenerationSuccessHandler implements OneTimeTokenGenerationSuccessHandler {
65-
66-
private static final String TOKEN_TEMPLATE = """
67-
********************************************************
68-
69-
Use this one-time token: %s
70-
71-
********************************************************""";
72-
73-
private final Log logger = LogFactory.getLog(this.getClass());
74-
75-
@Override
76-
public void handle(HttpServletRequest request, HttpServletResponse response, OneTimeToken oneTimeToken)
77-
throws IOException {
78-
this.logger.info(String.format(TOKEN_TEMPLATE, oneTimeToken.getTokenValue()));
79-
response.sendRedirect("/login/ott");
80-
}
81-
82-
}
31+
32+
@Bean
33+
SecurityFilterChain app(HttpSecurity http, AuthorizationManager<Object> passwordIn5m) {
34+
http.authorizeHttpRequests(
35+
(authz) -> authz.requestMatchers("/profile").access(passwordIn5m).anyRequest().authenticated())
36+
.formLogin(Customizer.withDefaults())
37+
.oneTimeTokenLogin(Customizer.withDefaults());
38+
return http.build();
39+
}
40+
41+
@Bean
42+
AuthorizationManager<Object> passwordIn5m() {
43+
return AuthorizationManagerFactories.multiFactor()
44+
.requireFactor((f) -> f.passwordAuthority().validDuration(Duration.ofMinutes(5)))
45+
.requireFactor((f) -> f.ottAuthority())
46+
.build()
47+
.authenticated();
48+
}
49+
50+
@Bean
51+
UserDetailsService users() {
52+
return new InMemoryUserDetailsManager(
53+
User.withDefaultPasswordEncoder().username("user").password("password").authorities("app").build());
54+
}
55+
56+
@Bean
57+
OneTimeTokenGenerationSuccessHandler ottSuccessHandler() {
58+
return new LoggingOneTimeTokenGenerationSuccessHandler();
59+
}
60+
61+
static final class LoggingOneTimeTokenGenerationSuccessHandler implements OneTimeTokenGenerationSuccessHandler {
62+
63+
private static final String TOKEN_TEMPLATE = """
64+
********************************************************
65+
66+
Use this one-time token: %s
67+
68+
********************************************************""";
69+
70+
private final Log logger = LogFactory.getLog(this.getClass());
71+
72+
@Override
73+
public void handle(HttpServletRequest request, HttpServletResponse response, OneTimeToken oneTimeToken)
74+
throws IOException {
75+
this.logger.info(String.format(TOKEN_TEMPLATE, oneTimeToken.getTokenValue()));
76+
response.sendRedirect("/login/ott");
77+
}
78+
79+
}
8380

8481
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
@AutoConfigureMockMvc
2020
@ActiveProfiles("custom-pages")
2121
class CustomPagesConfigTests {
22+
2223
@Autowired
2324
private MockMvc mvc;
2425

@@ -52,4 +53,5 @@ void indexWhenAuthenticatedWithPasswordThenRedirectsToOtt() throws Exception {
5253
.andExpect(status().is3xxRedirection())
5354
.andExpect(redirectedUrl("http://localhost/auth/ott?factor=ott"));
5455
}
56+
5557
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
@AutoConfigureMockMvc
2020
@ActiveProfiles("default")
2121
class DefaultConfigTests {
22+
2223
@Autowired
2324
private MockMvc mvc;
2425

@@ -52,4 +53,5 @@ void indexWhenAuthenticatedWithPasswordThenRedirectsToOtt() throws Exception {
5253
.andExpect(status().is3xxRedirection())
5354
.andExpect(redirectedUrl("http://localhost/login?factor=ott"));
5455
}
56+
5557
}

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
@AutoConfigureMockMvc
2020
@ActiveProfiles("elevated-security")
2121
class ElevatedSecurityPageConfigTests {
22+
2223
@Autowired
2324
private MockMvc mvc;
2425

@@ -32,22 +33,19 @@ void indexWhenUnauthenticatedThenRedirectsToLogin() throws Exception {
3233
@Test
3334
@WithMockUser
3435
void indexWhenAuthenticatedButNoFactorsThenAllows() throws Exception {
35-
this.mvc.perform(get("/"))
36-
.andExpect(status().isOk());
36+
this.mvc.perform(get("/")).andExpect(status().isOk());
3737
}
3838

3939
@Test
4040
@WithMockUser(authorities = OTT_AUTHORITY)
4141
void indexWhenAuthenticatedWithOttThenAllows() throws Exception {
42-
this.mvc.perform(get("/"))
43-
.andExpect(status().isOk());
42+
this.mvc.perform(get("/")).andExpect(status().isOk());
4443
}
4544

4645
@Test
4746
@WithMockUser(authorities = PASSWORD_AUTHORITY)
4847
void indexWhenAuthenticatedWithPasswordThenAllows() throws Exception {
49-
this.mvc.perform(get("/"))
50-
.andExpect(status().isOk());
48+
this.mvc.perform(get("/")).andExpect(status().isOk());
5149
}
5250

5351
@Test
@@ -61,7 +59,7 @@ void profileWhenAuthenticatedWithPasswordThenRedirectsToOtt() throws Exception {
6159
@Test
6260
@WithMockUser(authorities = OTT_AUTHORITY)
6361
void profileWhenAuthenticatedWithOttThenAllows() throws Exception {
64-
this.mvc.perform(get("/profile"))
65-
.andExpect(status().isOk());
62+
this.mvc.perform(get("/profile")).andExpect(status().isOk());
6663
}
64+
6765
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@ public static void main(String[] args) {
3434

3535
@Controller
3636
static class AppController {
37+
3738
@GetMapping("/profile")
3839
String profile() {
3940
return "profile";
4041
}
42+
4143
}
4244

4345
@Bean
@@ -49,4 +51,5 @@ InMemoryUserDetailsManager userDetailsService() {
4951
.build();
5052
return new InMemoryUserDetailsManager(user);
5153
}
54+
5255
}

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ class SecurityConfig {
4747
static final String SCOPE = "https://www.googleapis.com/auth/gmail.readonly";
4848

4949
@Bean
50-
public SecurityFilterChain securityFilterChain(HttpSecurity http, AuthenticationEntryPoint oauth2) throws Exception {
50+
public SecurityFilterChain securityFilterChain(HttpSecurity http, AuthenticationEntryPoint oauth2)
51+
throws Exception {
5152
// @formatter:off
5253
http
5354
.authorizeHttpRequests((authz) -> authz
@@ -79,20 +80,23 @@ static class OAuth2ScopeAuthenticationEntryPoint implements AuthenticationEntryP
7980

8081
private final OAuth2AuthorizationRequestResolver authorizationRequestResolver;
8182

82-
private final AuthorizationRequestRepository<OAuth2AuthorizationRequest> authorizationRequestRepository =
83-
new HttpSessionOAuth2AuthorizationRequestRepository();
83+
private final AuthorizationRequestRepository<OAuth2AuthorizationRequest> authorizationRequestRepository = new HttpSessionOAuth2AuthorizationRequestRepository();
8484

8585
OAuth2ScopeAuthenticationEntryPoint(ClientRegistrationRepository clients) {
8686
this.google = clients.findByRegistrationId("google");
8787
this.authorizationRequestResolver = new DefaultOAuth2AuthorizationRequestResolver(clients);
8888
}
8989

9090
@Override
91-
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException ex) throws IOException, ServletException {
92-
OAuth2AuthorizationRequest oauth2 = this.authorizationRequestResolver.resolve(request, this.google.getRegistrationId());
91+
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException ex)
92+
throws IOException, ServletException {
93+
OAuth2AuthorizationRequest oauth2 = this.authorizationRequestResolver.resolve(request,
94+
this.google.getRegistrationId());
9395
oauth2 = OAuth2AuthorizationRequest.from(oauth2).scopes(Set.of(SCOPE)).build();
9496
this.authorizationRequestRepository.saveAuthorizationRequest(oauth2, request, response);
9597
response.sendRedirect(oauth2.getAuthorizationRequestUri());
9698
}
99+
97100
}
101+
98102
}

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,22 @@ class FormLoginOAuth2ApplicationTests {
3939
@Test
4040
@WithMockUser
4141
void indexWhenAuthenticatedThenAllows() throws Exception {
42-
this.mvc.perform(get("/"))
43-
.andExpect(status().isOk());
42+
this.mvc.perform(get("/")).andExpect(status().isOk());
4443
}
4544

4645
@Test
4746
@WithMockUser
4847
void profileWhenAuthenticatedThenRedirectsToAuthorizationServer() throws Exception {
4948
this.mvc.perform(get("/profile"))
5049
.andExpect(status().is3xxRedirection())
51-
.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")));
50+
.andExpect(header().string("Location", startsWith(
51+
"https://accounts.google.com/o/oauth2/v2/auth?response_type=code&client_id=id&scope=https://www.googleapis.com/auth/gmail.readonly")));
5252
}
5353

5454
@Test
5555
@WithMockUser(authorities = "SCOPE_" + SecurityConfig.SCOPE)
5656
void profileWhenAuthenticatedWithScopeThenAllows() throws Exception {
57-
this.mvc.perform(get("/profile"))
58-
.andExpect(status().isOk());
57+
this.mvc.perform(get("/profile")).andExpect(status().isOk());
5958
}
6059

6160
}

0 commit comments

Comments
 (0)