Search in sources :

Example 1 with OAuth2Registration

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

the class OAuth2RegistrationEntity method toData.

@Override
public OAuth2Registration toData() {
    OAuth2Registration registration = new OAuth2Registration();
    registration.setId(new OAuth2RegistrationId(id));
    registration.setCreatedTime(createdTime);
    registration.setOauth2ParamsId(new OAuth2ParamsId(oauth2ParamsId));
    registration.setAdditionalInfo(additionalInfo);
    registration.setMapperConfig(OAuth2MapperConfig.builder().allowUserCreation(allowUserCreation).activateUser(activateUser).type(type).basic((type == MapperType.BASIC || type == MapperType.GITHUB || type == MapperType.APPLE) ? OAuth2BasicMapperConfig.builder().emailAttributeKey(emailAttributeKey).firstNameAttributeKey(firstNameAttributeKey).lastNameAttributeKey(lastNameAttributeKey).tenantNameStrategy(tenantNameStrategy).tenantNamePattern(tenantNamePattern).customerNamePattern(customerNamePattern).defaultDashboardName(defaultDashboardName).alwaysFullScreen(alwaysFullScreen).build() : null).custom(type == MapperType.CUSTOM ? OAuth2CustomMapperConfig.builder().url(url).username(username).password(password).sendToken(sendToken).build() : null).build());
    registration.setClientId(clientId);
    registration.setClientSecret(clientSecret);
    registration.setAuthorizationUri(authorizationUri);
    registration.setAccessTokenUri(tokenUri);
    registration.setScope(Arrays.asList(scope.split(",")));
    registration.setPlatforms(StringUtils.isNotEmpty(platforms) ? Arrays.stream(platforms.split(",")).map(str -> PlatformType.valueOf(str)).collect(Collectors.toList()) : Collections.emptyList());
    registration.setUserInfoUri(userInfoUri);
    registration.setUserNameAttributeName(userNameAttributeName);
    registration.setJwkSetUri(jwkSetUri);
    registration.setClientAuthenticationMethod(clientAuthenticationMethod);
    registration.setLoginButtonLabel(loginButtonLabel);
    registration.setLoginButtonIcon(loginButtonIcon);
    return registration;
}
Also used : JsonStringType(org.thingsboard.server.dao.util.mapping.JsonStringType) Arrays(java.util.Arrays) BaseSqlEntity(org.thingsboard.server.dao.model.BaseSqlEntity) OAuth2RegistrationId(org.thingsboard.server.common.data.id.OAuth2RegistrationId) OAuth2BasicMapperConfig(org.thingsboard.server.common.data.oauth2.OAuth2BasicMapperConfig) EnumType(javax.persistence.EnumType) Table(javax.persistence.Table) Enumerated(javax.persistence.Enumerated) JsonNode(com.fasterxml.jackson.databind.JsonNode) PlatformType(org.thingsboard.server.common.data.oauth2.PlatformType) StringUtils(io.micrometer.core.instrument.util.StringUtils) TenantNameStrategyType(org.thingsboard.server.common.data.oauth2.TenantNameStrategyType) Entity(javax.persistence.Entity) OAuth2CustomMapperConfig(org.thingsboard.server.common.data.oauth2.OAuth2CustomMapperConfig) MapperType(org.thingsboard.server.common.data.oauth2.MapperType) UUID(java.util.UUID) EqualsAndHashCode(lombok.EqualsAndHashCode) Type(org.hibernate.annotations.Type) Collectors(java.util.stream.Collectors) OAuth2ParamsId(org.thingsboard.server.common.data.id.OAuth2ParamsId) Column(javax.persistence.Column) OAuth2MapperConfig(org.thingsboard.server.common.data.oauth2.OAuth2MapperConfig) Data(lombok.Data) TypeDef(org.hibernate.annotations.TypeDef) OAuth2Registration(org.thingsboard.server.common.data.oauth2.OAuth2Registration) Collections(java.util.Collections) ModelConstants(org.thingsboard.server.dao.model.ModelConstants) OAuth2ParamsId(org.thingsboard.server.common.data.id.OAuth2ParamsId) OAuth2RegistrationId(org.thingsboard.server.common.data.id.OAuth2RegistrationId) OAuth2Registration(org.thingsboard.server.common.data.oauth2.OAuth2Registration)

Example 2 with OAuth2Registration

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

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

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

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

the class AppleOAuth2ClientMapper method getOrCreateUserByClientPrincipal.

@Override
public SecurityUser getOrCreateUserByClientPrincipal(HttpServletRequest request, OAuth2AuthenticationToken token, String providerAccessToken, OAuth2Registration registration) {
    OAuth2MapperConfig config = registration.getMapperConfig();
    Map<String, Object> attributes = updateAttributesFromRequestParams(request, token.getPrincipal().getAttributes());
    String email = BasicMapperUtils.getStringAttributeByKey(attributes, config.getBasic().getEmailAttributeKey());
    OAuth2User oauth2User = BasicMapperUtils.getOAuth2User(email, attributes, config);
    return getOrCreateSecurityUserFromOAuth2User(oauth2User, registration);
}
Also used : OAuth2User(org.thingsboard.server.dao.oauth2.OAuth2User) OAuth2MapperConfig(org.thingsboard.server.common.data.oauth2.OAuth2MapperConfig)

Aggregations

OAuth2MapperConfig (org.thingsboard.server.common.data.oauth2.OAuth2MapperConfig)8 OAuth2Registration (org.thingsboard.server.common.data.oauth2.OAuth2Registration)6 OAuth2User (org.thingsboard.server.dao.oauth2.OAuth2User)5 Arrays (java.util.Arrays)3 UUID (java.util.UUID)3 Collectors (java.util.stream.Collectors)3 MapperType (org.thingsboard.server.common.data.oauth2.MapperType)3 OAuth2CustomMapperConfig (org.thingsboard.server.common.data.oauth2.OAuth2CustomMapperConfig)3 OAuth2Info (org.thingsboard.server.common.data.oauth2.OAuth2Info)3 PlatformType (org.thingsboard.server.common.data.oauth2.PlatformType)3 IOException (java.io.IOException)2 Collections (java.util.Collections)2 List (java.util.List)2 Transactional (javax.transaction.Transactional)2 Test (org.junit.Test)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 OAuth2BasicMapperConfig (org.thingsboard.server.common.data.oauth2.OAuth2BasicMapperConfig)2 OAuth2ClientInfo (org.thingsboard.server.common.data.oauth2.OAuth2ClientInfo)2 OAuth2DomainInfo (org.thingsboard.server.common.data.oauth2.OAuth2DomainInfo)2 OAuth2MobileInfo (org.thingsboard.server.common.data.oauth2.OAuth2MobileInfo)2