Commit 12e2d2efb716c284c11fbe94848f9bc9250f442a
1 parent
1b53971e
Packed domain objects to ClientParams
Showing
6 changed files
with
82 additions
and
50 deletions
... | ... | @@ -25,6 +25,7 @@ import org.thingsboard.server.common.data.exception.ThingsboardException; |
25 | 25 | import org.thingsboard.server.common.data.id.OAuth2ClientRegistrationId; |
26 | 26 | import org.thingsboard.server.common.data.oauth2.OAuth2ClientInfo; |
27 | 27 | import org.thingsboard.server.common.data.oauth2.OAuth2ClientsDomainParams; |
28 | +import org.thingsboard.server.common.data.oauth2.OAuth2ClientsParams; | |
28 | 29 | import org.thingsboard.server.queue.util.TbCoreComponent; |
29 | 30 | |
30 | 31 | import javax.servlet.http.HttpServletRequest; |
... | ... | @@ -51,9 +52,9 @@ public class OAuth2Controller extends BaseController { |
51 | 52 | @PreAuthorize("hasAnyAuthority('SYS_ADMIN')") |
52 | 53 | @RequestMapping(value = "/oauth2/config", method = RequestMethod.GET, produces = "application/json") |
53 | 54 | @ResponseBody |
54 | - public List<OAuth2ClientsDomainParams> getCurrentClientsParams() throws ThingsboardException { | |
55 | + public OAuth2ClientsParams getCurrentOAuth2Params() throws ThingsboardException { | |
55 | 56 | try { |
56 | - return oAuth2Service.findDomainsParams(); | |
57 | + return oAuth2Service.findOAuth2Params(); | |
57 | 58 | } catch (Exception e) { |
58 | 59 | throw handleException(e); |
59 | 60 | } |
... | ... | @@ -62,9 +63,9 @@ public class OAuth2Controller extends BaseController { |
62 | 63 | @PreAuthorize("hasAnyAuthority('SYS_ADMIN')") |
63 | 64 | @RequestMapping(value = "/oauth2/config", method = RequestMethod.POST) |
64 | 65 | @ResponseStatus(value = HttpStatus.OK) |
65 | - public List<OAuth2ClientsDomainParams> saveClientParams(@RequestBody List<OAuth2ClientsDomainParams> domainsParams) throws ThingsboardException { | |
66 | + public OAuth2ClientsParams saveOAuth2Params(@RequestBody OAuth2ClientsParams oauth2Params) throws ThingsboardException { | |
66 | 67 | try { |
67 | - return oAuth2Service.saveDomainsParams(domainsParams); | |
68 | + return oAuth2Service.saveOAuth2Params(oauth2Params); | |
68 | 69 | } catch (Exception e) { |
69 | 70 | throw handleException(e); |
70 | 71 | } | ... | ... |
... | ... | @@ -16,10 +16,9 @@ |
16 | 16 | package org.thingsboard.server.dao.oauth2; |
17 | 17 | |
18 | 18 | import org.thingsboard.server.common.data.id.OAuth2ClientRegistrationId; |
19 | -import org.thingsboard.server.common.data.id.TenantId; | |
20 | 19 | import org.thingsboard.server.common.data.oauth2.OAuth2ClientInfo; |
21 | 20 | import org.thingsboard.server.common.data.oauth2.OAuth2ClientRegistration; |
22 | -import org.thingsboard.server.common.data.oauth2.OAuth2ClientsDomainParams; | |
21 | +import org.thingsboard.server.common.data.oauth2.OAuth2ClientsParams; | |
23 | 22 | |
24 | 23 | import java.util.List; |
25 | 24 | import java.util.UUID; |
... | ... | @@ -27,9 +26,9 @@ import java.util.UUID; |
27 | 26 | public interface OAuth2Service { |
28 | 27 | List<OAuth2ClientInfo> getOAuth2Clients(String domainName); |
29 | 28 | |
30 | - List<OAuth2ClientsDomainParams> saveDomainsParams(List<OAuth2ClientsDomainParams> domainsParams); | |
29 | + OAuth2ClientsParams saveOAuth2Params(OAuth2ClientsParams oauth2Params); | |
31 | 30 | |
32 | - List<OAuth2ClientsDomainParams> findDomainsParams(); | |
31 | + OAuth2ClientsParams findOAuth2Params(); | |
33 | 32 | |
34 | 33 | OAuth2ClientRegistration findClientRegistration(UUID id); |
35 | 34 | ... | ... |
common/data/src/main/java/org/thingsboard/server/common/data/oauth2/OAuth2ClientsParams.java
0 → 100644
1 | +/** | |
2 | + * Copyright © 2016-2020 The Thingsboard Authors | |
3 | + * | |
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | + * you may not use this file except in compliance with the License. | |
6 | + * You may obtain a copy of the License at | |
7 | + * | |
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | + * | |
10 | + * Unless required by applicable law or agreed to in writing, software | |
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | + * See the License for the specific language governing permissions and | |
14 | + * limitations under the License. | |
15 | + */ | |
16 | +package org.thingsboard.server.common.data.oauth2; | |
17 | + | |
18 | +import lombok.*; | |
19 | +import org.thingsboard.server.common.data.id.TenantId; | |
20 | + | |
21 | +import java.util.Collection; | |
22 | +import java.util.List; | |
23 | +import java.util.Objects; | |
24 | + | |
25 | +@EqualsAndHashCode | |
26 | +@Data | |
27 | +@ToString | |
28 | +@Builder(toBuilder = true) | |
29 | +@NoArgsConstructor | |
30 | +@AllArgsConstructor | |
31 | +public class OAuth2ClientsParams { | |
32 | + private List<OAuth2ClientsDomainParams> oAuth2DomainDtos; | |
33 | +} | |
\ No newline at end of file | ... | ... |
... | ... | @@ -24,7 +24,6 @@ import org.thingsboard.server.common.data.id.TenantId; |
24 | 24 | import org.thingsboard.server.common.data.oauth2.*; |
25 | 25 | import org.thingsboard.server.dao.entity.AbstractEntityService; |
26 | 26 | import org.thingsboard.server.dao.exception.DataValidationException; |
27 | -import org.thingsboard.server.dao.tenant.TenantService; | |
28 | 27 | |
29 | 28 | import javax.transaction.Transactional; |
30 | 29 | import java.util.*; |
... | ... | @@ -55,20 +54,20 @@ public class OAuth2ServiceImpl extends AbstractEntityService implements OAuth2Se |
55 | 54 | |
56 | 55 | @Override |
57 | 56 | @Transactional |
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); | |
57 | + public OAuth2ClientsParams saveOAuth2Params(OAuth2ClientsParams oauth2Params) { | |
58 | + log.trace("Executing saveOAuth2Params [{}]", oauth2Params); | |
59 | + clientParamsValidator.accept(oauth2Params); | |
60 | + List<OAuth2ClientRegistration> inputClientRegistrations = OAuth2Utils.toClientRegistrations(oauth2Params); | |
62 | 61 | List<OAuth2ClientRegistration> savedClientRegistrations = inputClientRegistrations.stream() |
63 | 62 | .map(clientRegistration -> clientRegistrationDao.save(TenantId.SYS_TENANT_ID, clientRegistration)) |
64 | 63 | .collect(Collectors.toList()); |
65 | - return OAuth2Utils.toDomainsParams(savedClientRegistrations); | |
64 | + return OAuth2Utils.toOAuth2Params(savedClientRegistrations); | |
66 | 65 | } |
67 | 66 | |
68 | 67 | @Override |
69 | - public List<OAuth2ClientsDomainParams> findDomainsParams() { | |
70 | - log.trace("Executing findDomainsParams"); | |
71 | - return OAuth2Utils.toDomainsParams(clientRegistrationDao.findAll()); | |
68 | + public OAuth2ClientsParams findOAuth2Params() { | |
69 | + log.trace("Executing findOAuth2Params"); | |
70 | + return OAuth2Utils.toOAuth2Params(clientRegistrationDao.findAll()); | |
72 | 71 | } |
73 | 72 | |
74 | 73 | @Override |
... | ... | @@ -99,11 +98,13 @@ public class OAuth2ServiceImpl extends AbstractEntityService implements OAuth2Se |
99 | 98 | clientRegistrationDao.removeByDomainName(domain); |
100 | 99 | } |
101 | 100 | |
102 | - private final Consumer<List<OAuth2ClientsDomainParams>> clientParamsValidator = domainsParams -> { | |
103 | - if (domainsParams == null || domainsParams.isEmpty()) { | |
101 | + private final Consumer<OAuth2ClientsParams> clientParamsValidator = oauth2Params -> { | |
102 | + if (oauth2Params == null | |
103 | + || oauth2Params.getOAuth2DomainDtos() == null | |
104 | + || oauth2Params.getOAuth2DomainDtos().isEmpty()) { | |
104 | 105 | throw new DataValidationException("Domain params should be specified!"); |
105 | 106 | } |
106 | - for (OAuth2ClientsDomainParams domainParams : domainsParams) { | |
107 | + for (OAuth2ClientsDomainParams domainParams : oauth2Params.getOAuth2DomainDtos()) { | |
107 | 108 | if (StringUtils.isEmpty(domainParams.getDomainName())) { |
108 | 109 | throw new DataValidationException("Domain name should be specified!"); |
109 | 110 | } | ... | ... |
... | ... | @@ -24,7 +24,6 @@ import java.util.Map; |
24 | 24 | import java.util.stream.Collectors; |
25 | 25 | |
26 | 26 | public class OAuth2Utils { |
27 | - public static final String ALLOW_OAUTH2_CONFIGURATION = "allowOAuth2Configuration"; | |
28 | 27 | public static final String OAUTH2_AUTHORIZATION_PATH_TEMPLATE = "/oauth2/authorization/%s"; |
29 | 28 | |
30 | 29 | public static OAuth2ClientInfo toClientInfo(OAuth2ClientRegistration clientRegistration) { |
... | ... | @@ -35,8 +34,8 @@ public class OAuth2Utils { |
35 | 34 | return client; |
36 | 35 | } |
37 | 36 | |
38 | - public static List<OAuth2ClientRegistration> toClientRegistrations(List<OAuth2ClientsDomainParams> domainsParams) { | |
39 | - return domainsParams.stream() | |
37 | + public static List<OAuth2ClientRegistration> toClientRegistrations(OAuth2ClientsParams oAuth2Params) { | |
38 | + return oAuth2Params.getOAuth2DomainDtos().stream() | |
40 | 39 | .flatMap(domainParams -> domainParams.getClientRegistrations().stream() |
41 | 40 | .map(clientRegistrationDto -> OAuth2Utils.toClientRegistration(domainParams.getDomainName(), |
42 | 41 | domainParams.getRedirectUriTemplate(), clientRegistrationDto) |
... | ... | @@ -44,7 +43,7 @@ public class OAuth2Utils { |
44 | 43 | .collect(Collectors.toList()); |
45 | 44 | } |
46 | 45 | |
47 | - public static List<OAuth2ClientsDomainParams> toDomainsParams(List<OAuth2ClientRegistration> clientRegistrations) { | |
46 | + public static OAuth2ClientsParams toOAuth2Params(List<OAuth2ClientRegistration> clientRegistrations) { | |
48 | 47 | Map<String, OAuth2ClientsDomainParams> domainParamsMap = new HashMap<>(); |
49 | 48 | for (OAuth2ClientRegistration clientRegistration : clientRegistrations) { |
50 | 49 | String domainName = clientRegistration.getDomainName(); |
... | ... | @@ -54,7 +53,7 @@ public class OAuth2Utils { |
54 | 53 | domainParams.getClientRegistrations() |
55 | 54 | .add(toClientRegistrationDto(clientRegistration)); |
56 | 55 | } |
57 | - return new ArrayList<>(domainParamsMap.values()); | |
56 | + return new OAuth2ClientsParams(new ArrayList<>(domainParamsMap.values())); | |
58 | 57 | } |
59 | 58 | |
60 | 59 | public static ClientRegistrationDto toClientRegistrationDto(OAuth2ClientRegistration oAuth2ClientRegistration) { | ... | ... |
... | ... | @@ -20,7 +20,6 @@ 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.id.TenantId; | |
24 | 23 | import org.thingsboard.server.common.data.oauth2.*; |
25 | 24 | import org.thingsboard.server.dao.oauth2.OAuth2Service; |
26 | 25 | import org.thingsboard.server.dao.oauth2.OAuth2Utils; |
... | ... | @@ -51,10 +50,10 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { |
51 | 50 | @Test |
52 | 51 | public void testCreateNewParams() { |
53 | 52 | OAuth2ClientRegistration clientRegistration = validClientRegistration("domain-name"); |
54 | - List<OAuth2ClientsDomainParams> savedDomainsParams = oAuth2Service.saveDomainsParams(OAuth2Utils.toDomainsParams(Collections.singletonList(clientRegistration))); | |
55 | - Assert.assertNotNull(savedDomainsParams); | |
53 | + OAuth2ClientsParams savedOAuth2Params = oAuth2Service.saveOAuth2Params(OAuth2Utils.toOAuth2Params(Collections.singletonList(clientRegistration))); | |
54 | + Assert.assertNotNull(savedOAuth2Params); | |
56 | 55 | |
57 | - List<OAuth2ClientRegistration> savedClientRegistrations = OAuth2Utils.toClientRegistrations(savedDomainsParams); | |
56 | + List<OAuth2ClientRegistration> savedClientRegistrations = OAuth2Utils.toClientRegistrations(savedOAuth2Params); | |
58 | 57 | Assert.assertEquals(1, savedClientRegistrations.size()); |
59 | 58 | |
60 | 59 | OAuth2ClientRegistration savedClientRegistration = savedClientRegistrations.get(0); |
... | ... | @@ -69,13 +68,13 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { |
69 | 68 | @Test |
70 | 69 | public void testFindDomainParams() { |
71 | 70 | OAuth2ClientRegistration clientRegistration = validClientRegistration(); |
72 | - oAuth2Service.saveDomainsParams(OAuth2Utils.toDomainsParams(Collections.singletonList(clientRegistration))); | |
71 | + oAuth2Service.saveOAuth2Params(OAuth2Utils.toOAuth2Params(Collections.singletonList(clientRegistration))); | |
73 | 72 | |
74 | - List<OAuth2ClientsDomainParams> foundDomainsParams = oAuth2Service.findDomainsParams(); | |
75 | - Assert.assertEquals(1, foundDomainsParams.size()); | |
73 | + OAuth2ClientsParams foundOAuth2Params = oAuth2Service.findOAuth2Params(); | |
74 | + Assert.assertEquals(1, foundOAuth2Params.getOAuth2DomainDtos().size()); | |
76 | 75 | Assert.assertEquals(1, oAuth2Service.findAllClientRegistrations().size()); |
77 | 76 | |
78 | - List<OAuth2ClientRegistration> foundClientRegistrations = OAuth2Utils.toClientRegistrations(foundDomainsParams); | |
77 | + List<OAuth2ClientRegistration> foundClientRegistrations = OAuth2Utils.toClientRegistrations(foundOAuth2Params); | |
79 | 78 | OAuth2ClientRegistration foundClientRegistration = foundClientRegistrations.get(0); |
80 | 79 | Assert.assertNotNull(foundClientRegistration); |
81 | 80 | clientRegistration.setId(foundClientRegistration.getId()); |
... | ... | @@ -89,8 +88,8 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { |
89 | 88 | OAuth2ClientRegistration first = validClientRegistration(testDomainName); |
90 | 89 | OAuth2ClientRegistration second = validClientRegistration(testDomainName); |
91 | 90 | |
92 | - oAuth2Service.saveDomainsParams(OAuth2Utils.toDomainsParams(Collections.singletonList(first))); | |
93 | - oAuth2Service.saveDomainsParams(OAuth2Utils.toDomainsParams(Collections.singletonList(second))); | |
91 | + oAuth2Service.saveOAuth2Params(OAuth2Utils.toOAuth2Params(Collections.singletonList(first))); | |
92 | + oAuth2Service.saveOAuth2Params(OAuth2Utils.toOAuth2Params(Collections.singletonList(second))); | |
94 | 93 | |
95 | 94 | List<OAuth2ClientInfo> oAuth2Clients = oAuth2Service.getOAuth2Clients(testDomainName); |
96 | 95 | |
... | ... | @@ -106,8 +105,8 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { |
106 | 105 | String testDomainName = "test_domain"; |
107 | 106 | OAuth2ClientRegistration tenantClientRegistration = validClientRegistration(testDomainName); |
108 | 107 | OAuth2ClientRegistration sysAdminClientRegistration = validClientRegistration(testDomainName); |
109 | - oAuth2Service.saveDomainsParams(OAuth2Utils.toDomainsParams(Collections.singletonList(tenantClientRegistration))); | |
110 | - oAuth2Service.saveDomainsParams(OAuth2Utils.toDomainsParams(Collections.singletonList(sysAdminClientRegistration))); | |
108 | + oAuth2Service.saveOAuth2Params(OAuth2Utils.toOAuth2Params(Collections.singletonList(tenantClientRegistration))); | |
109 | + oAuth2Service.saveOAuth2Params(OAuth2Utils.toOAuth2Params(Collections.singletonList(sysAdminClientRegistration))); | |
111 | 110 | List<OAuth2ClientInfo> oAuth2Clients = oAuth2Service.getOAuth2Clients("random-domain"); |
112 | 111 | Assert.assertTrue(oAuth2Clients.isEmpty()); |
113 | 112 | } |
... | ... | @@ -117,13 +116,13 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { |
117 | 116 | OAuth2ClientRegistration first = validClientRegistration(); |
118 | 117 | OAuth2ClientRegistration second = validClientRegistration(); |
119 | 118 | |
120 | - List<OAuth2ClientsDomainParams> savedFirstDomainsParams = oAuth2Service.saveDomainsParams( | |
121 | - OAuth2Utils.toDomainsParams(Collections.singletonList(first))); | |
122 | - List<OAuth2ClientsDomainParams> savedSecondDomainsParams = oAuth2Service.saveDomainsParams( | |
123 | - OAuth2Utils.toDomainsParams(Collections.singletonList(second))); | |
119 | + OAuth2ClientsParams savedFirstOAuth2Params = oAuth2Service.saveOAuth2Params( | |
120 | + OAuth2Utils.toOAuth2Params(Collections.singletonList(first))); | |
121 | + OAuth2ClientsParams savedSecondOAuth2Params = oAuth2Service.saveOAuth2Params( | |
122 | + OAuth2Utils.toOAuth2Params(Collections.singletonList(second))); | |
124 | 123 | |
125 | - OAuth2ClientRegistration savedFirstRegistration = toClientRegistrations(savedFirstDomainsParams).get(0); | |
126 | - OAuth2ClientRegistration savedSecondRegistration = toClientRegistrations(savedSecondDomainsParams).get(0); | |
124 | + OAuth2ClientRegistration savedFirstRegistration = toClientRegistrations(savedFirstOAuth2Params).get(0); | |
125 | + OAuth2ClientRegistration savedSecondRegistration = toClientRegistrations(savedSecondOAuth2Params).get(0); | |
127 | 126 | |
128 | 127 | oAuth2Service.deleteClientRegistrationById(savedFirstRegistration.getId()); |
129 | 128 | List<OAuth2ClientRegistration> foundRegistrations = oAuth2Service.findAllClientRegistrations(); |
... | ... | @@ -133,24 +132,24 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { |
133 | 132 | |
134 | 133 | @Test |
135 | 134 | public void testDeleteDomainOAuth2ClientRegistrations() { |
136 | - oAuth2Service.saveDomainsParams(OAuth2Utils.toDomainsParams(Arrays.asList( | |
135 | + oAuth2Service.saveOAuth2Params(OAuth2Utils.toOAuth2Params(Arrays.asList( | |
137 | 136 | validClientRegistration("domain1"), |
138 | 137 | validClientRegistration("domain1"), |
139 | 138 | validClientRegistration("domain2") |
140 | 139 | ))); |
141 | - oAuth2Service.saveDomainsParams(OAuth2Utils.toDomainsParams(Arrays.asList( | |
140 | + oAuth2Service.saveOAuth2Params(OAuth2Utils.toOAuth2Params(Arrays.asList( | |
142 | 141 | validClientRegistration("domain2") |
143 | 142 | ))); |
144 | 143 | Assert.assertEquals(4, oAuth2Service.findAllClientRegistrations().size()); |
145 | - List<OAuth2ClientsDomainParams> domainsParams = oAuth2Service.findDomainsParams(); | |
146 | - List<OAuth2ClientRegistration> clientRegistrations = toClientRegistrations(domainsParams); | |
147 | - Assert.assertEquals(2, domainsParams.size()); | |
144 | + OAuth2ClientsParams oAuth2Params = oAuth2Service.findOAuth2Params(); | |
145 | + List<OAuth2ClientRegistration> clientRegistrations = toClientRegistrations(oAuth2Params); | |
146 | + Assert.assertEquals(2, oAuth2Params.getOAuth2DomainDtos().size()); | |
148 | 147 | Assert.assertEquals(4, clientRegistrations.size()); |
149 | 148 | |
150 | 149 | oAuth2Service.deleteClientRegistrationsByDomain("domain1"); |
151 | 150 | Assert.assertEquals(2, oAuth2Service.findAllClientRegistrations().size()); |
152 | - Assert.assertEquals(1, oAuth2Service.findDomainsParams().size()); | |
153 | - Assert.assertEquals(2, toClientRegistrations(oAuth2Service.findDomainsParams()).size()); | |
151 | + Assert.assertEquals(1, oAuth2Service.findOAuth2Params().getOAuth2DomainDtos().size()); | |
152 | + Assert.assertEquals(2, toClientRegistrations(oAuth2Service.findOAuth2Params()).size()); | |
154 | 153 | } |
155 | 154 | |
156 | 155 | private OAuth2ClientRegistration validClientRegistration() { | ... | ... |