Commit 91f05282ea51b355cb2c04948919600cffb73f87
1 parent
9c5b353a
Validate domains for SchemeTypes combinations
Showing
2 changed files
with
102 additions
and
6 deletions
@@ -119,10 +119,17 @@ public class OAuth2ServiceImpl extends AbstractEntityService implements OAuth2Se | @@ -119,10 +119,17 @@ public class OAuth2ServiceImpl extends AbstractEntityService implements OAuth2Se | ||
119 | if (StringUtils.isEmpty(domainInfo.getName())) { | 119 | if (StringUtils.isEmpty(domainInfo.getName())) { |
120 | throw new DataValidationException("Domain name should be specified!"); | 120 | throw new DataValidationException("Domain name should be specified!"); |
121 | } | 121 | } |
122 | - if (StringUtils.isEmpty(domainInfo.getScheme())) { | 122 | + if (domainInfo.getScheme() == null) { |
123 | throw new DataValidationException("Domain scheme should be specified!"); | 123 | throw new DataValidationException("Domain scheme should be specified!"); |
124 | } | 124 | } |
125 | } | 125 | } |
126 | + domainParams.getDomainInfos().stream() | ||
127 | + .collect(Collectors.groupingBy(DomainInfo::getName)) | ||
128 | + .forEach((domainName, domainInfos) -> { | ||
129 | + if (domainInfos.size() > 1 && domainInfos.stream().anyMatch(domainInfo -> domainInfo.getScheme() == SchemeType.MIXED)) { | ||
130 | + throw new DataValidationException("MIXED scheme type shouldn't be combined with another scheme type!"); | ||
131 | + } | ||
132 | + }); | ||
126 | if (domainParams.getClientRegistrations() == null || domainParams.getClientRegistrations().isEmpty()) { | 133 | if (domainParams.getClientRegistrations() == null || domainParams.getClientRegistrations().isEmpty()) { |
127 | throw new DataValidationException("Client registrations should be specified!"); | 134 | throw new DataValidationException("Client registrations should be specified!"); |
128 | } | 135 | } |
@@ -22,6 +22,7 @@ import org.junit.Before; | @@ -22,6 +22,7 @@ import org.junit.Before; | ||
22 | import org.junit.Test; | 22 | import org.junit.Test; |
23 | import org.springframework.beans.factory.annotation.Autowired; | 23 | import org.springframework.beans.factory.annotation.Autowired; |
24 | import org.thingsboard.server.common.data.oauth2.*; | 24 | import org.thingsboard.server.common.data.oauth2.*; |
25 | +import org.thingsboard.server.dao.exception.DataValidationException; | ||
25 | import org.thingsboard.server.dao.oauth2.OAuth2Service; | 26 | import org.thingsboard.server.dao.oauth2.OAuth2Service; |
26 | 27 | ||
27 | import java.util.*; | 28 | import java.util.*; |
@@ -45,6 +46,44 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { | @@ -45,6 +46,44 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { | ||
45 | Assert.assertTrue(oAuth2Service.findOAuth2Params().getDomainsParams().isEmpty()); | 46 | Assert.assertTrue(oAuth2Service.findOAuth2Params().getDomainsParams().isEmpty()); |
46 | } | 47 | } |
47 | 48 | ||
49 | + @Test(expected = DataValidationException.class) | ||
50 | + public void testSaveHttpAndMixedDomainsTogether() { | ||
51 | + OAuth2ClientsParams clientsParams = new OAuth2ClientsParams(true, Sets.newHashSet( | ||
52 | + OAuth2ClientsDomainParams.builder() | ||
53 | + .domainInfos(Sets.newHashSet( | ||
54 | + DomainInfo.builder().name("first-domain").scheme(SchemeType.HTTP).build(), | ||
55 | + DomainInfo.builder().name("first-domain").scheme(SchemeType.MIXED).build(), | ||
56 | + DomainInfo.builder().name("third-domain").scheme(SchemeType.HTTPS).build() | ||
57 | + )) | ||
58 | + .clientRegistrations(Sets.newHashSet( | ||
59 | + validClientRegistrationDto(), | ||
60 | + validClientRegistrationDto(), | ||
61 | + validClientRegistrationDto() | ||
62 | + )) | ||
63 | + .build() | ||
64 | + )); | ||
65 | + oAuth2Service.saveOAuth2Params(clientsParams); | ||
66 | + } | ||
67 | + | ||
68 | + @Test(expected = DataValidationException.class) | ||
69 | + public void testSaveHttpsAndMixedDomainsTogether() { | ||
70 | + OAuth2ClientsParams clientsParams = new OAuth2ClientsParams(true, Sets.newHashSet( | ||
71 | + OAuth2ClientsDomainParams.builder() | ||
72 | + .domainInfos(Sets.newHashSet( | ||
73 | + DomainInfo.builder().name("first-domain").scheme(SchemeType.HTTPS).build(), | ||
74 | + DomainInfo.builder().name("first-domain").scheme(SchemeType.MIXED).build(), | ||
75 | + DomainInfo.builder().name("third-domain").scheme(SchemeType.HTTPS).build() | ||
76 | + )) | ||
77 | + .clientRegistrations(Sets.newHashSet( | ||
78 | + validClientRegistrationDto(), | ||
79 | + validClientRegistrationDto(), | ||
80 | + validClientRegistrationDto() | ||
81 | + )) | ||
82 | + .build() | ||
83 | + )); | ||
84 | + oAuth2Service.saveOAuth2Params(clientsParams); | ||
85 | + } | ||
86 | + | ||
48 | @Test | 87 | @Test |
49 | public void testCreateAndFindParams() { | 88 | public void testCreateAndFindParams() { |
50 | OAuth2ClientsParams clientsParams = createDefaultClientsParams(); | 89 | OAuth2ClientsParams clientsParams = createDefaultClientsParams(); |
@@ -178,7 +217,7 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { | @@ -178,7 +217,7 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { | ||
178 | Assert.assertTrue(nonExistentDomainClients.isEmpty()); | 217 | Assert.assertTrue(nonExistentDomainClients.isEmpty()); |
179 | 218 | ||
180 | List<OAuth2ClientInfo> firstDomainHttpClients = oAuth2Service.getOAuth2Clients("http", "first-domain"); | 219 | List<OAuth2ClientInfo> firstDomainHttpClients = oAuth2Service.getOAuth2Clients("http", "first-domain"); |
181 | - Assert.assertEquals(firstDomainHttpClients.size(), firstDomainHttpClients.size()); | 220 | + Assert.assertEquals(firstGroupClientInfos.size(), firstDomainHttpClients.size()); |
182 | firstGroupClientInfos.forEach(firstGroupClientInfo -> { | 221 | firstGroupClientInfos.forEach(firstGroupClientInfo -> { |
183 | Assert.assertTrue( | 222 | Assert.assertTrue( |
184 | firstDomainHttpClients.stream().anyMatch(clientInfo -> | 223 | firstDomainHttpClients.stream().anyMatch(clientInfo -> |
@@ -191,7 +230,7 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { | @@ -191,7 +230,7 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { | ||
191 | Assert.assertTrue(firstDomainHttpsClients.isEmpty()); | 230 | Assert.assertTrue(firstDomainHttpsClients.isEmpty()); |
192 | 231 | ||
193 | List<OAuth2ClientInfo> fourthDomainHttpClients = oAuth2Service.getOAuth2Clients("http", "fourth-domain"); | 232 | List<OAuth2ClientInfo> fourthDomainHttpClients = oAuth2Service.getOAuth2Clients("http", "fourth-domain"); |
194 | - Assert.assertEquals(fourthDomainHttpClients.size(), secondGroupClientInfos.size()); | 233 | + Assert.assertEquals(secondGroupClientInfos.size(), fourthDomainHttpClients.size()); |
195 | secondGroupClientInfos.forEach(secondGroupClientInfo -> { | 234 | secondGroupClientInfos.forEach(secondGroupClientInfo -> { |
196 | Assert.assertTrue( | 235 | Assert.assertTrue( |
197 | fourthDomainHttpClients.stream().anyMatch(clientInfo -> | 236 | fourthDomainHttpClients.stream().anyMatch(clientInfo -> |
@@ -200,7 +239,7 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { | @@ -200,7 +239,7 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { | ||
200 | ); | 239 | ); |
201 | }); | 240 | }); |
202 | List<OAuth2ClientInfo> fourthDomainHttpsClients = oAuth2Service.getOAuth2Clients("https", "fourth-domain"); | 241 | List<OAuth2ClientInfo> fourthDomainHttpsClients = oAuth2Service.getOAuth2Clients("https", "fourth-domain"); |
203 | - Assert.assertEquals(fourthDomainHttpsClients.size(), secondGroupClientInfos.size()); | 242 | + Assert.assertEquals(secondGroupClientInfos.size(), fourthDomainHttpsClients.size()); |
204 | secondGroupClientInfos.forEach(secondGroupClientInfo -> { | 243 | secondGroupClientInfos.forEach(secondGroupClientInfo -> { |
205 | Assert.assertTrue( | 244 | Assert.assertTrue( |
206 | fourthDomainHttpsClients.stream().anyMatch(clientInfo -> | 245 | fourthDomainHttpsClients.stream().anyMatch(clientInfo -> |
@@ -210,7 +249,7 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { | @@ -210,7 +249,7 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { | ||
210 | }); | 249 | }); |
211 | 250 | ||
212 | List<OAuth2ClientInfo> secondDomainHttpClients = oAuth2Service.getOAuth2Clients("http", "second-domain"); | 251 | List<OAuth2ClientInfo> secondDomainHttpClients = oAuth2Service.getOAuth2Clients("http", "second-domain"); |
213 | - Assert.assertEquals(secondDomainHttpClients.size(), firstGroupClientInfos.size() + secondGroupClientInfos.size()); | 252 | + Assert.assertEquals(firstGroupClientInfos.size() + secondGroupClientInfos.size(), secondDomainHttpClients.size()); |
214 | firstGroupClientInfos.forEach(firstGroupClientInfo -> { | 253 | firstGroupClientInfos.forEach(firstGroupClientInfo -> { |
215 | Assert.assertTrue( | 254 | Assert.assertTrue( |
216 | secondDomainHttpClients.stream().anyMatch(clientInfo -> | 255 | secondDomainHttpClients.stream().anyMatch(clientInfo -> |
@@ -227,7 +266,7 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { | @@ -227,7 +266,7 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { | ||
227 | }); | 266 | }); |
228 | 267 | ||
229 | List<OAuth2ClientInfo> secondDomainHttpsClients = oAuth2Service.getOAuth2Clients("https", "second-domain"); | 268 | List<OAuth2ClientInfo> secondDomainHttpsClients = oAuth2Service.getOAuth2Clients("https", "second-domain"); |
230 | - Assert.assertEquals(secondDomainHttpsClients.size(), firstGroupClientInfos.size() + thirdGroupClientInfos.size()); | 269 | + Assert.assertEquals(firstGroupClientInfos.size() + thirdGroupClientInfos.size(), secondDomainHttpsClients.size()); |
231 | firstGroupClientInfos.forEach(firstGroupClientInfo -> { | 270 | firstGroupClientInfos.forEach(firstGroupClientInfo -> { |
232 | Assert.assertTrue( | 271 | Assert.assertTrue( |
233 | secondDomainHttpsClients.stream().anyMatch(clientInfo -> | 272 | secondDomainHttpsClients.stream().anyMatch(clientInfo -> |
@@ -245,6 +284,56 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { | @@ -245,6 +284,56 @@ public class BaseOAuth2ServiceTest extends AbstractServiceTest { | ||
245 | } | 284 | } |
246 | 285 | ||
247 | @Test | 286 | @Test |
287 | + public void testGetOAuth2ClientsForHttpAndHttps() { | ||
288 | + Set<ClientRegistrationDto> firstGroup = Sets.newHashSet( | ||
289 | + validClientRegistrationDto(), | ||
290 | + validClientRegistrationDto(), | ||
291 | + validClientRegistrationDto(), | ||
292 | + validClientRegistrationDto() | ||
293 | + ); | ||
294 | + OAuth2ClientsParams clientsParams = new OAuth2ClientsParams(true, Sets.newHashSet( | ||
295 | + OAuth2ClientsDomainParams.builder() | ||
296 | + .domainInfos(Sets.newHashSet( | ||
297 | + DomainInfo.builder().name("first-domain").scheme(SchemeType.HTTP).build(), | ||
298 | + DomainInfo.builder().name("second-domain").scheme(SchemeType.MIXED).build(), | ||
299 | + DomainInfo.builder().name("first-domain").scheme(SchemeType.HTTPS).build() | ||
300 | + )) | ||
301 | + .clientRegistrations(firstGroup) | ||
302 | + .build() | ||
303 | + )); | ||
304 | + | ||
305 | + oAuth2Service.saveOAuth2Params(clientsParams); | ||
306 | + OAuth2ClientsParams foundClientsParams = oAuth2Service.findOAuth2Params(); | ||
307 | + Assert.assertNotNull(foundClientsParams); | ||
308 | + Assert.assertEquals(clientsParams, foundClientsParams); | ||
309 | + | ||
310 | + List<OAuth2ClientInfo> firstGroupClientInfos = firstGroup.stream() | ||
311 | + .map(clientRegistrationDto -> new OAuth2ClientInfo( | ||
312 | + clientRegistrationDto.getLoginButtonLabel(), clientRegistrationDto.getLoginButtonIcon(), null)) | ||
313 | + .collect(Collectors.toList()); | ||
314 | + | ||
315 | + List<OAuth2ClientInfo> firstDomainHttpClients = oAuth2Service.getOAuth2Clients("http", "first-domain"); | ||
316 | + Assert.assertEquals(firstGroupClientInfos.size(), firstDomainHttpClients.size()); | ||
317 | + firstGroupClientInfos.forEach(firstGroupClientInfo -> { | ||
318 | + Assert.assertTrue( | ||
319 | + firstDomainHttpClients.stream().anyMatch(clientInfo -> | ||
320 | + clientInfo.getIcon().equals(firstGroupClientInfo.getIcon()) | ||
321 | + && clientInfo.getName().equals(firstGroupClientInfo.getName())) | ||
322 | + ); | ||
323 | + }); | ||
324 | + | ||
325 | + List<OAuth2ClientInfo> firstDomainHttpsClients = oAuth2Service.getOAuth2Clients("https", "first-domain"); | ||
326 | + Assert.assertEquals(firstGroupClientInfos.size(), firstDomainHttpsClients.size()); | ||
327 | + firstGroupClientInfos.forEach(firstGroupClientInfo -> { | ||
328 | + Assert.assertTrue( | ||
329 | + firstDomainHttpsClients.stream().anyMatch(clientInfo -> | ||
330 | + clientInfo.getIcon().equals(firstGroupClientInfo.getIcon()) | ||
331 | + && clientInfo.getName().equals(firstGroupClientInfo.getName())) | ||
332 | + ); | ||
333 | + }); | ||
334 | + } | ||
335 | + | ||
336 | + @Test | ||
248 | public void testGetDisabledOAuth2Clients() { | 337 | public void testGetDisabledOAuth2Clients() { |
249 | OAuth2ClientsParams clientsParams = new OAuth2ClientsParams(true, Sets.newHashSet( | 338 | OAuth2ClientsParams clientsParams = new OAuth2ClientsParams(true, Sets.newHashSet( |
250 | OAuth2ClientsDomainParams.builder() | 339 | OAuth2ClientsDomainParams.builder() |