Commit ef0a0d41 authored by Andreas Häber's avatar Andreas Häber

Support OAuth access_token

For OAuth the uri query parameter should be named access_token instead of
private_token. To support both a enum TokenType is introduced and should
be used to select which token type to be used for the API access.

For backwards compatibility TokenType#PRIVATE_TOKEN will be used by
default.
Signed-off-by: 's avatarAndreas Häber <andreas.haber@intele.com>
parent 71927310
...@@ -30,22 +30,28 @@ public class GitlabAPI { ...@@ -30,22 +30,28 @@ public class GitlabAPI {
private final String hostUrl; private final String hostUrl;
private final String apiToken; private final String apiToken;
private final TokenType tokenType;
private boolean ignoreCertificateErrors = false; private boolean ignoreCertificateErrors = false;
private GitlabAPI(String hostUrl, String apiToken) { private GitlabAPI(String hostUrl, String apiToken, TokenType tokenType) {
this.hostUrl = hostUrl.endsWith("/") ? hostUrl.replaceAll("/$", "") : hostUrl; this.hostUrl = hostUrl.endsWith("/") ? hostUrl.replaceAll("/$", "") : hostUrl;
this.apiToken = apiToken; this.apiToken = apiToken;
this.tokenType = tokenType;
} }
public static GitlabSession connect(String hostUrl, String username, String password) throws IOException { public static GitlabSession connect(String hostUrl, String username, String password) throws IOException {
String tailUrl = GitlabSession.URL; String tailUrl = GitlabSession.URL;
GitlabAPI api = connect(hostUrl, null); GitlabAPI api = connect(hostUrl, null, (TokenType) null);
return api.dispatch().with("login", username).with("password", password) return api.dispatch().with("login", username).with("password", password)
.to(tailUrl, GitlabSession.class); .to(tailUrl, GitlabSession.class);
} }
public static GitlabAPI connect(String hostUrl, String apiToken) { public static GitlabAPI connect(String hostUrl, String apiToken) {
return new GitlabAPI(hostUrl, apiToken); return new GitlabAPI(hostUrl, apiToken, TokenType.PRIVATE_TOKEN);
}
public static GitlabAPI connect(String hostUrl, String apiToken, TokenType tokenType) {
return new GitlabAPI(hostUrl, apiToken, tokenType);
} }
public GitlabAPI ignoreCertificateErrors(boolean ignoreCertificateErrors) { public GitlabAPI ignoreCertificateErrors(boolean ignoreCertificateErrors) {
...@@ -67,7 +73,7 @@ public class GitlabAPI { ...@@ -67,7 +73,7 @@ public class GitlabAPI {
public URL getAPIUrl(String tailAPIUrl) throws IOException { public URL getAPIUrl(String tailAPIUrl) throws IOException {
if (apiToken != null) { if (apiToken != null) {
tailAPIUrl = tailAPIUrl + (tailAPIUrl.indexOf('?') > 0 ? '&' : '?') + "private_token=" + apiToken; tailAPIUrl = tailAPIUrl + (tailAPIUrl.indexOf('?') > 0 ? '&' : '?') + tokenType.getTokenParamName() + "=" + apiToken;
} }
if (!tailAPIUrl.startsWith("/")) { if (!tailAPIUrl.startsWith("/")) {
......
package org.gitlab.api;
public enum TokenType {
PRIVATE_TOKEN("private_token")
, ACCESS_TOKEN("access_token"),
;
private final String tokenParamName;
TokenType(String tokenParamName) {
this.tokenParamName = tokenParamName;
}
public String getTokenParamName() {
return tokenParamName;
}
}
package org.gitlab.api.http; package org.gitlab.api.http;
import org.gitlab.api.GitlabAPI; import org.gitlab.api.GitlabAPI;
import org.gitlab.api.TokenType;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment