Search in sources :

Example 6 with OAuth2Info

use of org.thingsboard.server.common.data.oauth2.OAuth2Info in project thingsboard by thingsboard.

the class BaseOAuth2ServiceTest method testFindRegistrationById.

@Test
public void testFindRegistrationById() {
    OAuth2Info oAuth2Info = new OAuth2Info(true, Lists.newArrayList(OAuth2ParamsInfo.builder().domainInfos(Lists.newArrayList(OAuth2DomainInfo.builder().name("first-domain").scheme(SchemeType.HTTP).build(), OAuth2DomainInfo.builder().name("second-domain").scheme(SchemeType.MIXED).build(), OAuth2DomainInfo.builder().name("third-domain").scheme(SchemeType.HTTPS).build())).clientRegistrations(Lists.newArrayList(validRegistrationInfo(), validRegistrationInfo(), validRegistrationInfo())).build(), OAuth2ParamsInfo.builder().domainInfos(Lists.newArrayList(OAuth2DomainInfo.builder().name("second-domain").scheme(SchemeType.HTTP).build(), OAuth2DomainInfo.builder().name("fourth-domain").scheme(SchemeType.MIXED).build())).clientRegistrations(Lists.newArrayList(validRegistrationInfo(), validRegistrationInfo())).build(), OAuth2ParamsInfo.builder().domainInfos(Lists.newArrayList(OAuth2DomainInfo.builder().name("second-domain").scheme(SchemeType.HTTPS).build(), OAuth2DomainInfo.builder().name("fifth-domain").scheme(SchemeType.HTTP).build())).clientRegistrations(Lists.newArrayList(validRegistrationInfo())).build()));
    oAuth2Service.saveOAuth2Info(oAuth2Info);
    List<OAuth2Registration> foundRegistrations = oAuth2Service.findAllRegistrations();
    foundRegistrations.forEach(registration -> {
        OAuth2Registration foundRegistration = oAuth2Service.findRegistration(registration.getUuidId());
        Assert.assertEquals(registration, foundRegistration);
    });
}
Also used : OAuth2Info(org.thingsboard.server.common.data.oauth2.OAuth2Info) OAuth2Registration(org.thingsboard.server.common.data.oauth2.OAuth2Registration) Test(org.junit.Test)

Example 7 with OAuth2Info

use of org.thingsboard.server.common.data.oauth2.OAuth2Info in project thingsboard by thingsboard.

the class BaseOAuth2ServiceTest method testCreateAndFindParams.

@Test
public void testCreateAndFindParams() {
    OAuth2Info oAuth2Info = createDefaultOAuth2Info();
    oAuth2Service.saveOAuth2Info(oAuth2Info);
    OAuth2Info foundOAuth2Info = oAuth2Service.findOAuth2Info();
    Assert.assertNotNull(foundOAuth2Info);
    // TODO ask if it's safe to check equality on AdditionalProperties
    Assert.assertEquals(oAuth2Info, foundOAuth2Info);
}
Also used : OAuth2Info(org.thingsboard.server.common.data.oauth2.OAuth2Info) Test(org.junit.Test)

Example 8 with OAuth2Info

use of org.thingsboard.server.common.data.oauth2.OAuth2Info in project thingsboard by thingsboard.

the class OAuth2ServiceImpl method saveOAuth2Info.

@Override
@Transactional
public void saveOAuth2Info(OAuth2Info oauth2Info) {
    log.trace("Executing saveOAuth2Info [{}]", oauth2Info);
    oauth2InfoValidator.accept(oauth2Info);
    oauth2ParamsDao.deleteAll();
    oauth2Info.getOauth2ParamsInfos().forEach(oauth2ParamsInfo -> {
        OAuth2Params oauth2Params = OAuth2Utils.infoToOAuth2Params(oauth2Info);
        OAuth2Params savedOauth2Params = oauth2ParamsDao.save(TenantId.SYS_TENANT_ID, oauth2Params);
        oauth2ParamsInfo.getClientRegistrations().forEach(registrationInfo -> {
            OAuth2Registration registration = OAuth2Utils.toOAuth2Registration(savedOauth2Params.getId(), registrationInfo);
            oauth2RegistrationDao.save(TenantId.SYS_TENANT_ID, registration);
        });
        oauth2ParamsInfo.getDomainInfos().forEach(domainInfo -> {
            OAuth2Domain domain = OAuth2Utils.toOAuth2Domain(savedOauth2Params.getId(), domainInfo);
            oauth2DomainDao.save(TenantId.SYS_TENANT_ID, domain);
        });
        if (oauth2ParamsInfo.getMobileInfos() != null) {
            oauth2ParamsInfo.getMobileInfos().forEach(mobileInfo -> {
                OAuth2Mobile mobile = OAuth2Utils.toOAuth2Mobile(savedOauth2Params.getId(), mobileInfo);
                oauth2MobileDao.save(TenantId.SYS_TENANT_ID, mobile);
            });
        }
    });
}
Also used : OAuth2Params(org.thingsboard.server.common.data.oauth2.OAuth2Params) OAuth2Registration(org.thingsboard.server.common.data.oauth2.OAuth2Registration) OAuth2Mobile(org.thingsboard.server.common.data.oauth2.OAuth2Mobile) OAuth2Domain(org.thingsboard.server.common.data.oauth2.OAuth2Domain) Transactional(javax.transaction.Transactional)

