Skip to content

Commit

Permalink
Add support for Job Token (#1188)
Browse files Browse the repository at this point in the history
Fixes #678
  • Loading branch information
jmini authored Nov 6, 2024
1 parent e883f9e commit c393f61
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/main/java/org/gitlab4j/api/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public interface Constants {
public enum TokenType {
ACCESS,
OAUTH2_ACCESS,
JOB_TOKEN,
PRIVATE;
}

Expand Down
25 changes: 23 additions & 2 deletions src/main/java/org/gitlab4j/api/GitLabApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -861,8 +862,8 @@ protected Invocation.Builder invocation(URL url, MultivaluedMap<String, String>
}
}

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);
Expand All @@ -886,6 +887,26 @@ protected Invocation.Builder invocation(URL url, MultivaluedMap<String, String>
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.
*/
Expand Down

0 comments on commit c393f61

Please sign in to comment.