Commit 013436b8 authored by Roman Torsten's avatar Roman Torsten
Browse files

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.
parent 3b1563bd
......@@ -5,6 +5,7 @@ import java.util.List;
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 class GitLabApi implements AutoCloseable {
return (apiClient.getAuthToken());
}
/**
* Set auth token supplier for gitlab api client.
* @param authTokenSupplier - supplier which provide actual auth token
*/
public void setAuthTokenSupplier(Supplier<String> authTokenSupplier) {
apiClient.setAuthTokenSupplier(authTokenSupplier);
}
/**
* Get the secret token.
*
......
......@@ -10,6 +10,7 @@ import java.security.cert.CertificateException;
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<String> authToken;
private String secretToken;
private boolean ignoreCertificateErrors;
private SSLContext openSslContext;
......@@ -215,7 +216,7 @@ public class GitLabApiClient implements AutoCloseable {
this.hostUrl += apiVersion.getApiNamespace();
this.tokenType = tokenType;
this.authToken = authToken;
this.authToken = () -> authToken;
if (secretToken != null) {
secretToken = secretToken.trim();
......@@ -293,7 +294,7 @@ public class GitLabApiClient implements AutoCloseable {
* @return the auth token being used by this client
*/
String getAuthToken() {
return (authToken);
return (authToken.get());
}
/**
......@@ -792,7 +793,7 @@ public class GitLabApiClient implements AutoCloseable {
}
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 class GitLabApiClient implements AutoCloseable {
return (true);
}
/**
* Set auth token supplier for gitlab api client.
* @param authTokenSupplier - supplier which provide actual auth token
*/
public void setAuthTokenSupplier(Supplier<String> authTokenSupplier) {
this.authToken = authTokenSupplier;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment