Skip to content

Commit feaaf03

Browse files
committed
Merge pull request timols#89 from wikiwi/header-auth-method
Implement Authentication via an HTTP Header
2 parents b6ba137 + ca41b31 commit feaaf03

File tree

5 files changed

+78
-17
lines changed

5 files changed

+78
-17
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.gitlab.api;
2+
3+
public enum AuthMethod {
4+
HEADER, URL_PARAMETER
5+
}

src/main/java/org/gitlab/api/GitlabAPI.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,33 @@ public class GitlabAPI {
3232

3333
private final String apiToken;
3434
private final TokenType tokenType;
35+
private AuthMethod authMethod;
3536
private boolean ignoreCertificateErrors = false;
3637

37-
private GitlabAPI(String hostUrl, String apiToken, TokenType tokenType) {
38+
private GitlabAPI(String hostUrl, String apiToken, TokenType tokenType, AuthMethod method) {
3839
this.hostUrl = hostUrl.endsWith("/") ? hostUrl.replaceAll("/$", "") : hostUrl;
3940
this.apiToken = apiToken;
4041
this.tokenType = tokenType;
42+
this.authMethod = method;
4143
}
4244

4345
public static GitlabSession connect(String hostUrl, String username, String password) throws IOException {
4446
String tailUrl = GitlabSession.URL;
45-
GitlabAPI api = connect(hostUrl, null, (TokenType) null);
47+
GitlabAPI api = connect(hostUrl, null, null, null);
4648
return api.dispatch().with("login", username).with("password", password)
4749
.to(tailUrl, GitlabSession.class);
4850
}
4951

5052
public static GitlabAPI connect(String hostUrl, String apiToken) {
51-
return new GitlabAPI(hostUrl, apiToken, TokenType.PRIVATE_TOKEN);
53+
return new GitlabAPI(hostUrl, apiToken, TokenType.PRIVATE_TOKEN, AuthMethod.HEADER);
5254
}
5355

5456
public static GitlabAPI connect(String hostUrl, String apiToken, TokenType tokenType) {
55-
return new GitlabAPI(hostUrl, apiToken, tokenType);
57+
return new GitlabAPI(hostUrl, apiToken, tokenType, AuthMethod.HEADER);
58+
}
59+
60+
public static GitlabAPI connect(String hostUrl, String apiToken, TokenType tokenType, AuthMethod method) {
61+
return new GitlabAPI(hostUrl, apiToken, tokenType, method);
5662
}
5763

5864
public GitlabAPI ignoreCertificateErrors(boolean ignoreCertificateErrors) {
@@ -61,22 +67,18 @@ public GitlabAPI ignoreCertificateErrors(boolean ignoreCertificateErrors) {
6167
}
6268

6369
public GitlabHTTPRequestor retrieve() {
64-
return new GitlabHTTPRequestor(this);
70+
return new GitlabHTTPRequestor(this).authenticate(apiToken, tokenType, authMethod);
6571
}
6672

6773
public GitlabHTTPRequestor dispatch() {
68-
return new GitlabHTTPRequestor(this).method("POST");
74+
return new GitlabHTTPRequestor(this).authenticate(apiToken, tokenType, authMethod).method("POST");
6975
}
7076

7177
public boolean isIgnoreCertificateErrors() {
7278
return ignoreCertificateErrors;
7379
}
7480

7581
public URL getAPIUrl(String tailAPIUrl) throws IOException {
76-
if (apiToken != null) {
77-
tailAPIUrl = tailAPIUrl + (tailAPIUrl.indexOf('?') > 0 ? '&' : '?') + tokenType.getTokenParamName() + "=" + apiToken;
78-
}
79-
8082
if (!tailAPIUrl.startsWith("/")) {
8183
tailAPIUrl = "/" + tailAPIUrl;
8284
}
Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,35 @@
11
package org.gitlab.api;
22

33
public enum TokenType {
4-
PRIVATE_TOKEN("private_token")
5-
, ACCESS_TOKEN("access_token"),
6-
;
4+
PRIVATE_TOKEN("private_token", "PRIVATE-TOKEN", "%s"),
5+
ACCESS_TOKEN("access_token", "Authorization", "Bearer %s");
76

87
private final String tokenParamName;
8+
private final String tokenHeaderName;
9+
private final String tokenHeaderFormat;
910

10-
TokenType(String tokenParamName) {
11+
/**
12+
* Constructor
13+
*
14+
* @param tokenParamName The url parameter name when using AuthMethod.URL_PARAMETER
15+
* @param tokenHeaderName The header name when using AuthMethod.HEADER
16+
* @param tokenHeaderFormat The header format for String.format when using AuthMethod.HEADER
17+
*/
18+
TokenType(String tokenParamName, String tokenHeaderName, String tokenHeaderFormat) {
1119
this.tokenParamName = tokenParamName;
20+
this.tokenHeaderName = tokenHeaderName;
21+
this.tokenHeaderFormat = tokenHeaderFormat;
1222
}
1323

1424
public String getTokenParamName() {
1525
return tokenParamName;
1626
}
27+
28+
public String getTokenHeaderName() {
29+
return tokenHeaderName;
30+
}
31+
32+
public String getTokenHeaderFormat() {
33+
return tokenHeaderFormat;
34+
}
1735
}

src/main/java/org/gitlab/api/http/GitlabHTTPRequestor.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package org.gitlab.api.http;
22

33
import org.apache.commons.io.IOUtils;
4+
import org.gitlab.api.AuthMethod;
45
import org.gitlab.api.GitlabAPI;
6+
import org.gitlab.api.TokenType;
57
import org.gitlab.api.models.GitlabCommit;
68

79
import javax.net.ssl.*;
@@ -34,6 +36,10 @@ public class GitlabHTTPRequestor {
3436
private String method = "GET"; // Default to GET requests
3537
private Map<String, Object> data = new HashMap<String, Object>();
3638

39+
private String apiToken;
40+
private TokenType tokenType;
41+
private AuthMethod authMethod;
42+
3743
private enum METHOD {
3844
GET, PUT, POST, PATCH, DELETE, HEAD, OPTIONS, TRACE;
3945

@@ -56,6 +62,22 @@ public GitlabHTTPRequestor(GitlabAPI root) {
5662
this.root = root;
5763
}
5864

65+
/**
66+
* Sets authentication data for the request.
67+
* Has a fluent api for method chaining.
68+
*
69+
* @param token The token value
70+
* @param type The type of the token
71+
* @param method The authentication method
72+
* @return this
73+
*/
74+
public GitlabHTTPRequestor authenticate(String token, TokenType type, AuthMethod method) {
75+
this.apiToken = token;
76+
this.tokenType = type;
77+
this.authMethod = method;
78+
return this;
79+
}
80+
5981
/**
6082
* Sets the HTTP Request method for the request.
6183
* Has a fluent api for method chaining.
@@ -236,11 +258,11 @@ private void findNextUrl() throws MalformedURLException {
236258
// there is a bug in the Gitlab CE API
237259
// (https://gitlab.com/gitlab-org/gitlab-ce/issues/759)
238260
// that starts pagination with page=0 for commits
239-
this.url = new URL(url + "&page=1");
261+
this.url = new URL(url + (url.indexOf('?') > 0 ? '&' : '?') + "page=1");
240262
} else {
241263
// Since the page query was not present, its safe to assume that we just
242264
// currently used the first page, so we can default to page 2
243-
this.url = new URL(url + "&page=2");
265+
this.url = new URL(url + (url.indexOf('?') > 0 ? '&' : '?') + "&page=2");
244266
}
245267
}
246268
}
@@ -262,7 +284,16 @@ private HttpURLConnection setupConnection(URL url) throws IOException {
262284
ignoreCertificateErrors();
263285
}
264286

287+
if (apiToken != null && authMethod == AuthMethod.URL_PARAMETER) {
288+
String urlWithAuth = url.toString();
289+
urlWithAuth = urlWithAuth + (urlWithAuth.indexOf('?') > 0 ? '&' : '?') + tokenType.getTokenParamName() + "=" + apiToken;
290+
url = new URL(urlWithAuth);
291+
}
292+
265293
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
294+
if (apiToken != null && authMethod == AuthMethod.HEADER) {
295+
connection.setRequestProperty(tokenType.getTokenHeaderName(), String.format(tokenType.getTokenHeaderFormat(), apiToken));
296+
}
266297

267298
try {
268299
connection.setRequestMethod(method);

src/test/java/org/gitlab/api/GitlabAPITest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,19 @@ public void setup() throws IOException {
4040
}
4141
}
4242

43+
@Test
44+
public void testAllProjects() throws IOException {
45+
api.getAllProjects();
46+
}
47+
4348
@Test
4449
public void testConnect() throws IOException {
4550
assertEquals(GitlabAPI.class, api.getClass());
4651
}
4752

4853
@Test
4954
public void testGetAPIUrl() throws IOException {
50-
URL expected = new URL(TEST_URL + "/api/v3/?private_token=" + TEST_TOKEN);
55+
URL expected = new URL(TEST_URL + "/api/v3/");
5156
assertEquals(expected, api.getAPIUrl(""));
5257
}
5358

0 commit comments

Comments
 (0)