Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Aug 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 3 additions & 13 deletions org.adempiere.base/src/org/compiere/util/EMail.java
Expand Up @@ -46,7 +46,6 @@
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;

Expand Down Expand Up @@ -273,13 +272,6 @@ public String send ()
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
{
Expand All @@ -297,13 +289,12 @@ public String send ()
{
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());
Expand Down Expand Up @@ -597,14 +588,13 @@ public String getMessageID()
*/
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;
Expand Down
35 changes: 32 additions & 3 deletions org.adempiere.base/src/org/compiere/util/EMailAuthenticator.java
Expand Up @@ -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
*
Expand All @@ -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)
{
Expand All @@ -51,18 +70,28 @@ public EMailAuthenticator (String username, String password)

/** 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
Expand Down