Commit c3407bfddcc7b6550149e244523e2ccd14d5d8a4
1 parent
14939c27
Group ClientRegistrations by Domain
Showing
8 changed files
with
293 additions
and
143 deletions
... | ... | @@ -28,10 +28,7 @@ import org.thingsboard.server.common.data.id.DashboardId; |
28 | 28 | import org.thingsboard.server.common.data.id.OAuth2ClientRegistrationId; |
29 | 29 | import org.thingsboard.server.common.data.id.OAuth2ClientRegistrationTemplateId; |
30 | 30 | import org.thingsboard.server.common.data.id.TenantId; |
31 | -import org.thingsboard.server.common.data.oauth2.OAuth2ClientInfo; | |
32 | -import org.thingsboard.server.common.data.oauth2.OAuth2ClientRegistration; | |
33 | -import org.thingsboard.server.common.data.oauth2.OAuth2ClientRegistrationTemplate; | |
34 | -import org.thingsboard.server.common.data.oauth2.OAuth2ClientsParams; | |
31 | +import org.thingsboard.server.common.data.oauth2.*; | |
35 | 32 | import org.thingsboard.server.common.data.security.Authority; |
36 | 33 | import org.thingsboard.server.dao.oauth2.OAuth2Service; |
37 | 34 | import org.thingsboard.server.queue.util.TbCoreComponent; |
... | ... | @@ -40,6 +37,7 @@ import org.thingsboard.server.service.security.permission.Resource; |
40 | 37 | |
41 | 38 | import javax.servlet.http.HttpServletRequest; |
42 | 39 | import java.util.List; |
40 | +import java.util.stream.Collectors; | |
43 | 41 | |
44 | 42 | @RestController |
45 | 43 | @TbCoreComponent |
... | ... | @@ -67,15 +65,13 @@ public class OAuth2Controller extends BaseController { |
67 | 65 | try { |
68 | 66 | Authority authority = getCurrentUser().getAuthority(); |
69 | 67 | checkOAuth2ConfigPermissions(Operation.READ); |
70 | - List<OAuth2ClientRegistration> clientRegistrations = null; | |
71 | 68 | if (Authority.SYS_ADMIN.equals(authority)) { |
72 | - clientRegistrations = oAuth2Service.findClientRegistrationsByTenantId(TenantId.SYS_TENANT_ID); | |
69 | + return oAuth2Service.findClientsParamsByTenantId(TenantId.SYS_TENANT_ID); | |
73 | 70 | } else if (Authority.TENANT_ADMIN.equals(authority)) { |
74 | - clientRegistrations = oAuth2Service.findClientRegistrationsByTenantId(getCurrentUser().getTenantId()); | |
71 | + return oAuth2Service.findClientsParamsByTenantId(getCurrentUser().getTenantId()); | |
75 | 72 | } else { |
76 | 73 | throw new IllegalStateException("Authority " + authority + " cannot get client registrations."); |
77 | 74 | } |
78 | - return new OAuth2ClientsParams(clientRegistrations); | |
79 | 75 | } catch (Exception e) { |
80 | 76 | throw handleException(e); |
81 | 77 | } |
... | ... | @@ -84,11 +80,24 @@ public class OAuth2Controller extends BaseController { |
84 | 80 | @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')") |
85 | 81 | @RequestMapping(value = "/oauth2/config", method = RequestMethod.POST) |
86 | 82 | @ResponseStatus(value = HttpStatus.OK) |
87 | - public OAuth2ClientRegistration saveClientRegistration(@RequestBody OAuth2ClientRegistration clientRegistration) throws ThingsboardException { | |
83 | + public OAuth2ClientsParams saveClientParams(@RequestBody OAuth2ClientsParams clientsParams) throws ThingsboardException { | |
88 | 84 | try { |
89 | - clientRegistration.setTenantId(getCurrentUser().getTenantId()); | |
90 | - checkEntity(clientRegistration.getId(), clientRegistration, Resource.OAUTH2_CONFIGURATION); | |
91 | - return oAuth2Service.saveClientRegistration(clientRegistration); | |
85 | + TenantId tenantId; | |
86 | + Authority authority = getCurrentUser().getAuthority(); | |
87 | + if (Authority.SYS_ADMIN.equals(authority)) { | |
88 | + tenantId = TenantId.SYS_TENANT_ID; | |
89 | + } else if (Authority.TENANT_ADMIN.equals(authority)) { | |
90 | + tenantId = getCurrentUser().getTenantId(); | |
91 | + } else { | |
92 | + throw new IllegalStateException("Authority " + authority + " cannot save client registrations."); | |
93 | + } | |
94 | + List<ClientRegistrationDto> clientRegistrationDtos = clientsParams.getOAuth2DomainDtos().stream() | |
95 | + .flatMap(domainParams -> domainParams.getClientRegistrations().stream()) | |
96 | + .collect(Collectors.toList()); | |
97 | + for (ClientRegistrationDto clientRegistrationDto : clientRegistrationDtos) { | |
98 | + checkEntity(clientRegistrationDto.getId(), () -> tenantId, Resource.OAUTH2_CONFIGURATION); | |
99 | + } | |
100 | + return oAuth2Service.saveClientsParams(tenantId, clientsParams); | |
92 | 101 | } catch (Exception e) { |
93 | 102 | throw handleException(e); |
94 | 103 | } | ... | ... |
... | ... | @@ -19,6 +19,7 @@ import org.thingsboard.server.common.data.id.OAuth2ClientRegistrationId; |
19 | 19 | import org.thingsboard.server.common.data.id.TenantId; |
20 | 20 | import org.thingsboard.server.common.data.oauth2.OAuth2ClientInfo; |
21 | 21 | import org.thingsboard.server.common.data.oauth2.OAuth2ClientRegistration; |
22 | +import org.thingsboard.server.common.data.oauth2.OAuth2ClientsParams; | |
22 | 23 | |
23 | 24 | import java.util.List; |
24 | 25 | import java.util.UUID; |
... | ... | @@ -26,9 +27,9 @@ import java.util.UUID; |
26 | 27 | public interface OAuth2Service { |
27 | 28 | List<OAuth2ClientInfo> getOAuth2Clients(String domainName); |
28 | 29 | |
29 | - OAuth2ClientRegistration saveClientRegistration(OAuth2ClientRegistration clientRegistration); | |
30 | + OAuth2ClientsParams saveClientsParams(TenantId tenantId, OAuth2ClientsParams clientsParams); | |
30 | 31 | |
31 | - List<OAuth2ClientRegistration> findClientRegistrationsByTenantId(TenantId tenantId); | |
32 | + OAuth2ClientsParams findClientsParamsByTenantId(TenantId tenantId); | |
32 | 33 | |
33 | 34 | OAuth2ClientRegistration findClientRegistration(UUID id); |
34 | 35 | ... | ... |
common/data/src/main/java/org/thingsboard/server/common/data/oauth2/ClientRegistrationDto.java
0 → 100644
1 | +package org.thingsboard.server.common.data.oauth2; | |
2 | + | |
3 | +import com.fasterxml.jackson.annotation.JsonProperty; | |
4 | +import lombok.*; | |
5 | +import org.thingsboard.server.common.data.id.OAuth2ClientRegistrationId; | |
6 | +import org.thingsboard.server.common.data.id.TenantId; | |
7 | + | |
8 | +import java.util.List; | |
9 | + | |
10 | +@EqualsAndHashCode | |
11 | +@Data | |
12 | +@ToString(exclude = {"clientSecret"}) | |
13 | +@NoArgsConstructor | |
14 | +@AllArgsConstructor | |
15 | +@Builder | |
16 | +public class ClientRegistrationDto { | |
17 | + private OAuth2ClientRegistrationId id; | |
18 | + private long createdTime; | |
19 | + private OAuth2MapperConfig mapperConfig; | |
20 | + private String clientId; | |
21 | + private String clientSecret; | |
22 | + private String authorizationUri; | |
23 | + private String accessTokenUri; | |
24 | + private List<String> scope; | |
25 | + private String userInfoUri; | |
26 | + private String userNameAttributeName; | |
27 | + private String jwkSetUri; | |
28 | + private String clientAuthenticationMethod; | |
29 | + private String loginButtonLabel; | |
30 | + private String loginButtonIcon; | |
31 | +} | ... | ... |
... | ... | @@ -28,5 +28,5 @@ import java.util.List; |
28 | 28 | public class OAuth2ClientsDomainParams { |
29 | 29 | private String domainName; |
30 | 30 | private String redirectUriTemplate; |
31 | - private List<OAuth2ClientRegistration> clientRegistrations; | |
31 | + private List<ClientRegistrationDto> clientRegistrations; | |
32 | 32 | } |
\ No newline at end of file | ... | ... |
... | ... | @@ -16,8 +16,11 @@ |
16 | 16 | package org.thingsboard.server.common.data.oauth2; |
17 | 17 | |
18 | 18 | import lombok.*; |
19 | +import org.thingsboard.server.common.data.id.TenantId; | |
19 | 20 | |
21 | +import java.util.Collection; | |
20 | 22 | import java.util.List; |
23 | +import java.util.Objects; | |
21 | 24 | |
22 | 25 | @EqualsAndHashCode |
23 | 26 | @Data |
... | ... | @@ -26,5 +29,5 @@ import java.util.List; |
26 | 29 | @NoArgsConstructor |
27 | 30 | @AllArgsConstructor |
28 | 31 | public class OAuth2ClientsParams { |
29 | - private List<OAuth2ClientRegistration> clientRegistrations; | |
32 | + private List<OAuth2ClientsDomainParams> oAuth2DomainDtos; | |
30 | 33 | } |
\ No newline at end of file | ... | ... |
... | ... | @@ -33,8 +33,9 @@ import org.thingsboard.server.dao.service.DataValidator; |
33 | 33 | import org.thingsboard.server.dao.tenant.TenantService; |
34 | 34 | |
35 | 35 | import javax.transaction.Transactional; |
36 | -import java.util.List; | |
37 | -import java.util.UUID; | |
36 | +import java.util.*; | |
37 | +import java.util.function.BiConsumer; | |
38 | +import java.util.function.Consumer; | |
38 | 39 | import java.util.stream.Collectors; |
39 | 40 | |
40 | 41 | import static org.thingsboard.server.dao.oauth2.OAuth2Utils.ALLOW_OAUTH2_CONFIGURATION; |
... | ... | @@ -64,17 +65,22 @@ public class OAuth2ServiceImpl extends AbstractEntityService implements OAuth2Se |
64 | 65 | } |
65 | 66 | |
66 | 67 | @Override |
67 | - public OAuth2ClientRegistration saveClientRegistration(OAuth2ClientRegistration clientRegistration) { | |
68 | - log.trace("Executing saveClientRegistration [{}]", clientRegistration); | |
69 | - clientRegistrationValidator.validate(clientRegistration, OAuth2ClientRegistration::getTenantId); | |
70 | - return clientRegistrationDao.save(clientRegistration.getTenantId(), clientRegistration); | |
68 | + @Transactional | |
69 | + public OAuth2ClientsParams saveClientsParams(TenantId tenantId, OAuth2ClientsParams clientsParams) { | |
70 | + log.trace("Executing saveClientsParams [{}] [{}]", tenantId, clientsParams); | |
71 | + clientParamsValidator.accept(tenantId, clientsParams); | |
72 | + List<OAuth2ClientRegistration> inputClientRegistrations = OAuth2Utils.toClientRegistrations(tenantId, clientsParams); | |
73 | + List<OAuth2ClientRegistration> savedClientRegistrations = inputClientRegistrations.stream() | |
74 | + .map(clientRegistration -> clientRegistrationDao.save(clientRegistration.getTenantId(), clientRegistration)) | |
75 | + .collect(Collectors.toList()); | |
76 | + return OAuth2Utils.toOAuth2ClientsParams(savedClientRegistrations); | |
71 | 77 | } |
72 | 78 | |
73 | 79 | @Override |
74 | - public List<OAuth2ClientRegistration> findClientRegistrationsByTenantId(TenantId tenantId) { | |
75 | - log.trace("Executing findClientRegistrationsByTenantId [{}]", tenantId); | |
80 | + public OAuth2ClientsParams findClientsParamsByTenantId(TenantId tenantId) { | |
81 | + log.trace("Executing findClientsParamsByTenantId [{}]", tenantId); | |
76 | 82 | validateId(tenantId, INCORRECT_TENANT_ID + tenantId); |
77 | - return clientRegistrationDao.findByTenantId(tenantId.getId()); | |
83 | + return OAuth2Utils.toOAuth2ClientsParams(clientRegistrationDao.findByTenantId(tenantId.getId())); | |
78 | 84 | } |
79 | 85 | |
80 | 86 | @Override |
... | ... | @@ -129,92 +135,90 @@ public class OAuth2ServiceImpl extends AbstractEntityService implements OAuth2Se |
129 | 135 | } |
130 | 136 | } |
131 | 137 | |
132 | - private final DataValidator<OAuth2ClientRegistration> clientRegistrationValidator = | |
133 | - new DataValidator<OAuth2ClientRegistration>() { | |
134 | - | |
135 | - @Override | |
136 | - protected void validateCreate(TenantId tenantId, OAuth2ClientRegistration clientRegistration) { | |
138 | + private final BiConsumer<TenantId, OAuth2ClientsParams> clientParamsValidator = (tenantId, clientsParams) -> { | |
139 | + if (clientsParams == null || clientsParams.getOAuth2DomainDtos() == null | |
140 | + || clientsParams.getOAuth2DomainDtos().isEmpty()) { | |
141 | + throw new DataValidationException("Domain params should be specified!"); | |
142 | + } | |
143 | + for (OAuth2ClientsDomainParams domainParams : clientsParams.getOAuth2DomainDtos()) { | |
144 | + if (StringUtils.isEmpty(domainParams.getDomainName())) { | |
145 | + throw new DataValidationException("Domain name should be specified!"); | |
146 | + } | |
147 | + if (StringUtils.isEmpty(domainParams.getRedirectUriTemplate())) { | |
148 | + throw new DataValidationException("Redirect URI template should be specified!"); | |
149 | + } | |
150 | + if (domainParams.getClientRegistrations() == null || domainParams.getClientRegistrations().isEmpty()) { | |
151 | + throw new DataValidationException("Client registrations should be specified!"); | |
152 | + } | |
153 | + for (ClientRegistrationDto clientRegistration : domainParams.getClientRegistrations()) { | |
154 | + if (StringUtils.isEmpty(clientRegistration.getClientId())) { | |
155 | + throw new DataValidationException("Client ID should be specified!"); | |
137 | 156 | } |
138 | - | |
139 | - @Override | |
140 | - protected void validateUpdate(TenantId tenantId, OAuth2ClientRegistration clientRegistration) { | |
157 | + if (StringUtils.isEmpty(clientRegistration.getClientSecret())) { | |
158 | + throw new DataValidationException("Client secret should be specified!"); | |
141 | 159 | } |
142 | - | |
143 | - @Override | |
144 | - protected void validateDataImpl(TenantId tenantId, OAuth2ClientRegistration clientRegistration) { | |
145 | - if (StringUtils.isEmpty(clientRegistration.getDomainName())) { | |
146 | - throw new DataValidationException("Domain name should be specified!"); | |
147 | - } | |
148 | - if (StringUtils.isEmpty(clientRegistration.getRedirectUriTemplate())) { | |
149 | - throw new DataValidationException("Redirect URI template should be specified!"); | |
150 | - } | |
151 | - if (StringUtils.isEmpty(clientRegistration.getClientId())) { | |
152 | - throw new DataValidationException("Client ID should be specified!"); | |
153 | - } | |
154 | - if (StringUtils.isEmpty(clientRegistration.getClientSecret())) { | |
155 | - throw new DataValidationException("Client secret should be specified!"); | |
156 | - } | |
157 | - if (StringUtils.isEmpty(clientRegistration.getAuthorizationUri())) { | |
158 | - throw new DataValidationException("Authorization uri should be specified!"); | |
159 | - } | |
160 | - if (StringUtils.isEmpty(clientRegistration.getAccessTokenUri())) { | |
161 | - throw new DataValidationException("Token uri should be specified!"); | |
162 | - } | |
163 | - if (StringUtils.isEmpty(clientRegistration.getScope())) { | |
164 | - throw new DataValidationException("Scope should be specified!"); | |
165 | - } | |
166 | - if (StringUtils.isEmpty(clientRegistration.getUserInfoUri())) { | |
167 | - throw new DataValidationException("User info uri should be specified!"); | |
168 | - } | |
169 | - if (StringUtils.isEmpty(clientRegistration.getUserNameAttributeName())) { | |
170 | - throw new DataValidationException("User name attribute name should be specified!"); | |
171 | - } | |
172 | - if (StringUtils.isEmpty(clientRegistration.getClientAuthenticationMethod())) { | |
173 | - throw new DataValidationException("Client authentication method should be specified!"); | |
174 | - } | |
175 | - if (StringUtils.isEmpty(clientRegistration.getLoginButtonLabel())) { | |
176 | - throw new DataValidationException("Login button label should be specified!"); | |
160 | + if (StringUtils.isEmpty(clientRegistration.getAuthorizationUri())) { | |
161 | + throw new DataValidationException("Authorization uri should be specified!"); | |
162 | + } | |
163 | + if (StringUtils.isEmpty(clientRegistration.getAccessTokenUri())) { | |
164 | + throw new DataValidationException("Token uri should be specified!"); | |
165 | + } | |
166 | + if (StringUtils.isEmpty(clientRegistration.getScope())) { | |
167 | + throw new DataValidationException("Scope should be specified!"); | |
168 | + } | |
169 | + if (StringUtils.isEmpty(clientRegistration.getUserInfoUri())) { | |
170 | + throw new DataValidationException("User info uri should be specified!"); | |
171 | + } | |
172 | + if (StringUtils.isEmpty(clientRegistration.getUserNameAttributeName())) { | |
173 | + throw new DataValidationException("User name attribute name should be specified!"); | |
174 | + } | |
175 | + if (StringUtils.isEmpty(clientRegistration.getClientAuthenticationMethod())) { | |
176 | + throw new DataValidationException("Client authentication method should be specified!"); | |
177 | + } | |
178 | + if (StringUtils.isEmpty(clientRegistration.getLoginButtonLabel())) { | |
179 | + throw new DataValidationException("Login button label should be specified!"); | |
180 | + } | |
181 | + OAuth2MapperConfig mapperConfig = clientRegistration.getMapperConfig(); | |
182 | + if (mapperConfig == null) { | |
183 | + throw new DataValidationException("Mapper config should be specified!"); | |
184 | + } | |
185 | + if (mapperConfig.getType() == null) { | |
186 | + throw new DataValidationException("Mapper config type should be specified!"); | |
187 | + } | |
188 | + if (mapperConfig.getType() == MapperType.BASIC) { | |
189 | + OAuth2BasicMapperConfig basicConfig = mapperConfig.getBasic(); | |
190 | + if (basicConfig == null) { | |
191 | + throw new DataValidationException("Basic config should be specified!"); | |
177 | 192 | } |
178 | - OAuth2MapperConfig mapperConfig = clientRegistration.getMapperConfig(); | |
179 | - if (mapperConfig == null) { | |
180 | - throw new DataValidationException("Mapper config should be specified!"); | |
193 | + if (StringUtils.isEmpty(basicConfig.getEmailAttributeKey())) { | |
194 | + throw new DataValidationException("Email attribute key should be specified!"); | |
181 | 195 | } |
182 | - if (mapperConfig.getType() == null) { | |
183 | - throw new DataValidationException("Mapper config type should be specified!"); | |
196 | + if (basicConfig.getTenantNameStrategy() == null) { | |
197 | + throw new DataValidationException("Tenant name strategy should be specified!"); | |
184 | 198 | } |
185 | - if (mapperConfig.getType() == MapperType.BASIC) { | |
186 | - OAuth2BasicMapperConfig basicConfig = mapperConfig.getBasic(); | |
187 | - if (basicConfig == null) { | |
188 | - throw new DataValidationException("Basic config should be specified!"); | |
189 | - } | |
190 | - if (StringUtils.isEmpty(basicConfig.getEmailAttributeKey())) { | |
191 | - throw new DataValidationException("Email attribute key should be specified!"); | |
192 | - } | |
193 | - if (basicConfig.getTenantNameStrategy() == null) { | |
194 | - throw new DataValidationException("Tenant name strategy should be specified!"); | |
195 | - } | |
196 | - if (basicConfig.getTenantNameStrategy() == TenantNameStrategyType.CUSTOM | |
197 | - && StringUtils.isEmpty(basicConfig.getTenantNamePattern())) { | |
198 | - throw new DataValidationException("Tenant name pattern should be specified!"); | |
199 | - } | |
199 | + if (basicConfig.getTenantNameStrategy() == TenantNameStrategyType.CUSTOM | |
200 | + && StringUtils.isEmpty(basicConfig.getTenantNamePattern())) { | |
201 | + throw new DataValidationException("Tenant name pattern should be specified!"); | |
200 | 202 | } |
201 | - if (mapperConfig.getType() == MapperType.CUSTOM) { | |
202 | - OAuth2CustomMapperConfig customConfig = mapperConfig.getCustom(); | |
203 | - if (customConfig == null) { | |
204 | - throw new DataValidationException("Custom config should be specified!"); | |
205 | - } | |
206 | - if (StringUtils.isEmpty(customConfig.getUrl())) { | |
207 | - throw new DataValidationException("Custom mapper URL should be specified!"); | |
208 | - } | |
203 | + } | |
204 | + if (mapperConfig.getType() == MapperType.CUSTOM) { | |
205 | + OAuth2CustomMapperConfig customConfig = mapperConfig.getCustom(); | |
206 | + if (customConfig == null) { | |
207 | + throw new DataValidationException("Custom config should be specified!"); | |
209 | 208 | } |
210 | - if (clientRegistration.getTenantId() == null) { | |
211 | - throw new DataValidationException("Client registration should be assigned to tenant!"); | |
212 | - } else if (!TenantId.SYS_TENANT_ID.equals(clientRegistration.getTenantId())) { | |
213 | - Tenant tenant = tenantService.findTenantById(clientRegistration.getTenantId()); | |
214 | - if (tenant == null) { | |
215 | - throw new DataValidationException("Client registration is referencing to non-existent tenant!"); | |
216 | - } | |
209 | + if (StringUtils.isEmpty(customConfig.getUrl())) { | |
210 | + throw new DataValidationException("Custom mapper URL should be specified!"); | |
217 | 211 | } |
218 | 212 | } |
219 | - }; | |
213 | + } | |
214 | + } | |
215 | + if (tenantId == null) { | |
216 | + throw new DataValidationException("Client registration should be assigned to tenant!"); | |
217 | + } else if (!TenantId.SYS_TENANT_ID.equals(tenantId)) { | |
218 | + Tenant tenant = tenantService.findTenantById(tenantId); | |
219 | + if (tenant == null) { | |
220 | + throw new DataValidationException("Client registration is referencing to non-existent tenant!"); | |
221 | + } | |
222 | + } | |
223 | + }; | |
220 | 224 | } | ... | ... |
... | ... | @@ -16,10 +16,14 @@ |
16 | 16 | package org.thingsboard.server.dao.oauth2; |
17 | 17 | |
18 | 18 | import org.springframework.util.StringUtils; |
19 | -import org.thingsboard.server.common.data.oauth2.OAuth2ClientInfo; | |
20 | -import org.thingsboard.server.common.data.oauth2.OAuth2ClientRegistration; | |
21 | -import org.thingsboard.server.common.data.oauth2.OAuth2ClientsParams; | |
19 | +import org.thingsboard.server.common.data.id.TenantId; | |
20 | +import org.thingsboard.server.common.data.oauth2.*; | |
22 | 21 | |
22 | +import java.util.ArrayList; | |
23 | +import java.util.HashMap; | |
24 | +import java.util.List; | |
25 | +import java.util.Map; | |
26 | +import java.util.stream.Collectors; | |
23 | 27 | import java.util.stream.Stream; |
24 | 28 | |
25 | 29 | public class OAuth2Utils { |
... | ... | @@ -33,4 +37,68 @@ public class OAuth2Utils { |
33 | 37 | client.setIcon(clientRegistration.getLoginButtonIcon()); |
34 | 38 | return client; |
35 | 39 | } |
40 | + | |
41 | + public static List<OAuth2ClientRegistration> toClientRegistrations(TenantId tenantId, OAuth2ClientsParams clientsParams) { | |
42 | + return clientsParams.getOAuth2DomainDtos().stream() | |
43 | + .flatMap(domainParams -> domainParams.getClientRegistrations().stream() | |
44 | + .map(clientRegistrationDto -> OAuth2Utils.toClientRegistration(tenantId, domainParams.getDomainName(), | |
45 | + domainParams.getRedirectUriTemplate(), clientRegistrationDto) | |
46 | + )) | |
47 | + .collect(Collectors.toList()); | |
48 | + } | |
49 | + | |
50 | + public static OAuth2ClientsParams toOAuth2ClientsParams(List<OAuth2ClientRegistration> clientRegistrations) { | |
51 | + Map<String, OAuth2ClientsDomainParams> domainParamsMap = new HashMap<>(); | |
52 | + for (OAuth2ClientRegistration clientRegistration : clientRegistrations) { | |
53 | + String domainName = clientRegistration.getDomainName(); | |
54 | + OAuth2ClientsDomainParams domainParams = domainParamsMap.computeIfAbsent(domainName, | |
55 | + key -> new OAuth2ClientsDomainParams(domainName, clientRegistration.getRedirectUriTemplate(), new ArrayList<>()) | |
56 | + ); | |
57 | + domainParams.getClientRegistrations() | |
58 | + .add(toClientRegistrationDto(clientRegistration)); | |
59 | + } | |
60 | + return new OAuth2ClientsParams(new ArrayList<>(domainParamsMap.values())); | |
61 | + } | |
62 | + | |
63 | + public static ClientRegistrationDto toClientRegistrationDto(OAuth2ClientRegistration oAuth2ClientRegistration) { | |
64 | + return ClientRegistrationDto.builder() | |
65 | + .id(oAuth2ClientRegistration.getId()) | |
66 | + .createdTime(oAuth2ClientRegistration.getCreatedTime()) | |
67 | + .mapperConfig(oAuth2ClientRegistration.getMapperConfig()) | |
68 | + .clientId(oAuth2ClientRegistration.getClientId()) | |
69 | + .clientSecret(oAuth2ClientRegistration.getClientSecret()) | |
70 | + .authorizationUri(oAuth2ClientRegistration.getAuthorizationUri()) | |
71 | + .accessTokenUri(oAuth2ClientRegistration.getAccessTokenUri()) | |
72 | + .scope(oAuth2ClientRegistration.getScope()) | |
73 | + .userInfoUri(oAuth2ClientRegistration.getUserInfoUri()) | |
74 | + .userNameAttributeName(oAuth2ClientRegistration.getUserNameAttributeName()) | |
75 | + .jwkSetUri(oAuth2ClientRegistration.getJwkSetUri()) | |
76 | + .clientAuthenticationMethod(oAuth2ClientRegistration.getClientAuthenticationMethod()) | |
77 | + .loginButtonLabel(oAuth2ClientRegistration.getLoginButtonLabel()) | |
78 | + .loginButtonIcon(oAuth2ClientRegistration.getLoginButtonIcon()) | |
79 | + .build(); | |
80 | + } | |
81 | + | |
82 | + public static OAuth2ClientRegistration toClientRegistration(TenantId tenantId, String domainName, String redirectUriTemplate, | |
83 | + ClientRegistrationDto clientRegistrationDto) { | |
84 | + OAuth2ClientRegistration clientRegistration = new OAuth2ClientRegistration(); | |
85 | + clientRegistration.setId(clientRegistrationDto.getId()); | |
86 | + clientRegistration.setTenantId(tenantId); | |
87 | + clientRegistration.setCreatedTime(clientRegistrationDto.getCreatedTime()); | |
88 | + clientRegistration.setDomainName(domainName); | |
89 | + clientRegistration.setRedirectUriTemplate(redirectUriTemplate); | |
90 | + clientRegistration.setMapperConfig(clientRegistrationDto.getMapperConfig()); | |
91 | + clientRegistration.setClientId(clientRegistrationDto.getClientId()); | |
92 | + clientRegistration.setClientSecret(clientRegistrationDto.getClientSecret()); | |
93 | + clientRegistration.setAuthorizationUri(clientRegistrationDto.getAuthorizationUri()); | |
94 | + clientRegistration.setAccessTokenUri(clientRegistrationDto.getAccessTokenUri()); | |
95 | + clientRegistration.setScope(clientRegistrationDto.getScope()); | |
96 | + clientRegistration.setUserInfoUri(clientRegistrationDto.getUserInfoUri()); | |
97 | + clientRegistration.setUserNameAttributeName(clientRegistrationDto.getUserNameAttributeName()); | |
98 | + clientRegistration.setJwkSetUri(clientRegistrationDto.getJwkSetUri()); | |
99 | + clientRegistration.setClientAuthenticationMethod(clientRegistrationDto.getClientAuthenticationMethod()); | |
100 | + clientRegistration.setLoginButtonLabel(clientRegistrationDto.getLoginButtonLabel()); | |
101 | + clientRegistration.setLoginButtonIcon(clientRegistrationDto.getLoginButtonIcon()); | |
102 | + return clientRegistration; | |
103 | + } | |
36 | 104 | } | ... | ... |
... | ... | @@ -38,6 +38,7 @@ import java.util.stream.Collectors; |
38 | 38 | import java.util.stream.Stream; |
39 | 39 | |
40 | 40 | import static org.thingsboard.server.dao.oauth2.OAuth2Utils.ALLOW_OAUTH2_CONFIGURATION; |
41 | +import static org.thingsboard.server.dao.oauth2.OAuth2Utils.toClientRegistrations; | |
41 | 42 | |
42 | 43 | public class BaseOAuth2ServiceTest extends AbstractServiceTest { |
43 | 44 | |
... | ... | @@ -90,9 +91,13 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { |
90 | 91 | @Test |
91 | 92 | public void testCreateNewSystemParams() { |
92 | 93 | OAuth2ClientRegistration clientRegistration = validClientRegistration(TenantId.SYS_TENANT_ID); |
93 | - OAuth2ClientRegistration savedClientRegistration = oAuth2Service.saveClientRegistration(clientRegistration); | |
94 | + OAuth2ClientsParams savedClientsParams = oAuth2Service.saveClientsParams(TenantId.SYS_TENANT_ID, OAuth2Utils.toOAuth2ClientsParams(Collections.singletonList(clientRegistration))); | |
95 | + Assert.assertNotNull(savedClientsParams); | |
94 | 96 | |
95 | - Assert.assertNotNull(savedClientRegistration); | |
97 | + List<OAuth2ClientRegistration> savedClientRegistrations = OAuth2Utils.toClientRegistrations(TenantId.SYS_TENANT_ID, savedClientsParams); | |
98 | + Assert.assertEquals(1, savedClientRegistrations.size()); | |
99 | + | |
100 | + OAuth2ClientRegistration savedClientRegistration = savedClientRegistrations.get(0); | |
96 | 101 | Assert.assertNotNull(savedClientRegistration.getId()); |
97 | 102 | clientRegistration.setId(savedClientRegistration.getId()); |
98 | 103 | clientRegistration.setCreatedTime(savedClientRegistration.getCreatedTime()); |
... | ... | @@ -102,12 +107,14 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { |
102 | 107 | @Test |
103 | 108 | public void testFindSystemParamsByTenant() { |
104 | 109 | OAuth2ClientRegistration clientRegistration = validClientRegistration(TenantId.SYS_TENANT_ID); |
105 | - oAuth2Service.saveClientRegistration(clientRegistration); | |
110 | + oAuth2Service.saveClientsParams(TenantId.SYS_TENANT_ID, OAuth2Utils.toOAuth2ClientsParams(Collections.singletonList(clientRegistration))); | |
106 | 111 | |
107 | - List<OAuth2ClientRegistration> clientRegistrationsByTenantId = oAuth2Service.findClientRegistrationsByTenantId(TenantId.SYS_TENANT_ID); | |
108 | - Assert.assertEquals(1, clientRegistrationsByTenantId.size()); | |
112 | + OAuth2ClientsParams foundClientsParams = oAuth2Service.findClientsParamsByTenantId(TenantId.SYS_TENANT_ID); | |
113 | + Assert.assertEquals(1, foundClientsParams.getOAuth2DomainDtos().size()); | |
109 | 114 | Assert.assertEquals(1, oAuth2Service.findAllClientRegistrations().size()); |
110 | - OAuth2ClientRegistration foundClientRegistration = clientRegistrationsByTenantId.get(0); | |
115 | + | |
116 | + List<OAuth2ClientRegistration> foundClientRegistrations = OAuth2Utils.toClientRegistrations(TenantId.SYS_TENANT_ID, foundClientsParams); | |
117 | + OAuth2ClientRegistration foundClientRegistration = foundClientRegistrations.get(0); | |
111 | 118 | Assert.assertNotNull(foundClientRegistration); |
112 | 119 | clientRegistration.setId(foundClientRegistration.getId()); |
113 | 120 | clientRegistration.setCreatedTime(foundClientRegistration.getCreatedTime()); |
... | ... | @@ -117,7 +124,13 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { |
117 | 124 | @Test |
118 | 125 | public void testCreateNewTenantParams() { |
119 | 126 | OAuth2ClientRegistration clientRegistration = validClientRegistration(tenantId); |
120 | - OAuth2ClientRegistration savedClientRegistration = oAuth2Service.saveClientRegistration(clientRegistration); | |
127 | + OAuth2ClientsParams savedClientsParams = oAuth2Service.saveClientsParams(tenantId, OAuth2Utils.toOAuth2ClientsParams(Collections.singletonList(clientRegistration))); | |
128 | + Assert.assertNotNull(savedClientsParams); | |
129 | + | |
130 | + List<OAuth2ClientRegistration> savedClientRegistrations = OAuth2Utils.toClientRegistrations(tenantId, savedClientsParams); | |
131 | + Assert.assertEquals(1, savedClientRegistrations.size()); | |
132 | + | |
133 | + OAuth2ClientRegistration savedClientRegistration = savedClientRegistrations.get(0); | |
121 | 134 | |
122 | 135 | Assert.assertNotNull(savedClientRegistration); |
123 | 136 | Assert.assertNotNull(savedClientRegistration.getId()); |
... | ... | @@ -129,12 +142,15 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { |
129 | 142 | @Test |
130 | 143 | public void testFindTenantParams() { |
131 | 144 | OAuth2ClientRegistration clientRegistration = validClientRegistration(tenantId); |
132 | - oAuth2Service.saveClientRegistration(clientRegistration); | |
145 | + oAuth2Service.saveClientsParams(tenantId, OAuth2Utils.toOAuth2ClientsParams(Collections.singletonList(clientRegistration))); | |
133 | 146 | |
134 | - List<OAuth2ClientRegistration> clientRegistrationsByTenantId = oAuth2Service.findClientRegistrationsByTenantId(tenantId); | |
135 | - Assert.assertEquals(1, clientRegistrationsByTenantId.size()); | |
147 | + OAuth2ClientsParams foundClientsParams = oAuth2Service.findClientsParamsByTenantId(tenantId); | |
148 | + Assert.assertEquals(1, foundClientsParams.getOAuth2DomainDtos().size()); | |
136 | 149 | Assert.assertEquals(1, oAuth2Service.findAllClientRegistrations().size()); |
137 | - OAuth2ClientRegistration foundClientRegistration = clientRegistrationsByTenantId.get(0); | |
150 | + | |
151 | + List<OAuth2ClientRegistration> foundClientRegistrations = OAuth2Utils.toClientRegistrations(tenantId, foundClientsParams); | |
152 | + OAuth2ClientRegistration foundClientRegistration = foundClientRegistrations.get(0); | |
153 | + | |
138 | 154 | Assert.assertNotNull(foundClientRegistration); |
139 | 155 | clientRegistration.setId(foundClientRegistration.getId()); |
140 | 156 | clientRegistration.setCreatedTime(foundClientRegistration.getCreatedTime()); |
... | ... | @@ -146,18 +162,20 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { |
146 | 162 | OAuth2ClientRegistration tenantClientRegistration = validClientRegistration(tenantId); |
147 | 163 | OAuth2ClientRegistration sysAdminClientRegistration = validClientRegistration(TenantId.SYS_TENANT_ID); |
148 | 164 | |
149 | - OAuth2ClientRegistration savedTenantClientRegistration = oAuth2Service.saveClientRegistration(tenantClientRegistration); | |
150 | - OAuth2ClientRegistration savedSysAdminClientRegistration = oAuth2Service.saveClientRegistration(sysAdminClientRegistration); | |
165 | + OAuth2ClientsParams savedTenantClientsParams = oAuth2Service.saveClientsParams(tenantId, | |
166 | + OAuth2Utils.toOAuth2ClientsParams(Collections.singletonList(tenantClientRegistration))); | |
167 | + OAuth2ClientsParams savedSysAdminClientsParams = oAuth2Service.saveClientsParams(TenantId.SYS_TENANT_ID, | |
168 | + OAuth2Utils.toOAuth2ClientsParams(Collections.singletonList(sysAdminClientRegistration))); | |
151 | 169 | |
152 | 170 | Assert.assertEquals(2, oAuth2Service.findAllClientRegistrations().size()); |
153 | 171 | |
154 | - Assert.assertEquals(savedTenantClientRegistration, oAuth2Service.findClientRegistrationsByTenantId(tenantId).get(0)); | |
155 | - Assert.assertEquals(savedSysAdminClientRegistration, oAuth2Service.findClientRegistrationsByTenantId(TenantId.SYS_TENANT_ID).get(0)); | |
172 | + Assert.assertEquals(savedTenantClientsParams, oAuth2Service.findClientsParamsByTenantId(tenantId)); | |
173 | + Assert.assertEquals(savedSysAdminClientsParams, oAuth2Service.findClientsParamsByTenantId(TenantId.SYS_TENANT_ID)); | |
156 | 174 | |
157 | - Assert.assertEquals(savedTenantClientRegistration, | |
158 | - oAuth2Service.findClientRegistration(savedTenantClientRegistration.getUuidId())); | |
159 | - Assert.assertEquals(savedSysAdminClientRegistration, | |
160 | - oAuth2Service.findClientRegistration(savedSysAdminClientRegistration.getUuidId())); | |
175 | + OAuth2ClientRegistration savedTenantClientRegistration = toClientRegistrations(tenantId, savedTenantClientsParams).get(0); | |
176 | + Assert.assertEquals(savedTenantClientRegistration, oAuth2Service.findClientRegistration(savedTenantClientRegistration.getUuidId())); | |
177 | + OAuth2ClientRegistration savedSysAdminClientRegistration = toClientRegistrations(TenantId.SYS_TENANT_ID, savedSysAdminClientsParams).get(0); | |
178 | + Assert.assertEquals(savedSysAdminClientRegistration, oAuth2Service.findClientRegistration(savedSysAdminClientRegistration.getUuidId())); | |
161 | 179 | } |
162 | 180 | |
163 | 181 | @Test |
... | ... | @@ -166,8 +184,8 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { |
166 | 184 | OAuth2ClientRegistration tenantClientRegistration = validClientRegistration(tenantId, testDomainName); |
167 | 185 | OAuth2ClientRegistration sysAdminClientRegistration = validClientRegistration(TenantId.SYS_TENANT_ID, testDomainName); |
168 | 186 | |
169 | - oAuth2Service.saveClientRegistration(tenantClientRegistration); | |
170 | - oAuth2Service.saveClientRegistration(sysAdminClientRegistration); | |
187 | + oAuth2Service.saveClientsParams(tenantId, OAuth2Utils.toOAuth2ClientsParams(Collections.singletonList(tenantClientRegistration))); | |
188 | + oAuth2Service.saveClientsParams(TenantId.SYS_TENANT_ID, OAuth2Utils.toOAuth2ClientsParams(Collections.singletonList(sysAdminClientRegistration))); | |
171 | 189 | |
172 | 190 | List<OAuth2ClientInfo> oAuth2Clients = oAuth2Service.getOAuth2Clients(testDomainName); |
173 | 191 | |
... | ... | @@ -183,8 +201,8 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { |
183 | 201 | String testDomainName = "test_domain"; |
184 | 202 | OAuth2ClientRegistration tenantClientRegistration = validClientRegistration(tenantId, testDomainName); |
185 | 203 | OAuth2ClientRegistration sysAdminClientRegistration = validClientRegistration(TenantId.SYS_TENANT_ID, testDomainName); |
186 | - oAuth2Service.saveClientRegistration(tenantClientRegistration); | |
187 | - oAuth2Service.saveClientRegistration(sysAdminClientRegistration); | |
204 | + oAuth2Service.saveClientsParams(tenantId, OAuth2Utils.toOAuth2ClientsParams(Collections.singletonList(tenantClientRegistration))); | |
205 | + oAuth2Service.saveClientsParams(TenantId.SYS_TENANT_ID, OAuth2Utils.toOAuth2ClientsParams(Collections.singletonList(sysAdminClientRegistration))); | |
188 | 206 | List<OAuth2ClientInfo> oAuth2Clients = oAuth2Service.getOAuth2Clients("random-domain"); |
189 | 207 | Assert.assertTrue(oAuth2Clients.isEmpty()); |
190 | 208 | } |
... | ... | @@ -193,8 +211,14 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { |
193 | 211 | public void testDeleteOAuth2ClientRegistration() { |
194 | 212 | OAuth2ClientRegistration tenantClientRegistration = validClientRegistration(tenantId); |
195 | 213 | OAuth2ClientRegistration sysAdminClientRegistration = validClientRegistration(TenantId.SYS_TENANT_ID); |
196 | - OAuth2ClientRegistration savedTenantRegistration = oAuth2Service.saveClientRegistration(tenantClientRegistration); | |
197 | - OAuth2ClientRegistration savedSysAdminRegistration = oAuth2Service.saveClientRegistration(sysAdminClientRegistration); | |
214 | + | |
215 | + OAuth2ClientsParams savedTenantClientsParams = oAuth2Service.saveClientsParams(tenantId, | |
216 | + OAuth2Utils.toOAuth2ClientsParams(Collections.singletonList(tenantClientRegistration))); | |
217 | + OAuth2ClientsParams savedSysAdminClientsParams = oAuth2Service.saveClientsParams(TenantId.SYS_TENANT_ID, | |
218 | + OAuth2Utils.toOAuth2ClientsParams(Collections.singletonList(sysAdminClientRegistration))); | |
219 | + | |
220 | + OAuth2ClientRegistration savedTenantRegistration = toClientRegistrations(tenantId, savedTenantClientsParams).get(0); | |
221 | + OAuth2ClientRegistration savedSysAdminRegistration = toClientRegistrations(TenantId.SYS_TENANT_ID, savedSysAdminClientsParams).get(0); | |
198 | 222 | |
199 | 223 | oAuth2Service.deleteClientRegistrationById(tenantId, savedTenantRegistration.getId()); |
200 | 224 | List<OAuth2ClientRegistration> foundRegistrations = oAuth2Service.findAllClientRegistrations(); |
... | ... | @@ -204,29 +228,39 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { |
204 | 228 | |
205 | 229 | @Test |
206 | 230 | public void testDeleteTenantOAuth2ClientRegistrations() { |
207 | - oAuth2Service.saveClientRegistration(validClientRegistration(tenantId)); | |
208 | - oAuth2Service.saveClientRegistration(validClientRegistration(tenantId)); | |
209 | - oAuth2Service.saveClientRegistration(validClientRegistration(tenantId)); | |
231 | + oAuth2Service.saveClientsParams(tenantId, OAuth2Utils.toOAuth2ClientsParams(Arrays.asList( | |
232 | + validClientRegistration(tenantId, "domain"), | |
233 | + validClientRegistration(tenantId, "domain"), | |
234 | + validClientRegistration(tenantId, "domain") | |
235 | + ))); | |
210 | 236 | Assert.assertEquals(3, oAuth2Service.findAllClientRegistrations().size()); |
211 | - Assert.assertEquals(3, oAuth2Service.findClientRegistrationsByTenantId(tenantId).size()); | |
237 | + Assert.assertEquals(1, oAuth2Service.findClientsParamsByTenantId(tenantId).getOAuth2DomainDtos().size()); | |
212 | 238 | |
213 | 239 | oAuth2Service.deleteClientRegistrationsByTenantId(tenantId); |
214 | 240 | Assert.assertEquals(0, oAuth2Service.findAllClientRegistrations().size()); |
215 | - Assert.assertEquals(0, oAuth2Service.findClientRegistrationsByTenantId(tenantId).size()); | |
241 | + Assert.assertEquals(0, oAuth2Service.findClientsParamsByTenantId(tenantId).getOAuth2DomainDtos().size()); | |
216 | 242 | } |
217 | 243 | |
218 | 244 | @Test |
219 | 245 | public void testDeleteTenantDomainOAuth2ClientRegistrations() { |
220 | - oAuth2Service.saveClientRegistration(validClientRegistration(tenantId, "domain1")); | |
221 | - oAuth2Service.saveClientRegistration(validClientRegistration(tenantId, "domain1")); | |
222 | - oAuth2Service.saveClientRegistration(validClientRegistration(tenantId, "domain2")); | |
223 | - oAuth2Service.saveClientRegistration(validClientRegistration(TenantId.SYS_TENANT_ID, "domain2")); | |
246 | + oAuth2Service.saveClientsParams(tenantId, OAuth2Utils.toOAuth2ClientsParams(Arrays.asList( | |
247 | + validClientRegistration(tenantId, "domain1"), | |
248 | + validClientRegistration(tenantId, "domain1"), | |
249 | + validClientRegistration(tenantId, "domain2") | |
250 | + ))); | |
251 | + oAuth2Service.saveClientsParams(TenantId.SYS_TENANT_ID, OAuth2Utils.toOAuth2ClientsParams(Arrays.asList( | |
252 | + validClientRegistration(TenantId.SYS_TENANT_ID, "domain2") | |
253 | + ))); | |
224 | 254 | Assert.assertEquals(4, oAuth2Service.findAllClientRegistrations().size()); |
225 | - Assert.assertEquals(3, oAuth2Service.findClientRegistrationsByTenantId(tenantId).size()); | |
255 | + OAuth2ClientsParams tenantClientsParams = oAuth2Service.findClientsParamsByTenantId(tenantId); | |
256 | + List<OAuth2ClientRegistration> tenantClientRegistrations = toClientRegistrations(tenantId, tenantClientsParams); | |
257 | + Assert.assertEquals(2, tenantClientsParams.getOAuth2DomainDtos().size()); | |
258 | + Assert.assertEquals(3, tenantClientRegistrations.size()); | |
226 | 259 | |
227 | 260 | oAuth2Service.deleteClientRegistrationsByDomain(tenantId, "domain1"); |
228 | 261 | Assert.assertEquals(2, oAuth2Service.findAllClientRegistrations().size()); |
229 | - Assert.assertEquals(1, oAuth2Service.findClientRegistrationsByTenantId(tenantId).size()); | |
262 | + Assert.assertEquals(1, oAuth2Service.findClientsParamsByTenantId(tenantId).getOAuth2DomainDtos().size()); | |
263 | + Assert.assertEquals(1, toClientRegistrations(tenantId, oAuth2Service.findClientsParamsByTenantId(tenantId)).size()); | |
230 | 264 | } |
231 | 265 | |
232 | 266 | private void updateTenantAllowOAuth2Setting(Boolean allowOAuth2) throws IOException { | ... | ... |