Search in sources :

Example 1 with OAuth2Info

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

the class OAuth2ServiceImpl method findOAuth2Info.

@Override
public OAuth2Info findOAuth2Info() {
    log.trace("Executing findOAuth2Info");
    OAuth2Info oauth2Info = new OAuth2Info();
    List<OAuth2Params> oauth2ParamsList = oauth2ParamsDao.find(TenantId.SYS_TENANT_ID);
    oauth2Info.setEnabled(oauth2ParamsList.stream().anyMatch(param -> param.isEnabled()));
    List<OAuth2ParamsInfo> oauth2ParamsInfos = new ArrayList<>();
    oauth2Info.setOauth2ParamsInfos(oauth2ParamsInfos);
    oauth2ParamsList.stream().sorted(Comparator.comparing(BaseData::getUuidId)).forEach(oauth2Params -> {
        List<OAuth2Registration> registrations = oauth2RegistrationDao.findByOAuth2ParamsId(oauth2Params.getId().getId());
        List<OAuth2Domain> domains = oauth2DomainDao.findByOAuth2ParamsId(oauth2Params.getId().getId());
        List<OAuth2Mobile> mobiles = oauth2MobileDao.findByOAuth2ParamsId(oauth2Params.getId().getId());
        oauth2ParamsInfos.add(OAuth2Utils.toOAuth2ParamsInfo(registrations, domains, mobiles));
    });
    return oauth2Info;
}
Also used : Arrays(java.util.Arrays) BaseData(org.thingsboard.server.common.data.BaseData) OAuth2Params(org.thingsboard.server.common.data.oauth2.OAuth2Params) SchemeType(org.thingsboard.server.common.data.oauth2.SchemeType) Autowired(org.springframework.beans.factory.annotation.Autowired) OAuth2Mobile(org.thingsboard.server.common.data.oauth2.OAuth2Mobile) TenantId(org.thingsboard.server.common.data.id.TenantId) ArrayList(java.util.ArrayList) OAuth2BasicMapperConfig(org.thingsboard.server.common.data.oauth2.OAuth2BasicMapperConfig) Validator.validateId(org.thingsboard.server.dao.service.Validator.validateId) Validator.validateString(org.thingsboard.server.dao.service.Validator.validateString) CollectionUtils(org.apache.commons.collections.CollectionUtils) Service(org.springframework.stereotype.Service) PlatformType(org.thingsboard.server.common.data.oauth2.PlatformType) OAuth2ParamsInfo(org.thingsboard.server.common.data.oauth2.OAuth2ParamsInfo) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) TenantNameStrategyType(org.thingsboard.server.common.data.oauth2.TenantNameStrategyType) Transactional(javax.transaction.Transactional) OAuth2ClientInfo(org.thingsboard.server.common.data.oauth2.OAuth2ClientInfo) OAuth2CustomMapperConfig(org.thingsboard.server.common.data.oauth2.OAuth2CustomMapperConfig) 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) Consumer(java.util.function.Consumer) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) Slf4j(lombok.extern.slf4j.Slf4j) AbstractEntityService(org.thingsboard.server.dao.entity.AbstractEntityService) List(java.util.List) OAuth2MobileInfo(org.thingsboard.server.common.data.oauth2.OAuth2MobileInfo) OAuth2MapperConfig(org.thingsboard.server.common.data.oauth2.OAuth2MapperConfig) OAuth2Registration(org.thingsboard.server.common.data.oauth2.OAuth2Registration) Comparator(java.util.Comparator) OAuth2Domain(org.thingsboard.server.common.data.oauth2.OAuth2Domain) StringUtils(org.springframework.util.StringUtils) OAuth2RegistrationInfo(org.thingsboard.server.common.data.oauth2.OAuth2RegistrationInfo) OAuth2Info(org.thingsboard.server.common.data.oauth2.OAuth2Info) OAuth2ParamsInfo(org.thingsboard.server.common.data.oauth2.OAuth2ParamsInfo) ArrayList(java.util.ArrayList) OAuth2Registration(org.thingsboard.server.common.data.oauth2.OAuth2Registration) OAuth2Mobile(org.thingsboard.server.common.data.oauth2.OAuth2Mobile) BaseData(org.thingsboard.server.common.data.BaseData) OAuth2Params(org.thingsboard.server.common.data.oauth2.OAuth2Params) OAuth2Domain(org.thingsboard.server.common.data.oauth2.OAuth2Domain)

Example 2 with OAuth2Info

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

the class BaseOAuth2ServiceTest method testFindAllRegistrations.

