diff --git a/src/main/java/org/gitlab4j/api/GitLabApi.java b/src/main/java/org/gitlab4j/api/GitLabApi.java index e3e931fce..d1193f8df 100644 --- a/src/main/java/org/gitlab4j/api/GitLabApi.java +++ b/src/main/java/org/gitlab4j/api/GitLabApi.java @@ -528,6 +528,18 @@ public GitLabApi withRequestResponseLogging(Logger logger, Level level) { return (this); } + + /** + * Enable basic authentication when connecting to the gitlab server. + * + * @param userName the user name + * @param password the password + */ + public GitLabApi enableBasicAuthentication(String userName, String password) { + apiClient.enableBasicAuthentication(userName, password); + return (this); + } + /** * Enable the logging of the requests to and the responses from the GitLab server API * using the GitLab4J shared Logger instance and Level.FINE as the level. diff --git a/src/main/java/org/gitlab4j/api/GitLabApiClient.java b/src/main/java/org/gitlab4j/api/GitLabApiClient.java index 04b9cd862..87164406f 100755 --- a/src/main/java/org/gitlab4j/api/GitLabApiClient.java +++ b/src/main/java/org/gitlab4j/api/GitLabApiClient.java @@ -68,6 +68,9 @@ public class GitLabApiClient implements AutoCloseable { private Integer sudoAsId; private Integer connectTimeout; private Integer readTimeout; + private boolean basicAuthIsEnabled = false; + private String basicAuthUserName; + private String basicAuthPassword; /** * Construct an instance to communicate with a GitLab API server using the specified GitLab API version, @@ -255,6 +258,18 @@ public void close() { } } + /** + * Enable basic authentication + * + * @param userName the user name + * @param password the password + */ + void enableBasicAuthentication(String userName, String password) { + basicAuthIsEnabled = true; + basicAuthUserName = userName; + basicAuthPassword = password; + } + /** * Enable the logging of the requests to and the responses from the GitLab server API. * @@ -799,6 +814,12 @@ protected Invocation.Builder invocation(URL url, MultivaluedMap<String, String> } else { builder = builder.header(authHeader, authValue).accept(accept); } + /* For servers with basic authentication enabled. */ + if (basicAuthIsEnabled) { + String rawString = basicAuthUserName + ":" + basicAuthPassword; + String headerValue = "Basic " + java.util.Base64.getEncoder().encodeToString(rawString.getBytes()); + builder.header(AUTHORIZATION_HEADER, headerValue); + } // If sudo as ID is set add the Sudo header if (sudoAsId != null && sudoAsId.intValue() > 0)