Skip to content

Commit 9fa0451

Browse files
committed
Switched from Google OAuth2 to the built-in OAuth2 support
1 parent fe8c671 commit 9fa0451

File tree

18 files changed

+92
-136
lines changed

18 files changed

+92
-136
lines changed

modules/batch-module/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
<dependency>
3535
<groupId>org.simplejavamail</groupId>
3636
<artifactId>smtp-connection-pool</artifactId>
37-
<version>2.1.0</version>
37+
<version>2.1.1</version>
3838
</dependency>
3939
</dependencies>
4040
</project>

modules/batch-module/src/main/java/org/simplejavamail/internal/batchsupport/BatchSupport.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
import org.jetbrains.annotations.Nullable;
99
import org.simplejavamail.api.internal.batchsupport.LifecycleDelegatingTransport;
1010
import org.simplejavamail.api.mailer.config.OperationalConfig;
11+
import org.simplejavamail.api.mailer.config.TransportStrategy;
1112
import org.simplejavamail.internal.batchsupport.concurrent.NonJvmBlockingThreadPoolExecutor;
1213
import org.simplejavamail.internal.modules.BatchModule;
1314
import org.simplejavamail.internal.util.concurrent.AsyncOperationHelper;
15+
import org.simplejavamail.smtpconnectionpool.SmtpConnectionPool;
1416
import org.simplejavamail.smtpconnectionpool.SmtpConnectionPoolClustered;
1517
import org.slf4j.Logger;
1618
import org.slf4j.LoggerFactory;
@@ -93,6 +95,7 @@ private void ensureClusterInitialized(@NotNull OperationalConfig operationalConf
9395
public LifecycleDelegatingTransport acquireTransport(@NotNull final UUID clusterKey, @NotNull final Session session, boolean stickySession) {
9496
try {
9597
requireNonNull(smtpConnectionPool, "Connection pool used before it was initialized. This shouldn't be possible.");
98+
checkConfigureOAuth2Token(session);
9699
final PoolableObject<Transport> pooledTransport = stickySession
97100
? smtpConnectionPool.claimResourceFromPool(new ResourceClusterAndPoolKey<>(clusterKey, session))
98101
: smtpConnectionPool.claimResourceFromCluster(clusterKey);
@@ -102,6 +105,15 @@ public LifecycleDelegatingTransport acquireTransport(@NotNull final UUID cluster
102105
}
103106
}
104107

108+
// since the SMTP connection pool doesn't know about Simple Java Mail,
109+
// it won't know where to look for the OAUTH2 token unless we copy the property
110+
private void checkConfigureOAuth2Token(Session session) {
111+
if (session.getProperties().containsKey(TransportStrategy.OAUTH2_TOKEN_PROPERTY)) {
112+
session.getProperties().setProperty(SmtpConnectionPool.OAUTH2_TOKEN_PROPERTY,
113+
session.getProperties().getProperty(TransportStrategy.OAUTH2_TOKEN_PROPERTY));
114+
}
115+
}
116+
105117
/**
106118
* @see BatchModule#shutdownConnectionPools(Session)
107119
*/

modules/core-module/src/main/java/org/simplejavamail/api/mailer/MailerRegularBuilder.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,6 @@ public interface MailerRegularBuilder<T extends MailerRegularBuilder<?>> extends
4848
*/
4949
T withSMTPServer(@Nullable String host, @Nullable Integer port, @Nullable String username, @Nullable String password);
5050

51-
/**
52-
* Delegates to {@link #withSMTPServerHost(String)}, {@link #withSMTPServerPort(Integer)}, {@link #withSMTPServerUsername(String)} and {@link
53-
* #withSMTPOAuth2Token(String)}.
54-
*
55-
* @param host Optional host that defaults to pre-configured property if left empty.
56-
* @param port Optional port number that defaults to pre-configured property if left empty.
57-
* @param username Optional username that defaults to pre-configured property if left empty.
58-
* @param oauth2Token Optional oauth2Token that defaults to pre-configured property if left empty.
59-
*/
60-
T withSMTPServerOAuth2(@Nullable String host, @Nullable Integer port, @Nullable String username, @Nullable String oauth2Token);
61-
6251
/**
6352
* Delegates to {@link #withSMTPServerHost(String)}, {@link #withSMTPServerPort(Integer)} and {@link #withSMTPServerUsername(String)}.
6453
*
@@ -111,14 +100,6 @@ public interface MailerRegularBuilder<T extends MailerRegularBuilder<?>> extends
111100
@Cli.ExcludeApi(reason = "API is a subset of another API method")
112101
T withSMTPServerPassword(@Nullable String password);
113102

114-
/**
115-
* Sets the optional OAuth2 token. Will default to pre-configured property if left empty.
116-
*
117-
* @param oauth2Token Optional oauth2 token that defaults to pre-configured property if left empty.
118-
*/
119-
@Cli.ExcludeApi(reason = "API is a subset of another API method")
120-
T withSMTPOAuth2Token(@Nullable String oauth2Token);
121-
122103
/**
123104
* Configures the session with the right property to use your own factory for obtaining SSL connections.
124105
* <p>

modules/core-module/src/main/java/org/simplejavamail/api/mailer/config/ServerConfig.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ public interface ServerConfig {
1010
@NotNull Integer getPort();
1111
@Nullable String getUsername();
1212
@Nullable String getPassword();
13-
@Nullable String getOAuth2Token();
1413
@Nullable String getCustomSSLFactoryClass();
1514
@Nullable SSLSocketFactory getCustomSSLFactoryInstance();
1615
}

modules/core-module/src/main/java/org/simplejavamail/api/mailer/config/TransportStrategy.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,7 @@ public String propertyNameCheckServerIdentity() {
496496

497497
/**
498498
* @see TransportStrategy#SMTP_OAUTH2
499+
* @see <a href="https://javaee.github.io/javamail/OAuth2">https://javaee.github.io/javamail/OAuth2</a>
499500
*/
500501
@Override
501502
public Properties generateProperties() {
@@ -504,8 +505,7 @@ public Properties generateProperties() {
504505
props.put("mail.smtp.starttls.enable", "true");
505506
props.put("mail.smtp.starttls.required", "true");
506507

507-
props.put("mail.smtp.sasl.enable", "true");
508-
props.put("mail.smtp.sasl.mechanisms", "XOAUTH2");
508+
props.put("mail.smtp.auth.mechanisms", "XOAUTH2");
509509
return props;
510510
}
511511

@@ -626,7 +626,8 @@ public String propertyNameCheckServerIdentity() {
626626
*/
627627
private static final String TRANSPORT_STRATEGY_MARKER = "simplejavamail.transportstrategy";
628628

629-
public static final String OAUTH2_TOKEN_PROPERTY = "mail.imaps.sasl.mechanisms.oauth2.oauthToken";
629+
// FIXME is this still the right place?
630+
public static final String OAUTH2_TOKEN_PROPERTY = "simplejavamail.oauth2.token";
630631

631632
/**
632633
* For internal use only.

modules/core-module/src/main/java/org/simplejavamail/config/ConfigLoader.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
* <li>simplejavamail.smtp.port</li>
3636
* <li>simplejavamail.smtp.username</li>
3737
* <li>simplejavamail.smtp.password</li>
38-
* <li>simplejavamail.smtp.oauth2token</li>
3938
* <li>simplejavamail.disable.all.clientvalidation</li>
4039
* <li>simplejavamail.custom.sslfactory.class</li>
4140
* <li>simplejavamail.proxy.host</li>
@@ -129,7 +128,6 @@ public enum Property {
129128
SMTP_PORT("simplejavamail.smtp.port"),
130129
SMTP_USERNAME("simplejavamail.smtp.username"),
131130
SMTP_PASSWORD("simplejavamail.smtp.password"),
132-
SMTP_OAUTH2_TOKEN("simplejavamail.smtp.oauth2token"),
133131
DISABLE_ALL_CLIENTVALIDATION("simplejavamail.disable.all.clientvalidation"),
134132
CUSTOM_SSLFACTORY_CLASS("simplejavamail.custom.sslfactory.class"),
135133
PROXY_HOST("simplejavamail.proxy.host"),

modules/core-test-module/src/main/java/demo/DemoAppBase.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ public class DemoAppBase {
1313

1414
static final Logger LOGGER = LoggerFactory.getLogger(DemoAppBase.class);
1515

16-
static final String YOUR_GMAIL_ADDRESS = "yourname@gmail.com";
16+
static final String YOUR_GMAIL_ADDRESS = "your_gmail_user@gmail.com";
1717

18-
// if you have 2-factor login turned on, you need to generate a once-per app password
18+
// if you have 2-factor login turned on, you need to generate a once-per app password:
1919
// https://security.google.com/settings/security/apppasswords
2020
private static final String YOUR_GMAIL_PASSWORD = "<your password>";
2121

modules/simple-java-mail/pom.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,6 @@
3131
<version>${project.version}</version>
3232
<scope>compile</scope>
3333
</dependency>
34-
<dependency>
35-
<groupId>org.simplejavamail</groupId>
36-
<artifactId>google-oauth2-provider</artifactId>
37-
<version>1.0.1</version>
38-
<scope>compile</scope>
39-
</dependency>
4034

4135
<!-- optional support modules -->
4236
<dependency>

modules/simple-java-mail/src/main/java/org/simplejavamail/mailer/internal/MailerException.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ class MailerException extends MailException {
1010

1111
static final String ERROR_READING_SMIME_FROM_INPUTSTREAM = "Was unable to read S/MIME data from input stream";
1212
static final String ERROR_READING_FROM_FILE = "Error reading from file: %s";
13-
static final String CANNOT_USE_BOTH_PASSWORD_AND_OAUTH2 = "Either authenticate using password or OAuth2 token, but not both";
14-
static final String MISSING_OAUTH2_TOKEN = "TransportStrategy is OAUTH2 but no OAUTH2 token provided";
15-
static final String WRONG_TRANSPORTSTRATEGY_FOR_OAUTH2_TOKEN = "OAUTH2 token provided, but TransportStrategy was %s instead of OAUTH2";
13+
static final String MISSING_OAUTH2_TOKEN = "TransportStrategy is OAUTH2 but no OAUTH2 token provided as password";
1614
static final String INVALID_PROXY_SLL_COMBINATION = "Proxy is not supported for SSL connections (this is a limitation by the underlying JavaMail framework)";
1715
static final String ERROR_CONNECTING_SMTP_SERVER = "Was unable to connect to SMTP server";
1816
static final String GENERIC_ERROR = "Failed to send email [%s], reason: Third party error";

modules/simple-java-mail/src/main/java/org/simplejavamail/mailer/internal/MailerImpl.java

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,21 @@
1010
import org.simplejavamail.api.mailer.Mailer;
1111
import org.simplejavamail.api.mailer.config.*;
1212
import org.simplejavamail.config.ConfigLoader;
13-
import org.simplejavamail.google.code.samples.oauth2.GoogleOAuth2Provider;
1413
import org.simplejavamail.internal.moduleloader.ModuleLoader;
1514
import org.simplejavamail.internal.util.concurrent.AsyncOperationHelper;
1615
import org.simplejavamail.mailer.MailerHelper;
1716
import org.simplejavamail.mailer.internal.util.SmtpAuthenticator;
1817
import org.slf4j.Logger;
1918
import org.slf4j.LoggerFactory;
2019

21-
import java.security.Security;
2220
import java.util.List;
2321
import java.util.Properties;
2422
import java.util.concurrent.CompletableFuture;
2523
import java.util.concurrent.Future;
2624
import java.util.concurrent.atomic.AtomicInteger;
2725

28-
import static java.lang.String.format;
2926
import static java.util.Optional.ofNullable;
27+
import static org.simplejavamail.api.mailer.config.TransportStrategy.SMTP_OAUTH2;
3028
import static org.simplejavamail.api.mailer.config.TransportStrategy.findStrategyForSession;
3129
import static org.simplejavamail.config.ConfigLoader.Property.EXTRA_PROPERTIES;
3230
import static org.simplejavamail.internal.util.ListUtil.getFirst;
@@ -166,26 +164,19 @@ public static Session createMailSession(@NotNull final ServerConfig serverConfig
166164
props.putAll(ConfigLoader.getProperty(EXTRA_PROPERTIES));
167165
}
168166

169-
if (serverConfig.getPassword() != null && serverConfig.getOAuth2Token() != null) {
170-
throw new MailerException(MailerException.CANNOT_USE_BOTH_PASSWORD_AND_OAUTH2);
171-
}
172-
173-
if (serverConfig.getOAuth2Token() != null && transportStrategy != TransportStrategy.SMTP_OAUTH2) {
174-
throw new MailerException(format(MailerException.WRONG_TRANSPORTSTRATEGY_FOR_OAUTH2_TOKEN, transportStrategy));
175-
}
176-
177-
if (transportStrategy == TransportStrategy.SMTP_OAUTH2 && serverConfig.getOAuth2Token() == null) {
167+
if (transportStrategy == TransportStrategy.SMTP_OAUTH2 && serverConfig.getPassword() == null) {
178168
throw new MailerException(MailerException.MISSING_OAUTH2_TOKEN);
179169
}
180170

181171
if (serverConfig.getPassword() != null) {
182-
props.put(transportStrategy.propertyNameAuthenticate(), "true");
183-
return Session.getInstance(props, new SmtpAuthenticator(serverConfig));
184-
} else if (serverConfig.getOAuth2Token() != null) {
185-
props.put(transportStrategy.propertyNameAuthenticate(), "false");
186-
props.put(TransportStrategy.OAUTH2_TOKEN_PROPERTY, serverConfig.getOAuth2Token());
187-
Security.addProvider(new GoogleOAuth2Provider());
188-
return Session.getInstance(props);
172+
if (transportStrategy != SMTP_OAUTH2) {
173+
props.put(transportStrategy.propertyNameAuthenticate(), "true");
174+
return Session.getInstance(props, new SmtpAuthenticator(serverConfig));
175+
} else {
176+
// props.put(transportStrategy.propertyNameAuthenticate(), "false");
177+
props.put(TransportStrategy.OAUTH2_TOKEN_PROPERTY, serverConfig.getPassword());
178+
return Session.getInstance(props);
179+
}
189180
} else {
190181
return Session.getInstance(props);
191182
}

0 commit comments

Comments
 (0)