@Test
public void testFindAllRegistrations() {
    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();
    Assert.assertEquals(6, foundRegistrations.size());
    oAuth2Info.getOauth2ParamsInfos().stream().flatMap(paramsInfo -> paramsInfo.getClientRegistrations().stream()).forEach(registrationInfo -> Assert.assertTrue(foundRegistrations.stream().anyMatch(registration -> registration.getClientId().equals(registrationInfo.getClientId()))));
}
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) OAuth2Registration(org.thingsboard.server.common.data.oauth2.OAuth2Registration) Test(org.junit.Test)

Example 3 with OAuth2Info

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

the class BaseOAuth2ServiceTest method testFindClientsByPackageAndPlatform.

@Test
public void testFindClientsByPackageAndPlatform() {
    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(Lists.newArrayList(validMobileInfo("com.test.pkg1", "testPkg1Callback"), validMobileInfo("com.test.pkg2", "testPkg2Callback"))).clientRegistrations(Lists.newArrayList(validRegistrationInfo("Google", Arrays.asList(PlatformType.WEB, PlatformType.ANDROID)), validRegistrationInfo("Facebook", Arrays.asList(PlatformType.IOS)), validRegistrationInfo("GitHub", Collections.emptyList()))).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(Lists.newArrayList(validRegistrationInfo(), validRegistrationInfo())).build()));
    oAuth2Service.saveOAuth2Info(oAuth2Info);
    OAuth2Info foundOAuth2Info = oAuth2Service.findOAuth2Info();
    Assert.assertEquals(oAuth2Info, foundOAuth2Info);
    List<OAuth2ClientInfo> firstDomainHttpClients = oAuth2Service.getOAuth2Clients("http", "first-domain", null, null);
    Assert.assertEquals(3, firstDomainHttpClients.size());
    List<OAuth2ClientInfo> pkg1Clients = oAuth2Service.getOAuth2Clients("http", "first-domain", "com.test.pkg1", null);
    Assert.assertEquals(3, pkg1Clients.size());
    List<OAuth2ClientInfo> pkg1AndroidClients = oAuth2Service.getOAuth2Clients("http", "first-domain", "com.test.pkg1", PlatformType.ANDROID);
    Assert.assertEquals(2, pkg1AndroidClients.size());
    Assert.assertTrue(pkg1AndroidClients.stream().anyMatch(client -> client.getName().equals("Google")));
    Assert.assertTrue(pkg1AndroidClients.stream().anyMatch(client -> client.getName().equals("GitHub")));
    List<OAuth2ClientInfo> pkg1IOSClients = oAuth2Service.getOAuth2Clients("http", "first-domain", "com.test.pkg1", PlatformType.IOS);
    Assert.assertEquals(2, pkg1IOSClients.size());
    Assert.assertTrue(pkg1IOSClients.stream().anyMatch(client -> client.getName().equals("Facebook")));
    Assert.assertTrue(pkg1IOSClients.stream().anyMatch(client -> client.getName().equals("GitHub")));
}
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) Test(org.junit.Test)

Example 4 with OAuth2Info

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

the class BaseOAuth2ServiceTest method testClearDomainParams.

@Test
public void testClearDomainParams() {
    OAuth2Info oAuth2Info = createDefaultOAuth2Info();
    oAuth2Service.saveOAuth2Info(oAuth2Info);
    OAuth2Info foundOAuth2Info = oAuth2Service.findOAuth2Info();
    Assert.assertNotNull(foundOAuth2Info);
    Assert.assertEquals(oAuth2Info, foundOAuth2Info);
    oAuth2Service.saveOAuth2Info(EMPTY_PARAMS);
    OAuth2Info foundAfterClearClientsParams = oAuth2Service.findOAuth2Info();
    Assert.assertNotNull(foundAfterClearClientsParams);
    Assert.assertEquals(EMPTY_PARAMS, foundAfterClearClientsParams);
}
Also used : OAuth2Info(org.thingsboard.server.common.data.oauth2.OAuth2Info) Test(org.junit.Test)

Example 5 with OAuth2Info

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

the class BaseOAuth2ServiceTest method testGetOAuth2ClientsForHttpAndHttps.

@Test
public void testGetOAuth2ClientsForHttpAndHttps() {
    List<OAuth2RegistrationInfo> firstGroup = Lists.newArrayList(validRegistrationInfo(), validRegistrationInfo(), validRegistrationInfo(), 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("first-domain").scheme(SchemeType.HTTPS).build())).mobileInfos(Collections.emptyList()).clientRegistrations(firstGroup).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> 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.assertEquals(firstGroupClientInfos.size(), firstDomainHttpsClients.size());
    firstGroupClientInfos.forEach(firstGroupClientInfo -> {
        Assert.assertTrue(firstDomainHttpsClients.stream().anyMatch(clientInfo -> clientInfo.getIcon().equals(firstGroupClientInfo.getIcon()) && clientInfo.getName().equals(firstGroupClientInfo.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)

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