diff --git a/src/main/java/fr/xephi/authme/mail/OAuth2SaslClientFactory.java b/src/main/java/fr/xephi/authme/mail/OAuth2SaslClientFactory.java index 4f7888439..09a448059 100644 --- a/src/main/java/fr/xephi/authme/mail/OAuth2SaslClientFactory.java +++ b/src/main/java/fr/xephi/authme/mail/OAuth2SaslClientFactory.java @@ -1,21 +1,21 @@ -/* Copyright 2012 Google Inc. +/* + * Copyright 2012 Google Inc. * - * 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 + * 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 + * 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. + * 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. */ package fr.xephi.authme.mail; import java.util.Map; - import java.util.logging.Logger; import javax.security.auth.callback.CallbackHandler; @@ -25,38 +25,34 @@ import javax.security.sasl.SaslClientFactory; /** * A SaslClientFactory that returns instances of OAuth2SaslClient. * - *

Only the "XOAUTH2" mechanism is supported. The {@code callbackHandler} is + *

+ * Only the "XOAUTH2" mechanism is supported. The {@code callbackHandler} is * passed to the OAuth2SaslClient. Other parameters are ignored. */ public class OAuth2SaslClientFactory implements SaslClientFactory { - private static final Logger logger = - Logger.getLogger(OAuth2SaslClientFactory.class.getName()); - public static final String OAUTH_TOKEN_PROP = - "mail.imaps.sasl.mechanisms.oauth2.oauthToken"; + private static final Logger logger = Logger.getLogger(OAuth2SaslClientFactory.class.getName()); - public SaslClient createSaslClient(String[] mechanisms, - String authorizationId, - String protocol, - String serverName, - Map props, - CallbackHandler callbackHandler) { - boolean matchedMechanism = false; - for (int i = 0; i < mechanisms.length; ++i) { - if ("XOAUTH2".equalsIgnoreCase(mechanisms[i])) { - matchedMechanism = true; - break; - } + public static final String OAUTH_TOKEN_PROP = "mail.smpt.sasl.mechanisms.oauth2.oauthToken"; + + public SaslClient createSaslClient(String[] mechanisms, + String authorizationId, String protocol, String serverName, + Map props, CallbackHandler callbackHandler) { + boolean matchedMechanism = false; + for (int i = 0; i < mechanisms.length; ++i) { + if ("XOAUTH2".equalsIgnoreCase(mechanisms[i])) { + matchedMechanism = true; + break; + } + } + if (!matchedMechanism) { + logger.info("Failed to match any mechanisms"); + return null; + } + return new OAuth2SaslClient((String) props.get(OAUTH_TOKEN_PROP), callbackHandler); } - if (!matchedMechanism) { - logger.info("Failed to match any mechanisms"); - return null; - } - return new OAuth2SaslClient((String) props.get(OAUTH_TOKEN_PROP), - callbackHandler); - } - public String[] getMechanismNames(Map props) { - return new String[] {"XOAUTH2"}; - } -} \ No newline at end of file + public String[] getMechanismNames(Map props) { + return new String[] { "XOAUTH2" }; + } +} diff --git a/src/main/java/fr/xephi/authme/mail/SendMailSSL.java b/src/main/java/fr/xephi/authme/mail/SendMailSSL.java index 3b6dde845..ab718b61f 100644 --- a/src/main/java/fr/xephi/authme/mail/SendMailSSL.java +++ b/src/main/java/fr/xephi/authme/mail/SendMailSSL.java @@ -1,24 +1,25 @@ package fr.xephi.authme.mail; +import java.io.File; +import java.io.IOException; +import java.security.Security; +import java.util.Properties; + +import javax.activation.DataSource; +import javax.activation.FileDataSource; +import javax.imageio.ImageIO; +import javax.mail.Session; + +import org.apache.commons.mail.EmailException; +import org.apache.commons.mail.HtmlEmail; +import org.bukkit.Bukkit; + import fr.xephi.authme.AuthMe; import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.ImageGenerator; import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.settings.Settings; - import fr.xephi.authme.util.StringUtils; -import org.apache.commons.mail.DefaultAuthenticator; -import org.apache.commons.mail.EmailException; -import org.apache.commons.mail.HtmlEmail; -import org.bukkit.Bukkit; - -import javax.activation.DataSource; -import javax.activation.FileDataSource; -import javax.imageio.ImageIO; -import java.io.File; -import java.io.IOException; -import java.security.Security; -import java.util.Properties; /** * @author Xephi59 @@ -42,8 +43,7 @@ public class SendMailSSL { try { email = initializeMail(auth); } catch (EmailException e) { - ConsoleLogger.showError("Failed to create email with the given settings: " - + StringUtils.formatException(e)); + ConsoleLogger.showError("Failed to create email with the given settings: " + StringUtils.formatException(e)); return; } @@ -55,8 +55,7 @@ public class SendMailSSL { file = generateImage(auth, plugin, newPass); content = embedImageIntoEmailContent(file, email, content); } catch (IOException | EmailException e) { - ConsoleLogger.showError("Unable to send new password as image for email " - + auth.getEmail() + ": " + StringUtils.formatException(e)); + ConsoleLogger.showError("Unable to send new password as image for email " + auth.getEmail() + ": " + StringUtils.formatException(e)); } } @@ -69,21 +68,23 @@ public class SendMailSSL { }); } - private static File generateImage(PlayerAuth auth, AuthMe plugin, String newPass) throws IOException { + private static File generateImage(PlayerAuth auth, AuthMe plugin, + String newPass) throws IOException { ImageGenerator gen = new ImageGenerator(newPass); File file = new File(plugin.getDataFolder() + File.separator + auth.getNickname() + "_new_pass.jpg"); ImageIO.write(gen.generateImage(), "jpg", file); return file; } - private static String embedImageIntoEmailContent(File image, HtmlEmail email, String content) - throws EmailException { + private static String embedImageIntoEmailContent(File image, + HtmlEmail email, String content) throws EmailException { DataSource source = new FileDataSource(image); String tag = email.embed(source, image.getName()); return content.replace("", ""); } - private static HtmlEmail initializeMail(PlayerAuth auth) throws EmailException { + private static HtmlEmail initializeMail(PlayerAuth auth) + throws EmailException { String senderName; if (StringUtils.isEmpty(Settings.getmailSenderName)) { senderName = Settings.getmailAccount; @@ -100,10 +101,7 @@ public class SendMailSSL { email.addTo(auth.getEmail()); email.setFrom(senderMail, senderName); email.setSubject(Settings.getMailSubject); - if (!StringUtils.isEmpty(senderMail) && !StringUtils.isEmpty(mailPassword)) { - String password = !Settings.emailOauth2Token.isEmpty() ? "" : mailPassword; - email.setAuthenticator(new DefaultAuthenticator(senderMail, password)); - } + email.setAuthentication(senderMail, mailPassword); setPropertiesForPort(email, port); return email; @@ -114,8 +112,7 @@ public class SendMailSSL { email.setHtmlMsg(content); email.setTextMsg(content); } catch (EmailException e) { - ConsoleLogger.showError("Your email.html config contains an error and cannot be sent: " - + StringUtils.formatException(e)); + ConsoleLogger.showError("Your email.html config contains an error and cannot be sent: " + StringUtils.formatException(e)); return false; } try { @@ -127,29 +124,32 @@ public class SendMailSSL { } } - private static String replaceMailTags(String mailText, AuthMe plugin, PlayerAuth auth, String newPass) { - return mailText - .replace("", auth.getNickname()) - .replace("", plugin.getServer().getServerName()) - .replace("", newPass); + private static String replaceMailTags(String mailText, AuthMe plugin, + PlayerAuth auth, String newPass) { + return mailText.replace("", auth.getNickname()).replace("", plugin.getServer().getServerName()).replace("", newPass); } - private static void setPropertiesForPort(HtmlEmail email, int port) throws EmailException { + private static void setPropertiesForPort(HtmlEmail email, int port) + throws EmailException { switch (port) { case 587: - email.setStartTLSEnabled(true); - email.setStartTLSRequired(true); if (!Settings.emailOauth2Token.isEmpty()) { if (Security.getProvider("Google OAuth2 Provider") == null) { Security.addProvider(new OAuth2Provider()); } - Properties mailProperties = email.getMailSession().getProperties(); - mailProperties.setProperty("mail.smtp.starttls.enable", "true"); - mailProperties.setProperty("mail.smtp.starttls.required", "true"); + mailProperties.setProperty("mail.smtp.ssl.enable", "true"); + mailProperties.setProperty("mail.smtp.auth.mechanisms", "XOAUTH2"); mailProperties.setProperty("mail.smtp.sasl.enable", "true"); mailProperties.setProperty("mail.smtp.sasl.mechanisms", "XOAUTH2"); - mailProperties.setProperty(OAuth2SaslClientFactory.OAUTH_TOKEN_PROP, Settings.getmailPassword); + mailProperties.setProperty("mail.smtp.auth.login.disable", "true"); + mailProperties.setProperty("mail.smtp.auth.plain.disable", "true"); + mailProperties.setProperty(OAuth2SaslClientFactory.OAUTH_TOKEN_PROP, Settings.emailOauth2Token); + email.setMailSession(Session.getInstance(mailProperties)); + } else { + email.setStartTLSEnabled(true); + email.setStartTLSRequired(true); + email.setTLS(true); } break; case 25: @@ -157,8 +157,8 @@ public class SendMailSSL { email.setSSLCheckServerIdentity(true); break; case 465: - email.setSSLOnConnect(true); - email.setSSLCheckServerIdentity(true); + email.setSslSmtpPort("" + port); + email.setSSL(true); break; default: email.setStartTLSEnabled(true);