package com.qmth.boot.core.security.config;

import com.qmth.boot.core.security.service.EncryptKeyProvider;
import com.qmth.boot.core.security.service.EncryptService;
import com.qmth.boot.core.security.service.impl.DefaultEncryptKeyProvider;
import org.jasypt.encryption.StringEncryptor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import javax.validation.constraints.NotNull;

@Configuration
@ComponentScan("com.qmth.boot.core.security")
public class SecurityAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean(EncryptKeyProvider.class)
    public EncryptKeyProvider encryptKeyProvider() {
        return new DefaultEncryptKeyProvider();
    }

    @Bean("jasyptStringEncryptor")
    public StringEncryptor stringEncryptor(@NotNull EncryptService encryptService) {
        return new StringEncryptor() {

            @Override
            public String encrypt(String s) {
                return encryptService.encrypt(s);
            }

            @Override
            public String decrypt(String s) {
                return encryptService.decrypt(s);
            }
        };
    }
}
