Skip to content

Commit c393f61

Browse files
authored
Add support for Job Token (#1188)
Fixes #678
1 parent e883f9e commit c393f61

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

Diff for: src/main/java/org/gitlab4j/api/Constants.java

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public interface Constants {
3838
public enum TokenType {
3939
ACCESS,
4040
OAUTH2_ACCESS,
41+
JOB_TOKEN,
4142
PRIVATE;
4243
}
4344

Diff for: src/main/java/org/gitlab4j/api/GitLabApiClient.java

+23-2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
public class GitLabApiClient implements AutoCloseable {
5757

5858
protected static final String PRIVATE_TOKEN_HEADER = "PRIVATE-TOKEN";
59+
protected static final String JOB_TOKEN_HEADER = "JOB-TOKEN";
5960
protected static final String SUDO_HEADER = "Sudo";
6061
protected static final String AUTHORIZATION_HEADER = "Authorization";
6162
protected static final String X_GITLAB_TOKEN_HEADER = "X-Gitlab-Token";
@@ -861,8 +862,8 @@ protected Invocation.Builder invocation(URL url, MultivaluedMap<String, String>
861862
}
862863
}
863864

864-
String authHeader = (tokenType == TokenType.OAUTH2_ACCESS ? AUTHORIZATION_HEADER : PRIVATE_TOKEN_HEADER);
865-
String authValue = (tokenType == TokenType.OAUTH2_ACCESS ? "Bearer " + authToken.get() : authToken.get());
865+
String authHeader = getAuthHeader();
866+
String authValue = getAuthValue();
866867
Invocation.Builder builder = target.request();
867868
if (accept == null || accept.trim().length() == 0) {
868869
builder = builder.header(authHeader, authValue);
@@ -886,6 +887,26 @@ protected Invocation.Builder invocation(URL url, MultivaluedMap<String, String>
886887
return (builder);
887888
}
888889

890+
private String getAuthValue() {
891+
switch (tokenType) {
892+
case OAUTH2_ACCESS:
893+
return "Bearer " + authToken.get();
894+
default:
895+
return authToken.get();
896+
}
897+
}
898+
899+
private String getAuthHeader() {
900+
switch (tokenType) {
901+
case OAUTH2_ACCESS:
902+
return AUTHORIZATION_HEADER;
903+
case JOB_TOKEN:
904+
return JOB_TOKEN_HEADER;
905+
default:
906+
return PRIVATE_TOKEN_HEADER;
907+
}
908+
}
909+
889910
/**
890911
* Used to set the host URL to be used by OAUTH2 login in GitLabApi.
891912
*/

0 commit comments

Comments
 (0)