|
| 1 | +package io.kafbat.ui.config; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 7 | +import static org.mockito.Mockito.when; |
| 8 | +import static org.springframework.security.oauth2.client.registration.ClientRegistration.withRegistrationId; |
| 9 | + |
| 10 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 11 | +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; |
| 12 | +import com.fasterxml.jackson.dataformat.yaml.YAMLMapper; |
| 13 | +import io.kafbat.ui.config.auth.OAuthProperties; |
| 14 | +import io.kafbat.ui.model.rbac.Role; |
| 15 | +import io.kafbat.ui.service.rbac.AccessControlService; |
| 16 | +import io.kafbat.ui.service.rbac.extractor.CognitoAuthorityExtractor; |
| 17 | +import io.kafbat.ui.service.rbac.extractor.GithubAuthorityExtractor; |
| 18 | +import io.kafbat.ui.service.rbac.extractor.GoogleAuthorityExtractor; |
| 19 | +import io.kafbat.ui.service.rbac.extractor.OauthAuthorityExtractor; |
| 20 | +import io.kafbat.ui.service.rbac.extractor.ProviderAuthorityExtractor; |
| 21 | +import io.kafbat.ui.util.AccessControlServiceMock; |
| 22 | +import java.io.IOException; |
| 23 | +import java.io.InputStream; |
| 24 | +import java.time.Instant; |
| 25 | +import java.time.temporal.ChronoUnit; |
| 26 | +import java.util.HashMap; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Map; |
| 29 | +import java.util.Set; |
| 30 | +import lombok.SneakyThrows; |
| 31 | +import org.junit.jupiter.api.BeforeEach; |
| 32 | +import org.junit.jupiter.api.Test; |
| 33 | +import org.springframework.security.core.authority.AuthorityUtils; |
| 34 | +import org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest; |
| 35 | +import org.springframework.security.oauth2.core.AuthorizationGrantType; |
| 36 | +import org.springframework.security.oauth2.core.OAuth2AccessToken; |
| 37 | +import org.springframework.security.oauth2.core.user.DefaultOAuth2User; |
| 38 | +import org.springframework.security.oauth2.core.user.OAuth2User; |
| 39 | + |
| 40 | +public class RegexBasedProviderAuthorityExtractorTest { |
| 41 | + |
| 42 | + |
| 43 | + private final AccessControlService accessControlService = new AccessControlServiceMock().getMock(); |
| 44 | + ProviderAuthorityExtractor extractor; |
| 45 | + |
| 46 | + @BeforeEach |
| 47 | + void setUp() throws IOException { |
| 48 | + |
| 49 | + YAMLMapper mapper = new YAMLMapper(); |
| 50 | + |
| 51 | + InputStream rolesFile = this.getClass() |
| 52 | + .getClassLoader() |
| 53 | + .getResourceAsStream("roles_definition.yaml"); |
| 54 | + |
| 55 | + Role[] roles = mapper.readValue(rolesFile, Role[].class); |
| 56 | + |
| 57 | + when(accessControlService.getRoles()).thenReturn(List.of(roles)); |
| 58 | + |
| 59 | + } |
| 60 | + |
| 61 | + @SneakyThrows |
| 62 | + @Test |
| 63 | + void extractOauth2Authorities() { |
| 64 | + |
| 65 | + extractor = new OauthAuthorityExtractor(); |
| 66 | + |
| 67 | + OAuth2User oauth2User = new DefaultOAuth2User( |
| 68 | + AuthorityUtils.createAuthorityList("SCOPE_message:read"), |
| 69 | + Map. of( "role_definition", Set. of( "ROLE-ADMIN", "ANOTHER-ROLE"), "user_name", "[email protected]"), |
| 70 | + "user_name"); |
| 71 | + |
| 72 | + HashMap<String, Object> additionalParams = new HashMap<>(); |
| 73 | + OAuthProperties.OAuth2Provider provider = new OAuthProperties.OAuth2Provider(); |
| 74 | + provider.setCustomParams(Map.of("roles-field", "role_definition")); |
| 75 | + additionalParams.put("provider", provider); |
| 76 | + |
| 77 | + Set<String> roles = extractor.extract(accessControlService, oauth2User, additionalParams).block(); |
| 78 | + |
| 79 | + assertNotNull(roles); |
| 80 | + assertEquals(Set.of("viewer", "admin"), roles); |
| 81 | + assertFalse(roles.contains("no one's role")); |
| 82 | + |
| 83 | + } |
| 84 | + |
| 85 | + @SneakyThrows |
| 86 | + @Test() |
| 87 | + void extractOauth2Authorities_blankEmail() { |
| 88 | + |
| 89 | + extractor = new OauthAuthorityExtractor(); |
| 90 | + |
| 91 | + OAuth2User oauth2User = new DefaultOAuth2User( |
| 92 | + AuthorityUtils.createAuthorityList("SCOPE_message:read"), |
| 93 | + Map.of("role_definition", Set.of("ROLE-ADMIN", "ANOTHER-ROLE"), "user_name", ""), |
| 94 | + "user_name"); |
| 95 | + |
| 96 | + HashMap<String, Object> additionalParams = new HashMap<>(); |
| 97 | + OAuthProperties.OAuth2Provider provider = new OAuthProperties.OAuth2Provider(); |
| 98 | + provider.setCustomParams(Map.of("roles-field", "role_definition")); |
| 99 | + additionalParams.put("provider", provider); |
| 100 | + |
| 101 | + Set<String> roles = extractor.extract(accessControlService, oauth2User, additionalParams).block(); |
| 102 | + |
| 103 | + assertNotNull(roles); |
| 104 | + assertFalse(roles.contains("viewer")); |
| 105 | + assertTrue(roles.contains("admin")); |
| 106 | + |
| 107 | + } |
| 108 | + |
| 109 | + @SneakyThrows |
| 110 | + @Test |
| 111 | + void extractCognitoAuthorities() { |
| 112 | + |
| 113 | + extractor = new CognitoAuthorityExtractor(); |
| 114 | + |
| 115 | + OAuth2User oauth2User = new DefaultOAuth2User( |
| 116 | + AuthorityUtils.createAuthorityList("SCOPE_message:read"), |
| 117 | + Map. of( "cognito:groups", List. of( "ROLE-ADMIN", "ANOTHER-ROLE"), "user_name", "[email protected]"), |
| 118 | + "user_name"); |
| 119 | + |
| 120 | + HashMap<String, Object> additionalParams = new HashMap<>(); |
| 121 | + |
| 122 | + OAuthProperties.OAuth2Provider provider = new OAuthProperties.OAuth2Provider(); |
| 123 | + provider.setCustomParams(Map.of("roles-field", "role_definition")); |
| 124 | + additionalParams.put("provider", provider); |
| 125 | + |
| 126 | + Set<String> roles = extractor.extract(accessControlService, oauth2User, additionalParams).block(); |
| 127 | + |
| 128 | + assertNotNull(roles); |
| 129 | + assertEquals(Set.of("viewer", "admin"), roles); |
| 130 | + assertFalse(roles.contains("no one's role")); |
| 131 | + |
| 132 | + } |
| 133 | + |
| 134 | + @SneakyThrows |
| 135 | + @Test |
| 136 | + void extractGithubAuthorities() { |
| 137 | + |
| 138 | + extractor = new GithubAuthorityExtractor(); |
| 139 | + |
| 140 | + OAuth2User oauth2User = new DefaultOAuth2User( |
| 141 | + AuthorityUtils.createAuthorityList("SCOPE_message:read"), |
| 142 | + Map. of( "login", "[email protected]"), |
| 143 | + "login"); |
| 144 | + |
| 145 | + HashMap<String, Object> additionalParams = new HashMap<>(); |
| 146 | + |
| 147 | + OAuthProperties.OAuth2Provider provider = new OAuthProperties.OAuth2Provider(); |
| 148 | + additionalParams.put("provider", provider); |
| 149 | + |
| 150 | + additionalParams.put("request", new OAuth2UserRequest( |
| 151 | + withRegistrationId("registration-1") |
| 152 | + .clientId("client-1") |
| 153 | + .clientSecret("secret") |
| 154 | + .redirectUri("https://client.com") |
| 155 | + .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE) |
| 156 | + .authorizationUri("https://provider.com/oauth2/authorization") |
| 157 | + .tokenUri("https://provider.com/oauth2/token") |
| 158 | + .clientName("Client 1") |
| 159 | + .build(), |
| 160 | + new OAuth2AccessToken(OAuth2AccessToken.TokenType.BEARER, "XXXX", Instant.now(), |
| 161 | + Instant.now().plus(10, ChronoUnit.HOURS)))); |
| 162 | + |
| 163 | + Set<String> roles = extractor.extract(accessControlService, oauth2User, additionalParams).block(); |
| 164 | + |
| 165 | + assertNotNull(roles); |
| 166 | + assertEquals(Set.of("viewer"), roles); |
| 167 | + assertFalse(roles.contains("no one's role")); |
| 168 | + |
| 169 | + } |
| 170 | + |
| 171 | + @SneakyThrows |
| 172 | + @Test |
| 173 | + void extractGoogleAuthorities() { |
| 174 | + |
| 175 | + extractor = new GoogleAuthorityExtractor(); |
| 176 | + |
| 177 | + OAuth2User oauth2User = new DefaultOAuth2User( |
| 178 | + AuthorityUtils.createAuthorityList("SCOPE_message:read"), |
| 179 | + Map. of( "hd", "memelord.lol", "email", "[email protected]"), |
| 180 | + "email"); |
| 181 | + |
| 182 | + HashMap<String, Object> additionalParams = new HashMap<>(); |
| 183 | + |
| 184 | + OAuthProperties.OAuth2Provider provider = new OAuthProperties.OAuth2Provider(); |
| 185 | + provider.setCustomParams(Map.of("roles-field", "role_definition")); |
| 186 | + additionalParams.put("provider", provider); |
| 187 | + |
| 188 | + Set<String> roles = extractor.extract(accessControlService, oauth2User, additionalParams).block(); |
| 189 | + |
| 190 | + assertNotNull(roles); |
| 191 | + assertEquals(Set.of("viewer", "admin"), roles); |
| 192 | + assertFalse(roles.contains("no one's role")); |
| 193 | + |
| 194 | + } |
| 195 | + |
| 196 | +} |
0 commit comments