Commit 1b53971eb5426c65d02b00ef2a3597e47809d445
Merge branch 'feature/dynamic-oauth2-new-table' of https://github.com/vzikratyi-…
…tb/thingsboard into feature/dynamic-oauth2-new-table
Showing
28 changed files
with
118 additions
and
498 deletions
... | ... | @@ -20,7 +20,6 @@ CREATE TABLE IF NOT EXISTS oauth2_client_registration ( |
20 | 20 | id uuid NOT NULL CONSTRAINT oauth2_client_registration_pkey PRIMARY KEY, |
21 | 21 | created_time bigint NOT NULL, |
22 | 22 | additional_info varchar, |
23 | - tenant_id uuid, | |
24 | 23 | domain_name varchar(255), |
25 | 24 | client_id varchar(255), |
26 | 25 | client_secret varchar(255), |
... | ... | @@ -57,7 +56,6 @@ CREATE TABLE IF NOT EXISTS oauth2_client_registration_template ( |
57 | 56 | id uuid NOT NULL CONSTRAINT oauth2_client_registration_template_pkey PRIMARY KEY, |
58 | 57 | created_time bigint NOT NULL, |
59 | 58 | additional_info varchar, |
60 | - tenant_id uuid, | |
61 | 59 | provider_id varchar(255), |
62 | 60 | authorization_uri varchar(255), |
63 | 61 | token_uri varchar(255), | ... | ... |
... | ... | @@ -27,19 +27,7 @@ import org.springframework.beans.factory.annotation.Value; |
27 | 27 | import org.springframework.security.core.Authentication; |
28 | 28 | import org.springframework.security.core.context.SecurityContextHolder; |
29 | 29 | import org.springframework.web.bind.annotation.ExceptionHandler; |
30 | -import org.thingsboard.server.common.data.Customer; | |
31 | -import org.thingsboard.server.common.data.Dashboard; | |
32 | -import org.thingsboard.server.common.data.DashboardInfo; | |
33 | -import org.thingsboard.server.common.data.DataConstants; | |
34 | -import org.thingsboard.server.common.data.Device; | |
35 | -import org.thingsboard.server.common.data.DeviceInfo; | |
36 | -import org.thingsboard.server.common.data.EntityType; | |
37 | -import org.thingsboard.server.common.data.EntityView; | |
38 | -import org.thingsboard.server.common.data.EntityViewInfo; | |
39 | -import org.thingsboard.server.common.data.HasName; | |
40 | -import org.thingsboard.server.common.data.HasTenantId; | |
41 | -import org.thingsboard.server.common.data.Tenant; | |
42 | -import org.thingsboard.server.common.data.User; | |
30 | +import org.thingsboard.server.common.data.*; | |
43 | 31 | import org.thingsboard.server.common.data.alarm.Alarm; |
44 | 32 | import org.thingsboard.server.common.data.alarm.AlarmInfo; |
45 | 33 | import org.thingsboard.server.common.data.asset.Asset; |
... | ... | @@ -50,8 +38,6 @@ import org.thingsboard.server.common.data.exception.ThingsboardException; |
50 | 38 | import org.thingsboard.server.common.data.id.*; |
51 | 39 | import org.thingsboard.server.common.data.kv.AttributeKvEntry; |
52 | 40 | import org.thingsboard.server.common.data.kv.DataType; |
53 | -import org.thingsboard.server.common.data.oauth2.OAuth2ClientRegistration; | |
54 | -import org.thingsboard.server.common.data.oauth2.OAuth2ClientRegistrationTemplate; | |
55 | 41 | import org.thingsboard.server.common.data.page.PageLink; |
56 | 42 | import org.thingsboard.server.common.data.page.SortOrder; |
57 | 43 | import org.thingsboard.server.common.data.page.TimePageLink; |
... | ... | @@ -388,10 +374,7 @@ public abstract class BaseController { |
388 | 374 | checkWidgetTypeId(new WidgetTypeId(entityId.getId()), operation); |
389 | 375 | return; |
390 | 376 | case OAUTH2_CLIENT_REGISTRATION: |
391 | - checkOAuth2ClientRegistrationId(new OAuth2ClientRegistrationId(entityId.getId()), operation); | |
392 | - return; | |
393 | 377 | case OAUTH2_CLIENT_REGISTRATION_TEMPLATE: |
394 | - checkOAuth2ClientRegistrationTemplateId(new OAuth2ClientRegistrationTemplateId(entityId.getId()), operation); | |
395 | 378 | return; |
396 | 379 | default: |
397 | 380 | throw new IllegalArgumentException("Unsupported entity type: " + entityId.getEntityType()); |
... | ... | @@ -545,30 +528,6 @@ public abstract class BaseController { |
545 | 528 | } |
546 | 529 | } |
547 | 530 | |
548 | - OAuth2ClientRegistration checkOAuth2ClientRegistrationId(OAuth2ClientRegistrationId clientRegistrationId, Operation operation) throws ThingsboardException { | |
549 | - try { | |
550 | - validateId(clientRegistrationId, "Incorrect oAuth2ClientRegistrationId " + clientRegistrationId); | |
551 | - OAuth2ClientRegistration clientRegistration = oAuth2Service.findClientRegistration(clientRegistrationId.getId()); | |
552 | - checkNotNull(clientRegistration); | |
553 | - accessControlService.checkPermission(getCurrentUser(), Resource.OAUTH2_CONFIGURATION, operation, clientRegistrationId, clientRegistration); | |
554 | - return clientRegistration; | |
555 | - } catch (Exception e) { | |
556 | - throw handleException(e, false); | |
557 | - } | |
558 | - } | |
559 | - | |
560 | - OAuth2ClientRegistrationTemplate checkOAuth2ClientRegistrationTemplateId(OAuth2ClientRegistrationTemplateId clientRegistrationTemplateId, Operation operation) throws ThingsboardException { | |
561 | - try { | |
562 | - validateId(clientRegistrationTemplateId, "Incorrect oAuth2ClientRegistrationTemplateId " + clientRegistrationTemplateId); | |
563 | - OAuth2ClientRegistrationTemplate clientRegistrationTemplate = oAuth2ConfigTemplateService.findClientRegistrationTemplateById(clientRegistrationTemplateId); | |
564 | - checkNotNull(clientRegistrationTemplate); | |
565 | - accessControlService.checkPermission(getCurrentUser(), Resource.OAUTH2_CONFIGURATION_TEMPLATE, operation, clientRegistrationTemplateId, clientRegistrationTemplate); | |
566 | - return clientRegistrationTemplate; | |
567 | - } catch (Exception e) { | |
568 | - throw handleException(e, false); | |
569 | - } | |
570 | - } | |
571 | - | |
572 | 531 | ComponentDescriptor checkComponentDescriptorByClazz(String clazz) throws ThingsboardException { |
573 | 532 | try { |
574 | 533 | log.debug("[{}] Lookup component descriptor", clazz); | ... | ... |
... | ... | @@ -25,8 +25,6 @@ import org.thingsboard.server.common.data.exception.ThingsboardException; |
25 | 25 | import org.thingsboard.server.common.data.id.OAuth2ClientRegistrationTemplateId; |
26 | 26 | import org.thingsboard.server.common.data.oauth2.OAuth2ClientRegistrationTemplate; |
27 | 27 | import org.thingsboard.server.queue.util.TbCoreComponent; |
28 | -import org.thingsboard.server.service.security.permission.Operation; | |
29 | -import org.thingsboard.server.service.security.permission.Resource; | |
30 | 28 | |
31 | 29 | import java.util.List; |
32 | 30 | |
... | ... | @@ -42,8 +40,6 @@ public class OAuth2ConfigTemplateController extends BaseController { |
42 | 40 | @ResponseStatus(value = HttpStatus.OK) |
43 | 41 | public OAuth2ClientRegistrationTemplate saveClientRegistrationTemplate(@RequestBody OAuth2ClientRegistrationTemplate clientRegistrationTemplate) throws ThingsboardException { |
44 | 42 | try { |
45 | - clientRegistrationTemplate.setTenantId(getCurrentUser().getTenantId()); | |
46 | - checkEntity(clientRegistrationTemplate.getId(), clientRegistrationTemplate, Resource.OAUTH2_CONFIGURATION_TEMPLATE); | |
47 | 43 | return oAuth2ConfigTemplateService.saveClientRegistrationTemplate(clientRegistrationTemplate); |
48 | 44 | } catch (Exception e) { |
49 | 45 | throw handleException(e); |
... | ... | @@ -57,10 +53,10 @@ public class OAuth2ConfigTemplateController extends BaseController { |
57 | 53 | checkParameter(CLIENT_REGISTRATION_TEMPLATE_ID, strClientRegistrationTemplateId); |
58 | 54 | try { |
59 | 55 | OAuth2ClientRegistrationTemplateId clientRegistrationTemplateId = new OAuth2ClientRegistrationTemplateId(toUUID(strClientRegistrationTemplateId)); |
60 | - OAuth2ClientRegistrationTemplate clientRegistrationTemplate = checkOAuth2ClientRegistrationTemplateId(clientRegistrationTemplateId, Operation.DELETE); | |
61 | 56 | oAuth2ConfigTemplateService.deleteClientRegistrationTemplateById(clientRegistrationTemplateId); |
62 | 57 | |
63 | - logEntityAction(clientRegistrationTemplateId, clientRegistrationTemplate, | |
58 | + logEntityAction(clientRegistrationTemplateId, | |
59 | + null, | |
64 | 60 | null, |
65 | 61 | ActionType.DELETED, null, strClientRegistrationTemplateId); |
66 | 62 | |
... | ... | @@ -80,14 +76,9 @@ public class OAuth2ConfigTemplateController extends BaseController { |
80 | 76 | @ResponseBody |
81 | 77 | public List<OAuth2ClientRegistrationTemplate> getClientRegistrationTemplates() throws ThingsboardException { |
82 | 78 | try { |
83 | - checkOAuth2ConfigTemplatePermissions(Operation.READ); | |
84 | 79 | return oAuth2ConfigTemplateService.findAllClientRegistrationTemplates(); |
85 | 80 | } catch (Exception e) { |
86 | 81 | throw handleException(e); |
87 | 82 | } |
88 | 83 | } |
89 | - | |
90 | - private void checkOAuth2ConfigTemplatePermissions(Operation operation) throws ThingsboardException { | |
91 | - accessControlService.checkPermission(getCurrentUser(), Resource.OAUTH2_CONFIGURATION_TEMPLATE, operation); | |
92 | - } | |
93 | 84 | } | ... | ... |
... | ... | @@ -23,16 +23,12 @@ import org.thingsboard.server.common.data.EntityType; |
23 | 23 | import org.thingsboard.server.common.data.audit.ActionType; |
24 | 24 | import org.thingsboard.server.common.data.exception.ThingsboardException; |
25 | 25 | import org.thingsboard.server.common.data.id.OAuth2ClientRegistrationId; |
26 | -import org.thingsboard.server.common.data.id.TenantId; | |
27 | -import org.thingsboard.server.common.data.oauth2.*; | |
28 | -import org.thingsboard.server.common.data.security.Authority; | |
26 | +import org.thingsboard.server.common.data.oauth2.OAuth2ClientInfo; | |
27 | +import org.thingsboard.server.common.data.oauth2.OAuth2ClientsDomainParams; | |
29 | 28 | import org.thingsboard.server.queue.util.TbCoreComponent; |
30 | -import org.thingsboard.server.service.security.permission.Operation; | |
31 | -import org.thingsboard.server.service.security.permission.Resource; | |
32 | 29 | |
33 | 30 | import javax.servlet.http.HttpServletRequest; |
34 | 31 | import java.util.List; |
35 | -import java.util.stream.Collectors; | |
36 | 32 | |
37 | 33 | @RestController |
38 | 34 | @TbCoreComponent |
... | ... | @@ -52,62 +48,39 @@ public class OAuth2Controller extends BaseController { |
52 | 48 | } |
53 | 49 | } |
54 | 50 | |
55 | - @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')") | |
51 | + @PreAuthorize("hasAnyAuthority('SYS_ADMIN')") | |
56 | 52 | @RequestMapping(value = "/oauth2/config", method = RequestMethod.GET, produces = "application/json") |
57 | 53 | @ResponseBody |
58 | 54 | public List<OAuth2ClientsDomainParams> getCurrentClientsParams() throws ThingsboardException { |
59 | 55 | try { |
60 | - Authority authority = getCurrentUser().getAuthority(); | |
61 | - checkOAuth2ConfigPermissions(Operation.READ); | |
62 | - if (Authority.SYS_ADMIN.equals(authority)) { | |
63 | - return oAuth2Service.findDomainsParamsByTenantId(TenantId.SYS_TENANT_ID); | |
64 | - } else if (Authority.TENANT_ADMIN.equals(authority)) { | |
65 | - return oAuth2Service.findDomainsParamsByTenantId(getCurrentUser().getTenantId()); | |
66 | - } else { | |
67 | - throw new IllegalStateException("Authority " + authority + " cannot get client registrations."); | |
68 | - } | |
56 | + return oAuth2Service.findDomainsParams(); | |
69 | 57 | } catch (Exception e) { |
70 | 58 | throw handleException(e); |
71 | 59 | } |
72 | 60 | } |
73 | 61 | |
74 | - @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')") | |
62 | + @PreAuthorize("hasAnyAuthority('SYS_ADMIN')") | |
75 | 63 | @RequestMapping(value = "/oauth2/config", method = RequestMethod.POST) |
76 | 64 | @ResponseStatus(value = HttpStatus.OK) |
77 | 65 | public List<OAuth2ClientsDomainParams> saveClientParams(@RequestBody List<OAuth2ClientsDomainParams> domainsParams) throws ThingsboardException { |
78 | 66 | try { |
79 | - TenantId tenantId; | |
80 | - Authority authority = getCurrentUser().getAuthority(); | |
81 | - if (Authority.SYS_ADMIN.equals(authority)) { | |
82 | - tenantId = TenantId.SYS_TENANT_ID; | |
83 | - } else if (Authority.TENANT_ADMIN.equals(authority)) { | |
84 | - tenantId = getCurrentUser().getTenantId(); | |
85 | - } else { | |
86 | - throw new IllegalStateException("Authority " + authority + " cannot save client registrations."); | |
87 | - } | |
88 | - List<ClientRegistrationDto> clientRegistrationDtos = domainsParams.stream() | |
89 | - .flatMap(domainParams -> domainParams.getClientRegistrations().stream()) | |
90 | - .collect(Collectors.toList()); | |
91 | - for (ClientRegistrationDto clientRegistrationDto : clientRegistrationDtos) { | |
92 | - checkEntity(clientRegistrationDto.getId(), () -> tenantId, Resource.OAUTH2_CONFIGURATION); | |
93 | - } | |
94 | - return oAuth2Service.saveDomainsParams(tenantId, domainsParams); | |
67 | + return oAuth2Service.saveDomainsParams(domainsParams); | |
95 | 68 | } catch (Exception e) { |
96 | 69 | throw handleException(e); |
97 | 70 | } |
98 | 71 | } |
99 | 72 | |
100 | - @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')") | |
73 | + @PreAuthorize("hasAnyAuthority('SYS_ADMIN')") | |
101 | 74 | @RequestMapping(value = "/oauth2/config/{clientRegistrationId}", method = RequestMethod.DELETE) |
102 | 75 | @ResponseStatus(value = HttpStatus.OK) |
103 | 76 | public void deleteClientRegistration(@PathVariable(CLIENT_REGISTRATION_ID) String strClientRegistrationId) throws ThingsboardException { |
104 | 77 | checkParameter(CLIENT_REGISTRATION_ID, strClientRegistrationId); |
105 | 78 | try { |
106 | 79 | OAuth2ClientRegistrationId clientRegistrationId = new OAuth2ClientRegistrationId(toUUID(strClientRegistrationId)); |
107 | - OAuth2ClientRegistration clientRegistration = checkOAuth2ClientRegistrationId(clientRegistrationId, Operation.DELETE); | |
108 | - oAuth2Service.deleteClientRegistrationById(getCurrentUser().getTenantId(), clientRegistrationId); | |
80 | + oAuth2Service.deleteClientRegistrationById(clientRegistrationId); | |
109 | 81 | |
110 | - logEntityAction(clientRegistrationId, clientRegistration, | |
82 | + logEntityAction(clientRegistrationId, | |
83 | + null, | |
111 | 84 | null, |
112 | 85 | ActionType.DELETED, null, strClientRegistrationId); |
113 | 86 | |
... | ... | @@ -123,13 +96,13 @@ public class OAuth2Controller extends BaseController { |
123 | 96 | } |
124 | 97 | |
125 | 98 | |
126 | - @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN')") | |
99 | + @PreAuthorize("hasAnyAuthority('SYS_ADMIN')") | |
127 | 100 | @RequestMapping(value = "/oauth2/config/domain/{domain}", method = RequestMethod.DELETE) |
128 | 101 | @ResponseStatus(value = HttpStatus.OK) |
129 | 102 | public void deleteClientRegistrationForDomain(@PathVariable(DOMAIN) String domain) throws ThingsboardException { |
130 | 103 | checkParameter(DOMAIN, domain); |
131 | 104 | try { |
132 | - oAuth2Service.deleteClientRegistrationsByDomain(getCurrentUser().getTenantId(), domain); | |
105 | + oAuth2Service.deleteClientRegistrationsByDomain(domain); | |
133 | 106 | |
134 | 107 | logEntityAction(emptyId(EntityType.OAUTH2_CLIENT_REGISTRATION), null, |
135 | 108 | null, |
... | ... | @@ -144,19 +117,4 @@ public class OAuth2Controller extends BaseController { |
144 | 117 | throw handleException(e); |
145 | 118 | } |
146 | 119 | } |
147 | - | |
148 | - @PreAuthorize("hasAnyAuthority('TENANT_ADMIN')") | |
149 | - @RequestMapping(value = "/oauth2/config/isAllowed", method = RequestMethod.GET) | |
150 | - @ResponseBody | |
151 | - public Boolean isOAuth2ConfigurationAllowed() throws ThingsboardException { | |
152 | - try { | |
153 | - return oAuth2Service.isOAuth2ClientRegistrationAllowed(getTenantId()); | |
154 | - } catch (Exception e) { | |
155 | - throw handleException(e); | |
156 | - } | |
157 | - } | |
158 | - | |
159 | - private void checkOAuth2ConfigPermissions(Operation operation) throws ThingsboardException { | |
160 | - accessControlService.checkPermission(getCurrentUser(), Resource.OAUTH2_CONFIGURATION, operation); | |
161 | - } | |
162 | 120 | } | ... | ... |
... | ... | @@ -224,7 +224,6 @@ public class InstallScripts { |
224 | 224 | try { |
225 | 225 | JsonNode oauth2ConfigTemplateJson = objectMapper.readTree(path.toFile()); |
226 | 226 | OAuth2ClientRegistrationTemplate clientRegistrationTemplate = objectMapper.treeToValue(oauth2ConfigTemplateJson, OAuth2ClientRegistrationTemplate.class); |
227 | - clientRegistrationTemplate.setTenantId(TenantId.SYS_TENANT_ID); | |
228 | 227 | oAuth2TemplateService.saveClientRegistrationTemplate(clientRegistrationTemplate); |
229 | 228 | } catch (Exception e) { |
230 | 229 | log.error("Unable to load oauth2 config templates from json: [{}]", path.toString()); | ... | ... |
... | ... | @@ -17,7 +17,6 @@ package org.thingsboard.server.service.security.auth.oauth2; |
17 | 17 | |
18 | 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
19 | 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
20 | -import com.google.common.base.Strings; | |
21 | 20 | import lombok.extern.slf4j.Slf4j; |
22 | 21 | import org.springframework.beans.factory.annotation.Autowired; |
23 | 22 | import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
... | ... | @@ -34,7 +33,6 @@ import org.thingsboard.server.common.data.id.IdBased; |
34 | 33 | import org.thingsboard.server.common.data.id.TenantId; |
35 | 34 | import org.thingsboard.server.common.data.page.PageData; |
36 | 35 | import org.thingsboard.server.common.data.page.PageLink; |
37 | -import org.thingsboard.server.common.data.page.TimePageLink; | |
38 | 36 | import org.thingsboard.server.common.data.security.Authority; |
39 | 37 | import org.thingsboard.server.common.data.security.UserCredentials; |
40 | 38 | import org.thingsboard.server.dao.customer.CustomerService; |
... | ... | @@ -49,7 +47,6 @@ import org.thingsboard.server.service.security.model.UserPrincipal; |
49 | 47 | import java.io.IOException; |
50 | 48 | import java.util.List; |
51 | 49 | import java.util.Optional; |
52 | -import java.util.concurrent.ExecutionException; | |
53 | 50 | import java.util.concurrent.locks.Lock; |
54 | 51 | import java.util.concurrent.locks.ReentrantLock; |
55 | 52 | |
... | ... | @@ -79,7 +76,7 @@ public abstract class AbstractOAuth2ClientMapper { |
79 | 76 | |
80 | 77 | private final Lock userCreationLock = new ReentrantLock(); |
81 | 78 | |
82 | - protected SecurityUser getOrCreateSecurityUserFromOAuth2User(TenantId parentTenantId, OAuth2User oauth2User, boolean allowUserCreation, boolean activateUser) { | |
79 | + protected SecurityUser getOrCreateSecurityUserFromOAuth2User(OAuth2User oauth2User, boolean allowUserCreation, boolean activateUser) { | |
83 | 80 | UserPrincipal principal = new UserPrincipal(UserPrincipal.Type.USER_NAME, oauth2User.getEmail()); |
84 | 81 | |
85 | 82 | User user = userService.findUserByEmail(TenantId.SYS_TENANT_ID, oauth2User.getEmail()); |
... | ... | @@ -99,13 +96,8 @@ public abstract class AbstractOAuth2ClientMapper { |
99 | 96 | } else { |
100 | 97 | user.setAuthority(Authority.CUSTOMER_USER); |
101 | 98 | } |
102 | - TenantId tenantId; | |
103 | - if (TenantId.SYS_TENANT_ID.equals(parentTenantId)) { | |
104 | - tenantId = oauth2User.getTenantId() != null ? | |
105 | - oauth2User.getTenantId() : getTenantId(oauth2User.getTenantName()); | |
106 | - } else { | |
107 | - tenantId = parentTenantId; | |
108 | - } | |
99 | + TenantId tenantId = oauth2User.getTenantId() != null ? | |
100 | + oauth2User.getTenantId() : getTenantId(oauth2User.getTenantName()); | |
109 | 101 | user.setTenantId(tenantId); |
110 | 102 | CustomerId customerId = oauth2User.getCustomerId() != null ? |
111 | 103 | oauth2User.getCustomerId() : getCustomerId(user.getTenantId(), oauth2User.getCustomerName()); | ... | ... |
... | ... | @@ -20,7 +20,6 @@ import org.apache.commons.lang3.text.StrSubstitutor; |
20 | 20 | import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken; |
21 | 21 | import org.springframework.stereotype.Service; |
22 | 22 | import org.springframework.util.StringUtils; |
23 | -import org.thingsboard.server.common.data.id.TenantId; | |
24 | 23 | import org.thingsboard.server.common.data.oauth2.OAuth2MapperConfig; |
25 | 24 | import org.thingsboard.server.dao.oauth2.OAuth2User; |
26 | 25 | import org.thingsboard.server.service.security.model.SecurityUser; |
... | ... | @@ -35,7 +34,7 @@ public class BasicOAuth2ClientMapper extends AbstractOAuth2ClientMapper implemen |
35 | 34 | private static final String END_PLACEHOLDER_PREFIX = "}"; |
36 | 35 | |
37 | 36 | @Override |
38 | - public SecurityUser getOrCreateUserByClientPrincipal(OAuth2AuthenticationToken token, String providerAccessToken, TenantId parentTenantId, OAuth2MapperConfig config) { | |
37 | + public SecurityUser getOrCreateUserByClientPrincipal(OAuth2AuthenticationToken token, String providerAccessToken, OAuth2MapperConfig config) { | |
39 | 38 | OAuth2User oauth2User = new OAuth2User(); |
40 | 39 | Map<String, Object> attributes = token.getPrincipal().getAttributes(); |
41 | 40 | String email = getStringAttributeByKey(attributes, config.getBasic().getEmailAttributeKey()); |
... | ... | @@ -59,7 +58,7 @@ public class BasicOAuth2ClientMapper extends AbstractOAuth2ClientMapper implemen |
59 | 58 | oauth2User.setDefaultDashboardName(config.getBasic().getDefaultDashboardName()); |
60 | 59 | } |
61 | 60 | |
62 | - return getOrCreateSecurityUserFromOAuth2User(parentTenantId, oauth2User, config.isAllowUserCreation(), config.isActivateUser()); | |
61 | + return getOrCreateSecurityUserFromOAuth2User(oauth2User, config.isAllowUserCreation(), config.isActivateUser()); | |
63 | 62 | } |
64 | 63 | |
65 | 64 | private String getTenantName(Map<String, Object> attributes, OAuth2MapperConfig config) { | ... | ... |
... | ... | @@ -23,7 +23,6 @@ import org.springframework.security.oauth2.client.authentication.OAuth2Authentic |
23 | 23 | import org.springframework.stereotype.Service; |
24 | 24 | import org.springframework.util.StringUtils; |
25 | 25 | import org.springframework.web.client.RestTemplate; |
26 | -import org.thingsboard.server.common.data.id.TenantId; | |
27 | 26 | import org.thingsboard.server.common.data.oauth2.OAuth2CustomMapperConfig; |
28 | 27 | import org.thingsboard.server.common.data.oauth2.OAuth2MapperConfig; |
29 | 28 | import org.thingsboard.server.dao.oauth2.OAuth2User; |
... | ... | @@ -39,9 +38,9 @@ public class CustomOAuth2ClientMapper extends AbstractOAuth2ClientMapper impleme |
39 | 38 | private RestTemplateBuilder restTemplateBuilder = new RestTemplateBuilder(); |
40 | 39 | |
41 | 40 | @Override |
42 | - public SecurityUser getOrCreateUserByClientPrincipal(OAuth2AuthenticationToken token, String providerAccessToken, TenantId parentTenantId, OAuth2MapperConfig config) { | |
41 | + public SecurityUser getOrCreateUserByClientPrincipal(OAuth2AuthenticationToken token, String providerAccessToken, OAuth2MapperConfig config) { | |
43 | 42 | OAuth2User oauth2User = getOAuth2User(token, providerAccessToken, config.getCustom()); |
44 | - return getOrCreateSecurityUserFromOAuth2User(parentTenantId, oauth2User, config.isAllowUserCreation(), config.isActivateUser()); | |
43 | + return getOrCreateSecurityUserFromOAuth2User(oauth2User, config.isAllowUserCreation(), config.isActivateUser()); | |
45 | 44 | } |
46 | 45 | |
47 | 46 | private synchronized OAuth2User getOAuth2User(OAuth2AuthenticationToken token, String providerAccessToken, OAuth2CustomMapperConfig custom) { | ... | ... |
... | ... | @@ -16,10 +16,9 @@ |
16 | 16 | package org.thingsboard.server.service.security.auth.oauth2; |
17 | 17 | |
18 | 18 | import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken; |
19 | -import org.thingsboard.server.common.data.id.TenantId; | |
20 | 19 | import org.thingsboard.server.common.data.oauth2.OAuth2MapperConfig; |
21 | 20 | import org.thingsboard.server.service.security.model.SecurityUser; |
22 | 21 | |
23 | 22 | public interface OAuth2ClientMapper { |
24 | - SecurityUser getOrCreateUserByClientPrincipal(OAuth2AuthenticationToken token, String providerAccessToken, TenantId parentTenantId, OAuth2MapperConfig config); | |
23 | + SecurityUser getOrCreateUserByClientPrincipal(OAuth2AuthenticationToken token, String providerAccessToken, OAuth2MapperConfig config); | |
25 | 24 | } | ... | ... |
... | ... | @@ -74,7 +74,7 @@ public class Oauth2AuthenticationSuccessHandler extends SimpleUrlAuthenticationS |
74 | 74 | token.getPrincipal().getName()); |
75 | 75 | OAuth2ClientMapper mapper = oauth2ClientMapperProvider.getOAuth2ClientMapperByType(clientRegistration.getMapperConfig().getType()); |
76 | 76 | SecurityUser securityUser = mapper.getOrCreateUserByClientPrincipal(token, oAuth2AuthorizedClient.getAccessToken().getTokenValue(), |
77 | - clientRegistration.getTenantId(), clientRegistration.getMapperConfig()); | |
77 | + clientRegistration.getMapperConfig()); | |
78 | 78 | |
79 | 79 | JwtToken accessToken = tokenFactory.createAccessJwtToken(securityUser); |
80 | 80 | JwtToken refreshToken = refreshTokenRepository.requestRefreshToken(securityUser); | ... | ... |
... | ... | @@ -19,14 +19,10 @@ import org.springframework.stereotype.Component; |
19 | 19 | import org.thingsboard.server.common.data.HasTenantId; |
20 | 20 | import org.thingsboard.server.common.data.User; |
21 | 21 | import org.thingsboard.server.common.data.id.EntityId; |
22 | -import org.thingsboard.server.common.data.id.TenantId; | |
23 | 22 | import org.thingsboard.server.common.data.id.UserId; |
24 | 23 | import org.thingsboard.server.common.data.security.Authority; |
25 | 24 | import org.thingsboard.server.service.security.model.SecurityUser; |
26 | 25 | |
27 | -import java.util.HashMap; | |
28 | -import java.util.Optional; | |
29 | - | |
30 | 26 | @Component(value="sysAdminPermissions") |
31 | 27 | public class SysAdminPermissions extends AbstractPermissions { |
32 | 28 | |
... | ... | @@ -39,7 +35,7 @@ public class SysAdminPermissions extends AbstractPermissions { |
39 | 35 | put(Resource.USER, userPermissionChecker); |
40 | 36 | put(Resource.WIDGETS_BUNDLE, systemEntityPermissionChecker); |
41 | 37 | put(Resource.WIDGET_TYPE, systemEntityPermissionChecker); |
42 | - put(Resource.OAUTH2_CONFIGURATION, sysAdminOAuth2ConfigPermissionChecker); | |
38 | + put(Resource.OAUTH2_CONFIGURATION, PermissionChecker.allowAllPermissionChecker); | |
43 | 39 | put(Resource.OAUTH2_CONFIGURATION_TEMPLATE, PermissionChecker.allowAllPermissionChecker); |
44 | 40 | } |
45 | 41 | |
... | ... | @@ -67,19 +63,4 @@ public class SysAdminPermissions extends AbstractPermissions { |
67 | 63 | |
68 | 64 | }; |
69 | 65 | |
70 | - private final PermissionChecker sysAdminOAuth2ConfigPermissionChecker = new PermissionChecker() { | |
71 | - @Override | |
72 | - public boolean hasPermission(SecurityUser user, Operation operation) { | |
73 | - return true; | |
74 | - } | |
75 | - | |
76 | - @Override | |
77 | - public boolean hasPermission(SecurityUser user, Operation operation, EntityId entityId, HasTenantId entity) { | |
78 | - if (entity.getTenantId() != null && !entity.getTenantId().isNullUid()) { | |
79 | - return false; | |
80 | - } | |
81 | - return true; | |
82 | - } | |
83 | - }; | |
84 | - | |
85 | 66 | } | ... | ... |
... | ... | @@ -15,25 +15,17 @@ |
15 | 15 | */ |
16 | 16 | package org.thingsboard.server.service.security.permission; |
17 | 17 | |
18 | -import org.springframework.beans.factory.annotation.Autowired; | |
19 | 18 | import org.springframework.stereotype.Component; |
20 | 19 | import org.thingsboard.server.common.data.HasTenantId; |
21 | 20 | import org.thingsboard.server.common.data.User; |
22 | 21 | import org.thingsboard.server.common.data.id.EntityId; |
23 | -import org.thingsboard.server.common.data.id.TenantId; | |
24 | 22 | import org.thingsboard.server.common.data.id.UserId; |
25 | 23 | import org.thingsboard.server.common.data.security.Authority; |
26 | -import org.thingsboard.server.dao.oauth2.OAuth2Service; | |
27 | 24 | import org.thingsboard.server.service.security.model.SecurityUser; |
28 | 25 | |
29 | -import java.util.HashMap; | |
30 | - | |
31 | 26 | @Component(value="tenantAdminPermissions") |
32 | 27 | public class TenantAdminPermissions extends AbstractPermissions { |
33 | 28 | |
34 | - @Autowired | |
35 | - private OAuth2Service oAuth2Service; | |
36 | - | |
37 | 29 | public TenantAdminPermissions() { |
38 | 30 | super(); |
39 | 31 | put(Resource.ALARM, tenantEntityPermissionChecker); |
... | ... | @@ -47,8 +39,6 @@ public class TenantAdminPermissions extends AbstractPermissions { |
47 | 39 | put(Resource.USER, userPermissionChecker); |
48 | 40 | put(Resource.WIDGETS_BUNDLE, widgetsPermissionChecker); |
49 | 41 | put(Resource.WIDGET_TYPE, widgetsPermissionChecker); |
50 | - put(Resource.OAUTH2_CONFIGURATION, tenantOAuth2ConfigPermissionChecker); | |
51 | - put(Resource.OAUTH2_CONFIGURATION_TEMPLATE, tenantOAuth2ConfigTemplatePermissionChecker); | |
52 | 42 | } |
53 | 43 | |
54 | 44 | public static final PermissionChecker tenantEntityPermissionChecker = new PermissionChecker() { |
... | ... | @@ -108,31 +98,4 @@ public class TenantAdminPermissions extends AbstractPermissions { |
108 | 98 | } |
109 | 99 | |
110 | 100 | }; |
111 | - | |
112 | - private final PermissionChecker tenantOAuth2ConfigPermissionChecker = new PermissionChecker() { | |
113 | - @Override | |
114 | - public boolean hasPermission(SecurityUser user, Operation operation) { | |
115 | - return oAuth2Service.isOAuth2ClientRegistrationAllowed(user.getTenantId()); | |
116 | - } | |
117 | - | |
118 | - @Override | |
119 | - public boolean hasPermission(SecurityUser user, Operation operation, EntityId entityId, HasTenantId entity) { | |
120 | - if (!user.getTenantId().equals(entity.getTenantId())) { | |
121 | - return false; | |
122 | - } | |
123 | - return hasPermission(user, operation); | |
124 | - } | |
125 | - }; | |
126 | - | |
127 | - private static final PermissionChecker tenantOAuth2ConfigTemplatePermissionChecker = new PermissionChecker() { | |
128 | - @Override | |
129 | - public boolean hasPermission(SecurityUser user, Operation operation) { | |
130 | - return operation == Operation.READ; | |
131 | - } | |
132 | - | |
133 | - @Override | |
134 | - public boolean hasPermission(SecurityUser user, Operation operation, EntityId entityId, HasTenantId entity) { | |
135 | - return operation == Operation.READ; | |
136 | - } | |
137 | - }; | |
138 | 101 | } | ... | ... |
... | ... | @@ -27,19 +27,15 @@ import java.util.UUID; |
27 | 27 | public interface OAuth2Service { |
28 | 28 | List<OAuth2ClientInfo> getOAuth2Clients(String domainName); |
29 | 29 | |
30 | - List<OAuth2ClientsDomainParams> saveDomainsParams(TenantId tenantId, List<OAuth2ClientsDomainParams> domainsParams); | |
30 | + List<OAuth2ClientsDomainParams> saveDomainsParams(List<OAuth2ClientsDomainParams> domainsParams); | |
31 | 31 | |
32 | - List<OAuth2ClientsDomainParams> findDomainsParamsByTenantId(TenantId tenantId); | |
32 | + List<OAuth2ClientsDomainParams> findDomainsParams(); | |
33 | 33 | |
34 | 34 | OAuth2ClientRegistration findClientRegistration(UUID id); |
35 | 35 | |
36 | 36 | List<OAuth2ClientRegistration> findAllClientRegistrations(); |
37 | 37 | |
38 | - void deleteClientRegistrationsByTenantId(TenantId tenantId); | |
38 | + void deleteClientRegistrationById(OAuth2ClientRegistrationId id); | |
39 | 39 | |
40 | - void deleteClientRegistrationById(TenantId tenantId, OAuth2ClientRegistrationId id); | |
41 | - | |
42 | - void deleteClientRegistrationsByDomain(TenantId tenantId, String domain); | |
43 | - | |
44 | - boolean isOAuth2ClientRegistrationAllowed(TenantId tenantId); | |
40 | + void deleteClientRegistrationsByDomain(String domain); | |
45 | 41 | } | ... | ... |
... | ... | @@ -32,9 +32,8 @@ import java.util.List; |
32 | 32 | @Data |
33 | 33 | @ToString(exclude = {"clientSecret"}) |
34 | 34 | @NoArgsConstructor |
35 | -public class OAuth2ClientRegistration extends SearchTextBasedWithAdditionalInfo<OAuth2ClientRegistrationId> implements HasTenantId, HasName { | |
35 | +public class OAuth2ClientRegistration extends SearchTextBasedWithAdditionalInfo<OAuth2ClientRegistrationId> implements HasName { | |
36 | 36 | |
37 | - private TenantId tenantId; | |
38 | 37 | private String domainName; |
39 | 38 | private String redirectUriTemplate; |
40 | 39 | private OAuth2MapperConfig mapperConfig; |
... | ... | @@ -52,7 +51,6 @@ public class OAuth2ClientRegistration extends SearchTextBasedWithAdditionalInfo< |
52 | 51 | |
53 | 52 | public OAuth2ClientRegistration(OAuth2ClientRegistration clientRegistration) { |
54 | 53 | super(clientRegistration); |
55 | - this.tenantId = clientRegistration.tenantId; | |
56 | 54 | this.domainName = clientRegistration.domainName; |
57 | 55 | this.redirectUriTemplate = clientRegistration.redirectUriTemplate; |
58 | 56 | this.mapperConfig = clientRegistration.mapperConfig; | ... | ... |
... | ... | @@ -31,9 +31,8 @@ import java.util.List; |
31 | 31 | @Data |
32 | 32 | @ToString |
33 | 33 | @NoArgsConstructor |
34 | -public class OAuth2ClientRegistrationTemplate extends SearchTextBasedWithAdditionalInfo<OAuth2ClientRegistrationTemplateId> implements HasTenantId, HasName { | |
34 | +public class OAuth2ClientRegistrationTemplate extends SearchTextBasedWithAdditionalInfo<OAuth2ClientRegistrationTemplateId> implements HasName { | |
35 | 35 | |
36 | - private TenantId tenantId; | |
37 | 36 | private String providerId; |
38 | 37 | private OAuth2BasicMapperConfig basic; |
39 | 38 | private String authorizationUri; |
... | ... | @@ -50,7 +49,6 @@ public class OAuth2ClientRegistrationTemplate extends SearchTextBasedWithAdditio |
50 | 49 | |
51 | 50 | public OAuth2ClientRegistrationTemplate(OAuth2ClientRegistrationTemplate clientRegistrationTemplate) { |
52 | 51 | super(clientRegistrationTemplate); |
53 | - this.tenantId = clientRegistrationTemplate.tenantId; | |
54 | 52 | this.providerId = clientRegistrationTemplate.providerId; |
55 | 53 | this.basic = clientRegistrationTemplate.basic; |
56 | 54 | this.authorizationUri = clientRegistrationTemplate.authorizationUri; | ... | ... |
... | ... | @@ -38,9 +38,6 @@ import java.util.UUID; |
38 | 38 | @Table(name = ModelConstants.OAUTH2_CLIENT_REGISTRATION_COLUMN_FAMILY_NAME) |
39 | 39 | public class OAuth2ClientRegistrationEntity extends BaseSqlEntity<OAuth2ClientRegistration> { |
40 | 40 | |
41 | - @Column(name = ModelConstants.OAUTH2_TENANT_ID_PROPERTY, columnDefinition = "uuid") | |
42 | - private UUID tenantId; | |
43 | - | |
44 | 41 | @Column(name = ModelConstants.OAUTH2_DOMAIN_NAME_PROPERTY) |
45 | 42 | private String domainName; |
46 | 43 | @Column(name = ModelConstants.OAUTH2_CLIENT_ID_PROPERTY) |
... | ... | @@ -112,9 +109,6 @@ public class OAuth2ClientRegistrationEntity extends BaseSqlEntity<OAuth2ClientRe |
112 | 109 | if (clientRegistration.getId() != null) { |
113 | 110 | this.setUuid(clientRegistration.getId().getId()); |
114 | 111 | } |
115 | - if (clientRegistration.getTenantId() != null) { | |
116 | - this.tenantId = clientRegistration.getTenantId().getId(); | |
117 | - } | |
118 | 112 | this.domainName = clientRegistration.getDomainName(); |
119 | 113 | this.createdTime = clientRegistration.getCreatedTime(); |
120 | 114 | this.clientId = clientRegistration.getClientId(); |
... | ... | @@ -160,7 +154,6 @@ public class OAuth2ClientRegistrationEntity extends BaseSqlEntity<OAuth2ClientRe |
160 | 154 | public OAuth2ClientRegistration toData() { |
161 | 155 | OAuth2ClientRegistration clientRegistration = new OAuth2ClientRegistration(); |
162 | 156 | clientRegistration.setId(new OAuth2ClientRegistrationId(id)); |
163 | - clientRegistration.setTenantId(new TenantId(tenantId)); | |
164 | 157 | clientRegistration.setCreatedTime(createdTime); |
165 | 158 | clientRegistration.setDomainName(domainName); |
166 | 159 | clientRegistration.setAdditionalInfo(additionalInfo); | ... | ... |
... | ... | @@ -39,9 +39,6 @@ import java.util.UUID; |
39 | 39 | @Table(name = ModelConstants.OAUTH2_CLIENT_REGISTRATION_TEMPLATE_COLUMN_FAMILY_NAME) |
40 | 40 | public class OAuth2ClientRegistrationTemplateEntity extends BaseSqlEntity<OAuth2ClientRegistrationTemplate> { |
41 | 41 | |
42 | - @Column(name = ModelConstants.OAUTH2_TENANT_ID_PROPERTY, columnDefinition = "uuid") | |
43 | - private UUID tenantId; | |
44 | - | |
45 | 42 | @Column(name = ModelConstants.OAUTH2_TEMPLATE_PROVIDER_ID_PROPERTY) |
46 | 43 | private String providerId; |
47 | 44 | @Column(name = ModelConstants.OAUTH2_AUTHORIZATION_URI_PROPERTY) |
... | ... | @@ -95,9 +92,6 @@ public class OAuth2ClientRegistrationTemplateEntity extends BaseSqlEntity<OAuth2 |
95 | 92 | if (clientRegistrationTemplate.getId() != null) { |
96 | 93 | this.setUuid(clientRegistrationTemplate.getId().getId()); |
97 | 94 | } |
98 | - if (clientRegistrationTemplate.getTenantId() != null) { | |
99 | - this.tenantId = clientRegistrationTemplate.getTenantId().getId(); | |
100 | - } | |
101 | 95 | this.createdTime = clientRegistrationTemplate.getCreatedTime(); |
102 | 96 | this.providerId = clientRegistrationTemplate.getProviderId(); |
103 | 97 | this.authorizationUri = clientRegistrationTemplate.getAuthorizationUri(); |
... | ... | @@ -129,7 +123,6 @@ public class OAuth2ClientRegistrationTemplateEntity extends BaseSqlEntity<OAuth2 |
129 | 123 | public OAuth2ClientRegistrationTemplate toData() { |
130 | 124 | OAuth2ClientRegistrationTemplate clientRegistrationTemplate = new OAuth2ClientRegistrationTemplate(); |
131 | 125 | clientRegistrationTemplate.setId(new OAuth2ClientRegistrationTemplateId(id)); |
132 | - clientRegistrationTemplate.setTenantId(new TenantId(tenantId)); | |
133 | 126 | clientRegistrationTemplate.setCreatedTime(createdTime); |
134 | 127 | clientRegistrationTemplate.setAdditionalInfo(additionalInfo); |
135 | 128 | ... | ... |
... | ... | @@ -24,11 +24,7 @@ import java.util.UUID; |
24 | 24 | public interface OAuth2ClientRegistrationDao extends Dao<OAuth2ClientRegistration> { |
25 | 25 | List<OAuth2ClientRegistration> findAll(); |
26 | 26 | |
27 | - List<OAuth2ClientRegistration> findByTenantId(UUID tenantId); | |
28 | - | |
29 | 27 | List<OAuth2ClientRegistration> findByDomainName(String domainName); |
30 | 28 | |
31 | - int removeByTenantIdAndDomainName(UUID tenantId, String domainName); | |
32 | - | |
33 | - int removeByTenantId(UUID tenantId); | |
29 | + int removeByDomainName(String domainName); | |
34 | 30 | } | ... | ... |
... | ... | @@ -44,10 +44,10 @@ public class OAuth2ConfigTemplateServiceImpl extends AbstractEntityService imple |
44 | 44 | @Override |
45 | 45 | public OAuth2ClientRegistrationTemplate saveClientRegistrationTemplate(OAuth2ClientRegistrationTemplate clientRegistrationTemplate) { |
46 | 46 | log.trace("Executing saveClientRegistrationTemplate [{}]", clientRegistrationTemplate); |
47 | - clientRegistrationTemplateValidator.validate(clientRegistrationTemplate, OAuth2ClientRegistrationTemplate::getTenantId); | |
47 | + clientRegistrationTemplateValidator.validate(clientRegistrationTemplate, o -> TenantId.SYS_TENANT_ID); | |
48 | 48 | OAuth2ClientRegistrationTemplate savedClientRegistrationTemplate; |
49 | 49 | try { |
50 | - savedClientRegistrationTemplate = clientRegistrationTemplateDao.save(clientRegistrationTemplate.getTenantId(), clientRegistrationTemplate); | |
50 | + savedClientRegistrationTemplate = clientRegistrationTemplateDao.save(TenantId.SYS_TENANT_ID, clientRegistrationTemplate); | |
51 | 51 | } catch (Exception t) { |
52 | 52 | ConstraintViolationException e = extractConstraintViolationException(t).orElse(null); |
53 | 53 | if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("oauth2_template_provider_id_unq_key")) { |
... | ... | @@ -98,10 +98,6 @@ public class OAuth2ConfigTemplateServiceImpl extends AbstractEntityService imple |
98 | 98 | if (clientRegistrationTemplate.getBasic() == null) { |
99 | 99 | throw new DataValidationException("Basic mapper config should be specified!"); |
100 | 100 | } |
101 | - if (clientRegistrationTemplate.getTenantId() == null | |
102 | - || !TenantId.SYS_TENANT_ID.equals(clientRegistrationTemplate.getTenantId())) { | |
103 | - throw new DataValidationException("Client registration template should be assigned to system admin!"); | |
104 | - } | |
105 | 101 | } |
106 | 102 | }; |
107 | 103 | } | ... | ... |
... | ... | @@ -15,30 +15,22 @@ |
15 | 15 | */ |
16 | 16 | package org.thingsboard.server.dao.oauth2; |
17 | 17 | |
18 | -import com.fasterxml.jackson.databind.JsonNode; | |
19 | 18 | import lombok.extern.slf4j.Slf4j; |
20 | -import org.hibernate.exception.ConstraintViolationException; | |
21 | 19 | import org.springframework.beans.factory.annotation.Autowired; |
22 | 20 | import org.springframework.stereotype.Service; |
23 | 21 | import org.springframework.util.StringUtils; |
24 | -import org.thingsboard.server.common.data.Tenant; | |
25 | -import org.thingsboard.server.common.data.exception.ThingsboardErrorCode; | |
26 | -import org.thingsboard.server.common.data.exception.ThingsboardException; | |
27 | 22 | import org.thingsboard.server.common.data.id.OAuth2ClientRegistrationId; |
28 | 23 | import org.thingsboard.server.common.data.id.TenantId; |
29 | 24 | import org.thingsboard.server.common.data.oauth2.*; |
30 | 25 | import org.thingsboard.server.dao.entity.AbstractEntityService; |
31 | 26 | import org.thingsboard.server.dao.exception.DataValidationException; |
32 | -import org.thingsboard.server.dao.service.DataValidator; | |
33 | 27 | import org.thingsboard.server.dao.tenant.TenantService; |
34 | 28 | |
35 | 29 | import javax.transaction.Transactional; |
36 | 30 | import java.util.*; |
37 | -import java.util.function.BiConsumer; | |
38 | 31 | import java.util.function.Consumer; |
39 | 32 | import java.util.stream.Collectors; |
40 | 33 | |
41 | -import static org.thingsboard.server.dao.oauth2.OAuth2Utils.ALLOW_OAUTH2_CONFIGURATION; | |
42 | 34 | import static org.thingsboard.server.dao.service.Validator.validateId; |
43 | 35 | import static org.thingsboard.server.dao.service.Validator.validateString; |
44 | 36 | |
... | ... | @@ -50,9 +42,6 @@ public class OAuth2ServiceImpl extends AbstractEntityService implements OAuth2Se |
50 | 42 | public static final String INCORRECT_DOMAIN_NAME = "Incorrect domainName "; |
51 | 43 | |
52 | 44 | @Autowired |
53 | - private TenantService tenantService; | |
54 | - | |
55 | - @Autowired | |
56 | 45 | private OAuth2ClientRegistrationDao clientRegistrationDao; |
57 | 46 | |
58 | 47 | @Override |
... | ... | @@ -66,21 +55,20 @@ public class OAuth2ServiceImpl extends AbstractEntityService implements OAuth2Se |
66 | 55 | |
67 | 56 | @Override |
68 | 57 | @Transactional |
69 | - public List<OAuth2ClientsDomainParams> saveDomainsParams(TenantId tenantId, List<OAuth2ClientsDomainParams> domainsParams) { | |
70 | - log.trace("Executing saveDomainsParams [{}] [{}]", tenantId, domainsParams); | |
71 | - clientParamsValidator.accept(tenantId, domainsParams); | |
72 | - List<OAuth2ClientRegistration> inputClientRegistrations = OAuth2Utils.toClientRegistrations(tenantId, domainsParams); | |
58 | + public List<OAuth2ClientsDomainParams> saveDomainsParams(List<OAuth2ClientsDomainParams> domainsParams) { | |
59 | + log.trace("Executing saveDomainsParams [{}]", domainsParams); | |
60 | + clientParamsValidator.accept(domainsParams); | |
61 | + List<OAuth2ClientRegistration> inputClientRegistrations = OAuth2Utils.toClientRegistrations(domainsParams); | |
73 | 62 | List<OAuth2ClientRegistration> savedClientRegistrations = inputClientRegistrations.stream() |
74 | - .map(clientRegistration -> clientRegistrationDao.save(clientRegistration.getTenantId(), clientRegistration)) | |
63 | + .map(clientRegistration -> clientRegistrationDao.save(TenantId.SYS_TENANT_ID, clientRegistration)) | |
75 | 64 | .collect(Collectors.toList()); |
76 | 65 | return OAuth2Utils.toDomainsParams(savedClientRegistrations); |
77 | 66 | } |
78 | 67 | |
79 | 68 | @Override |
80 | - public List<OAuth2ClientsDomainParams> findDomainsParamsByTenantId(TenantId tenantId) { | |
81 | - log.trace("Executing findDomainsParamsByTenantId [{}]", tenantId); | |
82 | - validateId(tenantId, INCORRECT_TENANT_ID + tenantId); | |
83 | - return OAuth2Utils.toDomainsParams(clientRegistrationDao.findByTenantId(tenantId.getId())); | |
69 | + public List<OAuth2ClientsDomainParams> findDomainsParams() { | |
70 | + log.trace("Executing findDomainsParams"); | |
71 | + return OAuth2Utils.toDomainsParams(clientRegistrationDao.findAll()); | |
84 | 72 | } |
85 | 73 | |
86 | 74 | @Override |
... | ... | @@ -97,45 +85,21 @@ public class OAuth2ServiceImpl extends AbstractEntityService implements OAuth2Se |
97 | 85 | } |
98 | 86 | |
99 | 87 | @Override |
100 | - @Transactional | |
101 | - public void deleteClientRegistrationsByTenantId(TenantId tenantId) { | |
102 | - log.trace("Executing deleteClientRegistrationsByTenantId [{}]", tenantId); | |
103 | - validateId(tenantId, INCORRECT_TENANT_ID + tenantId); | |
104 | - clientRegistrationDao.removeByTenantId(tenantId.getId()); | |
105 | - } | |
106 | - | |
107 | - @Override | |
108 | - public void deleteClientRegistrationById(TenantId tenantId, OAuth2ClientRegistrationId id) { | |
109 | - log.trace("Executing deleteClientRegistrationById [{}], [{}]", tenantId, id); | |
110 | - validateId(tenantId, INCORRECT_TENANT_ID + tenantId); | |
88 | + public void deleteClientRegistrationById(OAuth2ClientRegistrationId id) { | |
89 | + log.trace("Executing deleteClientRegistrationById [{}]", id); | |
111 | 90 | validateId(id, INCORRECT_CLIENT_REGISTRATION_ID + id); |
112 | - clientRegistrationDao.removeById(tenantId, id.getId()); | |
91 | + clientRegistrationDao.removeById(TenantId.SYS_TENANT_ID, id.getId()); | |
113 | 92 | } |
114 | 93 | |
115 | 94 | @Override |
116 | 95 | @Transactional |
117 | - public void deleteClientRegistrationsByDomain(TenantId tenantId, String domain) { | |
118 | - log.trace("Executing deleteClientRegistrationsByDomain [{}], [{}]", tenantId, domain); | |
119 | - validateId(tenantId, INCORRECT_TENANT_ID + tenantId); | |
96 | + public void deleteClientRegistrationsByDomain(String domain) { | |
97 | + log.trace("Executing deleteClientRegistrationsByDomain [{}]", domain); | |
120 | 98 | validateString(domain, INCORRECT_DOMAIN_NAME + domain); |
121 | - clientRegistrationDao.removeByTenantIdAndDomainName(tenantId.getId(), domain); | |
99 | + clientRegistrationDao.removeByDomainName(domain); | |
122 | 100 | } |
123 | 101 | |
124 | - @Override | |
125 | - public boolean isOAuth2ClientRegistrationAllowed(TenantId tenantId) { | |
126 | - log.trace("Executing isOAuth2ClientRegistrationAllowed [{}]", tenantId); | |
127 | - validateId(tenantId, INCORRECT_TENANT_ID + tenantId); | |
128 | - Tenant tenant = tenantService.findTenantById(tenantId); | |
129 | - if (tenant == null) return false; | |
130 | - JsonNode allowOAuth2ConfigurationJsonNode = tenant.getAdditionalInfo() != null ? tenant.getAdditionalInfo().get(ALLOW_OAUTH2_CONFIGURATION) : null; | |
131 | - if (allowOAuth2ConfigurationJsonNode == null) { | |
132 | - return false; | |
133 | - } else { | |
134 | - return allowOAuth2ConfigurationJsonNode.asBoolean(); | |
135 | - } | |
136 | - } | |
137 | - | |
138 | - private final BiConsumer<TenantId, List<OAuth2ClientsDomainParams>> clientParamsValidator = (tenantId, domainsParams) -> { | |
102 | + private final Consumer<List<OAuth2ClientsDomainParams>> clientParamsValidator = domainsParams -> { | |
139 | 103 | if (domainsParams == null || domainsParams.isEmpty()) { |
140 | 104 | throw new DataValidationException("Domain params should be specified!"); |
141 | 105 | } |
... | ... | @@ -211,13 +175,5 @@ public class OAuth2ServiceImpl extends AbstractEntityService implements OAuth2Se |
211 | 175 | } |
212 | 176 | } |
213 | 177 | } |
214 | - if (tenantId == null) { | |
215 | - throw new DataValidationException("Client registration should be assigned to tenant!"); | |
216 | - } else if (!TenantId.SYS_TENANT_ID.equals(tenantId)) { | |
217 | - Tenant tenant = tenantService.findTenantById(tenantId); | |
218 | - if (tenant == null) { | |
219 | - throw new DataValidationException("Client registration is referencing to non-existent tenant!"); | |
220 | - } | |
221 | - } | |
222 | 178 | }; |
223 | 179 | } | ... | ... |
... | ... | @@ -15,8 +15,6 @@ |
15 | 15 | */ |
16 | 16 | package org.thingsboard.server.dao.oauth2; |
17 | 17 | |
18 | -import org.springframework.util.StringUtils; | |
19 | -import org.thingsboard.server.common.data.id.TenantId; | |
20 | 18 | import org.thingsboard.server.common.data.oauth2.*; |
21 | 19 | |
22 | 20 | import java.util.ArrayList; |
... | ... | @@ -24,7 +22,6 @@ import java.util.HashMap; |
24 | 22 | import java.util.List; |
25 | 23 | import java.util.Map; |
26 | 24 | import java.util.stream.Collectors; |
27 | -import java.util.stream.Stream; | |
28 | 25 | |
29 | 26 | public class OAuth2Utils { |
30 | 27 | public static final String ALLOW_OAUTH2_CONFIGURATION = "allowOAuth2Configuration"; |
... | ... | @@ -38,10 +35,10 @@ public class OAuth2Utils { |
38 | 35 | return client; |
39 | 36 | } |
40 | 37 | |
41 | - public static List<OAuth2ClientRegistration> toClientRegistrations(TenantId tenantId, List<OAuth2ClientsDomainParams> domainsParams) { | |
38 | + public static List<OAuth2ClientRegistration> toClientRegistrations(List<OAuth2ClientsDomainParams> domainsParams) { | |
42 | 39 | return domainsParams.stream() |
43 | 40 | .flatMap(domainParams -> domainParams.getClientRegistrations().stream() |
44 | - .map(clientRegistrationDto -> OAuth2Utils.toClientRegistration(tenantId, domainParams.getDomainName(), | |
41 | + .map(clientRegistrationDto -> OAuth2Utils.toClientRegistration(domainParams.getDomainName(), | |
45 | 42 | domainParams.getRedirectUriTemplate(), clientRegistrationDto) |
46 | 43 | )) |
47 | 44 | .collect(Collectors.toList()); |
... | ... | @@ -80,11 +77,10 @@ public class OAuth2Utils { |
80 | 77 | .build(); |
81 | 78 | } |
82 | 79 | |
83 | - public static OAuth2ClientRegistration toClientRegistration(TenantId tenantId, String domainName, String redirectUriTemplate, | |
84 | - ClientRegistrationDto clientRegistrationDto) { | |
80 | + public static OAuth2ClientRegistration toClientRegistration(String domainName, String redirectUriTemplate, | |
81 | + ClientRegistrationDto clientRegistrationDto) { | |
85 | 82 | OAuth2ClientRegistration clientRegistration = new OAuth2ClientRegistration(); |
86 | 83 | clientRegistration.setId(clientRegistrationDto.getId()); |
87 | - clientRegistration.setTenantId(tenantId); | |
88 | 84 | clientRegistration.setCreatedTime(clientRegistrationDto.getCreatedTime()); |
89 | 85 | clientRegistration.setDomainName(domainName); |
90 | 86 | clientRegistration.setRedirectUriTemplate(redirectUriTemplate); | ... | ... |
... | ... | @@ -26,7 +26,6 @@ import org.thingsboard.server.dao.sql.JpaAbstractDao; |
26 | 26 | |
27 | 27 | import java.util.ArrayList; |
28 | 28 | import java.util.List; |
29 | -import java.util.Optional; | |
30 | 29 | import java.util.UUID; |
31 | 30 | import java.util.stream.Collectors; |
32 | 31 | |
... | ... | @@ -56,24 +55,13 @@ public class JpaOAuth2ClientRegistrationDao extends JpaAbstractDao<OAuth2ClientR |
56 | 55 | } |
57 | 56 | |
58 | 57 | @Override |
59 | - public List<OAuth2ClientRegistration> findByTenantId(UUID tenantId) { | |
60 | - List<OAuth2ClientRegistrationEntity> entities = repository.findAllByTenantId(tenantId); | |
61 | - return entities.stream().map(DaoUtil::getData).collect(Collectors.toList()); | |
62 | - } | |
63 | - | |
64 | - @Override | |
65 | 58 | public List<OAuth2ClientRegistration> findByDomainName(String domainName) { |
66 | 59 | List<OAuth2ClientRegistrationEntity> entities = repository.findAllByDomainName(domainName); |
67 | 60 | return entities.stream().map(DaoUtil::getData).collect(Collectors.toList()); |
68 | 61 | } |
69 | 62 | |
70 | 63 | @Override |
71 | - public int removeByTenantIdAndDomainName(UUID tenantId, String domainName) { | |
72 | - return repository.deleteByTenantIdAndDomainName(tenantId, domainName); | |
73 | - } | |
74 | - | |
75 | - @Override | |
76 | - public int removeByTenantId(UUID tenantId) { | |
77 | - return repository.deleteByTenantId(tenantId); | |
64 | + public int removeByDomainName(String domainName) { | |
65 | + return repository.deleteByDomainName(domainName); | |
78 | 66 | } |
79 | 67 | } | ... | ... |
... | ... | @@ -22,11 +22,7 @@ import java.util.List; |
22 | 22 | import java.util.UUID; |
23 | 23 | |
24 | 24 | public interface OAuth2ClientRegistrationRepository extends CrudRepository<OAuth2ClientRegistrationEntity, UUID> { |
25 | - List<OAuth2ClientRegistrationEntity> findAllByTenantId(UUID tenantId); | |
26 | - | |
27 | 25 | List<OAuth2ClientRegistrationEntity> findAllByDomainName(String domainName); |
28 | 26 | |
29 | - int deleteByTenantIdAndDomainName(UUID tenantId, String domainName); | |
30 | - | |
31 | - int deleteByTenantId(UUID tenantId); | |
27 | + int deleteByDomainName(String domainName); | |
32 | 28 | } | ... | ... |
... | ... | @@ -105,7 +105,6 @@ public class TenantServiceImpl extends AbstractEntityService implements TenantSe |
105 | 105 | public void deleteTenant(TenantId tenantId) { |
106 | 106 | log.trace("Executing deleteTenant [{}]", tenantId); |
107 | 107 | Validator.validateId(tenantId, INCORRECT_TENANT_ID + tenantId); |
108 | - oAuth2Service.deleteClientRegistrationsByTenantId(tenantId); | |
109 | 108 | customerService.deleteCustomersByTenantId(tenantId); |
110 | 109 | widgetsBundleService.deleteWidgetsBundlesByTenantId(tenantId); |
111 | 110 | dashboardService.deleteDashboardsByTenantId(tenantId); | ... | ... |
... | ... | @@ -295,7 +295,6 @@ CREATE TABLE IF NOT EXISTS oauth2_client_registration ( |
295 | 295 | id uuid NOT NULL CONSTRAINT oauth2_client_registration_pkey PRIMARY KEY, |
296 | 296 | created_time bigint NOT NULL, |
297 | 297 | additional_info varchar, |
298 | - tenant_id uuid, | |
299 | 298 | domain_name varchar(255), |
300 | 299 | client_id varchar(255), |
301 | 300 | client_secret varchar(255), |
... | ... | @@ -330,7 +329,6 @@ CREATE TABLE IF NOT EXISTS oauth2_client_registration_template ( |
330 | 329 | id uuid NOT NULL CONSTRAINT oauth2_client_registration_template_pkey PRIMARY KEY, |
331 | 330 | created_time bigint NOT NULL, |
332 | 331 | additional_info varchar, |
333 | - tenant_id uuid, | |
334 | 332 | provider_id varchar(255), |
335 | 333 | authorization_uri varchar(255), |
336 | 334 | token_uri varchar(255), | ... | ... |
... | ... | @@ -320,7 +320,6 @@ CREATE TABLE IF NOT EXISTS oauth2_client_registration ( |
320 | 320 | id uuid NOT NULL CONSTRAINT oauth2_client_registration_pkey PRIMARY KEY, |
321 | 321 | created_time bigint NOT NULL, |
322 | 322 | additional_info varchar, |
323 | - tenant_id uuid, | |
324 | 323 | domain_name varchar(255), |
325 | 324 | client_id varchar(255), |
326 | 325 | client_secret varchar(255), |
... | ... | @@ -355,7 +354,6 @@ CREATE TABLE IF NOT EXISTS oauth2_client_registration_template ( |
355 | 354 | id uuid NOT NULL CONSTRAINT oauth2_client_registration_template_pkey PRIMARY KEY, |
356 | 355 | created_time bigint NOT NULL, |
357 | 356 | additional_info varchar, |
358 | - tenant_id uuid, | |
359 | 357 | provider_id varchar(255), |
360 | 358 | authorization_uri varchar(255), |
361 | 359 | token_uri varchar(255), | ... | ... |
... | ... | @@ -34,8 +34,6 @@ public class BaseOAuth2ConfigTemplateServiceTest extends AbstractServiceTest { |
34 | 34 | @Autowired |
35 | 35 | protected OAuth2ConfigTemplateService oAuth2ConfigTemplateService; |
36 | 36 | |
37 | - private TenantId tenantId; | |
38 | - | |
39 | 37 | @Before |
40 | 38 | public void beforeRun() throws Exception { |
41 | 39 | Assert.assertTrue(oAuth2ConfigTemplateService.findAllClientRegistrationTemplates().isEmpty()); |
... | ... | @@ -53,15 +51,15 @@ public class BaseOAuth2ConfigTemplateServiceTest extends AbstractServiceTest { |
53 | 51 | |
54 | 52 | @Test(expected = DataValidationException.class) |
55 | 53 | public void testSaveDuplicateProviderId() { |
56 | - OAuth2ClientRegistrationTemplate first = validClientRegistrationTemplate(TenantId.SYS_TENANT_ID, "providerId"); | |
57 | - OAuth2ClientRegistrationTemplate second = validClientRegistrationTemplate(TenantId.SYS_TENANT_ID, "providerId"); | |
54 | + OAuth2ClientRegistrationTemplate first = validClientRegistrationTemplate("providerId"); | |
55 | + OAuth2ClientRegistrationTemplate second = validClientRegistrationTemplate("providerId"); | |
58 | 56 | oAuth2ConfigTemplateService.saveClientRegistrationTemplate(first); |
59 | 57 | oAuth2ConfigTemplateService.saveClientRegistrationTemplate(second); |
60 | 58 | } |
61 | 59 | |
62 | 60 | @Test |
63 | 61 | public void testCreateNewTemplate() { |
64 | - OAuth2ClientRegistrationTemplate clientRegistrationTemplate = validClientRegistrationTemplate(TenantId.SYS_TENANT_ID, UUID.randomUUID().toString()); | |
62 | + OAuth2ClientRegistrationTemplate clientRegistrationTemplate = validClientRegistrationTemplate(UUID.randomUUID().toString()); | |
65 | 63 | OAuth2ClientRegistrationTemplate savedClientRegistrationTemplate = oAuth2ConfigTemplateService.saveClientRegistrationTemplate(clientRegistrationTemplate); |
66 | 64 | |
67 | 65 | Assert.assertNotNull(savedClientRegistrationTemplate); |
... | ... | @@ -73,7 +71,7 @@ public class BaseOAuth2ConfigTemplateServiceTest extends AbstractServiceTest { |
73 | 71 | |
74 | 72 | @Test |
75 | 73 | public void testFindTemplate() { |
76 | - OAuth2ClientRegistrationTemplate clientRegistrationTemplate = validClientRegistrationTemplate(TenantId.SYS_TENANT_ID, UUID.randomUUID().toString()); | |
74 | + OAuth2ClientRegistrationTemplate clientRegistrationTemplate = validClientRegistrationTemplate(UUID.randomUUID().toString()); | |
77 | 75 | OAuth2ClientRegistrationTemplate savedClientRegistrationTemplate = oAuth2ConfigTemplateService.saveClientRegistrationTemplate(clientRegistrationTemplate); |
78 | 76 | |
79 | 77 | OAuth2ClientRegistrationTemplate foundClientRegistrationTemplate = oAuth2ConfigTemplateService.findClientRegistrationTemplateById(savedClientRegistrationTemplate.getId()); |
... | ... | @@ -82,17 +80,17 @@ public class BaseOAuth2ConfigTemplateServiceTest extends AbstractServiceTest { |
82 | 80 | |
83 | 81 | @Test |
84 | 82 | public void testFindAll() { |
85 | - oAuth2ConfigTemplateService.saveClientRegistrationTemplate(validClientRegistrationTemplate(TenantId.SYS_TENANT_ID, UUID.randomUUID().toString())); | |
86 | - oAuth2ConfigTemplateService.saveClientRegistrationTemplate(validClientRegistrationTemplate(TenantId.SYS_TENANT_ID, UUID.randomUUID().toString())); | |
83 | + oAuth2ConfigTemplateService.saveClientRegistrationTemplate(validClientRegistrationTemplate(UUID.randomUUID().toString())); | |
84 | + oAuth2ConfigTemplateService.saveClientRegistrationTemplate(validClientRegistrationTemplate(UUID.randomUUID().toString())); | |
87 | 85 | |
88 | 86 | Assert.assertEquals(2, oAuth2ConfigTemplateService.findAllClientRegistrationTemplates().size()); |
89 | 87 | } |
90 | 88 | |
91 | 89 | @Test |
92 | 90 | public void testDeleteTemplate() { |
93 | - oAuth2ConfigTemplateService.saveClientRegistrationTemplate(validClientRegistrationTemplate(TenantId.SYS_TENANT_ID, UUID.randomUUID().toString())); | |
94 | - oAuth2ConfigTemplateService.saveClientRegistrationTemplate(validClientRegistrationTemplate(TenantId.SYS_TENANT_ID, UUID.randomUUID().toString())); | |
95 | - OAuth2ClientRegistrationTemplate saved = oAuth2ConfigTemplateService.saveClientRegistrationTemplate(validClientRegistrationTemplate(TenantId.SYS_TENANT_ID, UUID.randomUUID().toString())); | |
91 | + oAuth2ConfigTemplateService.saveClientRegistrationTemplate(validClientRegistrationTemplate(UUID.randomUUID().toString())); | |
92 | + oAuth2ConfigTemplateService.saveClientRegistrationTemplate(validClientRegistrationTemplate(UUID.randomUUID().toString())); | |
93 | + OAuth2ClientRegistrationTemplate saved = oAuth2ConfigTemplateService.saveClientRegistrationTemplate(validClientRegistrationTemplate(UUID.randomUUID().toString())); | |
96 | 94 | |
97 | 95 | Assert.assertEquals(3, oAuth2ConfigTemplateService.findAllClientRegistrationTemplates().size()); |
98 | 96 | Assert.assertNotNull(oAuth2ConfigTemplateService.findClientRegistrationTemplateById(saved.getId())); |
... | ... | @@ -103,10 +101,9 @@ public class BaseOAuth2ConfigTemplateServiceTest extends AbstractServiceTest { |
103 | 101 | Assert.assertNull(oAuth2ConfigTemplateService.findClientRegistrationTemplateById(saved.getId())); |
104 | 102 | } |
105 | 103 | |
106 | - private OAuth2ClientRegistrationTemplate validClientRegistrationTemplate(TenantId tenantId, String providerId) { | |
104 | + private OAuth2ClientRegistrationTemplate validClientRegistrationTemplate(String providerId) { | |
107 | 105 | OAuth2ClientRegistrationTemplate clientRegistrationTemplate = new OAuth2ClientRegistrationTemplate(); |
108 | 106 | clientRegistrationTemplate.setProviderId(providerId); |
109 | - clientRegistrationTemplate.setTenantId(tenantId); | |
110 | 107 | clientRegistrationTemplate.setAdditionalInfo(mapper.createObjectNode().put(UUID.randomUUID().toString(), UUID.randomUUID().toString())); |
111 | 108 | clientRegistrationTemplate.setBasic( |
112 | 109 | OAuth2BasicMapperConfig.builder() | ... | ... |
... | ... | @@ -20,18 +20,14 @@ import org.junit.Assert; |
20 | 20 | import org.junit.Before; |
21 | 21 | import org.junit.Test; |
22 | 22 | import org.springframework.beans.factory.annotation.Autowired; |
23 | -import org.thingsboard.server.common.data.Tenant; | |
24 | 23 | import org.thingsboard.server.common.data.id.TenantId; |
25 | 24 | import org.thingsboard.server.common.data.oauth2.*; |
26 | -import org.thingsboard.server.dao.attributes.AttributesService; | |
27 | 25 | import org.thingsboard.server.dao.oauth2.OAuth2Service; |
28 | 26 | import org.thingsboard.server.dao.oauth2.OAuth2Utils; |
29 | 27 | |
30 | -import java.io.IOException; | |
31 | 28 | import java.util.*; |
32 | 29 | import java.util.stream.Collectors; |
33 | 30 | |
34 | -import static org.thingsboard.server.dao.oauth2.OAuth2Utils.ALLOW_OAUTH2_CONFIGURATION; | |
35 | 31 | import static org.thingsboard.server.dao.oauth2.OAuth2Utils.toClientRegistrations; |
36 | 32 | |
37 | 33 | public class BaseOAuth2ServiceTest extends AbstractServiceTest { |
... | ... | @@ -39,56 +35,26 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { |
39 | 35 | @Autowired |
40 | 36 | protected OAuth2Service oAuth2Service; |
41 | 37 | |
42 | - @Autowired | |
43 | - protected AttributesService attributesService; | |
44 | - | |
45 | - private TenantId tenantId; | |
46 | - | |
47 | 38 | @Before |
48 | - public void beforeRun() throws Exception { | |
49 | - Tenant tenant = new Tenant(); | |
50 | - tenant.setTitle("My tenant"); | |
51 | - Tenant savedTenant = tenantService.saveTenant(tenant); | |
52 | - Assert.assertNotNull(savedTenant); | |
53 | - tenantId = savedTenant.getId(); | |
54 | - | |
39 | + public void beforeRun() { | |
55 | 40 | Assert.assertTrue(oAuth2Service.findAllClientRegistrations().isEmpty()); |
56 | 41 | } |
57 | 42 | |
58 | 43 | @After |
59 | - public void after() throws Exception { | |
60 | - tenantService.deleteTenant(tenantId); | |
61 | - oAuth2Service.deleteClientRegistrationsByTenantId(TenantId.SYS_TENANT_ID); | |
62 | - | |
44 | + public void after() { | |
45 | + oAuth2Service.findAllClientRegistrations().forEach(clientRegistration -> { | |
46 | + oAuth2Service.deleteClientRegistrationById(clientRegistration.getId()); | |
47 | + }); | |
63 | 48 | Assert.assertTrue(oAuth2Service.findAllClientRegistrations().isEmpty()); |
64 | 49 | } |
65 | 50 | |
66 | 51 | @Test |
67 | - public void testIsOAuth2Allowed_null() throws IOException { | |
68 | - updateTenantAllowOAuth2Setting(null); | |
69 | - Assert.assertFalse(oAuth2Service.isOAuth2ClientRegistrationAllowed(tenantId)); | |
70 | - } | |
71 | - | |
72 | - @Test | |
73 | - public void testIsOAuth2Allowed_false() throws IOException { | |
74 | - updateTenantAllowOAuth2Setting(false); | |
75 | - Assert.assertFalse(oAuth2Service.isOAuth2ClientRegistrationAllowed(tenantId)); | |
76 | - } | |
77 | - | |
78 | - @Test | |
79 | - public void testIsOAuth2Allowed_true() throws IOException { | |
80 | - updateTenantAllowOAuth2Setting(true); | |
81 | - Assert.assertTrue(oAuth2Service.isOAuth2ClientRegistrationAllowed(tenantId)); | |
82 | - } | |
83 | - | |
84 | - | |
85 | - @Test | |
86 | - public void testCreateNewSystemParams() { | |
87 | - OAuth2ClientRegistration clientRegistration = validClientRegistration(TenantId.SYS_TENANT_ID); | |
88 | - List<OAuth2ClientsDomainParams> savedDomainsParams = oAuth2Service.saveDomainsParams(TenantId.SYS_TENANT_ID, OAuth2Utils.toDomainsParams(Collections.singletonList(clientRegistration))); | |
52 | + public void testCreateNewParams() { | |
53 | + OAuth2ClientRegistration clientRegistration = validClientRegistration("domain-name"); | |
54 | + List<OAuth2ClientsDomainParams> savedDomainsParams = oAuth2Service.saveDomainsParams(OAuth2Utils.toDomainsParams(Collections.singletonList(clientRegistration))); | |
89 | 55 | Assert.assertNotNull(savedDomainsParams); |
90 | 56 | |
91 | - List<OAuth2ClientRegistration> savedClientRegistrations = OAuth2Utils.toClientRegistrations(TenantId.SYS_TENANT_ID, savedDomainsParams); | |
57 | + List<OAuth2ClientRegistration> savedClientRegistrations = OAuth2Utils.toClientRegistrations(savedDomainsParams); | |
92 | 58 | Assert.assertEquals(1, savedClientRegistrations.size()); |
93 | 59 | |
94 | 60 | OAuth2ClientRegistration savedClientRegistration = savedClientRegistrations.get(0); |
... | ... | @@ -96,55 +62,21 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { |
96 | 62 | clientRegistration.setId(savedClientRegistration.getId()); |
97 | 63 | clientRegistration.setCreatedTime(savedClientRegistration.getCreatedTime()); |
98 | 64 | Assert.assertEquals(clientRegistration, savedClientRegistration); |
99 | - } | |
100 | - | |
101 | - @Test | |
102 | - public void testFindSystemParamsByTenant() { | |
103 | - OAuth2ClientRegistration clientRegistration = validClientRegistration(TenantId.SYS_TENANT_ID); | |
104 | - oAuth2Service.saveDomainsParams(TenantId.SYS_TENANT_ID, OAuth2Utils.toDomainsParams(Collections.singletonList(clientRegistration))); | |
105 | - | |
106 | - List<OAuth2ClientsDomainParams> foundDomainsParams = oAuth2Service.findDomainsParamsByTenantId(TenantId.SYS_TENANT_ID); | |
107 | - Assert.assertEquals(1, foundDomainsParams.size()); | |
108 | - Assert.assertEquals(1, oAuth2Service.findAllClientRegistrations().size()); | |
109 | 65 | |
110 | - List<OAuth2ClientRegistration> foundClientRegistrations = OAuth2Utils.toClientRegistrations(TenantId.SYS_TENANT_ID, foundDomainsParams); | |
111 | - OAuth2ClientRegistration foundClientRegistration = foundClientRegistrations.get(0); | |
112 | - Assert.assertNotNull(foundClientRegistration); | |
113 | - clientRegistration.setId(foundClientRegistration.getId()); | |
114 | - clientRegistration.setCreatedTime(foundClientRegistration.getCreatedTime()); | |
115 | - Assert.assertEquals(clientRegistration, foundClientRegistration); | |
66 | + oAuth2Service.deleteClientRegistrationsByDomain("domain-name"); | |
116 | 67 | } |
117 | 68 | |
118 | 69 | @Test |
119 | - public void testCreateNewTenantParams() { | |
120 | - OAuth2ClientRegistration clientRegistration = validClientRegistration(tenantId); | |
121 | - List<OAuth2ClientsDomainParams> savedDomainsParams = oAuth2Service.saveDomainsParams(tenantId, OAuth2Utils.toDomainsParams(Collections.singletonList(clientRegistration))); | |
122 | - Assert.assertNotNull(savedDomainsParams); | |
70 | + public void testFindDomainParams() { | |
71 | + OAuth2ClientRegistration clientRegistration = validClientRegistration(); | |
72 | + oAuth2Service.saveDomainsParams(OAuth2Utils.toDomainsParams(Collections.singletonList(clientRegistration))); | |
123 | 73 | |
124 | - List<OAuth2ClientRegistration> savedClientRegistrations = OAuth2Utils.toClientRegistrations(tenantId, savedDomainsParams); | |
125 | - Assert.assertEquals(1, savedClientRegistrations.size()); | |
126 | - | |
127 | - OAuth2ClientRegistration savedClientRegistration = savedClientRegistrations.get(0); | |
128 | - | |
129 | - Assert.assertNotNull(savedClientRegistration); | |
130 | - Assert.assertNotNull(savedClientRegistration.getId()); | |
131 | - clientRegistration.setId(savedClientRegistration.getId()); | |
132 | - clientRegistration.setCreatedTime(savedClientRegistration.getCreatedTime()); | |
133 | - Assert.assertEquals(clientRegistration, savedClientRegistration); | |
134 | - } | |
135 | - | |
136 | - @Test | |
137 | - public void testFindTenantParams() { | |
138 | - OAuth2ClientRegistration clientRegistration = validClientRegistration(tenantId); | |
139 | - oAuth2Service.saveDomainsParams(tenantId, OAuth2Utils.toDomainsParams(Collections.singletonList(clientRegistration))); | |
140 | - | |
141 | - List<OAuth2ClientsDomainParams> foundDomainsParams = oAuth2Service.findDomainsParamsByTenantId(tenantId); | |
74 | + List<OAuth2ClientsDomainParams> foundDomainsParams = oAuth2Service.findDomainsParams(); | |
142 | 75 | Assert.assertEquals(1, foundDomainsParams.size()); |
143 | 76 | Assert.assertEquals(1, oAuth2Service.findAllClientRegistrations().size()); |
144 | 77 | |
145 | - List<OAuth2ClientRegistration> foundClientRegistrations = OAuth2Utils.toClientRegistrations(tenantId, foundDomainsParams); | |
78 | + List<OAuth2ClientRegistration> foundClientRegistrations = OAuth2Utils.toClientRegistrations(foundDomainsParams); | |
146 | 79 | OAuth2ClientRegistration foundClientRegistration = foundClientRegistrations.get(0); |
147 | - | |
148 | 80 | Assert.assertNotNull(foundClientRegistration); |
149 | 81 | clientRegistration.setId(foundClientRegistration.getId()); |
150 | 82 | clientRegistration.setCreatedTime(foundClientRegistration.getCreatedTime()); |
... | ... | @@ -152,39 +84,18 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { |
152 | 84 | } |
153 | 85 | |
154 | 86 | @Test |
155 | - public void testGetClientRegistrationWithTenant() { | |
156 | - OAuth2ClientRegistration tenantClientRegistration = validClientRegistration(tenantId); | |
157 | - OAuth2ClientRegistration sysAdminClientRegistration = validClientRegistration(TenantId.SYS_TENANT_ID); | |
158 | - | |
159 | - List<OAuth2ClientsDomainParams> savedTenantDomainsParams = oAuth2Service.saveDomainsParams(tenantId, | |
160 | - OAuth2Utils.toDomainsParams(Collections.singletonList(tenantClientRegistration))); | |
161 | - List<OAuth2ClientsDomainParams> savedSysAdminDomainsParams = oAuth2Service.saveDomainsParams(TenantId.SYS_TENANT_ID, | |
162 | - OAuth2Utils.toDomainsParams(Collections.singletonList(sysAdminClientRegistration))); | |
163 | - | |
164 | - Assert.assertEquals(2, oAuth2Service.findAllClientRegistrations().size()); | |
165 | - | |
166 | - Assert.assertEquals(savedTenantDomainsParams, oAuth2Service.findDomainsParamsByTenantId(tenantId)); | |
167 | - Assert.assertEquals(savedSysAdminDomainsParams, oAuth2Service.findDomainsParamsByTenantId(TenantId.SYS_TENANT_ID)); | |
168 | - | |
169 | - OAuth2ClientRegistration savedTenantClientRegistration = toClientRegistrations(tenantId, savedTenantDomainsParams).get(0); | |
170 | - Assert.assertEquals(savedTenantClientRegistration, oAuth2Service.findClientRegistration(savedTenantClientRegistration.getUuidId())); | |
171 | - OAuth2ClientRegistration savedSysAdminClientRegistration = toClientRegistrations(TenantId.SYS_TENANT_ID, savedSysAdminDomainsParams).get(0); | |
172 | - Assert.assertEquals(savedSysAdminClientRegistration, oAuth2Service.findClientRegistration(savedSysAdminClientRegistration.getUuidId())); | |
173 | - } | |
174 | - | |
175 | - @Test | |
176 | 87 | public void testGetOAuth2Clients() { |
177 | 88 | String testDomainName = "test_domain"; |
178 | - OAuth2ClientRegistration tenantClientRegistration = validClientRegistration(tenantId, testDomainName); | |
179 | - OAuth2ClientRegistration sysAdminClientRegistration = validClientRegistration(TenantId.SYS_TENANT_ID, testDomainName); | |
89 | + OAuth2ClientRegistration first = validClientRegistration(testDomainName); | |
90 | + OAuth2ClientRegistration second = validClientRegistration(testDomainName); | |
180 | 91 | |
181 | - oAuth2Service.saveDomainsParams(tenantId, OAuth2Utils.toDomainsParams(Collections.singletonList(tenantClientRegistration))); | |
182 | - oAuth2Service.saveDomainsParams(TenantId.SYS_TENANT_ID, OAuth2Utils.toDomainsParams(Collections.singletonList(sysAdminClientRegistration))); | |
92 | + oAuth2Service.saveDomainsParams(OAuth2Utils.toDomainsParams(Collections.singletonList(first))); | |
93 | + oAuth2Service.saveDomainsParams(OAuth2Utils.toDomainsParams(Collections.singletonList(second))); | |
183 | 94 | |
184 | 95 | List<OAuth2ClientInfo> oAuth2Clients = oAuth2Service.getOAuth2Clients(testDomainName); |
185 | 96 | |
186 | - Set<String> actualLabels = new HashSet<>(Arrays.asList(tenantClientRegistration.getLoginButtonLabel(), | |
187 | - sysAdminClientRegistration.getLoginButtonLabel())); | |
97 | + Set<String> actualLabels = new HashSet<>(Arrays.asList(first.getLoginButtonLabel(), | |
98 | + second.getLoginButtonLabel())); | |
188 | 99 | |
189 | 100 | Set<String> foundLabels = oAuth2Clients.stream().map(OAuth2ClientInfo::getName).collect(Collectors.toSet()); |
190 | 101 | Assert.assertEquals(actualLabels, foundLabels); |
... | ... | @@ -193,88 +104,61 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { |
193 | 104 | @Test |
194 | 105 | public void testGetEmptyOAuth2Clients() { |
195 | 106 | String testDomainName = "test_domain"; |
196 | - OAuth2ClientRegistration tenantClientRegistration = validClientRegistration(tenantId, testDomainName); | |
197 | - OAuth2ClientRegistration sysAdminClientRegistration = validClientRegistration(TenantId.SYS_TENANT_ID, testDomainName); | |
198 | - oAuth2Service.saveDomainsParams(tenantId, OAuth2Utils.toDomainsParams(Collections.singletonList(tenantClientRegistration))); | |
199 | - oAuth2Service.saveDomainsParams(TenantId.SYS_TENANT_ID, OAuth2Utils.toDomainsParams(Collections.singletonList(sysAdminClientRegistration))); | |
107 | + OAuth2ClientRegistration tenantClientRegistration = validClientRegistration(testDomainName); | |
108 | + OAuth2ClientRegistration sysAdminClientRegistration = validClientRegistration(testDomainName); | |
109 | + oAuth2Service.saveDomainsParams(OAuth2Utils.toDomainsParams(Collections.singletonList(tenantClientRegistration))); | |
110 | + oAuth2Service.saveDomainsParams(OAuth2Utils.toDomainsParams(Collections.singletonList(sysAdminClientRegistration))); | |
200 | 111 | List<OAuth2ClientInfo> oAuth2Clients = oAuth2Service.getOAuth2Clients("random-domain"); |
201 | 112 | Assert.assertTrue(oAuth2Clients.isEmpty()); |
202 | 113 | } |
203 | 114 | |
204 | 115 | @Test |
205 | 116 | public void testDeleteOAuth2ClientRegistration() { |
206 | - OAuth2ClientRegistration tenantClientRegistration = validClientRegistration(tenantId); | |
207 | - OAuth2ClientRegistration sysAdminClientRegistration = validClientRegistration(TenantId.SYS_TENANT_ID); | |
117 | + OAuth2ClientRegistration first = validClientRegistration(); | |
118 | + OAuth2ClientRegistration second = validClientRegistration(); | |
208 | 119 | |
209 | - List<OAuth2ClientsDomainParams> savedTenantDomainsParams = oAuth2Service.saveDomainsParams(tenantId, | |
210 | - OAuth2Utils.toDomainsParams(Collections.singletonList(tenantClientRegistration))); | |
211 | - List<OAuth2ClientsDomainParams> savedSysAdminDomainsParams = oAuth2Service.saveDomainsParams(TenantId.SYS_TENANT_ID, | |
212 | - OAuth2Utils.toDomainsParams(Collections.singletonList(sysAdminClientRegistration))); | |
120 | + List<OAuth2ClientsDomainParams> savedFirstDomainsParams = oAuth2Service.saveDomainsParams( | |
121 | + OAuth2Utils.toDomainsParams(Collections.singletonList(first))); | |
122 | + List<OAuth2ClientsDomainParams> savedSecondDomainsParams = oAuth2Service.saveDomainsParams( | |
123 | + OAuth2Utils.toDomainsParams(Collections.singletonList(second))); | |
213 | 124 | |
214 | - OAuth2ClientRegistration savedTenantRegistration = toClientRegistrations(tenantId, savedTenantDomainsParams).get(0); | |
215 | - OAuth2ClientRegistration savedSysAdminRegistration = toClientRegistrations(TenantId.SYS_TENANT_ID, savedSysAdminDomainsParams).get(0); | |
125 | + OAuth2ClientRegistration savedFirstRegistration = toClientRegistrations(savedFirstDomainsParams).get(0); | |
126 | + OAuth2ClientRegistration savedSecondRegistration = toClientRegistrations(savedSecondDomainsParams).get(0); | |
216 | 127 | |
217 | - oAuth2Service.deleteClientRegistrationById(tenantId, savedTenantRegistration.getId()); | |
128 | + oAuth2Service.deleteClientRegistrationById(savedFirstRegistration.getId()); | |
218 | 129 | List<OAuth2ClientRegistration> foundRegistrations = oAuth2Service.findAllClientRegistrations(); |
219 | 130 | Assert.assertEquals(1, foundRegistrations.size()); |
220 | - Assert.assertEquals(savedSysAdminRegistration, foundRegistrations.get(0)); | |
131 | + Assert.assertEquals(savedSecondRegistration, foundRegistrations.get(0)); | |
221 | 132 | } |
222 | 133 | |
223 | 134 | @Test |
224 | - public void testDeleteTenantOAuth2ClientRegistrations() { | |
225 | - oAuth2Service.saveDomainsParams(tenantId, OAuth2Utils.toDomainsParams(Arrays.asList( | |
226 | - validClientRegistration(tenantId, "domain"), | |
227 | - validClientRegistration(tenantId, "domain"), | |
228 | - validClientRegistration(tenantId, "domain") | |
135 | + public void testDeleteDomainOAuth2ClientRegistrations() { | |
136 | + oAuth2Service.saveDomainsParams(OAuth2Utils.toDomainsParams(Arrays.asList( | |
137 | + validClientRegistration("domain1"), | |
138 | + validClientRegistration("domain1"), | |
139 | + validClientRegistration("domain2") | |
229 | 140 | ))); |
230 | - Assert.assertEquals(3, oAuth2Service.findAllClientRegistrations().size()); | |
231 | - Assert.assertEquals(1, oAuth2Service.findDomainsParamsByTenantId(tenantId).size()); | |
232 | - | |
233 | - oAuth2Service.deleteClientRegistrationsByTenantId(tenantId); | |
234 | - Assert.assertEquals(0, oAuth2Service.findAllClientRegistrations().size()); | |
235 | - Assert.assertEquals(0, oAuth2Service.findDomainsParamsByTenantId(tenantId).size()); | |
236 | - } | |
237 | - | |
238 | - @Test | |
239 | - public void testDeleteTenantDomainOAuth2ClientRegistrations() { | |
240 | - oAuth2Service.saveDomainsParams(tenantId, OAuth2Utils.toDomainsParams(Arrays.asList( | |
241 | - validClientRegistration(tenantId, "domain1"), | |
242 | - validClientRegistration(tenantId, "domain1"), | |
243 | - validClientRegistration(tenantId, "domain2") | |
244 | - ))); | |
245 | - oAuth2Service.saveDomainsParams(TenantId.SYS_TENANT_ID, OAuth2Utils.toDomainsParams(Arrays.asList( | |
246 | - validClientRegistration(TenantId.SYS_TENANT_ID, "domain2") | |
141 | + oAuth2Service.saveDomainsParams(OAuth2Utils.toDomainsParams(Arrays.asList( | |
142 | + validClientRegistration("domain2") | |
247 | 143 | ))); |
248 | 144 | Assert.assertEquals(4, oAuth2Service.findAllClientRegistrations().size()); |
249 | - List<OAuth2ClientsDomainParams> tenantDomainsParams = oAuth2Service.findDomainsParamsByTenantId(tenantId); | |
250 | - List<OAuth2ClientRegistration> tenantClientRegistrations = toClientRegistrations(tenantId, tenantDomainsParams); | |
251 | - Assert.assertEquals(2, tenantDomainsParams.size()); | |
252 | - Assert.assertEquals(3, tenantClientRegistrations.size()); | |
145 | + List<OAuth2ClientsDomainParams> domainsParams = oAuth2Service.findDomainsParams(); | |
146 | + List<OAuth2ClientRegistration> clientRegistrations = toClientRegistrations(domainsParams); | |
147 | + Assert.assertEquals(2, domainsParams.size()); | |
148 | + Assert.assertEquals(4, clientRegistrations.size()); | |
253 | 149 | |
254 | - oAuth2Service.deleteClientRegistrationsByDomain(tenantId, "domain1"); | |
150 | + oAuth2Service.deleteClientRegistrationsByDomain("domain1"); | |
255 | 151 | Assert.assertEquals(2, oAuth2Service.findAllClientRegistrations().size()); |
256 | - Assert.assertEquals(1, oAuth2Service.findDomainsParamsByTenantId(tenantId).size()); | |
257 | - Assert.assertEquals(1, toClientRegistrations(tenantId, oAuth2Service.findDomainsParamsByTenantId(tenantId)).size()); | |
258 | - } | |
259 | - | |
260 | - private void updateTenantAllowOAuth2Setting(Boolean allowOAuth2) throws IOException { | |
261 | - Tenant tenant = tenantService.findTenantById(tenantId); | |
262 | - if (allowOAuth2 == null) { | |
263 | - tenant.setAdditionalInfo(mapper.readTree("{}")); | |
264 | - } else { | |
265 | - String additionalInfo = "{\"" + ALLOW_OAUTH2_CONFIGURATION + "\":" + allowOAuth2 + "}"; | |
266 | - tenant.setAdditionalInfo(mapper.readTree(additionalInfo)); | |
267 | - tenantService.saveTenant(tenant); | |
268 | - } | |
152 | + Assert.assertEquals(1, oAuth2Service.findDomainsParams().size()); | |
153 | + Assert.assertEquals(2, toClientRegistrations(oAuth2Service.findDomainsParams()).size()); | |
269 | 154 | } |
270 | 155 | |
271 | - private OAuth2ClientRegistration validClientRegistration(TenantId tenantId) { | |
272 | - return validClientRegistration(tenantId, "domainName"); | |
156 | + private OAuth2ClientRegistration validClientRegistration() { | |
157 | + return validClientRegistration("domainName"); | |
273 | 158 | } |
274 | 159 | |
275 | - private OAuth2ClientRegistration validClientRegistration(TenantId tenantId, String domainName) { | |
160 | + private OAuth2ClientRegistration validClientRegistration(String domainName) { | |
276 | 161 | OAuth2ClientRegistration clientRegistration = new OAuth2ClientRegistration(); |
277 | - clientRegistration.setTenantId(tenantId); | |
278 | 162 | clientRegistration.setDomainName(domainName); |
279 | 163 | clientRegistration.setMapperConfig( |
280 | 164 | OAuth2MapperConfig.builder() | ... | ... |