mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-11-27 20:57:35 +01:00
Fix some problems with Emails
This commit is contained in:
parent
cc67d6daea
commit
3860b3ba5a
@ -1,21 +1,21 @@
|
|||||||
/* Copyright 2012 Google Inc.
|
/*
|
||||||
|
* Copyright 2012 Google Inc.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
||||||
* you may not use this file except in compliance with the License.
|
* use this file except in compliance with the License. You may obtain a copy of
|
||||||
* You may obtain a copy of the License at
|
* 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
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
* See the License for the specific language governing permissions and
|
* License for the specific language governing permissions and limitations under
|
||||||
* limitations under the License.
|
* the License.
|
||||||
*/
|
*/
|
||||||
package fr.xephi.authme.mail;
|
package fr.xephi.authme.mail;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import javax.security.auth.callback.CallbackHandler;
|
import javax.security.auth.callback.CallbackHandler;
|
||||||
@ -25,22 +25,19 @@ import javax.security.sasl.SaslClientFactory;
|
|||||||
/**
|
/**
|
||||||
* A SaslClientFactory that returns instances of OAuth2SaslClient.
|
* A SaslClientFactory that returns instances of OAuth2SaslClient.
|
||||||
*
|
*
|
||||||
* <p>Only the "XOAUTH2" mechanism is supported. The {@code callbackHandler} is
|
* <p>
|
||||||
|
* Only the "XOAUTH2" mechanism is supported. The {@code callbackHandler} is
|
||||||
* passed to the OAuth2SaslClient. Other parameters are ignored.
|
* passed to the OAuth2SaslClient. Other parameters are ignored.
|
||||||
*/
|
*/
|
||||||
public class OAuth2SaslClientFactory implements SaslClientFactory {
|
public class OAuth2SaslClientFactory implements SaslClientFactory {
|
||||||
private static final Logger logger =
|
|
||||||
Logger.getLogger(OAuth2SaslClientFactory.class.getName());
|
|
||||||
|
|
||||||
public static final String OAUTH_TOKEN_PROP =
|
private static final Logger logger = Logger.getLogger(OAuth2SaslClientFactory.class.getName());
|
||||||
"mail.imaps.sasl.mechanisms.oauth2.oauthToken";
|
|
||||||
|
public static final String OAUTH_TOKEN_PROP = "mail.smpt.sasl.mechanisms.oauth2.oauthToken";
|
||||||
|
|
||||||
public SaslClient createSaslClient(String[] mechanisms,
|
public SaslClient createSaslClient(String[] mechanisms,
|
||||||
String authorizationId,
|
String authorizationId, String protocol, String serverName,
|
||||||
String protocol,
|
Map<String, ?> props, CallbackHandler callbackHandler) {
|
||||||
String serverName,
|
|
||||||
Map<String, ?> props,
|
|
||||||
CallbackHandler callbackHandler) {
|
|
||||||
boolean matchedMechanism = false;
|
boolean matchedMechanism = false;
|
||||||
for (int i = 0; i < mechanisms.length; ++i) {
|
for (int i = 0; i < mechanisms.length; ++i) {
|
||||||
if ("XOAUTH2".equalsIgnoreCase(mechanisms[i])) {
|
if ("XOAUTH2".equalsIgnoreCase(mechanisms[i])) {
|
||||||
@ -52,11 +49,10 @@ public class OAuth2SaslClientFactory implements SaslClientFactory {
|
|||||||
logger.info("Failed to match any mechanisms");
|
logger.info("Failed to match any mechanisms");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return new OAuth2SaslClient((String) props.get(OAUTH_TOKEN_PROP),
|
return new OAuth2SaslClient((String) props.get(OAUTH_TOKEN_PROP), callbackHandler);
|
||||||
callbackHandler);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String[] getMechanismNames(Map<String, ?> props) {
|
public String[] getMechanismNames(Map<String, ?> props) {
|
||||||
return new String[] {"XOAUTH2"};
|
return new String[] { "XOAUTH2" };
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,24 +1,25 @@
|
|||||||
package fr.xephi.authme.mail;
|
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.AuthMe;
|
||||||
import fr.xephi.authme.ConsoleLogger;
|
import fr.xephi.authme.ConsoleLogger;
|
||||||
import fr.xephi.authme.ImageGenerator;
|
import fr.xephi.authme.ImageGenerator;
|
||||||
import fr.xephi.authme.cache.auth.PlayerAuth;
|
import fr.xephi.authme.cache.auth.PlayerAuth;
|
||||||
import fr.xephi.authme.settings.Settings;
|
import fr.xephi.authme.settings.Settings;
|
||||||
|
|
||||||
import fr.xephi.authme.util.StringUtils;
|
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
|
* @author Xephi59
|
||||||
@ -42,8 +43,7 @@ public class SendMailSSL {
|
|||||||
try {
|
try {
|
||||||
email = initializeMail(auth);
|
email = initializeMail(auth);
|
||||||
} catch (EmailException e) {
|
} catch (EmailException e) {
|
||||||
ConsoleLogger.showError("Failed to create email with the given settings: "
|
ConsoleLogger.showError("Failed to create email with the given settings: " + StringUtils.formatException(e));
|
||||||
+ StringUtils.formatException(e));
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -55,8 +55,7 @@ public class SendMailSSL {
|
|||||||
file = generateImage(auth, plugin, newPass);
|
file = generateImage(auth, plugin, newPass);
|
||||||
content = embedImageIntoEmailContent(file, email, content);
|
content = embedImageIntoEmailContent(file, email, content);
|
||||||
} catch (IOException | EmailException e) {
|
} catch (IOException | EmailException e) {
|
||||||
ConsoleLogger.showError("Unable to send new password as image for email "
|
ConsoleLogger.showError("Unable to send new password as image for email " + auth.getEmail() + ": " + StringUtils.formatException(e));
|
||||||
+ 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);
|
ImageGenerator gen = new ImageGenerator(newPass);
|
||||||
File file = new File(plugin.getDataFolder() + File.separator + auth.getNickname() + "_new_pass.jpg");
|
File file = new File(plugin.getDataFolder() + File.separator + auth.getNickname() + "_new_pass.jpg");
|
||||||
ImageIO.write(gen.generateImage(), "jpg", file);
|
ImageIO.write(gen.generateImage(), "jpg", file);
|
||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String embedImageIntoEmailContent(File image, HtmlEmail email, String content)
|
private static String embedImageIntoEmailContent(File image,
|
||||||
throws EmailException {
|
HtmlEmail email, String content) throws EmailException {
|
||||||
DataSource source = new FileDataSource(image);
|
DataSource source = new FileDataSource(image);
|
||||||
String tag = email.embed(source, image.getName());
|
String tag = email.embed(source, image.getName());
|
||||||
return content.replace("<image />", "<img src=\"cid:" + tag + "\">");
|
return content.replace("<image />", "<img src=\"cid:" + tag + "\">");
|
||||||
}
|
}
|
||||||
|
|
||||||
private static HtmlEmail initializeMail(PlayerAuth auth) throws EmailException {
|
private static HtmlEmail initializeMail(PlayerAuth auth)
|
||||||
|
throws EmailException {
|
||||||
String senderName;
|
String senderName;
|
||||||
if (StringUtils.isEmpty(Settings.getmailSenderName)) {
|
if (StringUtils.isEmpty(Settings.getmailSenderName)) {
|
||||||
senderName = Settings.getmailAccount;
|
senderName = Settings.getmailAccount;
|
||||||
@ -100,10 +101,7 @@ public class SendMailSSL {
|
|||||||
email.addTo(auth.getEmail());
|
email.addTo(auth.getEmail());
|
||||||
email.setFrom(senderMail, senderName);
|
email.setFrom(senderMail, senderName);
|
||||||
email.setSubject(Settings.getMailSubject);
|
email.setSubject(Settings.getMailSubject);
|
||||||
if (!StringUtils.isEmpty(senderMail) && !StringUtils.isEmpty(mailPassword)) {
|
email.setAuthentication(senderMail, mailPassword);
|
||||||
String password = !Settings.emailOauth2Token.isEmpty() ? "" : mailPassword;
|
|
||||||
email.setAuthenticator(new DefaultAuthenticator(senderMail, password));
|
|
||||||
}
|
|
||||||
|
|
||||||
setPropertiesForPort(email, port);
|
setPropertiesForPort(email, port);
|
||||||
return email;
|
return email;
|
||||||
@ -114,8 +112,7 @@ public class SendMailSSL {
|
|||||||
email.setHtmlMsg(content);
|
email.setHtmlMsg(content);
|
||||||
email.setTextMsg(content);
|
email.setTextMsg(content);
|
||||||
} catch (EmailException e) {
|
} catch (EmailException e) {
|
||||||
ConsoleLogger.showError("Your email.html config contains an error and cannot be sent: "
|
ConsoleLogger.showError("Your email.html config contains an error and cannot be sent: " + StringUtils.formatException(e));
|
||||||
+ StringUtils.formatException(e));
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
@ -127,29 +124,32 @@ public class SendMailSSL {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String replaceMailTags(String mailText, AuthMe plugin, PlayerAuth auth, String newPass) {
|
private static String replaceMailTags(String mailText, AuthMe plugin,
|
||||||
return mailText
|
PlayerAuth auth, String newPass) {
|
||||||
.replace("<playername />", auth.getNickname())
|
return mailText.replace("<playername />", auth.getNickname()).replace("<servername />", plugin.getServer().getServerName()).replace("<generatedpass />", newPass);
|
||||||
.replace("<servername />", plugin.getServer().getServerName())
|
|
||||||
.replace("<generatedpass />", newPass);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void setPropertiesForPort(HtmlEmail email, int port) throws EmailException {
|
private static void setPropertiesForPort(HtmlEmail email, int port)
|
||||||
|
throws EmailException {
|
||||||
switch (port) {
|
switch (port) {
|
||||||
case 587:
|
case 587:
|
||||||
email.setStartTLSEnabled(true);
|
|
||||||
email.setStartTLSRequired(true);
|
|
||||||
if (!Settings.emailOauth2Token.isEmpty()) {
|
if (!Settings.emailOauth2Token.isEmpty()) {
|
||||||
if (Security.getProvider("Google OAuth2 Provider") == null) {
|
if (Security.getProvider("Google OAuth2 Provider") == null) {
|
||||||
Security.addProvider(new OAuth2Provider());
|
Security.addProvider(new OAuth2Provider());
|
||||||
}
|
}
|
||||||
|
|
||||||
Properties mailProperties = email.getMailSession().getProperties();
|
Properties mailProperties = email.getMailSession().getProperties();
|
||||||
mailProperties.setProperty("mail.smtp.starttls.enable", "true");
|
mailProperties.setProperty("mail.smtp.ssl.enable", "true");
|
||||||
mailProperties.setProperty("mail.smtp.starttls.required", "true");
|
mailProperties.setProperty("mail.smtp.auth.mechanisms", "XOAUTH2");
|
||||||
mailProperties.setProperty("mail.smtp.sasl.enable", "true");
|
mailProperties.setProperty("mail.smtp.sasl.enable", "true");
|
||||||
mailProperties.setProperty("mail.smtp.sasl.mechanisms", "XOAUTH2");
|
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;
|
break;
|
||||||
case 25:
|
case 25:
|
||||||
@ -157,8 +157,8 @@ public class SendMailSSL {
|
|||||||
email.setSSLCheckServerIdentity(true);
|
email.setSSLCheckServerIdentity(true);
|
||||||
break;
|
break;
|
||||||
case 465:
|
case 465:
|
||||||
email.setSSLOnConnect(true);
|
email.setSslSmtpPort("" + port);
|
||||||
email.setSSLCheckServerIdentity(true);
|
email.setSSL(true);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
email.setStartTLSEnabled(true);
|
email.setStartTLSEnabled(true);
|
||||||
|
Loading…
Reference in New Issue
Block a user