From c393f61a5aec8cb916da6b19b974ee67ba800979 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Bresson?= Date: Wed, 6 Nov 2024 16:31:00 +0100 Subject: [PATCH] Add support for Job Token (#1188) Fixes #678 --- src/main/java/org/gitlab4j/api/Constants.java | 1 + .../org/gitlab4j/api/GitLabApiClient.java | 25 +++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/gitlab4j/api/Constants.java b/src/main/java/org/gitlab4j/api/Constants.java index 604dd3fe5..09c87eea9 100644 --- a/src/main/java/org/gitlab4j/api/Constants.java +++ b/src/main/java/org/gitlab4j/api/Constants.java @@ -38,6 +38,7 @@ public interface Constants { public enum TokenType { ACCESS, OAUTH2_ACCESS, + JOB_TOKEN, PRIVATE; } diff --git a/src/main/java/org/gitlab4j/api/GitLabApiClient.java b/src/main/java/org/gitlab4j/api/GitLabApiClient.java index b1e7a0f68..989537015 100755 --- a/src/main/java/org/gitlab4j/api/GitLabApiClient.java +++ b/src/main/java/org/gitlab4j/api/GitLabApiClient.java @@ -56,6 +56,7 @@ public class GitLabApiClient implements AutoCloseable { protected static final String PRIVATE_TOKEN_HEADER = "PRIVATE-TOKEN"; + protected static final String JOB_TOKEN_HEADER = "JOB-TOKEN"; protected static final String SUDO_HEADER = "Sudo"; protected static final String AUTHORIZATION_HEADER = "Authorization"; protected static final String X_GITLAB_TOKEN_HEADER = "X-Gitlab-Token"; @@ -861,8 +862,8 @@ 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.get() : authToken.get()); + String authHeader = getAuthHeader(); + String authValue = getAuthValue(); Invocation.Builder builder = target.request(); if (accept == null || accept.trim().length() == 0) { builder = builder.header(authHeader, authValue); @@ -886,6 +887,26 @@ protected Invocation.Builder invocation(URL url, MultivaluedMap return (builder); } + private String getAuthValue() { + switch (tokenType) { + case OAUTH2_ACCESS: + return "Bearer " + authToken.get(); + default: + return authToken.get(); + } + } + + private String getAuthHeader() { + switch (tokenType) { + case OAUTH2_ACCESS: + return AUTHORIZATION_HEADER; + case JOB_TOKEN: + return JOB_TOKEN_HEADER; + default: + return PRIVATE_TOKEN_HEADER; + } + } + /** * Used to set the host URL to be used by OAUTH2 login in GitLabApi. */