From 013436b838bf7106d3558e894d8b25d949a600ca Mon Sep 17 00:00:00 2001 From: Roman Torsten Date: Wed, 24 Nov 2021 17:16:17 +0300 Subject: [PATCH] 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. --- src/main/java/org/gitlab4j/api/GitLabApi.java | 9 +++++++++ .../java/org/gitlab4j/api/GitLabApiClient.java | 17 +++++++++++++---- 2 files changed, 22 insertions(+), 4 deletions(-) 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; + } }