Example 9 with OAuth2Info

use of org.thingsboard.server.common.data.oauth2.OAuth2Info in project thingsboard by thingsboard.

the class BaseOAuth2ServiceTest method testGetOAuth2Clients.

@Test
public void testGetOAuth2Clients() {
    List<OAuth2RegistrationInfo> firstGroup = Lists.newArrayList(validRegistrationInfo(), validRegistrationInfo(), validRegistrationInfo(), validRegistrationInfo());
    List<OAuth2RegistrationInfo> secondGroup = Lists.newArrayList(validRegistrationInfo(), validRegistrationInfo());
    List<OAuth2RegistrationInfo> thirdGroup = Lists.newArrayList(validRegistrationInfo());
    OAuth2Info oAuth2Info = new OAuth2Info(true, Lists.newArrayList(OAuth2ParamsInfo.builder().domainInfos(Lists.newArrayList(OAuth2DomainInfo.builder().name("first-domain").scheme(SchemeType.HTTP).build(), OAuth2DomainInfo.builder().name("second-domain").scheme(SchemeType.MIXED).build(), OAuth2DomainInfo.builder().name("third-domain").scheme(SchemeType.HTTPS).build())).mobileInfos(Collections.emptyList()).clientRegistrations(firstGroup).build(), OAuth2ParamsInfo.builder().domainInfos(Lists.newArrayList(OAuth2DomainInfo.builder().name("second-domain").scheme(SchemeType.HTTP).build(), OAuth2DomainInfo.builder().name("fourth-domain").scheme(SchemeType.MIXED).build())).mobileInfos(Collections.emptyList()).clientRegistrations(secondGroup).build(), OAuth2ParamsInfo.builder().domainInfos(Lists.newArrayList(OAuth2DomainInfo.builder().name("second-domain").scheme(SchemeType.HTTPS).build(), OAuth2DomainInfo.builder().name("fifth-domain").scheme(SchemeType.HTTP).build())).mobileInfos(Collections.emptyList()).clientRegistrations(thirdGroup).build()));
    oAuth2Service.saveOAuth2Info(oAuth2Info);
    OAuth2Info foundOAuth2Info = oAuth2Service.findOAuth2Info();
    Assert.assertNotNull(foundOAuth2Info);
    Assert.assertEquals(oAuth2Info, foundOAuth2Info);
    List<OAuth2ClientInfo> firstGroupClientInfos = firstGroup.stream().map(registrationInfo -> new OAuth2ClientInfo(registrationInfo.getLoginButtonLabel(), registrationInfo.getLoginButtonIcon(), null)).collect(Collectors.toList());
    List<OAuth2ClientInfo> secondGroupClientInfos = secondGroup.stream().map(registrationInfo -> new OAuth2ClientInfo(registrationInfo.getLoginButtonLabel(), registrationInfo.getLoginButtonIcon(), null)).collect(Collectors.toList());
    List<OAuth2ClientInfo> thirdGroupClientInfos = thirdGroup.stream().map(registrationInfo -> new OAuth2ClientInfo(registrationInfo.getLoginButtonLabel(), registrationInfo.getLoginButtonIcon(), null)).collect(Collectors.toList());
    List<OAuth2ClientInfo> nonExistentDomainClients = oAuth2Service.getOAuth2Clients("http", "non-existent-domain", null, null);
    Assert.assertTrue(nonExistentDomainClients.isEmpty());
    List<OAuth2ClientInfo> firstDomainHttpClients = oAuth2Service.getOAuth2Clients("http", "first-domain", null, null);
    Assert.assertEquals(firstGroupClientInfos.size(), firstDomainHttpClients.size());
    firstGroupClientInfos.forEach(firstGroupClientInfo -> {
        Assert.assertTrue(firstDomainHttpClients.stream().anyMatch(clientInfo -> clientInfo.getIcon().equals(firstGroupClientInfo.getIcon()) && clientInfo.getName().equals(firstGroupClientInfo.getName())));
    });
    List<OAuth2ClientInfo> firstDomainHttpsClients = oAuth2Service.getOAuth2Clients("https", "first-domain", null, null);
    Assert.assertTrue(firstDomainHttpsClients.isEmpty());
    List<OAuth2ClientInfo> fourthDomainHttpClients = oAuth2Service.getOAuth2Clients("http", "fourth-domain", null, null);
    Assert.assertEquals(secondGroupClientInfos.size(), fourthDomainHttpClients.size());
    secondGroupClientInfos.forEach(secondGroupClientInfo -> {
        Assert.assertTrue(fourthDomainHttpClients.stream().anyMatch(clientInfo -> clientInfo.getIcon().equals(secondGroupClientInfo.getIcon()) && clientInfo.getName().equals(secondGroupClientInfo.getName())));
    });
    List<OAuth2ClientInfo> fourthDomainHttpsClients = oAuth2Service.getOAuth2Clients("https", "fourth-domain", null, null);
    Assert.assertEquals(secondGroupClientInfos.size(), fourthDomainHttpsClients.size());
    secondGroupClientInfos.forEach(secondGroupClientInfo -> {
        Assert.assertTrue(fourthDomainHttpsClients.stream().anyMatch(clientInfo -> clientInfo.getIcon().equals(secondGroupClientInfo.getIcon()) && clientInfo.getName().equals(secondGroupClientInfo.getName())));
    });
    List<OAuth2ClientInfo> secondDomainHttpClients = oAuth2Service.getOAuth2Clients("http", "second-domain", null, null);
    Assert.assertEquals(firstGroupClientInfos.size() + secondGroupClientInfos.size(), secondDomainHttpClients.size());
    firstGroupClientInfos.forEach(firstGroupClientInfo -> {
        Assert.assertTrue(secondDomainHttpClients.stream().anyMatch(clientInfo -> clientInfo.getIcon().equals(firstGroupClientInfo.getIcon()) && clientInfo.getName().equals(firstGroupClientInfo.getName())));
    });
    secondGroupClientInfos.forEach(secondGroupClientInfo -> {
        Assert.assertTrue(secondDomainHttpClients.stream().anyMatch(clientInfo -> clientInfo.getIcon().equals(secondGroupClientInfo.getIcon()) && clientInfo.getName().equals(secondGroupClientInfo.getName())));
    });
    List<OAuth2ClientInfo> secondDomainHttpsClients = oAuth2Service.getOAuth2Clients("https", "second-domain", null, null);
    Assert.assertEquals(firstGroupClientInfos.size() + thirdGroupClientInfos.size(), secondDomainHttpsClients.size());
    firstGroupClientInfos.forEach(firstGroupClientInfo -> {
        Assert.assertTrue(secondDomainHttpsClients.stream().anyMatch(clientInfo -> clientInfo.getIcon().equals(firstGroupClientInfo.getIcon()) && clientInfo.getName().equals(firstGroupClientInfo.getName())));
    });
    thirdGroupClientInfos.forEach(thirdGroupClientInfo -> {
        Assert.assertTrue(secondDomainHttpsClients.stream().anyMatch(clientInfo -> clientInfo.getIcon().equals(thirdGroupClientInfo.getIcon()) && clientInfo.getName().equals(thirdGroupClientInfo.getName())));
    });
}
Also used : Arrays(java.util.Arrays) SchemeType(org.thingsboard.server.common.data.oauth2.SchemeType) Autowired(org.springframework.beans.factory.annotation.Autowired) Lists(com.google.common.collect.Lists) After(org.junit.After) OAuth2Service(org.thingsboard.server.dao.oauth2.OAuth2Service) PlatformType(org.thingsboard.server.common.data.oauth2.PlatformType) OAuth2ParamsInfo(org.thingsboard.server.common.data.oauth2.OAuth2ParamsInfo) Before(org.junit.Before) OAuth2ClientInfo(org.thingsboard.server.common.data.oauth2.OAuth2ClientInfo) OAuth2CustomMapperConfig(org.thingsboard.server.common.data.oauth2.OAuth2CustomMapperConfig) Test(org.junit.Test) MapperType(org.thingsboard.server.common.data.oauth2.MapperType) OAuth2DomainInfo(org.thingsboard.server.common.data.oauth2.OAuth2DomainInfo) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) OAuth2Info(org.thingsboard.server.common.data.oauth2.OAuth2Info) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) List(java.util.List) OAuth2MobileInfo(org.thingsboard.server.common.data.oauth2.OAuth2MobileInfo) OAuth2MapperConfig(org.thingsboard.server.common.data.oauth2.OAuth2MapperConfig) RandomStringUtils(org.apache.commons.lang3.RandomStringUtils) Assert(org.junit.Assert) OAuth2Registration(org.thingsboard.server.common.data.oauth2.OAuth2Registration) Collections(java.util.Collections) OAuth2RegistrationInfo(org.thingsboard.server.common.data.oauth2.OAuth2RegistrationInfo) OAuth2Info(org.thingsboard.server.common.data.oauth2.OAuth2Info) OAuth2ClientInfo(org.thingsboard.server.common.data.oauth2.OAuth2ClientInfo) OAuth2RegistrationInfo(org.thingsboard.server.common.data.oauth2.OAuth2RegistrationInfo) Test(org.junit.Test)

