You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by sg...@apache.org on 2024/11/22 21:10:32 UTC

(commons-email) branch Features/EMAIL-163 created (now 5256ea9)

This is an automated email from the ASF dual-hosted git repository.

sgoeschl pushed a change to branch Features/EMAIL-163
in repository https://gitbox.apache.org/repos/asf/commons-email.git


      at 5256ea9  EMAIL-163 Support for OAuth2 authentication

This branch includes the following new commits:

     new 5256ea9  EMAIL-163 Support for OAuth2 authentication

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(commons-email) 01/01: EMAIL-163 Support for OAuth2 authentication

Posted by sg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

sgoeschl pushed a commit to branch Features/EMAIL-163
in repository https://gitbox.apache.org/repos/asf/commons-email.git

commit 5256ea99e7d0b4521f516662c49ddac98d5ff402
Author: sgoeschl <si...@gmail.com>
AuthorDate: Fri Nov 22 22:09:51 2024 +0100

    EMAIL-163 Support for OAuth2 authentication
---
 .../apache/commons/mail2/core/EmailConstants.java  |  3 ++
 .../org/apache/commons/mail2/jakarta/Email.java    | 36 ++++++++++++++++++++++
 .../apache/commons/mail2/jakarta/EmailTest.java    | 12 ++++++++
 .../java/org/apache/commons/mail2/javax/Email.java | 36 ++++++++++++++++++++++
 .../org/apache/commons/mail2/javax/EmailTest.java  | 12 ++++++++
 5 files changed, 99 insertions(+)

