Skip to content

Commit 1f73f0f

Browse files
committed
chore: rename bearerToken flow to refreshToken internally
Document public and internal APIs.
1 parent 9a07f77 commit 1f73f0f

7 files changed

Lines changed: 183 additions & 9 deletions

File tree

src/main/java/io/weaviate/client6/v1/api/Authorization.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,63 @@
1010
public interface Authorization {
1111
TokenProvider getTokenProvider(RestTransport transport);
1212

13+
/**
14+
* Authorize using a static API key.
15+
*
16+
* @param apiKey Weaviate API key.
17+
*/
1318
public static Authorization apiKey(String apiKey) {
1419
return __ -> TokenProvider.staticToken(apiKey);
1520
}
1621

22+
/**
23+
* Authorize using an existing access_token + refresh_token
24+
* pair.
25+
*
26+
* @param accessToken Access token.
27+
* @param refreshToken Refresh token.
28+
* @param expiresIn Remaining token lifetime in seconds.
29+
*
30+
* @return Authorization provider.
31+
* @throws WeaviateOAuthException if an error occurred at any point of the
32+
* exchange process.
33+
*/
1734
public static Authorization bearerToken(String accessToken, String refreshToken, long expiresIn) {
1835
return transport -> {
1936
OidcConfig oidc = OidcUtils.getConfig(transport);
2037
return TokenProvider.bearerToken(oidc, accessToken, refreshToken, expiresIn);
2138
};
2239
}
2340

41+
/**
42+
* Authorize using Resource Owner Password authorization grant.
43+
*
44+
* @param username Resource owner username.
45+
* @param password Resource owner password.
46+
* @param scopes Client scopes.
47+
*
48+
* @return Authorization provider.
49+
* @throws WeaviateOAuthException if an error occured at any point of the token
50+
* exchange process.
51+
*/
2452
public static Authorization resourceOwnerPassword(String username, String password, List<String> scopes) {
2553
return transport -> {
2654
OidcConfig oidc = OidcUtils.getConfig(transport);
2755
return TokenProvider.resourceOwnerPassword(oidc, username, password);
2856
};
2957
}
3058

59+
/**
60+
* Authorize using Client Credentials authorization grant.
61+
*
62+
* @param clientId Client ID.
63+
* @param clientSecret Client secret.
64+
* @param scopes Client scopes.
65+
*
66+
* @return Authorization provider.
67+
* @throws WeaviateOAuthException if an error occured at any point while
68+
* obtaining a new token.
69+
*/
3170
public static Authorization clientCredentials(String clientId, String clientSecret, List<String> scopes) {
3271
return transport -> {
3372
OidcConfig oidc = OidcUtils.getConfig(transport);

src/main/java/io/weaviate/client6/v1/internal/ExchangeTokenProvider.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,21 @@
22

33
import io.weaviate.client6.v1.internal.oidc.OidcConfig;
44

5+
/**
6+
* ExchangeTokenProvider obtains a new {@link Token} from "single-use"
7+
* {@link TokenProvider}, usually one using an Resource Owner Password grant.
8+
* It then creates a new internal TokenProvider to refresh the token each time
9+
* {@link #getToken} is called.
10+
*
11+
* <p>
12+
* Usage:
13+
*
14+
* <pre>{@code
15+
* var initialGrant = TokenProvider.resourceOwnerPassword(oidc, username, password);
16+
* var exchange = new ExchangeTokenProvider(oidc, initialGrant);
17+
* var token = exchange.getToken();
18+
* } </pre>
19+
*/
520
class ExchangeTokenProvider implements TokenProvider {
621
private final TokenProvider bearer;
722

src/main/java/io/weaviate/client6/v1/internal/ReuseTokenProvider.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@
55
/**
66
* ReuseTokenProvider returns the same token as long as its valid and obtains a
77
* new token from a {@link TokenProvider} otherwise.
8+
*
9+
* <p>
10+
* Usage:
11+
*
12+
* <pre>{@code
13+
* // Create an TokenProvider that can rotate tokens as they expire.
14+
* var myProvider = new MyTokenProvider();
15+
*
16+
* // Create a reusable TokenProvider.
17+
* var tokenProvider = ReuseTokenProvider.wrap(myProvider);
18+
* }</pre>
819
*/
920
@ThreadSafe
1021
final class ReuseTokenProvider implements TokenProvider {

src/main/java/io/weaviate/client6/v1/internal/TokenProvider.java

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,21 @@
22

33
import java.time.Instant;
44

5+
import io.weaviate.client6.v1.api.WeaviateOAuthException;
56
import io.weaviate.client6.v1.internal.oidc.OidcConfig;
67
import io.weaviate.client6.v1.internal.oidc.nimbus.NimbusTokenProvider;
78

9+
/** TokenProvider obtains authentication tokens. */
810
@FunctionalInterface
911
public interface TokenProvider {
1012
Token getToken();
1113

14+
/** Token represents an access_token + refresh_token pair. */
1215
public record Token(String accessToken, String refreshToken, Instant createdAt, long expiresIn) {
16+
/**
17+
* Returns {@code true} if remaining lifetime of the token is greater than 0.
18+
* Tokens created with {@link #expireNever} are always valid.
19+
*/
1320
public boolean isValid() {
1421
if (expiresIn == -1) {
1522
return true;
@@ -18,17 +25,39 @@ public boolean isValid() {
1825
return Instant.now().isAfter(createdAt.plusSeconds(expiresIn));
1926
}
2027

28+
/**
29+
* Create a token with an expiration and a refresh_token.
30+
*
31+
* @param accessToken Access token.
32+
* @param refreshToken Refresh token.
33+
* @param expiresIn Remaining token lifetime in seconds.
34+
*
35+
* @return A new Token.
36+
*/
2137
public static Token expireAfter(String accessToken, String refreshToken, long expiresIn) {
2238
return new Token(accessToken, refreshToken, Instant.now(), expiresIn);
2339
}
2440

41+
/**
42+
* Create a token that does not have a refresh_token.
43+
*
44+
* @param accessToken Access token.
45+
* @param expiresIn Remaining token lifetime in seconds.
46+
*
47+
* @return A new Token.
48+
*/
2549
public static Token expireAfter(String accessToken, long expiresIn) {
2650
return expireAfter(accessToken, null, expiresIn);
2751
}
2852

29-
/** Create a token that never expires. */
53+
/**
54+
* Create a token that never expires.
55+
*
56+
* @param accessToken Access token.
57+
* @return A new Token.
58+
*/
3059
public static Token expireNever(String accessToken) {
31-
return Token.expireAfter(accessToken, "", -1);
60+
return Token.expireAfter(accessToken, -1);
3261
}
3362
}
3463

@@ -37,26 +66,68 @@ public static TokenProvider staticToken(String accessToken) {
3766
return () -> token;
3867
}
3968

69+
/**
70+
* Create a TokenProvider that uses an existing access_token + refresh_token
71+
* pair.
72+
*
73+
* @param oidc OIDC config.
74+
* @param accessToken Access token.
75+
* @param refreshToken Refresh token.
76+
* @param expiresIn Remaining token lifetime in seconds.
77+
*
78+
* @return Internal TokenProvider implementation.
79+
* @throws WeaviateOAuthException if an error occurred at any point of the
80+
* exchange process.
81+
*/
4082
public static TokenProvider bearerToken(OidcConfig oidc, String accessToken, String refreshToken, long expiresIn) {
4183
final var token = Token.expireAfter(accessToken, refreshToken, expiresIn);
42-
final var provider = NimbusTokenProvider.bearerToken(oidc, token);
84+
final var provider = NimbusTokenProvider.refreshToken(oidc, token);
4385
return reuse(token, provider);
4486
}
4587

88+
/**
89+
* Create a TokenProvider that uses Resource Owner Password authorization grant.
90+
*
91+
* @param oidc OIDC config.
92+
* @param username Resource owner username.
93+
* @param password Resource owner password.
94+
*
95+
* @return Internal TokenProvider implementation.
96+
* @throws WeaviateOAuthException if an error occured at any point of the token
97+
* exchange process.
98+
*/
4699
public static TokenProvider resourceOwnerPassword(OidcConfig oidc, String username, String password) {
47100
final var passwordGrant = NimbusTokenProvider.resourceOwnerPassword(oidc, username, password);
48101
return reuse(null, exchange(oidc, passwordGrant));
49102
}
50103

104+
/**
105+
* Create a TokenProvider that uses Client Credentials authorization grant.
106+
*
107+
* @param oidc OIDC config.
108+
* @param clientId Client ID.
109+
* @param clientSecret Client secret.
110+
*
111+
* @return Internal TokenProvider implementation.
112+
* @throws WeaviateOAuthException if an error occured at any point while
113+
* obtaining a new token.
114+
*/
51115
public static TokenProvider clientCredentials(OidcConfig oidc, String clientId, String clientSecret) {
52116
final var provider = NimbusTokenProvider.clientCredentials(oidc, clientId, clientSecret);
53117
return reuse(null, provider);
54118
}
55119

120+
/**
121+
* Obtain a TokenProvider that exchanges an authorization grant for a new Token.
122+
*/
56123
static TokenProvider exchange(OidcConfig oidc, TokenProvider tp) {
57124
return new ExchangeTokenProvider(oidc, tp);
58125
}
59126

127+
/**
128+
* Obtain a TokenProvider which reuses tokens obtained
129+
* from another TokenProvider until they expire.
130+
*/
60131
static TokenProvider reuse(Token t, TokenProvider tp) {
61132
return ReuseTokenProvider.wrap(t, tp);
62133
}

src/main/java/io/weaviate/client6/v1/internal/oidc/nimbus/Flow.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ default ClientAuthentication getClientAuthentication() {
1818
return null;
1919
}
2020

21-
static Flow bearerToken(Token t) {
22-
return new BearerTokenFlow(t);
21+
static Flow refreshToken(Token t) {
22+
return new RefreshTokenFlow(t);
2323
}
2424

2525
static Flow resourceOwnerPassword(String username, String password) {

src/main/java/io/weaviate/client6/v1/internal/oidc/nimbus/NimbusTokenProvider.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,46 @@ public final class NimbusTokenProvider implements TokenProvider {
2626
private final String redirectUrl;
2727
private final Flow flow;
2828

29-
public static NimbusTokenProvider bearerToken(OidcConfig oidc, Token t) {
30-
return new NimbusTokenProvider(oidc, Flow.bearerToken(t));
29+
/**
30+
* Create a TokenProvider that uses Refresh Token authorization grant.
31+
*
32+
* @param oidc OIDC config.
33+
* @param t Current token. Must not be null.
34+
*
35+
* @return A new instance of NimbusTokenProvider. Instances are never cached.
36+
* @throws WeaviateOAuthException if an error occurred at any point of the
37+
* exchange process.
38+
*/
39+
public static NimbusTokenProvider refreshToken(OidcConfig oidc, Token t) {
40+
return new NimbusTokenProvider(oidc, Flow.refreshToken(t));
3141
}
3242

43+
/**
44+
* Create a TokenProvider that uses Resource Owner Password authorization grant.
45+
*
46+
* @param oidc OIDC config.
47+
* @param username Resource owner username.
48+
* @param password Resource owner password.
49+
*
50+
* @return A new instance of NimbusTokenProvider. Instances are never cached.
51+
* @throws WeaviateOAuthException if an error occured at any point of the
52+
* exchange process.
53+
*/
3354
public static NimbusTokenProvider resourceOwnerPassword(OidcConfig oidc, String username, String password) {
3455
return new NimbusTokenProvider(oidc, Flow.resourceOwnerPassword(username, password));
3556
}
3657

58+
/**
59+
* Create a TokenProvider that uses Client Credentials authorization grant.
60+
*
61+
* @param oidc OIDC config.
62+
* @param clientId Client ID.
63+
* @param clientSecret Client secret.
64+
*
65+
* @return A new instance of NimbusTokenProvider. Instances are never cached.
66+
* @throws WeaviateOAuthException if an error occured at any point of the
67+
* exchange process.
68+
*/
3769
public static NimbusTokenProvider clientCredentials(OidcConfig oidc, String clientId, String clientSecret) {
3870
return new NimbusTokenProvider(oidc, Flow.clientCredentials(clientId, clientSecret));
3971
}

src/main/java/io/weaviate/client6/v1/internal/oidc/nimbus/BearerTokenFlow.java renamed to src/main/java/io/weaviate/client6/v1/internal/oidc/nimbus/RefreshTokenFlow.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,17 @@
88

99
import io.weaviate.client6.v1.internal.TokenProvider.Token;
1010

11+
/**
12+
* RefreshTokenFlow provides {@link RefreshTokenGrant} with a refresh_token.
13+
* Once the caller has obtained a new {@link Token} it must be updated using
14+
* {@link #setToken} to ensure RefreshTokenFlow continues to return valid
15+
* authorization grants.
16+
*/
1117
@NotThreadSafe
12-
final class BearerTokenFlow implements Flow {
18+
final class RefreshTokenFlow implements Flow {
1319
private Token t;
1420

15-
BearerTokenFlow(Token t) {
21+
RefreshTokenFlow(Token t) {
1622
this.t = t;
1723
}
1824

0 commit comments

Comments
 (0)