Store 2FA account config in UserAuthSettings table
This commit is contained in:
		
							parent
							
								
									922436d38b
								
							
						
					
					
						commit
						190430ffc4
					
				@ -36,10 +36,10 @@ import org.thingsboard.server.common.data.exception.ThingsboardException;
 | 
			
		||||
import org.thingsboard.server.queue.util.TbCoreComponent;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.TwoFactorAuthService;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.config.TwoFactorAuthConfigManager;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.config.TwoFactorAuthSettings;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.config.account.TotpTwoFactorAuthAccountConfig;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.config.account.TwoFactorAuthAccountConfig;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.provider.TwoFactorAuthProviderType;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.TwoFactorAuthSettings;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.account.TotpTwoFactorAuthAccountConfig;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.account.TwoFactorAuthAccountConfig;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.provider.TwoFactorAuthProviderType;
 | 
			
		||||
import org.thingsboard.server.service.security.model.SecurityUser;
 | 
			
		||||
 | 
			
		||||
import javax.servlet.ServletOutputStream;
 | 
			
		||||
 | 
			
		||||
@ -31,8 +31,8 @@ import org.thingsboard.server.dao.user.UserService;
 | 
			
		||||
import org.thingsboard.server.queue.util.TbCoreComponent;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.TwoFactorAuthService;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.config.TwoFactorAuthConfigManager;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.config.account.TwoFactorAuthAccountConfig;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.provider.TwoFactorAuthProviderType;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.account.TwoFactorAuthAccountConfig;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.provider.TwoFactorAuthProviderType;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.rest.RestAuthenticationDetails;
 | 
			
		||||
import org.thingsboard.server.service.security.model.JwtTokenPair;
 | 
			
		||||
import org.thingsboard.server.service.security.model.SecurityUser;
 | 
			
		||||
 | 
			
		||||
@ -25,15 +25,15 @@ import org.thingsboard.server.common.data.exception.ThingsboardErrorCode;
 | 
			
		||||
import org.thingsboard.server.common.data.exception.ThingsboardException;
 | 
			
		||||
import org.thingsboard.server.common.data.id.TenantId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.UserId;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.provider.TwoFactorAuthProviderType;
 | 
			
		||||
import org.thingsboard.server.common.msg.tools.TbRateLimits;
 | 
			
		||||
import org.thingsboard.server.dao.user.UserService;
 | 
			
		||||
import org.thingsboard.server.queue.util.TbCoreComponent;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.config.TwoFactorAuthConfigManager;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.config.TwoFactorAuthSettings;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.config.account.TwoFactorAuthAccountConfig;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.config.provider.TwoFactorAuthProviderConfig;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.TwoFactorAuthSettings;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.account.TwoFactorAuthAccountConfig;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.provider.TwoFactorAuthProviderConfig;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.provider.TwoFactorAuthProvider;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.provider.TwoFactorAuthProviderType;
 | 
			
		||||
import org.thingsboard.server.service.security.model.SecurityUser;
 | 
			
		||||
import org.thingsboard.server.service.security.system.SystemSecurityService;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -17,8 +17,8 @@ package org.thingsboard.server.service.security.auth.mfa;
 | 
			
		||||
 | 
			
		||||
import org.thingsboard.server.common.data.User;
 | 
			
		||||
import org.thingsboard.server.common.data.exception.ThingsboardException;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.config.account.TwoFactorAuthAccountConfig;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.provider.TwoFactorAuthProviderType;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.account.TwoFactorAuthAccountConfig;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.provider.TwoFactorAuthProviderType;
 | 
			
		||||
import org.thingsboard.server.service.security.model.SecurityUser;
 | 
			
		||||
 | 
			
		||||
public interface TwoFactorAuthService {
 | 
			
		||||
 | 
			
		||||
@ -15,8 +15,6 @@
 | 
			
		||||
 */
 | 
			
		||||
package org.thingsboard.server.service.security.auth.mfa.config;
 | 
			
		||||
 | 
			
		||||
import com.fasterxml.jackson.databind.JsonNode;
 | 
			
		||||
import com.fasterxml.jackson.databind.node.ObjectNode;
 | 
			
		||||
import lombok.RequiredArgsConstructor;
 | 
			
		||||
import lombok.SneakyThrows;
 | 
			
		||||
import org.springframework.stereotype.Service;
 | 
			
		||||
@ -29,31 +27,30 @@ import org.thingsboard.server.common.data.id.TenantId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.UserId;
 | 
			
		||||
import org.thingsboard.server.common.data.kv.BaseAttributeKvEntry;
 | 
			
		||||
import org.thingsboard.server.common.data.kv.JsonDataEntry;
 | 
			
		||||
import org.thingsboard.server.common.data.security.UserCredentials;
 | 
			
		||||
import org.thingsboard.server.common.data.security.UserAuthSettings;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.TwoFactorAuthSettings;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.account.TwoFactorAuthAccountConfig;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.provider.TwoFactorAuthProviderConfig;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.provider.TwoFactorAuthProviderType;
 | 
			
		||||
import org.thingsboard.server.dao.attributes.AttributesService;
 | 
			
		||||
import org.thingsboard.server.dao.service.ConstraintValidator;
 | 
			
