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

Fix AccessTokenUtils causing test failures #683

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
47 changes: 43 additions & 4 deletions src/main/java/org/gitlab4j/api/utils/AccessTokenUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public String toString() {
protected static final String USER_AGENT = "GitLab4J Client";
protected static final String COOKIES_HEADER = "Set-Cookie";

protected static final String NEW_USER_AUTHENTICITY_TOKEN_REGEX = "\"new_user\".*name=\\\"authenticity_token\\\"\\svalue=\\\"([^\\\"]*)\\\".*new_new_user";
protected static final String NEW_USER_AUTHENTICITY_TOKEN_REGEX = "\"new_user\".*name=\\\"authenticity_token\\\"\\svalue=\\\"([^\\\"]*)\\\"";
protected static final Pattern NEW_USER_AUTHENTICITY_TOKEN_PATTERN = Pattern.compile(NEW_USER_AUTHENTICITY_TOKEN_REGEX);

protected static final String AUTHENTICITY_TOKEN_REGEX = "name=\\\"authenticity_token\\\"\\svalue=\\\"([^\\\"]*)\\\"";
Expand Down Expand Up @@ -170,8 +170,21 @@ public static final String createPersonalAccessToken(final String baseUrl, final
connection.setReadTimeout(10000);
connection.setConnectTimeout(10000);

// Make sure the response code is 200, otherwise there is a failure
// Check if a redirect was provided, follow it if so (profile URLs are prefixed with -/ now)
int responseCode = connection.getResponseCode();
if (responseCode == 301) {
// Follow the redirect with the provided session cookie
String profileRedirectUrl = connection.getHeaderField("Location");
url = new URL(profileRedirectUrl);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("User-Agent", USER_AGENT);
connection.setRequestProperty("Cookie", cookies);
connection.setReadTimeout(10000);
connection.setConnectTimeout(10000);
}

// Make sure the response code is 200, otherwise there is a failure
responseCode = connection.getResponseCode();
if (responseCode != 200) {
throw new GitLabApiException("Failure loading Access Tokens page, aborting!");
}
Expand Down Expand Up @@ -322,8 +335,21 @@ public static final void revokePersonalAccessToken(final String baseUrl, final S
connection.setReadTimeout(10000);
connection.setConnectTimeout(10000);

// Make sure the response code is 200, otherwise there is a failure
// Check if a redirect was provided, follow it if so (profile URLs are prefixed with -/ now)
int responseCode = connection.getResponseCode();
if (responseCode == 301) {
// Follow the redirect with the provided session cookie
String profileRedirectUrl = connection.getHeaderField("Location");
url = new URL(profileRedirectUrl);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("User-Agent", USER_AGENT);
connection.setRequestProperty("Cookie", cookies);
connection.setReadTimeout(10000);
connection.setConnectTimeout(10000);
}

// Make sure the response code is 200, otherwise there is a failure
responseCode = connection.getResponseCode();
if (responseCode != 200) {
throw new GitLabApiException("Failure loading Access Tokens page, aborting!");
}
Expand Down Expand Up @@ -465,8 +491,21 @@ public static final String getFeedToken(final String baseUrl, final String usern
connection.setReadTimeout(10000);
connection.setConnectTimeout(10000);

// Make sure the response code is 200, otherwise there is a failure
// Check if a redirect was provided, follow it if so (profile URLs are prefixed with -/ now)
int responseCode = connection.getResponseCode();
if (responseCode == 301) {
// Follow the redirect with the provided session cookie
String profileRedirectUrl = connection.getHeaderField("Location");
url = new URL(profileRedirectUrl);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("User-Agent", USER_AGENT);
connection.setRequestProperty("Cookie", cookies);
connection.setReadTimeout(10000);
connection.setConnectTimeout(10000);
}

// Make sure the response code is 200, otherwise there is a failure
responseCode = connection.getResponseCode();
if (responseCode != 200) {
throw new GitLabApiException("Failure loading Access Tokens page, aborting!");
}
Expand Down