Skip to content

Commit 694a70a

Browse files
committed
Update to Latest MFA Authorization responses
1 parent d810164 commit 694a70a

File tree

5 files changed

+61
-85
lines changed

5 files changed

+61
-85
lines changed

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,33 +25,32 @@ class CustomPagesConfigTests {
2525

2626
@Test
2727
void indexWhenUnauthenticatedThenRedirectsToLogin() throws Exception {
28-
this.mvc.perform(get("/"))
29-
.andExpect(status().is3xxRedirection())
30-
.andExpect(redirectedUrl("http://localhost/auth/password"));
28+
this.mvc.perform(get("/")).andExpect(status().is3xxRedirection()).andExpect(redirectedUrl("/auth/password"));
3129
}
3230

3331
@Test
3432
@WithMockUser
3533
void indexWhenAuthenticatedButNoFactorsThenRedirectsToLogin() throws Exception {
3634
this.mvc.perform(get("/"))
3735
.andExpect(status().is3xxRedirection())
38-
.andExpect(redirectedUrl("http://localhost/auth/password?factor=password"));
36+
.andExpect(redirectedUrl(
37+
"/auth/password?factor.type=password&factor.type=ott&factor.reason=missing&factor.reason=missing"));
3938
}
4039

4140
@Test
4241
@WithMockUser(authorities = OTT_AUTHORITY)
4342
void indexWhenAuthenticatedWithX509ThenRedirectsToLogin() throws Exception {
4443
this.mvc.perform(get("/"))
4544
.andExpect(status().is3xxRedirection())
46-
.andExpect(redirectedUrl("http://localhost/auth/password?factor=password"));
45+
.andExpect(redirectedUrl("/auth/password?factor.type=password&factor.reason=missing"));
4746
}
4847

4948
@Test
5049
@WithMockUser(authorities = PASSWORD_AUTHORITY)
5150
void indexWhenAuthenticatedWithPasswordThenRedirectsToOtt() throws Exception {
5251
this.mvc.perform(get("/"))
5352
.andExpect(status().is3xxRedirection())
54-
.andExpect(redirectedUrl("http://localhost/auth/ott?factor=ott"));
53+
.andExpect(redirectedUrl("/auth/ott?factor.type=ott&factor.reason=missing"));
5554
}
5655

5756
}

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

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
package example;
22

3+
import java.time.Instant;
4+
35
import org.junit.jupiter.api.Test;
46

57
import org.springframework.beans.factory.annotation.Autowired;
68
import org.springframework.boot.test.context.SpringBootTest;
79
import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc;
10+
import org.springframework.security.core.authority.FactorGrantedAuthority;
11+
import org.springframework.security.core.userdetails.User;
12+
import org.springframework.security.core.userdetails.UserDetails;
813
import org.springframework.security.test.context.support.WithMockUser;
914
import org.springframework.test.context.ActiveProfiles;
1015
import org.springframework.test.web.servlet.MockMvc;
1116

