KEYCLOAK-114 Use SMTP server settings from realm

This commit is contained in:
Stian Thorgersen 2013-10-22 13:46:56 +01:00
parent a88dcace3d
commit 6e3dd959eb
6 changed files with 21 additions and 36 deletions

View file

@ -22,6 +22,7 @@
package org.keycloak.services.email;
import java.net.URI;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
@ -52,20 +53,15 @@ public class EmailSender {
private Properties properties;
public EmailSender() {
public EmailSender(Map<String, String> config) {
properties = new Properties();
for (Entry<Object, Object> e : System.getProperties().entrySet()) {
String key = (String) e.getKey();
if (key.startsWith("keycloak.mail.smtp.")) {
key = key.replace("keycloak.mail.smtp.", "mail.smtp.");
properties.put(key, e.getValue());
}
for (Entry<String, String> e : config.entrySet()) {
properties.put("mail.smtp." + e.getKey(), e.getValue());
}
}
public void send(String address, String subject, String body) throws AddressException, MessagingException {
Session session = Session.getDefaultInstance(properties);
public void send(String address, String subject, String body) throws MessagingException {
Session session = Session.getInstance(properties);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(properties.getProperty("mail.smtp.from")));

View file

@ -260,7 +260,7 @@ public class RequiredActionsService {
accessCode.setRequiredActions(requiredActions);
accessCode.setExpiration(System.currentTimeMillis() / 1000 + realm.getAccessCodeLifespanUserAction());
new EmailSender().sendPasswordReset(user, realm, accessCode, uriInfo);
new EmailSender(realm.getSmtpConfig()).sendPasswordReset(user, realm, accessCode, uriInfo);
return Flows.forms(realm, request, uriInfo).setError("emailSent").setErrorType(FormFlows.MessageType.SUCCESS)
.forwardToPasswordReset();

View file

@ -89,7 +89,7 @@ public class FormFlows {
case UPDATE_PASSWORD:
return forwardToActionForm(Pages.LOGIN_UPDATE_PASSWORD, Messages.ACTION_WARN_PASSWD);
case VERIFY_EMAIL:
new EmailSender().sendEmailVerification(userModel, realm, accessCode, uriInfo);
new EmailSender(realm.getSmtpConfig()).sendEmailVerification(userModel, realm, accessCode, uriInfo);
return forwardToActionForm(Pages.LOGIN_VERIFY_EMAIL, Messages.ACTION_WARN_EMAIL);
default:
return Response.serverError().build();

View file

@ -3,6 +3,7 @@ package org.keycloak.services.email;
import java.io.IOException;
import java.lang.Thread.UncaughtExceptionHandler;
import java.net.SocketException;
import java.util.HashMap;
import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
@ -19,6 +20,7 @@ import com.icegreen.greenmail.util.ServerSetup;
public class EmailSenderTest {
private GreenMail greenMail;
private EmailSender emailSender;
@Before
public void before() {
@ -27,9 +29,12 @@ public class EmailSenderTest {
greenMail = new GreenMail(setup);
greenMail.start();
System.setProperty("keycloak.mail.smtp.from", "auto@keycloak.org");
System.setProperty("keycloak.mail.smtp.host", "localhost");
System.setProperty("keycloak.mail.smtp.port", "3025");
HashMap<String,String> config = new HashMap<String, String>();
config.put("from", "auto@keycloak.org");
config.put("host", "localhost");
config.put("port", "3025");
emailSender = new EmailSender(config);
}
@After
@ -53,7 +58,6 @@ public class EmailSenderTest {
@Test
public void sendMail() throws AddressException, MessagingException, IOException {
EmailSender emailSender = new EmailSender();
emailSender.send("test@test.com", "Test subject", "Test body");
MimeMessage[] receivedMessages = greenMail.getReceivedMessages();

View file

@ -41,23 +41,8 @@ public class GreenMailRule extends ExternalResource {
private GreenMail greenMail;
private Properties originalProperties = new Properties();
@Override
protected void before() throws Throwable {
Iterator<Entry<Object, Object>> itr = System.getProperties().entrySet().iterator();
while (itr.hasNext()) {
Entry<Object, Object> e = itr.next();
if (((String) e.getKey()).startsWith("keycloak.mail")) {
originalProperties.put(e.getKey(), e.getValue());
itr.remove();
}
}
System.setProperty("keycloak.mail.smtp.from", "auto@keycloak.org");
System.setProperty("keycloak.mail.smtp.host", "localhost");
System.setProperty("keycloak.mail.smtp.port", "3025");
ServerSetup setup = new ServerSetup(3025, "localhost", "smtp");
greenMail = new GreenMail(setup);
@ -81,11 +66,6 @@ public class GreenMailRule extends ExternalResource {
greenMail.stop();
}
System.getProperties().remove("keycloak.mail.smtp.from");
System.getProperties().remove("keycloak.mail.smtp.host");
System.getProperties().remove("keycloak.mail.smtp.port");
System.getProperties().putAll(originalProperties);
}
public MimeMessage[] getReceivedMessages() {

View file

@ -16,6 +16,11 @@
"requiredApplicationCredentials": [ "password" ],
"requiredOAuthClientCredentials": [ "password" ],
"defaultRoles": [ "user" ],
"smtpServer": {
"from": "auto@keycloak.org",
"host": "localhost",
"port":"3025"
},
"users" : [
{
"username" : "test-user@localhost",