Skip to content

Commit f53bba4

Browse files
committed
Document OAuth 2 support class
1 parent d1e0652 commit f53bba4

File tree

11 files changed

+81
-13
lines changed

11 files changed

+81
-13
lines changed

src/main/java/com/rabbitmq/client/amqp/impl/AmqpConnection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@ private void close(Throwable cause) {
796796
if (this.closed.compareAndSet(false, true)) {
797797
this.state(CLOSING, cause);
798798
LOGGER.debug("Closing connection {}", this);
799-
this.credentialsRegistration.unregister();
799+
this.credentialsRegistration.close();
800800
this.environment.removeConnection(this);
801801
BiConsumer<String, RunnableWithException> safeClose =
802802
(label, action) -> {

src/main/java/com/rabbitmq/client/amqp/impl/UsernamePasswordCredentialsManager.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,9 @@
2222

2323
final class UsernamePasswordCredentialsManager implements CredentialsManager {
2424

25-
private final UsernamePasswordCredentialsProvider provider;
2625
private final Registration registration;
2726

2827
UsernamePasswordCredentialsManager(UsernamePasswordCredentialsProvider provider) {
29-
this.provider = provider;
3028
this.registration = new RegistrationImpl(provider);
3129
}
3230

@@ -49,6 +47,6 @@ public void connect(AuthenticationCallback callback) {
4947
}
5048

5149
@Override
52-
public void unregister() {}
50+
public void close() {}
5351
}
5452
}

src/main/java/com/rabbitmq/client/amqp/oauth2/CredentialsManager.java

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,57 @@
1717
1818
package com.rabbitmq.client.amqp.oauth2;
1919

20+
/**
21+
* Contract to authenticate and possibly re-authenticate application components.
22+
*
23+
* <p>A typical "application component" is a connection.
24+
*/
2025
public interface CredentialsManager {
2126

27+
/** No-op credentials manager. */
2228
CredentialsManager NO_OP = new NoOpCredentialsManager();
2329

30+
/**
31+
* Register a component for authentication.
32+
*
33+
* @param name component name (must be unique)
34+
* @param updateCallback callback to update the component authentication
35+
* @return the registration (must be closed when no longer necessary)
36+
*/
2437
Registration register(String name, AuthenticationCallback updateCallback);
2538

26-
interface Registration {
39+
/** A component registration. */
40+
interface Registration extends AutoCloseable {
2741

42+
/**
43+
* Connection request from the component.
44+
*
45+
* <p>The component calls this method when it needs to authenticate. The underlying credentials
46+
* manager implementation must take of providing the component with the appropriate credentials
47+
* in the callback.
48+
*
49+
* @param callback client code to authenticate the component
50+
*/
2851
void connect(AuthenticationCallback callback);
2952

30-
void unregister();
53+
/** Close the registration. */
54+
void close();
3155
}
3256

57+
/**
58+
* Component authentication callback.
59+
*
60+
* <p>The component provides the logic and the manager implementation calls it with the
61+
* appropriate credentials.
62+
*/
3363
interface AuthenticationCallback {
3464

65+
/**
66+
* Authentication logic.
67+
*
68+
* @param username username
69+
* @param password password
70+
*/
3571
void authenticate(String username, String password);
3672
}
3773

@@ -49,6 +85,6 @@ class NoOpRegistration implements Registration {
4985
public void connect(AuthenticationCallback callback) {}
5086

5187
@Override
52-
public void unregister() {}
88+
public void close() {}
5389
}
5490
}

src/main/java/com/rabbitmq/client/amqp/oauth2/GsonTokenParser.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
import java.time.Instant;
2424
import java.util.Map;
2525

26+
/**
27+
* Token parser for <a href="https://www.rfc-editor.org/rfc/rfc6749#section-5.1">JSON OAuth 2 Access
28+
* tokens</a>.
29+
*
30+
* <p>Uses <a href="https://github.com/google/gson">GSON</a> for the JSON parsing.
31+
*/
2632
public class GsonTokenParser implements TokenParser {
2733

2834
private static final Gson GSON = new Gson();

src/main/java/com/rabbitmq/client/amqp/oauth2/HttpTokenRequester.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@
3131
import java.util.Map;
3232
import java.util.function.Consumer;
3333

34+
/**
35+
* Token requester using HTTP(S) to request an OAuth2 Access token.
36+
*
37+
* <p>Uses {@link HttpClient} for the HTTP operations.
38+
*/
3439
public final class HttpTokenRequester implements TokenRequester {
3540

3641
private static final Duration REQUEST_TIMEOUT = Duration.ofSeconds(60);

src/main/java/com/rabbitmq/client/amqp/oauth2/OAuth2Exception.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
1818
package com.rabbitmq.client.amqp.oauth2;
1919

20+
/** OAuth 2-related exception. */
2021
public class OAuth2Exception extends RuntimeException {
2122

2223
public OAuth2Exception(String message) {

src/main/java/com/rabbitmq/client/amqp/oauth2/Token.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,20 @@
1919

2020
import java.time.Instant;
2121

22+
/** A token. */
2223
public interface Token {
2324

25+
/**
26+
* The value of the token.
27+
*
28+
* @return the token value
29+
*/
2430
String value();
2531

32+
/**
33+
* The expiration time of the token.
34+
*
35+
* @return the expiration time
36+
*/
2637
Instant expirationTime();
2738
}

src/main/java/com/rabbitmq/client/amqp/oauth2/TokenCredentialsManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ public void connect(AuthenticationCallback callback) {
237237
}
238238

239239
@Override
240-
public void unregister() {
240+
public void close() {
241241
if (this.closed.compareAndSet(false, true)) {
242242
registrations.remove(this.id);
243243
ScheduledFuture<?> task = refreshTask;

src/main/java/com/rabbitmq/client/amqp/oauth2/TokenParser.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,14 @@
1717
1818
package com.rabbitmq.client.amqp.oauth2;
1919

20+
/** Contract to parse a token from a string. */
2021
public interface TokenParser {
2122

23+
/**
24+
* Parse the token.
25+
*
26+
* @param tokenAsString token as a string
27+
* @return the token
28+
*/
2229
Token parse(String tokenAsString);
2330
}

src/main/java/com/rabbitmq/client/amqp/oauth2/TokenRequester.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@
1717
1818
package com.rabbitmq.client.amqp.oauth2;
1919

20+
/** Contract to request a token (usually on HTTP). */
2021
public interface TokenRequester {
2122

23+
/**
24+
* Request a token.
25+
*
26+
* @return the token
27+
*/
2228
Token request();
2329
}

0 commit comments

Comments
 (0)