|
| 1 | +/* |
| 2 | + * Copyright 2004-present the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.security.authorization; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.Collection; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 24 | +import org.mockito.ArgumentCaptor; |
| 25 | +import org.mockito.Captor; |
| 26 | +import org.mockito.Mock; |
| 27 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 28 | + |
| 29 | +import org.springframework.security.access.hierarchicalroles.RoleHierarchy; |
| 30 | +import org.springframework.security.authentication.TestingAuthenticationToken; |
| 31 | +import org.springframework.security.core.Authentication; |
| 32 | +import org.springframework.security.core.GrantedAuthority; |
| 33 | +import org.springframework.security.core.authority.AuthorityUtils; |
| 34 | + |
| 35 | +import static org.assertj.core.api.Assertions.assertThat; |
| 36 | +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; |
| 37 | +import static org.mockito.ArgumentMatchers.any; |
| 38 | +import static org.mockito.BDDMockito.given; |
| 39 | +import static org.mockito.Mockito.verify; |
| 40 | + |
| 41 | +@ExtendWith(MockitoExtension.class) |
| 42 | +class AllAuthoritiesAuthorizationManagerTests { |
| 43 | + |
| 44 | + public static final String ROLE_USER = "ROLE_USER"; |
| 45 | + |
| 46 | + public static final String ROLE_ADMIN = "ROLE_ADMIN"; |
| 47 | + |
| 48 | + @Mock |
| 49 | + private RoleHierarchy roleHierarchy; |
| 50 | + |
| 51 | + @Captor |
| 52 | + private ArgumentCaptor<Collection<? extends GrantedAuthority>> authoritiesCaptor; |
| 53 | + |
| 54 | + @Test |
| 55 | + void hasAllAuthoritiesWhenNullAuthoritiesThenIllegalArgumentException() { |
| 56 | + String[] requiredAuthorities = null; |
| 57 | + assertThatIllegalArgumentException() |
| 58 | + .isThrownBy(() -> AllAuthoritiesAuthorizationManager.hasAllAuthorities(requiredAuthorities)); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void hasAllAuthortiesWhenEmptyAuthoritiesThenIllegalArgumentException() { |
| 63 | + assertThatIllegalArgumentException() |
| 64 | + .isThrownBy(() -> AllAuthoritiesAuthorizationManager.hasAllAuthorities((new String[0]))); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void authorizeWhenGranted() { |
| 69 | + Authentication authentication = new TestingAuthenticationToken("user", "password", ROLE_USER); |
| 70 | + AllAuthoritiesAuthorizationManager manager = AllAuthoritiesAuthorizationManager.hasAllAuthorities(ROLE_USER); |
| 71 | + assertThat(manager.authorize(() -> authentication, "").isGranted()).isTrue(); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void hasAllRolesAuthorizeWhenGranted() { |
| 76 | + Authentication authentication = new TestingAuthenticationToken("user", "password", ROLE_USER); |
| 77 | + AllAuthoritiesAuthorizationManager manager = AllAuthoritiesAuthorizationManager.hasAllRoles("USER"); |
| 78 | + assertThat(manager.authorize(() -> authentication, "").isGranted()).isTrue(); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void hasAllPrefixedAuthoritiesAuthorizeWhenGranted() { |
| 83 | + String prefix = "PREFIX_"; |
| 84 | + String authority1 = "AUTHORITY1"; |
| 85 | + String authority2 = "AUTHORITY2"; |
| 86 | + Authentication authentication = new TestingAuthenticationToken("user", "password", prefix + authority1, |
| 87 | + prefix + authority2); |
| 88 | + AllAuthoritiesAuthorizationManager manager = AllAuthoritiesAuthorizationManager |
| 89 | + .hasAllPrefixedAuthorities(prefix, authority1, authority2); |
| 90 | + assertThat(manager.authorize(() -> authentication, "").isGranted()).isTrue(); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + void authorizeWhenSingleMissingThenDenied() { |
| 95 | + Authentication authentication = new TestingAuthenticationToken("user", "password", ROLE_USER); |
| 96 | + AllAuthoritiesAuthorizationManager manager = AllAuthoritiesAuthorizationManager.hasAllAuthorities(ROLE_ADMIN); |
| 97 | + assertThat(manager.authorize(() -> authentication, "").isGranted()).isFalse(); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + void authorizeWhenMultipleMissingOneThenDenied() { |
| 102 | + Authentication authentication = new TestingAuthenticationToken("user", "password", ROLE_USER); |
| 103 | + AllAuthoritiesAuthorizationManager manager = AllAuthoritiesAuthorizationManager.hasAllAuthorities(ROLE_ADMIN, |
| 104 | + ROLE_USER); |
| 105 | + AuthorityAuthorizationDecision result = manager.authorize(() -> authentication, ""); |
| 106 | + assertThat(result.isGranted()).isFalse(); |
| 107 | + assertThat(result.getAuthorities()).hasSize(1); |
| 108 | + assertThat(new ArrayList<>(result.getAuthorities()).get(0).getAuthority()).isEqualTo(ROLE_ADMIN); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + void setRoleHierarchyWhenNullThenIllegalArgumentException() { |
| 113 | + AllAuthoritiesAuthorizationManager<?> manager = AllAuthoritiesAuthorizationManager.hasAllAuthorities(ROLE_USER); |
| 114 | + assertThatIllegalArgumentException().isThrownBy(() -> manager.setRoleHierarchy(null)); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + void setRoleHierarchyThenUsesResult() { |
| 119 | + Collection result = AuthorityUtils.createAuthorityList(ROLE_USER, ROLE_ADMIN); |
| 120 | + given(this.roleHierarchy.getReachableGrantedAuthorities(any())).willReturn(result); |
| 121 | + AllAuthoritiesAuthorizationManager<Object> manager = AllAuthoritiesAuthorizationManager |
| 122 | + .hasAllAuthorities(ROLE_USER); |
| 123 | + manager.setRoleHierarchy(this.roleHierarchy); |
| 124 | + |
| 125 | + Authentication authentication = new TestingAuthenticationToken("user", "password", ROLE_USER); |
| 126 | + |
| 127 | + AuthorityAuthorizationDecision authz = manager.authorize(() -> authentication, ""); |
| 128 | + assertThat(authz.isGranted()).isTrue(); |
| 129 | + verify(this.roleHierarchy).getReachableGrantedAuthorities(this.authoritiesCaptor.capture()); |
| 130 | + assertThat(this.authoritiesCaptor.getValue()).map(GrantedAuthority::getAuthority).contains(ROLE_USER); |
| 131 | + } |
| 132 | + |
| 133 | +} |
0 commit comments