1217
import static org.springframework.security.core.authority.FactorGrantedAuthority.OTT_AUTHORITY;
1318
import static org.springframework.security.core.authority.FactorGrantedAuthority.PASSWORD_AUTHORITY;
19+
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user;
1420
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
1521
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl;
1622
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@@ -25,33 +31,73 @@ class DefaultConfigTests {
2531

2632
@Test
2733
void indexWhenUnauthenticatedThenRedirectsToLogin() throws Exception {
28-
this.mvc.perform(get("/"))
29-
.andExpect(status().is3xxRedirection())
30-
.andExpect(redirectedUrl("http://localhost/login"));
34+
this.mvc.perform(get("/")).andExpect(status().is3xxRedirection()).andExpect(redirectedUrl("/login"));
3135
}
3236

3337
@Test
3438
@WithMockUser
3539
void indexWhenAuthenticatedButNoFactorsThenRedirectsToLogin() throws Exception {
3640
this.mvc.perform(get("/"))
3741
.andExpect(status().is3xxRedirection())
38-
.andExpect(redirectedUrl("http://localhost/login?factor=password"));
42+
.andExpect(redirectedUrl(
43+
"/login?factor.type=password&factor.type=ott&factor.reason=missing&factor.reason=missing"));
3944
}
4045

4146
@Test
4247
@WithMockUser(authorities = OTT_AUTHORITY)
4348
void indexWhenAuthenticatedWithX509ThenRedirectsToLogin() throws Exception {
4449
this.mvc.perform(get("/"))
4550
.andExpect(status().is3xxRedirection())
46-
.andExpect(redirectedUrl("http://localhost/login?factor=password"));
51+
.andExpect(redirectedUrl("/login?factor.type=password&factor.reason=missing"));
4752
}
4853

4954
@Test
5055
@WithMockUser(authorities = PASSWORD_AUTHORITY)
5156
void indexWhenAuthenticatedWithPasswordThenRedirectsToOtt() throws Exception {
5257
this.mvc.perform(get("/"))
5358
.andExpect(status().is3xxRedirection())
54-
.andExpect(redirectedUrl("http://localhost/login?factor=ott"));
59+
.andExpect(redirectedUrl("/login?factor.type=ott&factor.reason=missing"));
60+
}
61+
62+
@Test
63+
void profileWhenAuthenticatedWithPasswordThenRedirectsToOtt() throws Exception {
64+
UserDetails user = User.withDefaultPasswordEncoder()
65+
.username("user")
66+
.authorities(FactorGrantedAuthority.fromAuthority(PASSWORD_AUTHORITY))
67+
.build();
68+
this.mvc.perform(get("/profile").with(user(user)))
69+
.andExpect(status().is3xxRedirection())
70+
.andExpect(redirectedUrl("/login?factor.type=ott&factor.reason=missing"));
71+
}
72+
73+
@Test
74+
void profileWhenAuthenticatedWithOttThenRedirectsToPassword() throws Exception {
75+
UserDetails user = User.withDefaultPasswordEncoder()
76+
.username("user")
77+
.authorities(FactorGrantedAuthority.fromAuthority(OTT_AUTHORITY))
78+
.build();
79+
this.mvc.perform(get("/profile").with(user(user)))
80+
.andExpect(status().is3xxRedirection())
81+
.andExpect(redirectedUrl("/login?factor.type=password&factor.reason=missing"));
82+
}
83+
84+
@Test
85+
void profileWhenExpiredPasswordAuthorityThenRedirectsToPassword() throws Exception {
86+
FactorGrantedAuthority expiredPassword = FactorGrantedAuthority.withAuthority(PASSWORD_AUTHORITY)
87+
.issuedAt(Instant.now().minusSeconds(600))
88+
.build();
89+
FactorGrantedAuthority ott = FactorGrantedAuthority.fromAuthority(OTT_AUTHORITY);
90+
UserDetails user = User.withDefaultPasswordEncoder().username("user").authorities(expiredPassword, ott).build();
91+
this.mvc.perform(get("/profile").with(user(user)))
92+
.andExpect(redirectedUrl("/login?factor.type=password&factor.reason=expired"));
93+
}
94+
95+
@Test
96+
void profileWhenAuthenticatedWithPasswordAndOttThenAllows() throws Exception {
97+
FactorGrantedAuthority password = FactorGrantedAuthority.fromAuthority(PASSWORD_AUTHORITY);
98+
FactorGrantedAuthority ott = FactorGrantedAuthority.fromAuthority(OTT_AUTHORITY);
99+
UserDetails user = User.withDefaultPasswordEncoder().username("user").authorities(password, ott).build();
100+
this.mvc.perform(get("/profile").with(user(user))).andExpect(status().isOk());
55101
}
56102

57103
}

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

Lines changed: 0 additions & 65 deletions
This file was deleted.

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ class MfaApplicationTests {
4242

4343
@Test
4444
void indexWhenUnauthenticatedThenRedirectsToLogin() throws Exception {
45-
this.mvc.perform(get("/"))
46-
.andExpect(status().is3xxRedirection())
47-
.andExpect(redirectedUrl("http://localhost/login"));
45+
this.mvc.perform(get("/")).andExpect(status().is3xxRedirection()).andExpect(redirectedUrl("/login"));
4846
}
4947

5048
@Test
@@ -58,7 +56,7 @@ void indexWhenAuthenticatedButNoFactorsThenForbidden() throws Exception {
5856
void indexWhenAuthenticatedWithX509ThenRedirectsToLogin() throws Exception {
5957
this.mvc.perform(get("/"))
6058
.andExpect(status().is3xxRedirection())
61-
.andExpect(redirectedUrl("http://localhost/login?factor=password"));
59+
.andExpect(redirectedUrl("/login?factor.type=password&factor.reason=missing"));
6260
}
6361

6462
@Test

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ public class X509WebAuthnMfaApplicationTests {
4242

4343
@Test
4444
void indexWhenUnauthenticatedThenRedirectsToLogin() throws Exception {
45-
this.mvc.perform(get("/"))
46-
.andExpect(status().is3xxRedirection())
47-
.andExpect(redirectedUrl("http://localhost/login"));
45+
this.mvc.perform(get("/")).andExpect(status().is3xxRedirection()).andExpect(redirectedUrl("/login"));
4846
}
4947

5048
@Test
@@ -64,7 +62,7 @@ void indexWhenAuthenticatedWithWebAuthnThenForbidden() throws Exception {
6462
void indexWhenAuthenticatedWithX509ThenRedirectsToWebAuthn() throws Exception {
6563
this.mvc.perform(get("/"))
6664
.andExpect(status().is3xxRedirection())
67-
.andExpect(redirectedUrl("http://localhost/login?factor=webauthn"));
65+
.andExpect(redirectedUrl("/login?factor.type=webauthn&factor.reason=missing"));
6866
}
6967

7068
}

0 commit comments

Comments
 (0)