From d5abe337be44763efa85129a47da3f0f9edcfc60 Mon Sep 17 00:00:00 2001 From: vzikratyi Date: Fri, 19 Jun 2020 16:04:10 +0300 Subject: [PATCH] Added OAuth2ClientRegistrationService --- .../OAuth2ClientRegistrationService.java | 30 ++++ .../OAuth2ClientRegistrationServiceImpl.java | 151 ++++++++++++++++++ 2 files changed, 181 insertions(+) create mode 100644 common/dao-api/src/main/java/org/thingsboard/server/dao/oauth2/OAuth2ClientRegistrationService.java create mode 100644 dao/src/main/java/org/thingsboard/server/dao/oauth2/OAuth2ClientRegistrationServiceImpl.java diff --git a/common/dao-api/src/main/java/org/thingsboard/server/dao/oauth2/OAuth2ClientRegistrationService.java b/common/dao-api/src/main/java/org/thingsboard/server/dao/oauth2/OAuth2ClientRegistrationService.java new file mode 100644 index 0000000000..a38fb78eb9 --- /dev/null +++ b/common/dao-api/src/main/java/org/thingsboard/server/dao/oauth2/OAuth2ClientRegistrationService.java @@ -0,0 +1,30 @@ +/** + * Copyright © 2016-2020 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.dao.oauth2; + +import org.thingsboard.server.common.data.oauth2.OAuth2ClientRegistration; + +import java.util.List; + +public interface OAuth2ClientRegistrationService { + OAuth2ClientRegistration saveClientRegistration(OAuth2ClientRegistration clientRegistration); + + List findClientRegistrations(); + + OAuth2ClientRegistration findClientRegistrationsByRegistrationId(String registrationId); + + void deleteClientRegistrationsByRegistrationId(String registrationId); +} diff --git a/dao/src/main/java/org/thingsboard/server/dao/oauth2/OAuth2ClientRegistrationServiceImpl.java b/dao/src/main/java/org/thingsboard/server/dao/oauth2/OAuth2ClientRegistrationServiceImpl.java new file mode 100644 index 0000000000..699f7acd81 --- /dev/null +++ b/dao/src/main/java/org/thingsboard/server/dao/oauth2/OAuth2ClientRegistrationServiceImpl.java @@ -0,0 +1,151 @@ +/** + * Copyright © 2016-2020 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.dao.oauth2; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.util.StringUtils; +import org.thingsboard.server.common.data.Customer; +import org.thingsboard.server.common.data.Tenant; +import org.thingsboard.server.common.data.id.CustomerId; +import org.thingsboard.server.common.data.oauth2.*; +import org.thingsboard.server.dao.exception.DataValidationException; + +import java.util.List; +import java.util.function.Consumer; + +import static org.thingsboard.server.dao.model.ModelConstants.NULL_UUID; +import static org.thingsboard.server.dao.service.Validator.validateId; +import static org.thingsboard.server.dao.service.Validator.validateString; + +@Slf4j +@Service +public class OAuth2ClientRegistrationServiceImpl implements OAuth2ClientRegistrationService { + public static final String INCORRECT_REGISTRATION_ID = "Incorrect registrationId "; + + @Autowired + private OAuth2ClientRegistrationDao clientRegistrationDao; + + @Override + public OAuth2ClientRegistration saveClientRegistration(OAuth2ClientRegistration clientRegistration) { + log.trace("Executing saveClientRegistration [{}]", clientRegistration); + return null; + } + + @Override + public List findClientRegistrations() { + log.trace("Executing findClientRegistrations []"); + return clientRegistrationDao.find(); + } + + @Override + public OAuth2ClientRegistration findClientRegistrationsByRegistrationId(String registrationId) { + log.trace("Executing findClientRegistrationsByRegistrationId [{}]", registrationId); + validateString(registrationId, INCORRECT_REGISTRATION_ID + registrationId); + return clientRegistrationDao.findByRegistrationId(registrationId); + } + + @Override + public void deleteClientRegistrationsByRegistrationId(String registrationId) { + log.trace("Executing deleteClientRegistrationsByRegistrationId [{}]", registrationId); + validateString(registrationId, INCORRECT_REGISTRATION_ID + registrationId); + clientRegistrationDao.removeByRegistrationId(registrationId); + } + + private Consumer validator = clientRegistration -> { + if (StringUtils.isEmpty(clientRegistration.getRegistrationId())) { + throw new DataValidationException("Registration ID should be specified!"); + } + if (StringUtils.isEmpty(clientRegistration.getClientId())) { + throw new DataValidationException("Client ID should be specified!"); + } + if (StringUtils.isEmpty(clientRegistration.getClientSecret())) { + throw new DataValidationException("Client secret should be specified!"); + } + if (StringUtils.isEmpty(clientRegistration.getAuthorizationUri())) { + throw new DataValidationException("Authorization uri should be specified!"); + } + if (StringUtils.isEmpty(clientRegistration.getTokenUri())) { + throw new DataValidationException("Token uri should be specified!"); + } + if (StringUtils.isEmpty(clientRegistration.getRedirectUriTemplate())) { + throw new DataValidationException("Redirect uri template should be specified!"); + } + if (StringUtils.isEmpty(clientRegistration.getScope())) { + throw new DataValidationException("Scope should be specified!"); + } + if (StringUtils.isEmpty(clientRegistration.getAuthorizationGrantType())) { + throw new DataValidationException("Authorization grant type should be specified!"); + } + if (StringUtils.isEmpty(clientRegistration.getUserInfoUri())) { + throw new DataValidationException("User info uri should be specified!"); + } + if (StringUtils.isEmpty(clientRegistration.getUserNameAttribute())) { + throw new DataValidationException("User name attribute should be specified!"); + } + if (StringUtils.isEmpty(clientRegistration.getJwkSetUri())) { + throw new DataValidationException("Jwk set uri should be specified!"); + } + if (StringUtils.isEmpty(clientRegistration.getClientAuthenticationMethod())) { + throw new DataValidationException("Client authentication method should be specified!"); + } + if (StringUtils.isEmpty(clientRegistration.getClientName())) { + throw new DataValidationException("Client name should be specified!"); + } + if (StringUtils.isEmpty(clientRegistration.getLoginButtonLabel())) { + throw new DataValidationException("Login button label should be specified!"); + } + OAuth2MapperConfig mapperConfig = clientRegistration.getMapperConfig(); + if (mapperConfig == null) { + throw new DataValidationException("Mapper config should be specified!"); + } + if (mapperConfig.getType() == null) { + throw new DataValidationException("Mapper config type should be specified!"); + } + if (mapperConfig.getType() == MapperType.BASIC) { + OAuth2BasicMapperConfig basicConfig = mapperConfig.getBasicConfig(); + if (basicConfig == null) { + throw new DataValidationException("Basic config should be specified!"); + } + if (StringUtils.isEmpty(basicConfig.getEmailAttributeKey())) { + throw new DataValidationException("Email attribute key should be specified!"); + } + if (basicConfig.getTenantNameStrategy() == null) { + throw new DataValidationException("Tenant name strategy should be specified!"); + } + if (basicConfig.getTenantNameStrategy() == TenantNameStrategyType.CUSTOM + && StringUtils.isEmpty(basicConfig.getTenantNamePattern())) { + throw new DataValidationException("Tenant name pattern should be specified!"); + } + } + if (mapperConfig.getType() == MapperType.CUSTOM) { + OAuth2CustomMapperConfig customConfig = mapperConfig.getCustomConfig(); + if (customConfig == null) { + throw new DataValidationException("Custom config should be specified!"); + } + if (StringUtils.isEmpty(customConfig.getUrl())) { + throw new DataValidationException("Custom mapper URL should be specified!"); + } + if (StringUtils.isEmpty(customConfig.getUsername())) { + throw new DataValidationException("Custom mapper username should be specified!"); + } + if (StringUtils.isEmpty(customConfig.getPassword())) { + throw new DataValidationException("Custom mapper password should be specified!"); + } + } + }; +}