diff --git a/commons-email2-core/src/main/java/org/apache/commons/mail2/core/EmailConstants.java b/commons-email2-core/src/main/java/org/apache/commons/mail2/core/EmailConstants.java
index 38e2e00..6103006 100644
--- a/commons-email2-core/src/main/java/org/apache/commons/mail2/core/EmailConstants.java
+++ b/commons-email2-core/src/main/java/org/apache/commons/mail2/core/EmailConstants.java
@@ -56,6 +56,9 @@ public final class EmailConstants {
     /** If set to true, tries to authenticate the user using the AUTH command. */
     public static final String MAIL_SMTP_AUTH = "mail.smtp.auth";
 
+    /** If set to true, tries to authenticate the user using the OAuth2. */
+    public static final String MAIL_SMTP_AUTH_MECHANISMS = "mail.smtp.auth.mechanisms";
+
     /** The SMTP user name. */
     public static final String MAIL_SMTP_USER = "mail.smtp.user";
 
diff --git a/commons-email2-jakarta/src/main/java/org/apache/commons/mail2/jakarta/Email.java b/commons-email2-jakarta/src/main/java/org/apache/commons/mail2/jakarta/Email.java
index bf5819a..dd64473 100644
--- a/commons-email2-jakarta/src/main/java/org/apache/commons/mail2/jakarta/Email.java
+++ b/commons-email2-jakarta/src/main/java/org/apache/commons/mail2/jakarta/Email.java
@@ -214,6 +214,11 @@ public abstract class Email {
      */
     private boolean startTlsRequired;
 
+    /**
+     * If true, use OAuth2 for authentication.
+     */
+    private boolean oauth2Required;
+
     /**
      * Does the current transport use SSL/TLS encryption upon connection?
      */
@@ -818,6 +823,10 @@ public abstract class Email {
                 properties.setProperty(EmailConstants.MAIL_SMTP_AUTH, "true");
             }
 
+            if(isOAuth2Required()) {
+                properties.put(EmailConstants.MAIL_SMTP_AUTH_MECHANISMS, "XOAUTH2");
+            }
+
             if (isSSLOnConnect()) {
                 properties.setProperty(EmailConstants.MAIL_PORT, sslSmtpPort);
                 properties.setProperty(EmailConstants.MAIL_SMTP_SOCKET_FACTORY_PORT, sslSmtpPort);
@@ -1057,6 +1066,16 @@ public abstract class Email {
         return startTlsRequired;
     }
 
+    /**
+     * Tests whether the client is configured to use OAuth2 authentication.
+     *
+     * @return true if using OAuth2 for authentication, false otherwise.
+     * @since 2.0
+     */
+    public boolean isOAuth2Required() {
+        return oauth2Required;
+    }
+
     /**
      * Sends the email. Internally we build a MimeMessage which is afterwards sent to the SMTP server.
      *
@@ -1635,6 +1654,23 @@ public abstract class Email {
         return this;
     }
 
+    /**
+     * Sets or disable OAuth2 authentication.
+     * <p>
+     * Defaults to {@link #smtpPort}; can be overridden by using {@link #setSmtpPort(int)}
+     * </p>
+     *
+     * @param oauth2Required true if OAUth2 authentication is required, false otherwise
+     * @return An Email.
+     * @throws IllegalStateException if the mail session is already initialized
+     * @since 2.0
+     */
+    public Email setOAuth2Required(final boolean oauth2Required) {
+        checkSessionAlreadyInitialized();
+        this.oauth2Required = oauth2Required;
+        return this;
+    }
+
     /**
      * Sets the email subject. Replaces end-of-line characters with spaces.
      *
diff --git a/commons-email2-jakarta/src/test/java/org/apache/commons/mail2/jakarta/EmailTest.java b/commons-email2-jakarta/src/test/java/org/apache/commons/mail2/jakarta/EmailTest.java
index 7d72698..6d7d4c9 100644
--- a/commons-email2-jakarta/src/test/java/org/apache/commons/mail2/jakarta/EmailTest.java
+++ b/commons-email2-jakarta/src/test/java/org/apache/commons/mail2/jakarta/EmailTest.java
@@ -19,6 +19,7 @@ package org.apache.commons.mail2.jakarta;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertNull;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -571,6 +572,17 @@ public class EmailTest extends AbstractEmailTest {
         assertEquals(strPassword, retrievedAuth.getPasswordAuthentication().getPassword());
     }
 
+    @Test
+    public void testSetOAuth2Required() throws EmailException {
+        email.setHostName(strTestMailServer);
+        email.setOAuth2Required(true);
+        final Session mailSession = email.getMailSession();
+
+        // tests
+        assertNotNull(mailSession);
+        assertEquals("XOAUTH2", mailSession.getProperties().getProperty("mail.smtp.auth.mechanisms"));
+    }
+
     @Test
     public void testGetSetAuthenticator() {
         // setup
diff --git a/commons-email2-javax/src/main/java/org/apache/commons/mail2/javax/Email.java b/commons-email2-javax/src/main/java/org/apache/commons/mail2/javax/Email.java
index b9d5d2f..8cccb4b 100644
--- a/commons-email2-javax/src/main/java/org/apache/commons/mail2/javax/Email.java
+++ b/commons-email2-javax/src/main/java/org/apache/commons/mail2/javax/Email.java
@@ -213,6 +213,11 @@ public abstract class Email {
      */
     private boolean startTlsRequired;
 
+    /**
+     * If true, use OAuth2 for authentication.
+     */
+    private boolean oauth2Required;
+
     /**
      * Does the current transport use SSL/TLS encryption upon connection?
      */
@@ -817,6 +822,10 @@ public abstract class Email {
                 properties.setProperty(EmailConstants.MAIL_SMTP_AUTH, "true");
             }
 
+            if(isOAuth2Required()) {
+                properties.put(EmailConstants.MAIL_SMTP_AUTH_MECHANISMS, "XOAUTH2");
+            }
+
             if (isSSLOnConnect()) {
                 properties.setProperty(EmailConstants.MAIL_PORT, sslSmtpPort);
                 properties.setProperty(EmailConstants.MAIL_SMTP_SOCKET_FACTORY_PORT, sslSmtpPort);
@@ -1056,6 +1065,16 @@ public abstract class Email {
         return startTlsRequired;
     }
 
+    /**
+     * Tests whether the client is configured to use OAuth2 authentication.
+     *
+     * @return true if using OAuth2 for authentication, false otherwise.
+     * @since 2.0
+     */
+    public boolean isOAuth2Required() {
+        return oauth2Required;
+    }
+
     /**
      * Sends the email. Internally we build a MimeMessage which is afterwards sent to the SMTP server.
      *
@@ -1634,6 +1653,23 @@ public abstract class Email {
         return this;
     }
 
+    /**
+     * Sets or disable OAuth2 authentication.
+     * <p>
+     * Defaults to {@link #smtpPort}; can be overridden by using {@link #setSmtpPort(int)}
+     * </p>
+     *
+     * @param oauth2Required true if OAUth2 authentication is required, false otherwise
+     * @return An Email.
+     * @throws IllegalStateException if the mail session is already initialized
+     * @since 2.0
+     */
+    public Email setOAuth2Required(final boolean oauth2Required) {
+        checkSessionAlreadyInitialized();
+        this.oauth2Required = oauth2Required;
+        return this;
+    }
+
     /**
      * Sets the email subject. Replaces end-of-line characters with spaces.
      *
diff --git a/commons-email2-javax/src/test/java/org/apache/commons/mail2/javax/EmailTest.java b/commons-email2-javax/src/test/java/org/apache/commons/mail2/javax/EmailTest.java
index 8adf2f0..e22b521 100644
--- a/commons-email2-javax/src/test/java/org/apache/commons/mail2/javax/EmailTest.java
+++ b/commons-email2-javax/src/test/java/org/apache/commons/mail2/javax/EmailTest.java
@@ -19,6 +19,7 @@ package org.apache.commons.mail2.javax;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertNull;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -571,6 +572,17 @@ public class EmailTest extends AbstractEmailTest {
         assertEquals(strPassword, retrievedAuth.getPasswordAuthentication().getPassword());
     }
 
+    @Test
+    public void testSetOAuth2Required() throws EmailException {
+        email.setHostName(strTestMailServer);
+        email.setOAuth2Required(true);
+        final Session mailSession = email.getMailSession();
+
+        // tests
+        assertNotNull(mailSession);
+        assertEquals("XOAUTH2", mailSession.getProperties().getProperty("mail.smtp.auth.mechanisms"));
+    }
+
     @Test
     public void testGetSetAuthenticator() {
         // setup