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