implemented tests for encrypted mail config
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -117,10 +117,10 @@ public class Application {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Optional<String> encryptionKey = config.get(ENCRYPTION_KEY);
|
Optional<String> encryptionKey = config.get(ENCRYPTION_KEY);
|
||||||
var salt = config.getOrDefault(SALT,uuid());
|
|
||||||
|
|
||||||
|
|
||||||
if (encryptionKey.isPresent()){
|
if (encryptionKey.isPresent()){
|
||||||
|
var salt = config.getOrDefault(SALT,uuid());
|
||||||
mailConfig = new EncryptedMailConfig(mailConfig,encryptionKey.get(),salt);
|
mailConfig = new EncryptedMailConfig(mailConfig,encryptionKey.get(),salt);
|
||||||
}
|
}
|
||||||
return mailConfig;
|
return mailConfig;
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ dependencies {
|
|||||||
testImplementation 'org.junit.jupiter:junit-jupiter'
|
testImplementation 'org.junit.jupiter:junit-jupiter'
|
||||||
implementation project(':de.srsoftware.oidc.api')
|
implementation project(':de.srsoftware.oidc.api')
|
||||||
implementation 'com.sun.mail:jakarta.mail:2.0.1'
|
implementation 'com.sun.mail:jakarta.mail:2.0.1'
|
||||||
|
implementation project(':de.srsoftware.utils')
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ public class EncryptedConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String encrypt(String plain) {
|
public String encrypt(String plain) {
|
||||||
|
if (plain == null) return null;
|
||||||
SecureRandom secureRandom = new SecureRandom();
|
SecureRandom secureRandom = new SecureRandom();
|
||||||
byte[] iv = new byte[16];
|
byte[] iv = new byte[16];
|
||||||
secureRandom.nextBytes(iv);
|
secureRandom.nextBytes(iv);
|
||||||
@@ -52,6 +53,8 @@ public class EncryptedConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String decrypt(String secret) {
|
public String decrypt(String secret) {
|
||||||
|
if (secret == null) return null;
|
||||||
|
if (secret.isBlank()) return "";
|
||||||
byte[] encryptedData = Base64.getDecoder().decode(secret);
|
byte[] encryptedData = Base64.getDecoder().decode(secret);
|
||||||
byte[] iv = new byte[16];
|
byte[] iv = new byte[16];
|
||||||
System.arraycopy(encryptedData, 0, iv, 0, iv.length);
|
System.arraycopy(encryptedData, 0, iv, 0, iv.length);
|
||||||
|
|||||||
@@ -14,8 +14,8 @@ public class EncryptedMailConfig extends EncryptedConfig implements MailConfig {
|
|||||||
private final MailConfig storage;
|
private final MailConfig storage;
|
||||||
private Authenticator auth;
|
private Authenticator auth;
|
||||||
|
|
||||||
public EncryptedMailConfig(MailConfig storage, String encryotionKey, String salt) {
|
public EncryptedMailConfig(MailConfig storage, String encryptionKey, String salt) {
|
||||||
super(encryotionKey, salt);
|
super(encryptionKey, salt);
|
||||||
this.storage = storage;
|
this.storage = storage;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
/* © SRSoftware 2024 */
|
||||||
|
import static de.srsoftware.utils.Strings.uuid;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import de.srsoftware.oidc.datastore.encrypted.EncryptedConfig;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
public class EncryptedConfigTest {
|
||||||
|
@Test
|
||||||
|
public void testEncryptionDecryption() {
|
||||||
|
var key = uuid();
|
||||||
|
var salt = uuid();
|
||||||
|
var secret = uuid();
|
||||||
|
var encryptor = new EncryptedConfig(key, salt);
|
||||||
|
var decryptor = new EncryptedConfig(key, salt);
|
||||||
|
var encrypted = encryptor.encrypt(secret);
|
||||||
|
var decrypted = decryptor.decrypt(encrypted);
|
||||||
|
assertEquals(secret, decrypted);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,129 @@
|
|||||||
|
/* © SRSoftware 2024 */
|
||||||
|
import static de.srsoftware.utils.Strings.uuid;
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
import de.srsoftware.oidc.api.MailConfig;
|
||||||
|
import de.srsoftware.oidc.datastore.encrypted.EncryptedMailConfig;
|
||||||
|
import jakarta.mail.Authenticator;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Properties;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
public class EncryptedMailConfigTest {
|
||||||
|
private class InMemoryMailConfig implements MailConfig {
|
||||||
|
private String smtpHost;
|
||||||
|
private int port;
|
||||||
|
private String addr;
|
||||||
|
private String pass;
|
||||||
|
private boolean tls;
|
||||||
|
private boolean auth;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String smtpHost() {
|
||||||
|
return smtpHost;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MailConfig smtpHost(String newValue) {
|
||||||
|
smtpHost = newValue;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int smtpPort() {
|
||||||
|
return port;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MailConfig smtpPort(int newValue) {
|
||||||
|
port = newValue;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String senderAddress() {
|
||||||
|
return addr;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MailConfig senderAddress(String newValue) {
|
||||||
|
addr = newValue;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String senderPassword() {
|
||||||
|
return pass;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MailConfig senderPassword(String newValue) {
|
||||||
|
pass = newValue;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean startTls() {
|
||||||
|
return tls;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MailConfig startTls(boolean newValue) {
|
||||||
|
tls = newValue;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean smtpAuth() {
|
||||||
|
return auth;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MailConfig smtpAuth(boolean newValue) {
|
||||||
|
auth = newValue;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Properties props() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<String, Object> map() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Authenticator authenticator() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MailConfig save() {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void TestEncryptedMailConfig() {
|
||||||
|
var key = uuid();
|
||||||
|
var salt = uuid();
|
||||||
|
|
||||||
|
|
||||||
|
var addr = uuid();
|
||||||
|
var pass = uuid();
|
||||||
|
var host = uuid();
|
||||||
|
|
||||||
|
var plainMailConfig = new InMemoryMailConfig();
|
||||||
|
var writer = new EncryptedMailConfig(plainMailConfig, key, salt);
|
||||||
|
|
||||||
|
writer.senderAddress(addr).senderPassword(pass).smtpHost(host).smtpAuth(true).startTls(false);
|
||||||
|
|
||||||
|
var reader = new EncryptedMailConfig(plainMailConfig, key, salt);
|
||||||
|
assertEquals(addr, reader.senderAddress());
|
||||||
|
assertEquals(host, reader.smtpHost());
|
||||||
|
assertEquals(pass, reader.senderPassword());
|
||||||
|
assertTrue(reader.smtpAuth());
|
||||||
|
assertFalse(reader.startTls());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user