-
Notifications
You must be signed in to change notification settings - Fork 122
EMAIL-163 Support for OAuth2 authentication #354
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
base: master
Are you sure you want to change the base?
Changes from all commits
5256ea9
971fa86
f1d76f2
ba7654b
2c791c3
90d5cae
e401e17
b08699f
b14cdf4
ecc7c02
1acfebd
e95c350
1491d05
23dbc39
82700b4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
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 void testGetSetAuthentication() { | |
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")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reuse |
||
} | ||
|
||
@Test | ||
public void testGetSetAuthenticator() { | ||
// setup | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -213,6 +213,11 @@ public abstract class Email { | |
*/ | ||
private boolean startTlsRequired; | ||
|
||
/** | ||
* If true, uses OAuth2 for authentication. | ||
*/ | ||
private boolean oauth2Required; | ||
|
||
/** | ||
* Does the current transport use SSL/TLS encryption upon connection? | ||
*/ | ||
|
@@ -822,6 +827,10 @@ public Session getMailSession() throws EmailException { | |
properties.setProperty(EmailConstants.MAIL_SMTP_AUTH, "true"); | ||
} | ||
|
||
if (isOAuth2Required()) { | ||
properties.put(EmailConstants.MAIL_SMTP_AUTH_MECHANISMS, "XOAUTH2"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Refactor |
||
} | ||
|
||
if (isSSLOnConnect()) { | ||
properties.setProperty(EmailConstants.MAIL_PORT, sslSmtpPort); | ||
properties.setProperty(EmailConstants.MAIL_SMTP_SOCKET_FACTORY_PORT, sslSmtpPort); | ||
|
@@ -1061,6 +1070,16 @@ public boolean isStartTLSRequired() { | |
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. | ||
* | ||
|
@@ -1639,6 +1658,20 @@ public Email setStartTLSRequired(final boolean startTlsRequired) { | |
return this; | ||
} | ||
|
||
/** | ||
* Sets or disables OAuth2 authentication. | ||
* | ||
* @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. | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
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 void testGetSetAuthentication() { | |
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")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reuse |
||
} | ||
|
||
@Test | ||
public void testGetSetAuthenticator() { | ||
// setup | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Javadoc since tag is missing.