Skip to content

Commit

Permalink
Use OAuth 2 lingo (not just OAuth)
Browse files Browse the repository at this point in the history
  • Loading branch information
acogoluegnes committed Dec 11, 2024
1 parent c0be0b4 commit 6ba98e4
Show file tree
Hide file tree
Showing 18 changed files with 108 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public interface ConnectionSettings<T> {
*/
Affinity<? extends T> affinity();

OAuthSettings<? extends T> oauth();
OAuth2Settings<? extends T> oauth2();

/**
* TLS settings.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
*
* @param <T> the type of object returned by methods, usually the object itself
*/
public interface OAuthSettings<T> {
public interface OAuth2Settings<T> {

/**
* Set the URI to access to get the token.
Expand All @@ -35,27 +35,29 @@ public interface OAuthSettings<T> {
* retrieve tokens.</em>
*
* @param uri access URI
* @return OAuth settings
* @return OAuth 2 settings
* @see #tls()
* @see TlsSettings#sslContext(SSLContext)
*/
OAuthSettings<T> tokenEndpointUri(String uri);
OAuth2Settings<T> tokenEndpointUri(String uri);

/**
* Set the OAuth 2 client ID
*
* <p>The client ID usually identifies the application that requests a token.
*
* @param clientId client ID
* @return OAuth settings
* @return OAuth 2 settings
*/
OAuthSettings<T> clientId(String clientId);
OAuth2Settings<T> clientId(String clientId);

/**
* Set the secret (password) to use to get a token.
*
* @param clientSecret client secret
* @return OAuth settings
* @return OAuth 2 settings
*/
OAuthSettings<T> clientSecret(String clientSecret);
OAuth2Settings<T> clientSecret(String clientSecret);

/**
* Set the grant type to use when requesting the token.
Expand All @@ -64,9 +66,9 @@ public interface OAuthSettings<T> {
* non-standard grant types to request tokens with extra-information.
*
* @param grantType grant type
* @return OAuth settings
* @return OAuth 2 settings
*/
OAuthSettings<T> grantType(String grantType);
OAuth2Settings<T> grantType(String grantType);

/**
* Set a parameter to pass in the request.
Expand All @@ -75,19 +77,19 @@ public interface OAuthSettings<T> {
*
* @param name name of the parameter
* @param value value of the parameter
* @return OAuth settings
* @return OAuth 2 settings
*/
OAuthSettings<T> parameter(String name, String value);
OAuth2Settings<T> parameter(String name, String value);

/**
* Whether to share the same token between connections.
*
* <p>Default is <true>true</true> (the token is shared between connections).
*
* @param shared flag to share the token between connections
* @return OAuth settings
* @return OAuth 2 settings
*/
OAuthSettings<T> shared(boolean shared);
OAuth2Settings<T> shared(boolean shared);

/**
* TLS configuration for requesting the token.
Expand Down Expand Up @@ -121,8 +123,8 @@ interface TlsSettings<T> {
/**
* Go back to the general OAuth 2 settings.
*
* @return OAuth settings
* @return OAuth 2 settings
*/
OAuthSettings<T> oauth();
OAuth2Settings<T> oauth2();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ public DefaultConnectionSettings.DefaultAffinity<? extends ConnectionBuilder> af
}

@Override
public OAuthSettings<? extends ConnectionBuilder> oauth() {
return this.connectionSettings.oauth();
public OAuth2Settings<? extends ConnectionBuilder> oauth2() {
return this.connectionSettings.oauth2();
}

@Override
Expand Down
34 changes: 17 additions & 17 deletions src/main/java/com/rabbitmq/client/amqp/impl/CredentialsFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@

import com.rabbitmq.client.amqp.CredentialsProvider;
import com.rabbitmq.client.amqp.UsernamePasswordCredentialsProvider;
import com.rabbitmq.client.amqp.oauth.GsonTokenParser;
import com.rabbitmq.client.amqp.oauth.HttpTokenRequester;
import com.rabbitmq.client.amqp.oauth2.GsonTokenParser;
import com.rabbitmq.client.amqp.oauth2.HttpTokenRequester;
import java.net.http.HttpClient;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Consumer;

final class CredentialsFactory {

private volatile Credentials globalOAuthCredentials;
private final Lock oauthCredentialsLock = new ReentrantLock();
private volatile Credentials globalOAuth2Credentials;
private final Lock oauth2CredentialsLock = new ReentrantLock();
private final AmqpEnvironment environment;

CredentialsFactory(AmqpEnvironment environment) {
Expand All @@ -39,11 +39,11 @@ final class CredentialsFactory {
Credentials credentials(DefaultConnectionSettings<?> settings) {
CredentialsProvider provider = settings.credentialsProvider();
Credentials credentials;
if (settings.oauth().enabled()) {
if (settings.oauth().shared()) {
credentials = globalOAuthCredentials(settings);
if (settings.oauth2().enabled()) {
if (settings.oauth2().shared()) {
credentials = globalOAuth2Credentials(settings);
} else {
credentials = createOAuthCredentials(settings);
credentials = createOAuth2Credentials(settings);
}
} else {
if (provider instanceof UsernamePasswordCredentialsProvider) {
Expand All @@ -57,25 +57,25 @@ Credentials credentials(DefaultConnectionSettings<?> settings) {
return credentials;
}

private Credentials globalOAuthCredentials(DefaultConnectionSettings<?> connectionSettings) {
Credentials result = this.globalOAuthCredentials;
private Credentials globalOAuth2Credentials(DefaultConnectionSettings<?> connectionSettings) {
Credentials result = this.globalOAuth2Credentials;
if (result != null) {
return result;
}

this.oauthCredentialsLock.lock();
this.oauth2CredentialsLock.lock();
try {
if (this.globalOAuthCredentials == null) {
this.globalOAuthCredentials = createOAuthCredentials(connectionSettings);
if (this.globalOAuth2Credentials == null) {
this.globalOAuth2Credentials = createOAuth2Credentials(connectionSettings);
}
return this.globalOAuthCredentials;
return this.globalOAuth2Credentials;
} finally {
this.oauthCredentialsLock.unlock();
this.oauth2CredentialsLock.unlock();
}
}

private Credentials createOAuthCredentials(DefaultConnectionSettings<?> connectionSettings) {
DefaultConnectionSettings.DefaultOAuthSettings<?> settings = connectionSettings.oauth();
private Credentials createOAuth2Credentials(DefaultConnectionSettings<?> connectionSettings) {
DefaultConnectionSettings.DefaultOAuth2Settings<?> settings = connectionSettings.oauth2();
Consumer<HttpClient.Builder> clientBuilderConsumer;
if (settings.tlsEnabled()) {
clientBuilderConsumer = b -> b.sslContext(settings.tls().sslContext());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ abstract class DefaultConnectionSettings<T> implements ConnectionSettings<T> {
private String saslMechanism = ConnectionSettings.SASL_MECHANISM_ANONYMOUS;
private final DefaultTlsSettings<T> tlsSettings = new DefaultTlsSettings<>(this);
private final DefaultAffinity<T> affinity = new DefaultAffinity<>(this);
private final DefaultOAuthSettings<T> oAuthSettings = new DefaultOAuthSettings<>(this);
private final DefaultOAuth2Settings<T> oAuth2Settings = new DefaultOAuth2Settings<>(this);

@Override
public T uri(String uriString) {
Expand Down Expand Up @@ -225,8 +225,8 @@ void copyTo(DefaultConnectionSettings<?> copy) {

this.affinity.copyTo(copy.affinity);

if (this.oAuthSettings.enabled()) {
this.oAuthSettings.copyTo(copy.oauth());
if (this.oAuth2Settings.enabled()) {
this.oAuth2Settings.copyTo(copy.oauth2());
}
}

Expand Down Expand Up @@ -301,8 +301,8 @@ public DefaultAffinity<? extends T> affinity() {
}

@Override
public DefaultOAuthSettings<? extends T> oauth() {
return this.oAuthSettings;
public DefaultOAuth2Settings<? extends T> oauth2() {
return this.oAuth2Settings;
}

static DefaultConnectionSettings<?> instance() {
Expand Down Expand Up @@ -501,7 +501,7 @@ void validate() {
}
}

static class DefaultOAuthSettings<T> implements OAuthSettings<T> {
static class DefaultOAuth2Settings<T> implements OAuth2Settings<T> {

private final DefaultConnectionSettings<T> connectionSettings;
private final DefaultOAuthTlsSettings<T> tls = new DefaultOAuthTlsSettings<>(this);
Expand All @@ -514,37 +514,37 @@ static class DefaultOAuthSettings<T> implements OAuthSettings<T> {
private Function<Instant, Duration> refreshDelayStrategy =
TokenCredentials.DEFAULT_REFRESH_DELAY_STRATEGY;

DefaultOAuthSettings(DefaultConnectionSettings<T> connectionSettings) {
DefaultOAuth2Settings(DefaultConnectionSettings<T> connectionSettings) {
this.connectionSettings = connectionSettings;
}

@Override
public OAuthSettings<T> tokenEndpointUri(String uri) {
public OAuth2Settings<T> tokenEndpointUri(String uri) {
this.connectionSettings.saslMechanism(SASL_MECHANISM_PLAIN);
this.tokenEndpointUri = uri;
return this;
}

@Override
public OAuthSettings<T> clientId(String clientId) {
public OAuth2Settings<T> clientId(String clientId) {
this.clientId = clientId;
return this;
}

@Override
public OAuthSettings<T> clientSecret(String clientSecret) {
public OAuth2Settings<T> clientSecret(String clientSecret) {
this.clientSecret = clientSecret;
return this;
}

@Override
public OAuthSettings<T> grantType(String grantType) {
public OAuth2Settings<T> grantType(String grantType) {
this.grantType = grantType;
return this;
}

@Override
public OAuthSettings<T> parameter(String name, String value) {
public OAuth2Settings<T> parameter(String name, String value) {
if (value == null) {
this.parameters.remove(name);
} else {
Expand All @@ -554,12 +554,13 @@ public OAuthSettings<T> parameter(String name, String value) {
}

@Override
public OAuthSettings<T> shared(boolean shared) {
public OAuth2Settings<T> shared(boolean shared) {
this.shared = shared;
return this;
}

DefaultOAuthSettings<T> refreshDelayStrategy(Function<Instant, Duration> refreshDelayStrategy) {
DefaultOAuth2Settings<T> refreshDelayStrategy(
Function<Instant, Duration> refreshDelayStrategy) {
this.refreshDelayStrategy = refreshDelayStrategy;
return this;
}
Expand All @@ -579,7 +580,7 @@ public T connection() {
return this.connectionSettings.toReturn();
}

void copyTo(DefaultOAuthSettings<?> copy) {
void copyTo(DefaultOAuth2Settings<?> copy) {
copy.tokenEndpointUri(this.tokenEndpointUri);
copy.clientId(this.clientId);
copy.clientSecret(this.clientSecret);
Expand Down Expand Up @@ -625,25 +626,25 @@ boolean tlsEnabled() {
}
}

static class DefaultOAuthTlsSettings<T> implements OAuthSettings.TlsSettings<T> {
static class DefaultOAuthTlsSettings<T> implements OAuth2Settings.TlsSettings<T> {

private final OAuthSettings<T> oAuthSettings;
private final OAuth2Settings<T> oAuth2Settings;
private SSLContext sslContext;
private boolean enabled = false;

DefaultOAuthTlsSettings(OAuthSettings<T> oAuthSettings) {
this.oAuthSettings = oAuthSettings;
DefaultOAuthTlsSettings(OAuth2Settings<T> oAuth2Settings) {
this.oAuth2Settings = oAuth2Settings;
}

@Override
public OAuthSettings.TlsSettings<T> sslContext(SSLContext sslContext) {
public OAuth2Settings.TlsSettings<T> sslContext(SSLContext sslContext) {
this.sslContext = sslContext;
return this;
}

@Override
public OAuthSettings<T> oauth() {
return this.oAuthSettings;
public OAuth2Settings<T> oauth2() {
return this.oAuth2Settings;
}

void enable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
// info@rabbitmq.com.
package com.rabbitmq.client.amqp.impl;

import com.rabbitmq.client.amqp.oauth.Token;
import com.rabbitmq.client.amqp.oauth.TokenRequester;
import com.rabbitmq.client.amqp.oauth2.Token;
import com.rabbitmq.client.amqp.oauth2.TokenRequester;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.time.Duration;
import java.time.Instant;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
//
// If you have any questions regarding licensing, please contact us at
// info@rabbitmq.com.
package com.rabbitmq.client.amqp.oauth;
package com.rabbitmq.client.amqp.oauth2;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
//
// If you have any questions regarding licensing, please contact us at
// info@rabbitmq.com.
package com.rabbitmq.client.amqp.oauth;
package com.rabbitmq.client.amqp.oauth2;

import static java.nio.charset.StandardCharsets.UTF_8;

Expand Down Expand Up @@ -46,7 +46,7 @@ public final class HttpTokenRequester implements TokenRequester {
private final HttpClient client;
private final Consumer<HttpRequest.Builder> requestBuilderConsumer;

private TokenParser parser;
private final TokenParser parser;

public HttpTokenRequester(
String tokenEndpointUri,
Expand Down Expand Up @@ -114,10 +114,10 @@ public Token request() {
checkContentType(response.headers().firstValue("content-type").orElse(null));
return this.parser.parse(response.body());
} catch (IOException e) {
throw new OAuthException("Error while retrieving OAuth 2 token", e);
throw new OAuth2Exception("Error while retrieving OAuth 2 token", e);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new OAuthException("Error while retrieving OAuth 2 token", e);
throw new OAuth2Exception("Error while retrieving OAuth 2 token", e);
}
}

Expand All @@ -144,13 +144,13 @@ private static String encode(String value) {

private static void checkContentType(String contentType) {
if (contentType == null || !contentType.toLowerCase().contains("json")) {
throw new OAuthException("HTTP request for token retrieval is not JSON: " + contentType);
throw new OAuth2Exception("HTTP request for token retrieval is not JSON: " + contentType);
}
}

private static void checkStatusCode(int statusCode) {
if (statusCode != 200) {
throw new OAuthException(
throw new OAuth2Exception(
"HTTP request for token retrieval did not " + "return 200 status code: " + statusCode);
}
}
Expand Down
Loading

0 comments on commit 6ba98e4

Please sign in to comment.