IDEMPIERE-4909 Allow empty password when using OAuth2 - refactor to allow sending email without AD_User (FHCA-2892) (#824)

This commit is contained in:
Carlos Ruiz 2021-08-12 17:06:44 +02:00 committed by GitHub
parent 729fc4a61e
commit 8ec5029e8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 16 deletions

View File

@ -46,7 +46,6 @@ import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import org.compiere.model.MAuthorizationAccount;
import org.compiere.model.MClient;
import org.compiere.model.MSysConfig;
@ -273,13 +272,6 @@ public final class EMail implements Serializable
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;
try
{
@ -297,13 +289,12 @@ public final class EMail implements Serializable
{
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.starttls.required", "true");
props.put("mail.smtp.auth.login.disable","true");
props.put("mail.smtp.auth.plain.disable","true");
props.put("mail.debug.auth", "true");
m_auth = new EMailAuthenticator (m_auth.getPasswordAuthentication().getUserName(), authAccount.refreshAndGetAccessToken());
}
session = Session.getInstance(props);
session.setDebug(CLogMgt.isLevelFinest());
@ -597,14 +588,13 @@ public final class EMail implements Serializable
*/
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;
}
else
{
// log.fine("setEMailUser: " + username + "/" + password);
m_auth = new EMailAuthenticator (username, password);
}
return m_auth;

View File

@ -16,11 +16,16 @@
*****************************************************************************/
package org.compiere.util;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.logging.Level;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import org.adempiere.exceptions.AdempiereException;
import org.compiere.model.MAuthorizationAccount;
/**
* Email User Authentification
*
@ -32,10 +37,24 @@ public class EMailAuthenticator extends Authenticator
/**
* Constructor
* @param username user name
* @param password user password
* @param password user password (ignored if is OAuth2 account)
*/
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);
if (username == null || username.length() == 0)
{
@ -51,18 +70,28 @@ public class EMailAuthenticator extends Authenticator
/** Password */
private PasswordAuthentication m_pass = null;
/** Is OAuth2 */
private boolean m_isOAuth2 = false;
/** Logger */
private static CLogger log = CLogger.getCLogger(EMailAuthenticator.class);
/**
* Ger PasswordAuthentication
* @return Password Autnetifucation
* Get Password Authentication
* @return Password Authentication
*/
protected PasswordAuthentication getPasswordAuthentication()
{
return m_pass;
} // getPasswordAuthentication
/**
* If the authenticator is using OAuth2 account
* @return boolean
*/
protected boolean isOAuth2() {
return m_isOAuth2;
}
/**
* Get String representation
* @return info