Skip to content

Commit 318b361

Browse files
committed
Fixed AccessTokenUtils.revokePersonalAccessToken() (#336).
1 parent 91fd03a commit 318b361

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

src/main/java/org/gitlab4j/api/utils/AccessTokenUtils.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ public final class AccessTokenUtils {
3636
protected static final String PERSONAL_ACCESS_TOKEN_REGEX = "name=\\\"created-personal-access-token\\\".*data-clipboard-text=\\\"([^\\\"]*)\\\".*\\/>";
3737
protected static final Pattern PERSONAL_ACCESS_TOKEN_PATTERN = Pattern.compile(PERSONAL_ACCESS_TOKEN_REGEX);
3838

39-
protected static final String REVOKE_PERSONAL_ACCESS_TOKEN_REGEX = "<td>%s<\\/td>.*<td>%s<\\/td>.*href=\\\"([^\\\"]*)\\\">Revoke";
39+
protected static final String REVOKE_PERSONAL_ACCESS_TOKEN_REGEX = "href=\\\"([^\\\"]*)\\\"";
40+
protected static final Pattern REVOKE_PERSONAL_ACCESS_TOKEN_PATTERN = Pattern.compile(REVOKE_PERSONAL_ACCESS_TOKEN_REGEX);
4041

4142
protected static final String FEED_TOKEN_REGEX = "name=\\\"feed_token\\\".*value=\\\"([^\\\"]*)\\\".*\\/>";
4243
protected static final Pattern FEED_TOKEN_PATTERN = Pattern.compile(FEED_TOKEN_REGEX);
@@ -230,16 +231,30 @@ public static final void revokePersonalAccessToken(final String baseUrl, final S
230231
* Step 3: Submit the /profile/personal_access_tokens page with the info to *
231232
* revoke the first matching personal access token. *
232233
*******************************************************************************/
234+
int indexOfTokenName = content.indexOf("<td>" + tokenName + "</td>");
235+
if (indexOfTokenName == -1) {
236+
throw new GitLabApiException("personal access token not found, aborting!");
237+
}
238+
239+
content = content.substring(indexOfTokenName);
240+
int indexOfLinkEnd = content.indexOf("</a>");
241+
if (indexOfTokenName == -1) {
242+
throw new GitLabApiException("personal access token not found, aborting!");
243+
}
244+
245+
content = content.substring(0, indexOfLinkEnd);
233246
String scopesText = "";
234247
if (scopes != null && scopes.size() > 0) {
235248
final StringJoiner joiner = new StringJoiner(", ");
236249
scopes.forEach(s -> joiner.add(s));
237250
scopesText = joiner.toString();
238251
}
239252

240-
String regex = String.format(REVOKE_PERSONAL_ACCESS_TOKEN_REGEX, tokenName, scopesText);
241-
Pattern pattern = Pattern.compile(regex);
242-
matcher = pattern.matcher(content);
253+
if (content.indexOf(scopesText) == -1) {
254+
throw new GitLabApiException("personal access token not found, aborting!");
255+
}
256+
257+
matcher = REVOKE_PERSONAL_ACCESS_TOKEN_PATTERN.matcher(content);
243258
if (!matcher.find()) {
244259
throw new GitLabApiException("personal access token not found, aborting!");
245260
}

src/test/java/org/gitlab4j/api/TestAccessTokenUtils.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public void testCreatePersonalAccessToken() throws GitLabApiException {
7272
String accessToken = AccessTokenUtils.createPersonalAccessToken(
7373
TEST_HOST_URL, TEST_LOGIN_USERNAME, TEST_LOGIN_PASSWORD,
7474
tokenName, Arrays.asList("api", "sudo"));
75-
System.out.println("Created personal access token: " + accessToken);
75+
System.out.format("Created '%s' personal access token: %s%n", tokenName, accessToken);
7676

7777
assertNotNull(accessToken);
7878
assertFalse(accessToken.trim().isEmpty());
@@ -82,7 +82,7 @@ public void testCreatePersonalAccessToken() throws GitLabApiException {
8282
AccessTokenUtils.revokePersonalAccessToken(
8383
TEST_HOST_URL, TEST_LOGIN_USERNAME, TEST_LOGIN_PASSWORD,
8484
tokenName, Arrays.asList("api", "sudo"));
85-
System.out.println("Revoked personal access token: " + accessToken);
85+
System.out.format("Revoked '%s' personal access token: %s%n", tokenName, accessToken);
8686
} catch (Exception ignore) {}
8787
}
8888

@@ -107,14 +107,14 @@ public void testRevokePersonalAccessToken() throws GitLabApiException {
107107
String accessToken = AccessTokenUtils.createPersonalAccessToken(
108108
TEST_HOST_URL, TEST_LOGIN_USERNAME, TEST_LOGIN_PASSWORD,
109109
tokenName, Arrays.asList("api", "sudo"));
110-
System.out.println("Created personal access token: " + accessToken);
110+
System.out.format("Created '%s' personal access token: %s%n", tokenName, accessToken);
111111
assertNotNull(accessToken);
112112
assertFalse(accessToken.trim().isEmpty());
113113

114114
AccessTokenUtils.revokePersonalAccessToken(
115115
TEST_HOST_URL, TEST_LOGIN_USERNAME, TEST_LOGIN_PASSWORD,
116116
tokenName, Arrays.asList("api", "sudo"));
117-
System.out.println("Revoked personal access token: " + accessToken);
117+
System.out.format("Revoked '%s' personal access token: %s%n", tokenName, accessToken);
118118
}
119119

120120
@Test

0 commit comments

Comments
 (0)