Search in sources :

Example 1 with OAuth2ClientInfo

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

the class OAuth2Controller method getOAuth2Clients.

@ApiOperation(value = "Get OAuth2 clients (getOAuth2Clients)", notes = "Get the list of OAuth2 clients " + "to log in with, available for such domain scheme (HTTP or HTTPS) (if x-forwarded-proto request header is present - " + "the scheme is known from it) and domain name and port (port may be known from x-forwarded-port header)")
@RequestMapping(value = "/noauth/oauth2Clients", method = RequestMethod.POST)
@ResponseBody
public List<OAuth2ClientInfo> getOAuth2Clients(HttpServletRequest request, @ApiParam(value = "Mobile application package name, to find OAuth2 clients " + "where there is configured mobile application with such package name") @RequestParam(required = false) String pkgName, @ApiParam(value = "Platform type to search OAuth2 clients for which " + "the usage with this platform type is allowed in the settings. " + "If platform type is not one of allowable values - it will just be ignored", allowableValues = "WEB, ANDROID, IOS") @RequestParam(required = false) String platform) throws ThingsboardException {
    try {
        if (log.isDebugEnabled()) {
            log.debug("Executing getOAuth2Clients: [{}][{}][{}]", request.getScheme(), request.getServerName(), request.getServerPort());
            Enumeration<String> headerNames = request.getHeaderNames();
            while (headerNames.hasMoreElements()) {
                String header = headerNames.nextElement();
                log.debug("Header: {} {}", header, request.getHeader(header));
            }
        }
        PlatformType platformType = null;
        if (StringUtils.isNotEmpty(platform)) {
            try {
                platformType = PlatformType.valueOf(platform);
            } catch (Exception e) {
            }
        }
        return oAuth2Service.getOAuth2Clients(MiscUtils.getScheme(request), MiscUtils.getDomainNameAndPort(request), pkgName, platformType);
    } catch (Exception e) {
        throw handleException(e);
    }
}
Also used : PlatformType(org.thingsboard.server.common.data.oauth2.PlatformType) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 2 with OAuth2ClientInfo

use of org.thingsboard.server.common.data.oauth2.OAuth2ClientInfo 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 3 with OAuth2ClientInfo

use of org.thingsboard.server.common.data.oauth2.OAuth2ClientInfo 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)

Example 4 with OAuth2ClientInfo

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

the class OAuth2ServiceImpl method getOAuth2Clients.

@Override
public List<OAuth2ClientInfo> getOAuth2Clients(String domainSchemeStr, String domainName, String pkgName, PlatformType platformType) {
    log.trace("Executing getOAuth2Clients [{}://{}] pkgName=[{}] platformType=[{}]", domainSchemeStr, domainName, pkgName, platformType);
    if (domainSchemeStr == null) {
        throw new IncorrectParameterException(INCORRECT_DOMAIN_SCHEME);
    }
    SchemeType domainScheme;
    try {
        domainScheme = SchemeType.valueOf(domainSchemeStr.toUpperCase());
    } catch (IllegalArgumentException e) {
        throw new IncorrectParameterException(INCORRECT_DOMAIN_SCHEME);
    }
    validateString(domainName, INCORRECT_DOMAIN_NAME + domainName);
    return oauth2RegistrationDao.findEnabledByDomainSchemesDomainNameAndPkgNameAndPlatformType(Arrays.asList(domainScheme, SchemeType.MIXED), domainName, pkgName, platformType).stream().map(OAuth2Utils::toClientInfo).collect(Collectors.toList());
}
Also used : SchemeType(org.thingsboard.server.common.data.oauth2.SchemeType) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException)

Example 5 with OAuth2ClientInfo

use of org.thingsboard.server.common.data.oauth2.OAuth2ClientInfo 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)

Aggregations

Test (org.junit.Test)5 OAuth2ClientInfo (org.thingsboard.server.common.data.oauth2.OAuth2ClientInfo)5 OAuth2Info (org.thingsboard.server.common.data.oauth2.OAuth2Info)5 PlatformType (org.thingsboard.server.common.data.oauth2.PlatformType)4 SchemeType (org.thingsboard.server.common.data.oauth2.SchemeType)4 Lists (com.google.common.collect.Lists)3 Arrays (java.util.Arrays)3 Collections (java.util.Collections)3 List (java.util.List)3 UUID (java.util.UUID)3 Collectors (java.util.stream.Collectors)3 RandomStringUtils (org.apache.commons.lang3.RandomStringUtils)3 After (org.junit.After)3 Assert (org.junit.Assert)3 Before (org.junit.Before)3 Autowired (org.springframework.beans.factory.annotation.Autowired)3 MapperType (org.thingsboard.server.common.data.oauth2.MapperType)3 OAuth2CustomMapperConfig (org.thingsboard.server.common.data.oauth2.OAuth2CustomMapperConfig)3 OAuth2DomainInfo (org.thingsboard.server.common.data.oauth2.OAuth2DomainInfo)3 OAuth2MapperConfig (org.thingsboard.server.common.data.oauth2.OAuth2MapperConfig)3