Example 10 with OAuth2Info

use of org.thingsboard.server.common.data.oauth2.OAuth2Info in project thingsboard by thingsboard.

the class BaseOAuth2ServiceTest method testSaveHttpAndMixedDomainsTogether.

@Test(expected = DataValidationException.class)
public void testSaveHttpAndMixedDomainsTogether() {
    OAuth2Info oAuth2Info = new OAuth2Info(true, Lists.newArrayList(OAuth2ParamsInfo.builder().domainInfos(Lists.newArrayList(OAuth2DomainInfo.builder().name("first-domain").scheme(SchemeType.HTTP).build(), OAuth2DomainInfo.builder().name("first-domain").scheme(SchemeType.MIXED).build(), OAuth2DomainInfo.builder().name("third-domain").scheme(SchemeType.HTTPS).build())).clientRegistrations(Lists.newArrayList(validRegistrationInfo(), validRegistrationInfo(), validRegistrationInfo())).build()));
    oAuth2Service.saveOAuth2Info(oAuth2Info);
}
Also used : OAuth2Info(org.thingsboard.server.common.data.oauth2.OAuth2Info) Test(org.junit.Test)

Aggregations

OAuth2Info (org.thingsboard.server.common.data.oauth2.OAuth2Info)14 Test (org.junit.Test)13 OAuth2ClientInfo (org.thingsboard.server.common.data.oauth2.OAuth2ClientInfo)7 OAuth2Registration (org.thingsboard.server.common.data.oauth2.OAuth2Registration)7 Arrays (java.util.Arrays)5 List (java.util.List)5 UUID (java.util.UUID)5 Collectors (java.util.stream.Collectors)5 Autowired (org.springframework.beans.factory.annotation.Autowired)5 MapperType (org.thingsboard.server.common.data.oauth2.MapperType)5 OAuth2CustomMapperConfig (org.thingsboard.server.common.data.oauth2.OAuth2CustomMapperConfig)5 OAuth2DomainInfo (org.thingsboard.server.common.data.oauth2.OAuth2DomainInfo)5 OAuth2MapperConfig (org.thingsboard.server.common.data.oauth2.OAuth2MapperConfig)5 OAuth2MobileInfo (org.thingsboard.server.common.data.oauth2.OAuth2MobileInfo)5 OAuth2ParamsInfo (org.thingsboard.server.common.data.oauth2.OAuth2ParamsInfo)5 OAuth2RegistrationInfo (org.thingsboard.server.common.data.oauth2.OAuth2RegistrationInfo)5 PlatformType (org.thingsboard.server.common.data.oauth2.PlatformType)5 SchemeType (org.thingsboard.server.common.data.oauth2.SchemeType)5 DataValidationException (org.thingsboard.server.dao.exception.DataValidationException)5 Lists (com.google.common.collect.Lists)4