2016-02-03 10:20:22 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Red Hat, Inc. and/or its affiliates
|
|
|
|
* and other contributors as indicated by the @author tags.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2015-11-25 12:59:47 +00:00
|
|
|
package org.keycloak.email;
|
|
|
|
|
2016-01-19 22:29:16 +00:00
|
|
|
import org.keycloak.truststore.HostnameVerificationPolicy;
|
|
|
|
import org.keycloak.truststore.JSSETruststoreConfigurator;
|
2016-01-05 14:14:31 +00:00
|
|
|
import org.keycloak.models.KeycloakSession;
|
2015-11-25 12:59:47 +00:00
|
|
|
import org.keycloak.models.RealmModel;
|
|
|
|
import org.keycloak.models.UserModel;
|
2016-01-18 19:32:05 +00:00
|
|
|
import org.keycloak.services.ServicesLogger;
|
2015-11-25 12:59:47 +00:00
|
|
|
|
|
|
|
import javax.mail.Message;
|
2016-04-12 05:06:50 +00:00
|
|
|
import javax.mail.MessagingException;
|
2015-11-25 12:59:47 +00:00
|
|
|
import javax.mail.Multipart;
|
|
|
|
import javax.mail.Session;
|
|
|
|
import javax.mail.Transport;
|
|
|
|
import javax.mail.internet.InternetAddress;
|
|
|
|
import javax.mail.internet.MimeBodyPart;
|
|
|
|
import javax.mail.internet.MimeMessage;
|
|
|
|
import javax.mail.internet.MimeMultipart;
|
2016-06-07 03:11:46 +00:00
|
|
|
import javax.mail.internet.MimeUtility;
|
2016-01-26 12:52:58 +00:00
|
|
|
import javax.net.ssl.SSLSocketFactory;
|
2016-01-05 14:14:31 +00:00
|
|
|
import java.security.KeyManagementException;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
2015-11-25 12:59:47 +00:00
|
|
|
import java.util.Date;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Properties;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
|
|
|
|
*/
|
|
|
|
public class DefaultEmailSenderProvider implements EmailSenderProvider {
|
|
|
|
|
2016-01-18 19:32:05 +00:00
|
|
|
private static final ServicesLogger logger = ServicesLogger.ROOT_LOGGER;
|
2015-11-25 12:59:47 +00:00
|
|
|
|
2016-01-05 14:14:31 +00:00
|
|
|
private final KeycloakSession session;
|
|
|
|
|
|
|
|
public DefaultEmailSenderProvider(KeycloakSession session) {
|
|
|
|
this.session = session;
|
|
|
|
}
|
|
|
|
|
2015-11-25 12:59:47 +00:00
|
|
|
@Override
|
|
|
|
public void send(RealmModel realm, UserModel user, String subject, String textBody, String htmlBody) throws EmailException {
|
2016-04-12 05:06:50 +00:00
|
|
|
Transport transport = null;
|
2015-11-25 12:59:47 +00:00
|
|
|
try {
|
|
|
|
String address = user.getEmail();
|
|
|
|
Map<String, String> config = realm.getSmtpConfig();
|
|
|
|
|
|
|
|
Properties props = new Properties();
|
|
|
|
props.setProperty("mail.smtp.host", config.get("host"));
|
|
|
|
|
|
|
|
boolean auth = "true".equals(config.get("auth"));
|
|
|
|
boolean ssl = "true".equals(config.get("ssl"));
|
|
|
|
boolean starttls = "true".equals(config.get("starttls"));
|
|
|
|
|
|
|
|
if (config.containsKey("port")) {
|
|
|
|
props.setProperty("mail.smtp.port", config.get("port"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auth) {
|
|
|
|
props.setProperty("mail.smtp.auth", "true");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ssl) {
|
|
|
|
props.setProperty("mail.smtp.ssl.enable", "true");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (starttls) {
|
|
|
|
props.setProperty("mail.smtp.starttls.enable", "true");
|
|
|
|
}
|
|
|
|
|
2016-01-05 14:14:31 +00:00
|
|
|
if (ssl || starttls) {
|
|
|
|
setupTruststore(props);
|
|
|
|
}
|
|
|
|
|
2015-11-25 12:59:47 +00:00
|
|
|
props.setProperty("mail.smtp.timeout", "10000");
|
|
|
|
props.setProperty("mail.smtp.connectiontimeout", "10000");
|
|
|
|
|
|
|
|
String from = config.get("from");
|
|
|
|
|
|
|
|
Session session = Session.getInstance(props);
|
|
|
|
|
|
|
|
Multipart multipart = new MimeMultipart("alternative");
|
|
|
|
|
|
|
|
if(textBody != null) {
|
|
|
|
MimeBodyPart textPart = new MimeBodyPart();
|
|
|
|
textPart.setText(textBody, "UTF-8");
|
|
|
|
multipart.addBodyPart(textPart);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(htmlBody != null) {
|
|
|
|
MimeBodyPart htmlPart = new MimeBodyPart();
|
|
|
|
htmlPart.setContent(htmlBody, "text/html; charset=UTF-8");
|
|
|
|
multipart.addBodyPart(htmlPart);
|
|
|
|
}
|
|
|
|
|
|
|
|
Message msg = new MimeMessage(session);
|
|
|
|
msg.setFrom(new InternetAddress(from));
|
|
|
|
msg.setHeader("To", address);
|
2016-06-07 03:11:46 +00:00
|
|
|
msg.setSubject(MimeUtility.encodeText(subject, "utf-8", "B"));
|
|
|
|
|
2015-11-25 12:59:47 +00:00
|
|
|
msg.setContent(multipart);
|
|
|
|
msg.saveChanges();
|
|
|
|
msg.setSentDate(new Date());
|
|
|
|
|
2016-04-12 05:06:50 +00:00
|
|
|
transport = session.getTransport("smtp");
|
2015-11-25 12:59:47 +00:00
|
|
|
if (auth) {
|
|
|
|
transport.connect(config.get("user"), config.get("password"));
|
|
|
|
} else {
|
|
|
|
transport.connect();
|
|
|
|
}
|
|
|
|
transport.sendMessage(msg, new InternetAddress[]{new InternetAddress(address)});
|
|
|
|
} catch (Exception e) {
|
2016-01-18 19:32:05 +00:00
|
|
|
logger.failedToSendEmail(e);
|
2015-11-25 12:59:47 +00:00
|
|
|
throw new EmailException(e);
|
2016-04-12 05:06:50 +00:00
|
|
|
} finally {
|
|
|
|
if (transport != null) {
|
|
|
|
try {
|
|
|
|
transport.close();
|
|
|
|
} catch (MessagingException e) {
|
|
|
|
logger.warn("Failed to close transport", e);
|
|
|
|
}
|
|
|
|
}
|
2015-11-25 12:59:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-05 14:14:31 +00:00
|
|
|
private void setupTruststore(Properties props) throws NoSuchAlgorithmException, KeyManagementException {
|
|
|
|
|
|
|
|
JSSETruststoreConfigurator configurator = new JSSETruststoreConfigurator(session);
|
|
|
|
|
2016-01-26 12:52:58 +00:00
|
|
|
SSLSocketFactory factory = configurator.getSSLSocketFactory();
|
|
|
|
if (factory != null) {
|
|
|
|
props.put("mail.smtp.ssl.socketFactory", factory);
|
|
|
|
if (configurator.getProvider().getPolicy() == HostnameVerificationPolicy.ANY) {
|
|
|
|
props.setProperty("mail.smtp.ssl.trust", "*");
|
|
|
|
}
|
2016-01-05 14:14:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-25 12:59:47 +00:00
|
|
|
@Override
|
|
|
|
public void close() {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|