diff --git a/src/main/java/org/gitlab4j/api/GitLabApi.java b/src/main/java/org/gitlab4j/api/GitLabApi.java index aa19398f5..772759f06 100644 --- a/src/main/java/org/gitlab4j/api/GitLabApi.java +++ b/src/main/java/org/gitlab4j/api/GitLabApi.java @@ -5,6 +5,7 @@ import java.util.Map; import java.util.Optional; import java.util.WeakHashMap; +import java.util.function.Supplier; import java.util.logging.Level; import java.util.logging.Logger; @@ -711,6 +712,14 @@ public String getAuthToken() { return (apiClient.getAuthToken()); } + /** + * Set auth token supplier for gitlab api client. + * @param authTokenSupplier - supplier which provide actual auth token + */ + public void setAuthTokenSupplier(Supplier authTokenSupplier) { + apiClient.setAuthTokenSupplier(authTokenSupplier); + } + /** * Get the secret token. * diff --git a/src/main/java/org/gitlab4j/api/GitLabApiClient.java b/src/main/java/org/gitlab4j/api/GitLabApiClient.java index 04b9cd862..d46128cd0 100755 --- a/src/main/java/org/gitlab4j/api/GitLabApiClient.java +++ b/src/main/java/org/gitlab4j/api/GitLabApiClient.java @@ -10,6 +10,7 @@ import java.security.cert.X509Certificate; import java.util.List; import java.util.Map; +import java.util.function.Supplier; import java.util.logging.Level; import java.util.logging.Logger; @@ -60,7 +61,7 @@ public class GitLabApiClient implements AutoCloseable { private String baseUrl; private String hostUrl; private TokenType tokenType = TokenType.PRIVATE; - private String authToken; + private Supplier authToken; private String secretToken; private boolean ignoreCertificateErrors; private SSLContext openSslContext; @@ -215,7 +216,7 @@ public GitLabApiClient(ApiVersion apiVersion, String hostUrl, TokenType tokenTyp this.hostUrl += apiVersion.getApiNamespace(); this.tokenType = tokenType; - this.authToken = authToken; + this.authToken = () -> authToken; if (secretToken != null) { secretToken = secretToken.trim(); @@ -293,7 +294,7 @@ void setRequestTimeout(Integer connectTimeout, Integer readTimeout) { * @return the auth token being used by this client */ String getAuthToken() { - return (authToken); + return (authToken.get()); } /** @@ -792,7 +793,7 @@ protected Invocation.Builder invocation(URL url, MultivaluedMap } String authHeader = (tokenType == TokenType.OAUTH2_ACCESS ? AUTHORIZATION_HEADER : PRIVATE_TOKEN_HEADER); - String authValue = (tokenType == TokenType.OAUTH2_ACCESS ? "Bearer " + authToken : authToken); + String authValue = (tokenType == TokenType.OAUTH2_ACCESS ? "Bearer " + authToken.get() : authToken.get()); Invocation.Builder builder = target.request(); if (accept == null || accept.trim().length() == 0) { builder = builder.header(authHeader, authValue); @@ -923,4 +924,12 @@ public boolean verify(String hostname, SSLSession session) { return (true); } + + /** + * Set auth token supplier for gitlab api client. + * @param authTokenSupplier - supplier which provide actual auth token + */ + public void setAuthTokenSupplier(Supplier authTokenSupplier) { + this.authToken = authTokenSupplier; + } }