		||||
import org.thingsboard.server.dao.settings.AdminSettingsDao;
 | 
			
		||||
import org.thingsboard.server.dao.settings.AdminSettingsService;
 | 
			
		||||
import org.thingsboard.server.dao.user.UserService;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.config.account.TwoFactorAuthAccountConfig;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.config.provider.TwoFactorAuthProviderConfig;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.provider.TwoFactorAuthProviderType;
 | 
			
		||||
import org.thingsboard.server.dao.user.UserAuthSettingsDao;
 | 
			
		||||
 | 
			
		||||
import java.util.Collections;
 | 
			
		||||
import java.util.Optional;
 | 
			
		||||
import java.util.concurrent.ExecutionException;
 | 
			
		||||
import java.util.function.Consumer;
 | 
			
		||||
 | 
			
		||||
@Service
 | 
			
		||||
@RequiredArgsConstructor
 | 
			
		||||
public class DefaultTwoFactorAuthConfigManager implements TwoFactorAuthConfigManager {
 | 
			
		||||
 | 
			
		||||
    private final UserService userService;
 | 
			
		||||
    private final UserAuthSettingsDao userAuthSettingsDao;
 | 
			
		||||
    private final AdminSettingsService adminSettingsService;
 | 
			
		||||
    private final AdminSettingsDao adminSettingsDao;
 | 
			
		||||
    private final AttributesService attributesService;
 | 
			
		||||
 | 
			
		||||
    protected static final String TWO_FACTOR_AUTH_ACCOUNT_CONFIG_KEY = "twoFaConfig";
 | 
			
