Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Job Token #1188

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading