IDEMPIERE-4909 Allow empty password when using OAuth2 - refactor to allow sending email without AD_User (FHCA-2892) (#824)
This commit is contained in:
parent
729fc4a61e
commit
8ec5029e8e
|
@ -46,7 +46,6 @@ import javax.mail.internet.MimeBodyPart;
|
||||||
import javax.mail.internet.MimeMessage;
|
import javax.mail.internet.MimeMessage;
|
||||||
import javax.mail.internet.MimeMultipart;
|
import javax.mail.internet.MimeMultipart;
|
||||||
|
|
||||||
import org.compiere.model.MAuthorizationAccount;
|
|
||||||
import org.compiere.model.MClient;
|
import org.compiere.model.MClient;
|
||||||
import org.compiere.model.MSysConfig;
|
import org.compiere.model.MSysConfig;
|
||||||
|
|
||||||
|
@ -273,13 +272,6 @@ public final class EMail implements Serializable
|
||||||
props.put("mail.debug", "true");
|
props.put("mail.debug", "true");
|
||||||
//
|
//
|
||||||
|
|
||||||
MAuthorizationAccount authAccount = null;
|
|
||||||
boolean isOAuth2 = false;
|
|
||||||
if (m_auth != null) {
|
|
||||||
authAccount = MAuthorizationAccount.getEMailAccount(m_auth.getPasswordAuthentication().getUserName());
|
|
||||||
isOAuth2 = (authAccount != null);
|
|
||||||
}
|
|
||||||
|
|
||||||
Session session = null;
|
Session session = null;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
@ -297,13 +289,12 @@ public final class EMail implements Serializable
|
||||||
{
|
{
|
||||||
props.put("mail.smtp.starttls.enable", "true");
|
props.put("mail.smtp.starttls.enable", "true");
|
||||||
}
|
}
|
||||||
if (isOAuth2) {
|
if (m_auth != null && m_auth.isOAuth2()) {
|
||||||
props.put("mail.smtp.auth.mechanisms", "XOAUTH2");
|
props.put("mail.smtp.auth.mechanisms", "XOAUTH2");
|
||||||
props.put("mail.smtp.starttls.required", "true");
|
props.put("mail.smtp.starttls.required", "true");
|
||||||
props.put("mail.smtp.auth.login.disable","true");
|
props.put("mail.smtp.auth.login.disable","true");
|
||||||
props.put("mail.smtp.auth.plain.disable","true");
|
props.put("mail.smtp.auth.plain.disable","true");
|
||||||
props.put("mail.debug.auth", "true");
|
props.put("mail.debug.auth", "true");
|
||||||
m_auth = new EMailAuthenticator (m_auth.getPasswordAuthentication().getUserName(), authAccount.refreshAndGetAccessToken());
|
|
||||||
}
|
}
|
||||||
session = Session.getInstance(props);
|
session = Session.getInstance(props);
|
||||||
session.setDebug(CLogMgt.isLevelFinest());
|
session.setDebug(CLogMgt.isLevelFinest());
|
||||||
|
@ -597,14 +588,13 @@ public final class EMail implements Serializable
|
||||||
*/
|
*/
|
||||||
public EMailAuthenticator createAuthenticator (String username, String password)
|
public EMailAuthenticator createAuthenticator (String username, String password)
|
||||||
{
|
{
|
||||||
if (username == null || password == null)
|
if (username == null)
|
||||||
{
|
{
|
||||||
log.warning("Ignored - " + username + "/" + password);
|
log.warning("Ignored - username null");
|
||||||
m_auth = null;
|
m_auth = null;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// log.fine("setEMailUser: " + username + "/" + password);
|
|
||||||
m_auth = new EMailAuthenticator (username, password);
|
m_auth = new EMailAuthenticator (username, password);
|
||||||
}
|
}
|
||||||
return m_auth;
|
return m_auth;
|
||||||
|
|
|
@ -16,11 +16,16 @@
|
||||||
*****************************************************************************/
|
*****************************************************************************/
|
||||||
package org.compiere.util;
|
package org.compiere.util;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.security.GeneralSecurityException;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
|
||||||
import javax.mail.Authenticator;
|
import javax.mail.Authenticator;
|
||||||
import javax.mail.PasswordAuthentication;
|
import javax.mail.PasswordAuthentication;
|
||||||
|
|
||||||
|
import org.adempiere.exceptions.AdempiereException;
|
||||||
|
import org.compiere.model.MAuthorizationAccount;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Email User Authentification
|
* Email User Authentification
|
||||||
*
|
*
|
||||||
|
@ -32,10 +37,24 @@ public class EMailAuthenticator extends Authenticator
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
* @param username user name
|
* @param username user name
|
||||||
* @param password user password
|
* @param password user password (ignored if is OAuth2 account)
|
||||||
*/
|
*/
|
||||||
public EMailAuthenticator (String username, String password)
|
public EMailAuthenticator (String username, String password)
|
||||||
{
|
{
|
||||||
|
MAuthorizationAccount authAccount = MAuthorizationAccount.getEMailAccount(username);
|
||||||
|
if (authAccount != null)
|
||||||
|
{
|
||||||
|
m_isOAuth2 = true;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
password = authAccount.refreshAndGetAccessToken();
|
||||||
|
}
|
||||||
|
catch (GeneralSecurityException | IOException e)
|
||||||
|
{
|
||||||
|
throw new AdempiereException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
m_pass = new PasswordAuthentication (username, password);
|
m_pass = new PasswordAuthentication (username, password);
|
||||||
if (username == null || username.length() == 0)
|
if (username == null || username.length() == 0)
|
||||||
{
|
{
|
||||||
|
@ -51,18 +70,28 @@ public class EMailAuthenticator extends Authenticator
|
||||||
|
|
||||||
/** Password */
|
/** Password */
|
||||||
private PasswordAuthentication m_pass = null;
|
private PasswordAuthentication m_pass = null;
|
||||||
|
/** Is OAuth2 */
|
||||||
|
private boolean m_isOAuth2 = false;
|
||||||
/** Logger */
|
/** Logger */
|
||||||
private static CLogger log = CLogger.getCLogger(EMailAuthenticator.class);
|
private static CLogger log = CLogger.getCLogger(EMailAuthenticator.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Ger PasswordAuthentication
|
* Get Password Authentication
|
||||||
* @return Password Autnetifucation
|
* @return Password Authentication
|
||||||
*/
|
*/
|
||||||
protected PasswordAuthentication getPasswordAuthentication()
|
protected PasswordAuthentication getPasswordAuthentication()
|
||||||
{
|
{
|
||||||
return m_pass;
|
return m_pass;
|
||||||
} // getPasswordAuthentication
|
} // getPasswordAuthentication
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If the authenticator is using OAuth2 account
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
protected boolean isOAuth2() {
|
||||||
|
return m_isOAuth2;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get String representation
|
* Get String representation
|
||||||
* @return info
|
* @return info
|
||||||
|
|
Loading…
Reference in New Issue