diff --git a/de.srsoftware.oidc.app/src/main/java/de/srsoftware/oidc/app/Application.java b/de.srsoftware.oidc.app/src/main/java/de/srsoftware/oidc/app/Application.java index 28e3660..6ca937f 100644 --- a/de.srsoftware.oidc.app/src/main/java/de/srsoftware/oidc/app/Application.java +++ b/de.srsoftware.oidc.app/src/main/java/de/srsoftware/oidc/app/Application.java @@ -117,10 +117,10 @@ public class Application { }; Optional encryptionKey = config.get(ENCRYPTION_KEY); - var salt = config.getOrDefault(SALT,uuid()); if (encryptionKey.isPresent()){ + var salt = config.getOrDefault(SALT,uuid()); mailConfig = new EncryptedMailConfig(mailConfig,encryptionKey.get(),salt); } return mailConfig; diff --git a/de.srsoftware.oidc.datastore.encrypted/build.gradle b/de.srsoftware.oidc.datastore.encrypted/build.gradle index dfeed0c..d735cd7 100644 --- a/de.srsoftware.oidc.datastore.encrypted/build.gradle +++ b/de.srsoftware.oidc.datastore.encrypted/build.gradle @@ -14,6 +14,7 @@ dependencies { testImplementation 'org.junit.jupiter:junit-jupiter' implementation project(':de.srsoftware.oidc.api') implementation 'com.sun.mail:jakarta.mail:2.0.1' + implementation project(':de.srsoftware.utils') } diff --git a/de.srsoftware.oidc.datastore.encrypted/src/main/java/de/srsoftware/oidc/datastore/encrypted/EncryptedConfig.java b/de.srsoftware.oidc.datastore.encrypted/src/main/java/de/srsoftware/oidc/datastore/encrypted/EncryptedConfig.java index 4720f7f..cb5074e 100644 --- a/de.srsoftware.oidc.datastore.encrypted/src/main/java/de/srsoftware/oidc/datastore/encrypted/EncryptedConfig.java +++ b/de.srsoftware.oidc.datastore.encrypted/src/main/java/de/srsoftware/oidc/datastore/encrypted/EncryptedConfig.java @@ -33,6 +33,7 @@ public class EncryptedConfig { } public String encrypt(String plain) { + if (plain == null) return null; SecureRandom secureRandom = new SecureRandom(); byte[] iv = new byte[16]; secureRandom.nextBytes(iv); @@ -52,6 +53,8 @@ public class EncryptedConfig { } public String decrypt(String secret) { + if (secret == null) return null; + if (secret.isBlank()) return ""; byte[] encryptedData = Base64.getDecoder().decode(secret); byte[] iv = new byte[16]; System.arraycopy(encryptedData, 0, iv, 0, iv.length); diff --git a/de.srsoftware.oidc.datastore.encrypted/src/main/java/de/srsoftware/oidc/datastore/encrypted/EncryptedMailConfig.java b/de.srsoftware.oidc.datastore.encrypted/src/main/java/de/srsoftware/oidc/datastore/encrypted/EncryptedMailConfig.java index 9aea706..64d819b 100644 --- a/de.srsoftware.oidc.datastore.encrypted/src/main/java/de/srsoftware/oidc/datastore/encrypted/EncryptedMailConfig.java +++ b/de.srsoftware.oidc.datastore.encrypted/src/main/java/de/srsoftware/oidc/datastore/encrypted/EncryptedMailConfig.java @@ -14,8 +14,8 @@ public class EncryptedMailConfig extends EncryptedConfig implements MailConfig { private final MailConfig storage; private Authenticator auth; - public EncryptedMailConfig(MailConfig storage, String encryotionKey, String salt) { - super(encryotionKey, salt); + public EncryptedMailConfig(MailConfig storage, String encryptionKey, String salt) { + super(encryptionKey, salt); this.storage = storage; } diff --git a/de.srsoftware.oidc.datastore.encrypted/src/test/java/EncryptedConfigTest.java b/de.srsoftware.oidc.datastore.encrypted/src/test/java/EncryptedConfigTest.java new file mode 100644 index 0000000..8acc0c6 --- /dev/null +++ b/de.srsoftware.oidc.datastore.encrypted/src/test/java/EncryptedConfigTest.java @@ -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); + } +} diff --git a/de.srsoftware.oidc.datastore.encrypted/src/test/java/EncryptedMailConfigTest.java b/de.srsoftware.oidc.datastore.encrypted/src/test/java/EncryptedMailConfigTest.java new file mode 100644 index 0000000..e7c3f36 --- /dev/null +++ b/de.srsoftware.oidc.datastore.encrypted/src/test/java/EncryptedMailConfigTest.java @@ -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 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()); + } +}