Skip to content

Commit 013436b

Browse files
committed
Feature: Replace auth token with supplier.
Use case: oauth2 access limited time tokens which should be updated on regular basis. Supplier allows us to move refresh logic outside client. Use specified setter to set up supplier after gitlab api object creation.
1 parent 3b1563b commit 013436b

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/main/java/org/gitlab4j/api/GitLabApi.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.Map;
66
import java.util.Optional;
77
import java.util.WeakHashMap;
8+
import java.util.function.Supplier;
89
import java.util.logging.Level;
910
import java.util.logging.Logger;
1011

@@ -711,6 +712,14 @@ public String getAuthToken() {
711712
return (apiClient.getAuthToken());
712713
}
713714

715+
/**
716+
* Set auth token supplier for gitlab api client.
717+
* @param authTokenSupplier - supplier which provide actual auth token
718+
*/
719+
public void setAuthTokenSupplier(Supplier<String> authTokenSupplier) {
720+
apiClient.setAuthTokenSupplier(authTokenSupplier);
721+
}
722+
714723
/**
715724
* Get the secret token.
716725
*

src/main/java/org/gitlab4j/api/GitLabApiClient.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.security.cert.X509Certificate;
1111
import java.util.List;
1212
import java.util.Map;
13+
import java.util.function.Supplier;
1314
import java.util.logging.Level;
1415
import java.util.logging.Logger;
1516

@@ -60,7 +61,7 @@ public class GitLabApiClient implements AutoCloseable {
6061
private String baseUrl;
6162
private String hostUrl;
6263
private TokenType tokenType = TokenType.PRIVATE;
63-
private String authToken;
64+
private Supplier<String> authToken;
6465
private String secretToken;
6566
private boolean ignoreCertificateErrors;
6667
private SSLContext openSslContext;
@@ -215,7 +216,7 @@ public GitLabApiClient(ApiVersion apiVersion, String hostUrl, TokenType tokenTyp
215216
this.hostUrl += apiVersion.getApiNamespace();
216217

217218
this.tokenType = tokenType;
218-
this.authToken = authToken;
219+
this.authToken = () -> authToken;
219220

220221
if (secretToken != null) {
221222
secretToken = secretToken.trim();
@@ -293,7 +294,7 @@ void setRequestTimeout(Integer connectTimeout, Integer readTimeout) {
293294
* @return the auth token being used by this client
294295
*/
295296
String getAuthToken() {
296-
return (authToken);
297+
return (authToken.get());
297298
}
298299

299300
/**
@@ -792,7 +793,7 @@ protected Invocation.Builder invocation(URL url, MultivaluedMap<String, String>
792793
}
793794

794795
String authHeader = (tokenType == TokenType.OAUTH2_ACCESS ? AUTHORIZATION_HEADER : PRIVATE_TOKEN_HEADER);
795-
String authValue = (tokenType == TokenType.OAUTH2_ACCESS ? "Bearer " + authToken : authToken);
796+
String authValue = (tokenType == TokenType.OAUTH2_ACCESS ? "Bearer " + authToken.get() : authToken.get());
796797
Invocation.Builder builder = target.request();
797798
if (accept == null || accept.trim().length() == 0) {
798799
builder = builder.header(authHeader, authValue);
@@ -923,4 +924,12 @@ public boolean verify(String hostname, SSLSession session) {
923924

924925
return (true);
925926
}
927+
928+
/**
929+
* Set auth token supplier for gitlab api client.
930+
* @param authTokenSupplier - supplier which provide actual auth token
931+
*/
932+
public void setAuthTokenSupplier(Supplier<String> authTokenSupplier) {
933+
this.authToken = authTokenSupplier;
934+
}
926935
}

0 commit comments

Comments
 (0)