		||||
    protected static final String TWO_FACTOR_AUTH_SETTINGS_KEY = "twoFaSettings";
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -64,12 +61,9 @@ public class DefaultTwoFactorAuthConfigManager implements TwoFactorAuthConfigMan
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public Optional<TwoFactorAuthAccountConfig> getTwoFaAccountConfig(TenantId tenantId, UserId userId) {
 | 
			
		||||
        return Optional.ofNullable(getAccountInfo(tenantId, userId).get(TWO_FACTOR_AUTH_ACCOUNT_CONFIG_KEY))
 | 
			
		||||
                .filter(JsonNode::isObject)
 | 
			
		||||
                .map(jsonNode -> JacksonUtil.treeToValue(jsonNode, TwoFactorAuthAccountConfig.class))
 | 
			
		||||
                .filter(twoFactorAuthAccountConfig -> {
 | 
			
		||||
                    return getTwoFaProviderConfig(tenantId, twoFactorAuthAccountConfig.getProviderType()).isPresent();
 | 
			
		||||
                });
 | 
			
		||||
        return Optional.ofNullable(userAuthSettingsDao.findByUserId(userId))
 | 
			
		||||
                .flatMap(userAuthSettings -> Optional.ofNullable(userAuthSettings.getTwoFaAccountConfig()))
 | 
			
		||||
                .filter(twoFaAccountConfig -> getTwoFaProviderConfig(tenantId, twoFaAccountConfig.getProviderType()).isPresent());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
@ -77,33 +71,23 @@ public class DefaultTwoFactorAuthConfigManager implements TwoFactorAuthConfigMan
 | 
			
		||||
        getTwoFaProviderConfig(tenantId, accountConfig.getProviderType())
 | 
			
		||||
                .orElseThrow(() -> new ThingsboardException("2FA provider is not configured", ThingsboardErrorCode.BAD_REQUEST_PARAMS));
 | 
			
		||||
 | 
			
		||||
        updateAccountInfo(tenantId, userId, accountInfo -> {
 | 
			
		||||
            accountInfo.set(TWO_FACTOR_AUTH_ACCOUNT_CONFIG_KEY, JacksonUtil.valueToTree(accountConfig));
 | 
			
		||||
        });
 | 
			
		||||
        UserAuthSettings userAuthSettings = Optional.ofNullable(userAuthSettingsDao.findByUserId(userId))
 | 
			
		||||
                .orElseGet(() -> {
 | 
			
		||||
                    UserAuthSettings newUserAuthSettings = new UserAuthSettings();
 | 
			
		||||
                    newUserAuthSettings.setUserId(userId);
 | 
			
		||||
                    return newUserAuthSettings;
 | 
			
		||||
                });
 | 
			
		||||
        userAuthSettings.setTwoFaAccountConfig(accountConfig);
 | 
			
		||||
        userAuthSettingsDao.save(tenantId, userAuthSettings);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void deleteTwoFaAccountConfig(TenantId tenantId, UserId userId) {
 | 
			
		||||
        updateAccountInfo(tenantId, userId, accountInfo -> {
 | 
			
		||||
            accountInfo.remove(TWO_FACTOR_AUTH_ACCOUNT_CONFIG_KEY);
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private ObjectNode getAccountInfo(TenantId tenantId, UserId userId) {
 | 
			
		||||
        return (ObjectNode) Optional.ofNullable(userService.findUserCredentialsByUserId(tenantId, userId).getAdditionalInfo())
 | 
			
		||||
                .filter(JsonNode::isObject)
 | 
			
		||||
                .orElseGet(JacksonUtil::newObjectNode);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // FIXME [viacheslav]: upgrade script for credentials' additional info
 | 
			
		||||
    private void updateAccountInfo(TenantId tenantId, UserId userId, Consumer<ObjectNode> updater) {
 | 
			
		||||
        UserCredentials credentials = userService.findUserCredentialsByUserId(tenantId, userId);
 | 
			
		||||
        ObjectNode additionalInfo = (ObjectNode) Optional.ofNullable(credentials.getAdditionalInfo())
 | 
			
		||||
                .filter(JsonNode::isObject)
 | 
			
		||||
                .orElseGet(JacksonUtil::newObjectNode);
 | 
			
		||||
        updater.accept(additionalInfo);
 | 
			
		||||
        credentials.setAdditionalInfo(additionalInfo);
 | 
			
		||||
        userService.saveUserCredentials(tenantId, credentials);
 | 
			
		||||
        Optional.ofNullable(userAuthSettingsDao.findByUserId(userId))
 | 
			
		||||
                .ifPresent(userAuthSettings -> {
 | 
			
		||||
                    userAuthSettings.setTwoFaAccountConfig(null);
 | 
			
		||||
                    userAuthSettingsDao.save(tenantId, userAuthSettings);
 | 
			
		||||
                });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -18,7 +18,8 @@ package org.thingsboard.server.service.security.auth.mfa.config;
 | 
			
		||||
import org.thingsboard.server.common.data.exception.ThingsboardException;
 | 
			
		||||
import org.thingsboard.server.common.data.id.TenantId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.UserId;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.config.account.TwoFactorAuthAccountConfig;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.TwoFactorAuthSettings;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.account.TwoFactorAuthAccountConfig;
 | 
			
		||||
 | 
			
		||||
import java.util.Optional;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -17,8 +17,9 @@ package org.thingsboard.server.service.security.auth.mfa.provider;
 | 
			
		||||
 | 
			
		||||
import org.thingsboard.server.common.data.User;
 | 
			
		||||
import org.thingsboard.server.common.data.exception.ThingsboardException;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.config.account.TwoFactorAuthAccountConfig;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.config.provider.TwoFactorAuthProviderConfig;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.account.TwoFactorAuthAccountConfig;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.provider.TwoFactorAuthProviderConfig;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.provider.TwoFactorAuthProviderType;
 | 
			
		||||
import org.thingsboard.server.service.security.model.SecurityUser;
 | 
			
		||||
 | 
			
		||||
public interface TwoFactorAuthProvider<C extends TwoFactorAuthProviderConfig, A extends TwoFactorAuthAccountConfig> {
 | 
			
		||||
 | 
			
		||||
@ -21,8 +21,8 @@ import org.springframework.cache.Cache;
 | 
			
		||||
import org.springframework.cache.CacheManager;
 | 
			
		||||
import org.thingsboard.server.common.data.CacheConstants;
 | 
			
		||||
import org.thingsboard.server.common.data.exception.ThingsboardException;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.config.account.OtpBasedTwoFactorAuthAccountConfig;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.config.provider.OtpBasedTwoFactorAuthProviderConfig;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.account.OtpBasedTwoFactorAuthAccountConfig;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.provider.OtpBasedTwoFactorAuthProviderConfig;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.provider.TwoFactorAuthProvider;
 | 
			
		||||
import org.thingsboard.server.service.security.model.SecurityUser;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -21,10 +21,10 @@ import org.thingsboard.rule.engine.api.SmsService;
 | 
			
		||||
import org.thingsboard.rule.engine.api.util.TbNodeUtils;
 | 
			
		||||
import org.thingsboard.server.common.data.User;
 | 
			
		||||
import org.thingsboard.server.common.data.exception.ThingsboardException;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.account.SmsTwoFactorAuthAccountConfig;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.provider.SmsTwoFactorAuthProviderConfig;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.provider.TwoFactorAuthProviderType;
 | 
			
		||||
import org.thingsboard.server.queue.util.TbCoreComponent;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.config.account.SmsTwoFactorAuthAccountConfig;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.config.provider.SmsTwoFactorAuthProviderConfig;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.provider.TwoFactorAuthProviderType;
 | 
			
		||||
import org.thingsboard.server.service.security.model.SecurityUser;
 | 
			
		||||
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
 | 
			
		||||
@ -24,11 +24,11 @@ import org.jboss.aerogear.security.otp.api.Base32;
 | 
			
		||||
import org.springframework.stereotype.Service;
 | 
			
		||||
import org.springframework.web.util.UriComponentsBuilder;
 | 
			
		||||
import org.thingsboard.server.common.data.User;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.provider.TwoFactorAuthProviderType;
 | 
			
		||||
import org.thingsboard.server.queue.util.TbCoreComponent;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.config.account.TotpTwoFactorAuthAccountConfig;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.config.provider.TotpTwoFactorAuthProviderConfig;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.account.TotpTwoFactorAuthAccountConfig;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.provider.TotpTwoFactorAuthProviderConfig;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.provider.TwoFactorAuthProvider;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.provider.TwoFactorAuthProviderType;
 | 
			
		||||
import org.thingsboard.server.service.security.model.SecurityUser;
 | 
			
		||||
 | 
			
		||||
@Service
 | 
			
		||||
 | 
			
		||||
@ -54,7 +54,7 @@ import org.thingsboard.server.dao.exception.DataValidationException;
 | 
			
		||||
import org.thingsboard.server.dao.settings.AdminSettingsService;
 | 
			
		||||
import org.thingsboard.server.dao.user.UserService;
 | 
			
		||||
import org.thingsboard.server.dao.user.UserServiceImpl;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.config.TwoFactorAuthSettings;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.TwoFactorAuthSettings;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.rest.RestAuthenticationDetails;
 | 
			
		||||
import org.thingsboard.server.service.security.exception.UserPasswordExpiredException;
 | 
			
		||||
import org.thingsboard.server.service.security.model.SecurityUser;
 | 
			
		||||
 | 
			
		||||
@ -23,7 +23,7 @@ import org.thingsboard.server.common.data.id.TenantId;
 | 
			
		||||
import org.thingsboard.server.common.data.security.UserCredentials;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.SecuritySettings;
 | 
			
		||||
import org.thingsboard.server.dao.exception.DataValidationException;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.config.TwoFactorAuthSettings;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.TwoFactorAuthSettings;
 | 
			
		||||
import org.thingsboard.server.service.security.model.SecurityUser;
 | 
			
		||||
 | 
			
		||||
import javax.servlet.http.HttpServletRequest;
 | 
			
		||||
 | 
			
		||||
@ -31,14 +31,14 @@ import org.thingsboard.rule.engine.api.SmsService;
 | 
			
		||||
import org.thingsboard.server.common.data.CacheConstants;
 | 
			
		||||
import org.thingsboard.server.common.data.id.TenantId;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.config.TwoFactorAuthConfigManager;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.config.TwoFactorAuthSettings;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.config.account.SmsTwoFactorAuthAccountConfig;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.config.account.TotpTwoFactorAuthAccountConfig;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.config.account.TwoFactorAuthAccountConfig;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.config.provider.SmsTwoFactorAuthProviderConfig;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.config.provider.TotpTwoFactorAuthProviderConfig;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.config.provider.TwoFactorAuthProviderConfig;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.provider.TwoFactorAuthProviderType;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.TwoFactorAuthSettings;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.account.SmsTwoFactorAuthAccountConfig;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.account.TotpTwoFactorAuthAccountConfig;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.account.TwoFactorAuthAccountConfig;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.provider.SmsTwoFactorAuthProviderConfig;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.provider.TotpTwoFactorAuthProviderConfig;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.provider.TwoFactorAuthProviderConfig;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.provider.TwoFactorAuthProviderType;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.provider.impl.OtpBasedTwoFactorAuthProvider;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.provider.impl.TotpTwoFactorAuthProvider;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -40,13 +40,13 @@ import org.thingsboard.server.dao.audit.AuditLogService;
 | 
			
		||||
import org.thingsboard.server.dao.user.UserService;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.TwoFactorAuthService;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.config.TwoFactorAuthConfigManager;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.config.TwoFactorAuthSettings;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.config.account.SmsTwoFactorAuthAccountConfig;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.config.account.TotpTwoFactorAuthAccountConfig;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.config.provider.SmsTwoFactorAuthProviderConfig;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.config.provider.TotpTwoFactorAuthProviderConfig;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.config.provider.TwoFactorAuthProviderConfig;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.provider.TwoFactorAuthProviderType;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.TwoFactorAuthSettings;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.account.SmsTwoFactorAuthAccountConfig;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.account.TotpTwoFactorAuthAccountConfig;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.provider.SmsTwoFactorAuthProviderConfig;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.provider.TotpTwoFactorAuthProviderConfig;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.provider.TwoFactorAuthProviderConfig;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.provider.TwoFactorAuthProviderType;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.rest.LoginRequest;
 | 
			
		||||
import org.thingsboard.server.service.security.model.JwtTokenPair;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -0,0 +1,26 @@
 | 
			
		||||
/**
 | 
			
		||||
 * Copyright © 2016-2022 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.common.data.id;
 | 
			
		||||
 | 
			
		||||
import java.util.UUID;
 | 
			
		||||
 | 
			
		||||
public class UserAuthSettingsId extends UUIDBased {
 | 
			
		||||
 | 
			
		||||
    public UserAuthSettingsId(UUID id) {
 | 
			
		||||
        super(id);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@ -0,0 +1,34 @@
 | 
			
		||||
/**
 | 
			
		||||
 * Copyright © 2016-2022 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.common.data.security;
 | 
			
		||||
 | 
			
		||||
import lombok.Data;
 | 
			
		||||
import lombok.EqualsAndHashCode;
 | 
			
		||||
import org.thingsboard.server.common.data.BaseData;
 | 
			
		||||
import org.thingsboard.server.common.data.id.UserAuthSettingsId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.UserId;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.account.TwoFactorAuthAccountConfig;
 | 
			
		||||
 | 
			
		||||
@Data
 | 
			
		||||
@EqualsAndHashCode(callSuper = true)
 | 
			
		||||
public class UserAuthSettings extends BaseData<UserAuthSettingsId> {
 | 
			
		||||
 | 
			
		||||
    private static final long serialVersionUID = 2628320657987010348L;
 | 
			
		||||
 | 
			
		||||
    private UserId userId;
 | 
			
		||||
    private TwoFactorAuthAccountConfig twoFaAccountConfig;
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@ -13,13 +13,13 @@
 | 
			
		||||
 * See the License for the specific language governing permissions and
 | 
			
		||||
 * limitations under the License.
 | 
			
		||||
 */
 | 
			
		||||
package org.thingsboard.server.service.security.auth.mfa.config;
 | 
			
		||||
package org.thingsboard.server.common.data.security.model.mfa;
 | 
			
		||||
 | 
			
		||||
import io.swagger.annotations.ApiModel;
 | 
			
		||||
import io.swagger.annotations.ApiModelProperty;
 | 
			
		||||
import lombok.Data;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.config.provider.TwoFactorAuthProviderConfig;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.provider.TwoFactorAuthProviderType;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.provider.TwoFactorAuthProviderType;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.provider.TwoFactorAuthProviderConfig;
 | 
			
		||||
 | 
			
		||||
import javax.validation.Valid;
 | 
			
		||||
import javax.validation.constraints.Min;
 | 
			
		||||
@ -13,7 +13,7 @@
 | 
			
		||||
 * See the License for the specific language governing permissions and
 | 
			
		||||
 * limitations under the License.
 | 
			
		||||
 */
 | 
			
		||||
package org.thingsboard.server.service.security.auth.mfa.config.account;
 | 
			
		||||
package org.thingsboard.server.common.data.security.model.mfa.account;
 | 
			
		||||
 | 
			
		||||
import lombok.Data;
 | 
			
		||||
 | 
			
		||||
@ -13,13 +13,13 @@
 | 
			
		||||
 * See the License for the specific language governing permissions and
 | 
			
		||||
 * limitations under the License.
 | 
			
		||||
 */
 | 
			
		||||
package org.thingsboard.server.service.security.auth.mfa.config.account;
 | 
			
		||||
package org.thingsboard.server.common.data.security.model.mfa.account;
 | 
			
		||||
 | 
			
		||||
import io.swagger.annotations.ApiModel;
 | 
			
		||||
import io.swagger.annotations.ApiModelProperty;
 | 
			
		||||
import lombok.Data;
 | 
			
		||||
import lombok.EqualsAndHashCode;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.provider.TwoFactorAuthProviderType;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.provider.TwoFactorAuthProviderType;
 | 
			
		||||
 | 
			
		||||
import javax.validation.constraints.NotBlank;
 | 
			
		||||
import javax.validation.constraints.Pattern;
 | 
			
		||||
@ -13,12 +13,12 @@
 | 
			
		||||
 * See the License for the specific language governing permissions and
 | 
			
		||||
 * limitations under the License.
 | 
			
		||||
 */
 | 
			
		||||
package org.thingsboard.server.service.security.auth.mfa.config.account;
 | 
			
		||||
package org.thingsboard.server.common.data.security.model.mfa.account;
 | 
			
		||||
 | 
			
		||||
import io.swagger.annotations.ApiModel;
 | 
			
		||||
import io.swagger.annotations.ApiModelProperty;
 | 
			
		||||
import lombok.Data;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.provider.TwoFactorAuthProviderType;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.provider.TwoFactorAuthProviderType;
 | 
			
		||||
 | 
			
		||||
import javax.validation.constraints.NotBlank;
 | 
			
		||||
import javax.validation.constraints.Pattern;
 | 
			
		||||
@ -13,14 +13,14 @@
 | 
			
		||||
 * See the License for the specific language governing permissions and
 | 
			
		||||
 * limitations under the License.
 | 
			
		||||
 */
 | 
			
		||||
package org.thingsboard.server.service.security.auth.mfa.config.account;
 | 
			
		||||
package org.thingsboard.server.common.data.security.model.mfa.account;
 | 
			
		||||
 | 
			
		||||
import com.fasterxml.jackson.annotation.JsonIgnore;
 | 
			
		||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
 | 
			
		||||
import com.fasterxml.jackson.annotation.JsonSubTypes;
 | 
			
		||||
import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
 | 
			
		||||
import com.fasterxml.jackson.annotation.JsonTypeInfo;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.provider.TwoFactorAuthProviderType;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.provider.TwoFactorAuthProviderType;
 | 
			
		||||
 | 
			
		||||
@JsonIgnoreProperties(ignoreUnknown = true)
 | 
			
		||||
@JsonTypeInfo(
 | 
			
		||||
@ -13,7 +13,7 @@
 | 
			
		||||
 * See the License for the specific language governing permissions and
 | 
			
		||||
 * limitations under the License.
 | 
			
		||||
 */
 | 
			
		||||
package org.thingsboard.server.service.security.auth.mfa.config.provider;
 | 
			
		||||
package org.thingsboard.server.common.data.security.model.mfa.provider;
 | 
			
		||||
 | 
			
		||||
import io.swagger.annotations.ApiModelProperty;
 | 
			
		||||
import lombok.Data;
 | 
			
		||||
@ -13,13 +13,12 @@
 | 
			
		||||
 * See the License for the specific language governing permissions and
 | 
			
		||||
 * limitations under the License.
 | 
			
		||||
 */
 | 
			
		||||
package org.thingsboard.server.service.security.auth.mfa.config.provider;
 | 
			
		||||
package org.thingsboard.server.common.data.security.model.mfa.provider;
 | 
			
		||||
 | 
			
		||||
import io.swagger.annotations.ApiModel;
 | 
			
		||||
import io.swagger.annotations.ApiModelProperty;
 | 
			
		||||
import lombok.Data;
 | 
			
		||||
import lombok.EqualsAndHashCode;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.provider.TwoFactorAuthProviderType;
 | 
			
		||||
 | 
			
		||||
import javax.validation.constraints.NotBlank;
 | 
			
		||||
import javax.validation.constraints.Pattern;
 | 
			
		||||
@ -13,12 +13,11 @@
 | 
			
		||||
 * See the License for the specific language governing permissions and
 | 
			
		||||
 * limitations under the License.
 | 
			
		||||
 */
 | 
			
		||||
package org.thingsboard.server.service.security.auth.mfa.config.provider;
 | 
			
		||||
package org.thingsboard.server.common.data.security.model.mfa.provider;
 | 
			
		||||
 | 
			
		||||
import io.swagger.annotations.ApiModel;
 | 
			
		||||
import io.swagger.annotations.ApiModelProperty;
 | 
			
		||||
import lombok.Data;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.provider.TwoFactorAuthProviderType;
 | 
			
		||||
 | 
			
		||||
import javax.validation.constraints.NotBlank;
 | 
			
		||||
 | 
			
		||||
@ -13,14 +13,13 @@
 | 
			
		||||
 * See the License for the specific language governing permissions and
 | 
			
		||||
 * limitations under the License.
 | 
			
		||||
 */
 | 
			
		||||
package org.thingsboard.server.service.security.auth.mfa.config.provider;
 | 
			
		||||
package org.thingsboard.server.common.data.security.model.mfa.provider;
 | 
			
		||||
 | 
			
		||||
import com.fasterxml.jackson.annotation.JsonIgnore;
 | 
			
		||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
 | 
			
		||||
import com.fasterxml.jackson.annotation.JsonSubTypes;
 | 
			
		||||
import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
 | 
			
		||||
import com.fasterxml.jackson.annotation.JsonTypeInfo;
 | 
			
		||||
import org.thingsboard.server.service.security.auth.mfa.provider.TwoFactorAuthProviderType;
 | 
			
		||||
 | 
			
		||||
@JsonIgnoreProperties(ignoreUnknown = true)
 | 
			
		||||
@JsonTypeInfo(
 | 
			
		||||
@ -13,7 +13,7 @@
 | 
			
		||||
 * See the License for the specific language governing permissions and
 | 
			
		||||
 * limitations under the License.
 | 
			
		||||
 */
 | 
			
		||||
package org.thingsboard.server.service.security.auth.mfa.provider;
 | 
			
		||||
package org.thingsboard.server.common.data.security.model.mfa.provider;
 | 
			
		||||
 | 
			
		||||
public enum TwoFactorAuthProviderType {
 | 
			
		||||
    TOTP,
 | 
			
		||||
@ -559,6 +559,13 @@ public class ModelConstants {
 | 
			
		||||
 | 
			
		||||
    public static final String EDGE_EVENT_BY_ID_VIEW_NAME = "edge_event_by_id";
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * User auth settings constants.
 | 
			
		||||
     * */
 | 
			
		||||
    public static final String USER_AUTH_SETTINGS_COLUMN_FAMILY_NAME = "user_auth_settings";
 | 
			
		||||
    public static final String USER_AUTH_SETTINGS_USER_ID_PROPERTY = USER_ID_PROPERTY;
 | 
			
		||||
    public static final String USER_AUTH_SETTINGS_TWO_FA_ACCOUNT_CONFIG_PROPERTY = "mfa_account_config";
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Cassandra attributes and timeseries constants.
 | 
			
		||||
     */
 | 
			
		||||
 | 
			
		||||
@ -0,0 +1,80 @@
 | 
			
		||||
/**
 | 
			
		||||
 * Copyright © 2016-2022 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.model.sql;
 | 
			
		||||
 | 
			
		||||
import com.fasterxml.jackson.databind.JsonNode;
 | 
			
		||||
import lombok.Data;
 | 
			
		||||
import lombok.EqualsAndHashCode;
 | 
			
		||||
import lombok.NoArgsConstructor;
 | 
			
		||||
import org.hibernate.annotations.Type;
 | 
			
		||||
import org.hibernate.annotations.TypeDef;
 | 
			
		||||
import org.thingsboard.common.util.JacksonUtil;
 | 
			
		||||
import org.thingsboard.server.common.data.id.UserAuthSettingsId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.UserId;
 | 
			
		||||
import org.thingsboard.server.common.data.security.UserAuthSettings;
 | 
			
		||||
import org.thingsboard.server.common.data.security.model.mfa.account.TwoFactorAuthAccountConfig;
 | 
			
		||||
import org.thingsboard.server.dao.model.BaseEntity;
 | 
			
		||||
import org.thingsboard.server.dao.model.BaseSqlEntity;
 | 
			
		||||
import org.thingsboard.server.dao.model.ModelConstants;
 | 
			
		||||
import org.thingsboard.server.dao.util.mapping.JsonStringType;
 | 
			
		||||
 | 
			
		||||
import javax.persistence.Column;
 | 
			
		||||
import javax.persistence.Entity;
 | 
			
		||||
import javax.persistence.Table;
 | 
			
		||||
import java.util.UUID;
 | 
			
		||||
 | 
			
		||||
@EqualsAndHashCode(callSuper = true)
 | 
			
		||||
@Data
 | 
			
		||||
@NoArgsConstructor
 | 
			
		||||
@TypeDef(name = "json", typeClass = JsonStringType.class)
 | 
			
		||||
@Entity
 | 
			
		||||
@Table(name = ModelConstants.USER_AUTH_SETTINGS_COLUMN_FAMILY_NAME) // FIXME [viacheslav]: add to upgrade script
 | 
			
		||||
public class UserAuthSettingsEntity extends BaseSqlEntity<UserAuthSettings> implements BaseEntity<UserAuthSettings> {
 | 
			
		||||
 | 
			
		||||
    @Column(name = ModelConstants.USER_AUTH_SETTINGS_USER_ID_PROPERTY, nullable = false, unique = true)
 | 
			
		||||
    private UUID userId;
 | 
			
		||||
    @Type(type = "json")
 | 
			
		||||
    @Column(name = ModelConstants.USER_AUTH_SETTINGS_TWO_FA_ACCOUNT_CONFIG_PROPERTY)
 | 
			
		||||
    private JsonNode twoFaAccountConfig;
 | 
			
		||||
 | 
			
		||||
    public UserAuthSettingsEntity(UserAuthSettings userAuthSettings) {
 | 
			
		||||
        if (userAuthSettings.getId() != null) {
 | 
			
		||||
            this.setId(userAuthSettings.getId().getId());
 | 
			
		||||
        }
 | 
			
		||||
        this.setCreatedTime(userAuthSettings.getCreatedTime());
 | 
			
		||||
        if (userAuthSettings.getUserId() != null) {
 | 
			
		||||
            this.userId = userAuthSettings.getUserId().getId();
 | 
			
		||||
        }
 | 
			
		||||
        if (userAuthSettings.getTwoFaAccountConfig() != null) {
 | 
			
		||||
            this.twoFaAccountConfig = JacksonUtil.valueToTree(userAuthSettings.getTwoFaAccountConfig());
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public UserAuthSettings toData() {
 | 
			
		||||
        UserAuthSettings userAuthSettings = new UserAuthSettings();
 | 
			
		||||
        userAuthSettings.setId(new UserAuthSettingsId(id));
 | 
			
		||||
        userAuthSettings.setCreatedTime(createdTime);
 | 
			
		||||
        if (userId != null) {
 | 
			
		||||
            userAuthSettings.setUserId(new UserId(userId));
 | 
			
		||||
        }
 | 
			
		||||
        if (twoFaAccountConfig != null) {
 | 
			
		||||
            userAuthSettings.setTwoFaAccountConfig(JacksonUtil.treeToValue(twoFaAccountConfig, TwoFactorAuthAccountConfig.class));
 | 
			
		||||
        }
 | 
			
		||||
        return userAuthSettings;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@ -0,0 +1,56 @@
 | 
			
		||||
/**
 | 
			
		||||
 * Copyright © 2016-2022 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.sql.user;
 | 
			
		||||
 | 
			
		||||
import lombok.RequiredArgsConstructor;
 | 
			
		||||
import org.springframework.data.jpa.repository.JpaRepository;
 | 
			
		||||
import org.springframework.stereotype.Component;
 | 
			
		||||
import org.thingsboard.server.common.data.id.UserId;
 | 
			
		||||
import org.thingsboard.server.common.data.security.UserAuthSettings;
 | 
			
		||||
import org.thingsboard.server.dao.DaoUtil;
 | 
			
		||||
import org.thingsboard.server.dao.model.sql.UserAuthSettingsEntity;
 | 
			
		||||
import org.thingsboard.server.dao.sql.JpaAbstractDao;
 | 
			
		||||
import org.thingsboard.server.dao.user.UserAuthSettingsDao;
 | 
			
		||||
 | 
			
		||||
import java.util.UUID;
 | 
			
		||||
 | 
			
		||||
@Component
 | 
			
		||||
@RequiredArgsConstructor
 | 
			
		||||
public class JpaUserAuthSettingsDao extends JpaAbstractDao<UserAuthSettingsEntity, UserAuthSettings> implements UserAuthSettingsDao {
 | 
			
		||||
 | 
			
		||||
    private final UserAuthSettingsRepository repository;
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public UserAuthSettings findByUserId(UserId userId) {
 | 
			
		||||
        return DaoUtil.getData(repository.findByUserId(userId.getId()));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void removeByUserId(UserId userId) {
 | 
			
		||||
        repository.deleteByUserId(userId.getId());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    protected Class<UserAuthSettingsEntity> getEntityClass() {
 | 
			
		||||
        return UserAuthSettingsEntity.class;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    protected JpaRepository<UserAuthSettingsEntity, UUID> getRepository() {
 | 
			
		||||
        return repository;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@ -0,0 +1,33 @@
 | 
			
		||||
/**
 | 
			
		||||
 * Copyright © 2016-2022 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.sql.user;
 | 
			
		||||
 | 
			
		||||
import org.springframework.data.jpa.repository.JpaRepository;
 | 
			
		||||
import org.springframework.stereotype.Repository;
 | 
			
		||||
import org.springframework.transaction.annotation.Transactional;
 | 
			
		||||
import org.thingsboard.server.dao.model.sql.UserAuthSettingsEntity;
 | 
			
		||||
 | 
			
		||||
import java.util.UUID;
 | 
			
		||||
 | 
			
		||||
@Repository
 | 
			
		||||
public interface UserAuthSettingsRepository extends JpaRepository<UserAuthSettingsEntity, UUID> {
 | 
			
		||||
 | 
			
		||||
    UserAuthSettingsEntity findByUserId(UUID userId);
 | 
			
		||||
 | 
			
		||||
    @Transactional
 | 
			
		||||
    void deleteByUserId(UUID userId);
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@ -0,0 +1,28 @@
 | 
			
		||||
/**
 | 
			
		||||
 * Copyright © 2016-2022 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.user;
 | 
			
		||||
 | 
			
		||||
import org.thingsboard.server.common.data.id.UserId;
 | 
			
		||||
import org.thingsboard.server.common.data.security.UserAuthSettings;
 | 
			
		||||
import org.thingsboard.server.dao.Dao;
 | 
			
		||||
 | 
			
		||||
public interface UserAuthSettingsDao extends Dao<UserAuthSettings> {
 | 
			
		||||
 | 
			
		||||
    UserAuthSettings findByUserId(UserId userId);
 | 
			
		||||
 | 
			
		||||
    void removeByUserId(UserId userId);
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@ -19,7 +19,6 @@ import com.fasterxml.jackson.core.type.TypeReference;
 | 
			
		||||
import com.fasterxml.jackson.databind.JsonNode;
 | 
			
		||||
import com.fasterxml.jackson.databind.node.ObjectNode;
 | 
			
		||||
import com.google.common.util.concurrent.ListenableFuture;
 | 
			
		||||
import lombok.AllArgsConstructor;
 | 
			
		||||
import lombok.extern.slf4j.Slf4j;
 | 
			
		||||
import org.apache.commons.lang3.RandomStringUtils;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Value;
 | 
			
		||||
@ -69,17 +68,20 @@ public class UserServiceImpl extends AbstractEntityService implements UserServic
 | 
			
		||||
 | 
			
		||||
    private final UserDao userDao;
 | 
			
		||||
    private final UserCredentialsDao userCredentialsDao;
 | 
			
		||||
    private final UserAuthSettingsDao userAuthSettingsDao;
 | 
			
		||||
    private final DataValidator<User> userValidator;
 | 
			
		||||
    private final DataValidator<UserCredentials> userCredentialsValidator;
 | 
			
		||||
    private final ApplicationEventPublisher eventPublisher;
 | 
			
		||||
 | 
			
		||||
    public UserServiceImpl(UserDao userDao,
 | 
			
		||||
                           UserCredentialsDao userCredentialsDao,
 | 
			
		||||
                           UserAuthSettingsDao userAuthSettingsDao,
 | 
			
		||||
                           DataValidator<User> userValidator,
 | 
			
		||||
                           DataValidator<UserCredentials> userCredentialsValidator,
 | 
			
		||||
                           ApplicationEventPublisher eventPublisher) {
 | 
			
		||||
        this.userDao = userDao;
 | 
			
		||||
        this.userCredentialsDao = userCredentialsDao;
 | 
			
		||||
        this.userAuthSettingsDao = userAuthSettingsDao;
 | 
			
		||||
        this.userValidator = userValidator;
 | 
			
		||||
        this.userCredentialsValidator = userCredentialsValidator;
 | 
			
		||||
        this.eventPublisher = eventPublisher;
 | 
			
		||||
@ -216,6 +218,7 @@ public class UserServiceImpl extends AbstractEntityService implements UserServic
 | 
			
		||||
        validateId(userId, INCORRECT_USER_ID + userId);
 | 
			
		||||
        UserCredentials userCredentials = userCredentialsDao.findByUserId(tenantId, userId.getId());
 | 
			
		||||
        userCredentialsDao.removeById(tenantId, userCredentials.getUuidId());
 | 
			
		||||
        userAuthSettingsDao.removeByUserId(userId);
 | 
			
		||||
        deleteEntityRelations(tenantId, userId);
 | 
			
		||||
        userDao.removeById(tenantId, userId.getId());
 | 
			
		||||
        eventPublisher.publishEvent(new UserAuthDataChangedEvent(userId));
 | 
			
		||||
 | 
			
		||||
@ -694,3 +694,11 @@ BEGIN
 | 
			
		||||
    deleted := ttl_deleted_count;
 | 
			
		||||
END
 | 
			
		||||
$$;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
CREATE TABLE IF NOT EXISTS user_auth_settings (
 | 
			
		||||
    id uuid NOT NULL CONSTRAINT user_auth_settings_pkey PRIMARY KEY,
 | 
			
		||||
    created_time bigint NOT NULL,
 | 
			
		||||
    user_id uuid UNIQUE NOT NULL CONSTRAINT fk_user_auth_settings_user_id REFERENCES tb_user(id),
 | 
			
		||||
    mfa_account_config varchar
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user