|
31 | 31 | import static org.assertj.core.api.Assertions.assertThat; |
32 | 32 | import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; |
33 | 33 | import static org.assertj.core.api.Assertions.assertThatIllegalStateException; |
| 34 | +import static org.assertj.core.api.InstanceOfAssertFactories.type; |
34 | 35 |
|
35 | 36 | /** |
36 | 37 | * Test {@link AllRequiredFactorsAuthorizationManager}. |
37 | 38 | * |
38 | 39 | * @author Rob Winch |
| 40 | + * @author Evgeniy Cheban |
39 | 41 | * @since 7.0 |
40 | 42 | */ |
41 | 43 | class AllRequiredFactorsAuthorizationManagerTests { |
42 | 44 |
|
43 | 45 | private static final Object DOES_NOT_MATTER = new Object(); |
44 | 46 |
|
45 | | - private static RequiredFactor REQUIRED_PASSWORD = RequiredFactor |
| 47 | + private static final RequiredFactor REQUIRED_PASSWORD = RequiredFactor |
46 | 48 | .withAuthority(FactorGrantedAuthority.PASSWORD_AUTHORITY) |
47 | 49 | .build(); |
48 | 50 |
|
49 | | - private static RequiredFactor EXPIRING_PASSWORD = RequiredFactor |
| 51 | + private static final RequiredFactor EXPIRING_PASSWORD = RequiredFactor |
50 | 52 | .withAuthority(FactorGrantedAuthority.PASSWORD_AUTHORITY) |
51 | 53 | .validDuration(Duration.ofHours(1)) |
52 | 54 | .build(); |
53 | 55 |
|
| 56 | + private static final RequiredFactor REQUIRED_OTT = RequiredFactor |
| 57 | + .withAuthority(FactorGrantedAuthority.OTT_AUTHORITY) |
| 58 | + .build(); |
| 59 | + |
| 60 | + private static final RequiredFactor EXPIRING_OTT = RequiredFactor |
| 61 | + .withAuthority(FactorGrantedAuthority.OTT_AUTHORITY) |
| 62 | + .validDuration(Duration.ofHours(1)) |
| 63 | + .build(); |
| 64 | + |
54 | 65 | @Test |
55 | 66 | void authorizeWhenGranted() { |
56 | 67 | AllRequiredFactorsAuthorizationManager<Object> allFactors = AllRequiredFactorsAuthorizationManager.builder() |
@@ -219,6 +230,53 @@ void authorizeWhenDifferentFactorGrantedAuthorityThenMissing() { |
219 | 230 | assertThat(result.getFactorErrors()).containsExactly(RequiredFactorError.createMissing(REQUIRED_PASSWORD)); |
220 | 231 | } |
221 | 232 |
|
| 233 | + @Test |
| 234 | + void anyOfWhenOneGrantedThenGranted() { |
| 235 | + AllRequiredFactorsAuthorizationManager<Object> expiringPasswordAndOtt = AllRequiredFactorsAuthorizationManager |
| 236 | + .builder() |
| 237 | + .requireFactor(EXPIRING_PASSWORD) |
| 238 | + .requireFactor(EXPIRING_OTT) |
| 239 | + .build(); |
| 240 | + AllRequiredFactorsAuthorizationManager<Object> passwordAndExpiringOtt = AllRequiredFactorsAuthorizationManager |
| 241 | + .builder() |
| 242 | + .requireFactor(REQUIRED_PASSWORD) |
| 243 | + .requireFactor(EXPIRING_OTT) |
| 244 | + .build(); |
| 245 | + FactorGrantedAuthority passwordFactor = FactorGrantedAuthority.withAuthority(EXPIRING_PASSWORD.getAuthority()) |
| 246 | + .issuedAt(Instant.now().minus(Duration.ofHours(2))) |
| 247 | + .build(); |
| 248 | + FactorGrantedAuthority ottFactor = FactorGrantedAuthority.withAuthority(EXPIRING_OTT.getAuthority()).build(); |
| 249 | + AuthorizationManager<Object> anyOf = AllRequiredFactorsAuthorizationManager.anyOf(expiringPasswordAndOtt, |
| 250 | + passwordAndExpiringOtt); |
| 251 | + Authentication authentication = new TestingAuthenticationToken("user", "password", passwordFactor, ottFactor); |
| 252 | + AuthorizationResult result = anyOf.authorize(() -> authentication, DOES_NOT_MATTER); |
| 253 | + assertThat(result).isNotNull(); |
| 254 | + assertThat(result.isGranted()).isTrue(); |
| 255 | + } |
| 256 | + |
| 257 | + @Test |
| 258 | + void anyOfWhenRequiredFactorMissingThenMissing() { |
| 259 | + AllRequiredFactorsAuthorizationManager<Object> passwordAndOtt = AllRequiredFactorsAuthorizationManager.builder() |
| 260 | + .requireFactor(REQUIRED_PASSWORD) |
| 261 | + .requireFactor(REQUIRED_OTT) |
| 262 | + .build(); |
| 263 | + AllRequiredFactorsAuthorizationManager<Object> passwordAndExpiringOtt = AllRequiredFactorsAuthorizationManager |
| 264 | + .builder() |
| 265 | + .requireFactor(REQUIRED_PASSWORD) |
| 266 | + .requireFactor(EXPIRING_OTT) |
| 267 | + .build(); |
| 268 | + FactorGrantedAuthority passwordFactor = FactorGrantedAuthority.withAuthority(REQUIRED_PASSWORD.getAuthority()) |
| 269 | + .build(); |
| 270 | + AuthorizationManager<Object> anyOf = AllRequiredFactorsAuthorizationManager.anyOf(passwordAndOtt, |
| 271 | + passwordAndExpiringOtt); |
| 272 | + Authentication authentication = new TestingAuthenticationToken("user", "password", passwordFactor); |
| 273 | + AuthorizationResult result = anyOf.authorize(() -> authentication, DOES_NOT_MATTER); |
| 274 | + assertThat(result).asInstanceOf(type(FactorAuthorizationDecision.class)).satisfies((decision) -> { |
| 275 | + assertThat(decision.isGranted()).isFalse(); |
| 276 | + assertThat(decision.getFactorErrors()).containsExactly(RequiredFactorError.createMissing(REQUIRED_OTT)); |
| 277 | + }); |
| 278 | + } |
| 279 | + |
222 | 280 | @Test |
223 | 281 | void setClockWhenNullThenIllegalArgumentException() { |
224 | 282 | AllRequiredFactorsAuthorizationManager<Object> allFactors = AllRequiredFactorsAuthorizationManager.builder() |
|
0 commit comments