Skip to content

Commit 6e76026

Browse files
committed
fix failing tests (fix AccessTokenUtils.revokePersonalAccessToken)
1 parent 1b09387 commit 6e76026

File tree

5 files changed

+42
-26
lines changed

5 files changed

+42
-26
lines changed

src/main/java/org/gitlab4j/api/models/OauthTokenResponse.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.gitlab4j.api.models;
22

3+
import com.fasterxml.jackson.annotation.JsonIgnore;
4+
35
import java.util.Date;
46

57
public class OauthTokenResponse {
@@ -60,6 +62,7 @@ public void setCreatedAt(Long createdAt) {
6062
}
6163

6264

65+
@JsonIgnore
6366
public Date getExpiresAt() {
6467
if (expiresIn == null) {
6568
return null;
@@ -69,6 +72,7 @@ public Date getExpiresAt() {
6972
return new Date(expiryTimeInSecondsSinceEpoch * 1000);
7073
}
7174

75+
@JsonIgnore
7276
public boolean isExpired() {
7377
Date expiresAt = getExpiresAt();
7478
if (expiresAt == null) {

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

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ public String toString() {
9898
protected static final String PERSONAL_ACCESS_TOKEN_REGEX = "\\{\"new_token\":\"([^\"]*)\"";
9999
protected static final Pattern PERSONAL_ACCESS_TOKEN_PATTERN = Pattern.compile(PERSONAL_ACCESS_TOKEN_REGEX);
100100

101-
protected static final String REVOKE_PERSONAL_ACCESS_TOKEN_REGEX = "href=\\\"([^\\\"]*)\\\"";
101+
protected static final String REVOKE_PERSONAL_ACCESS_TOKEN_REGEX =
102+
"\\{.*?"name":"(.*?)",.*?"scopes":\\[(.*?)\\],.*?"revoke_path":"(.*?)"\\}";
102103
protected static final Pattern REVOKE_PERSONAL_ACCESS_TOKEN_PATTERN = Pattern.compile(REVOKE_PERSONAL_ACCESS_TOKEN_REGEX);
103104

104105
protected static final String FEED_TOKEN_REGEX = "<div data-tokens-data=\\\"\\{.*&quot;feed_token&quot;:\\{.*?&quot;token&quot;:&quot;(.*?)&quot;,";
@@ -325,49 +326,59 @@ public static final void revokePersonalAccessToken(final String baseUrl, final S
325326
* Step 3: Submit the /profile/personal_access_tokens page with the info to *
326327
* revoke the first matching personal access token. *
327328
*******************************************************************************/
328-
int indexOfTokenName = content.indexOf("<td>" + tokenName + "</td>");
329-
if (indexOfTokenName == -1) {
329+
String tokensDataAttribute = "data-initial-active-access-tokens";
330+
int indexOfStartOfTokensJsonData = content.indexOf(tokensDataAttribute);
331+
if (indexOfStartOfTokensJsonData == -1) {
330332
throw new GitLabApiException("personal access token not found, aborting!");
331333
}
334+
indexOfStartOfTokensJsonData += tokensDataAttribute.length() + 2; // attribute name + ="
332335

333-
content = content.substring(indexOfTokenName);
334-
int indexOfLinkEnd = content.indexOf("</a>");
335-
if (indexOfTokenName == -1) {
336+
content = content.substring(indexOfStartOfTokensJsonData);
337+
int indexOfEndOfTokensJsonData = content.indexOf("\"");
338+
if (indexOfEndOfTokensJsonData == -1) {
336339
throw new GitLabApiException("personal access token not found, aborting!");
337340
}
338341

339-
content = content.substring(0, indexOfLinkEnd);
340-
String scopesText = "";
341-
if (scopes != null && scopes.size() > 0) {
342-
final StringJoiner joiner = new StringJoiner(", ");
343-
scopes.forEach(s -> joiner.add(s.toString()));
344-
scopesText = joiner.toString();
345-
}
342+
content = content.substring(0, indexOfEndOfTokensJsonData);
346343

347-
if (content.indexOf(scopesText) == -1) {
344+
matcher = REVOKE_PERSONAL_ACCESS_TOKEN_PATTERN.matcher(content);
345+
if (!matcher.find()) {
348346
throw new GitLabApiException("personal access token not found, aborting!");
349347
}
350348

351-
matcher = REVOKE_PERSONAL_ACCESS_TOKEN_PATTERN.matcher(content);
352-
if (!matcher.find()) {
349+
// the first token returned should be the one we want to revoke
350+
// iterate over the remaining matches if that changes
351+
String foundTokenName = matcher.group(1);
352+
String foundTokenScopes = matcher.group(2); // eg: &quot;api&quot;,&quot;sudo&quot;
353+
foundTokenScopes = foundTokenScopes.replace("&quot;", "");
354+
String foundTokenRevokePath = matcher.group(3);
355+
356+
String expectedScopesText = "";
357+
if (scopes != null && scopes.size() > 0) {
358+
final StringJoiner joiner = new StringJoiner(",");
359+
scopes.forEach(s -> joiner.add(s.toString()));
360+
expectedScopesText = joiner.toString();
361+
}
362+
363+
if (!foundTokenName.equals(tokenName) || !foundTokenScopes.equals(expectedScopesText)) {
353364
throw new GitLabApiException("personal access token not found, aborting!");
354365
}
355366

356-
String revokePath = matcher.group(1);
357-
url = new URL(baseUrl + revokePath);
367+
url = new URL(baseUrl + foundTokenRevokePath);
358368
connection = (HttpURLConnection) url.openConnection();
359369
connection.setRequestProperty("User-Agent", USER_AGENT);
360370
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
361371
connection.setRequestProperty("Charset", "utf-8");
362372
connection.setRequestProperty("Cookie", cookies);
363373
connection.setReadTimeout(10000);
364374
connection.setConnectTimeout(10000);
365-
connection.setRequestMethod("PUT");
375+
connection.setRequestMethod("POST");
366376
connection.setDoInput(true);
367377
connection.setDoOutput(true);
368378

369379
// Submit the form
370380
StringBuilder formData = new StringBuilder();
381+
addFormData(formData, "_method", "put");
371382
addFormData(formData, "authenticity_token", csrfToken);
372383
connection.setRequestProperty("Content-Length", String.valueOf(formData.length()));
373384
OutputStream output = connection.getOutputStream();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ public void testRequestAccess() throws GitLabApiException {
219219
}
220220

221221
Stream<AccessRequest> requests = gitLabApi.getGroupApi().getAccessRequestsStream(testGroup);
222-
assertTrue(requests.anyMatch(r -> r.getId() == userId));
222+
assertTrue(requests.anyMatch(r -> r.getId().equals(userId)));
223223

224224
AccessRequest accessRequest = gitLabApi.getGroupApi().approveAccessRequest(testGroup, user.getId(), AccessLevel.DEVELOPER);
225225
assertNotNull(accessRequest);
@@ -264,7 +264,7 @@ public void testDenyRequestAccess() throws GitLabApiException {
264264
}
265265

266266
List<AccessRequest> requests = gitLabApi.getGroupApi().getAccessRequests(testGroup);
267-
assertTrue(requests.stream().anyMatch(r -> r.getId() == userId));
267+
assertTrue(requests.stream().anyMatch(r -> r.getId().equals(userId)));
268268

269269
gitLabApi.getGroupApi().denyAccessRequest(testGroup, userId);
270270

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,7 @@ public void testRequestAccess() throws GitLabApiException {
807807
}
808808

809809
Stream<AccessRequest> requests = gitLabApi.getProjectApi().getAccessRequestsStream(testProject);
810-
assertTrue(requests.anyMatch(r -> r.getId() == userId));
810+
assertTrue(requests.anyMatch(r -> r.getId().equals(userId)));
811811

812812
AccessRequest accessRequest = gitLabApi.getProjectApi().approveAccessRequest(testProject, user.getId(), AccessLevel.DEVELOPER);
813813
assertNotNull(accessRequest);
@@ -817,7 +817,7 @@ public void testRequestAccess() throws GitLabApiException {
817817
user = null;
818818

819819
requests = gitLabApi.getProjectApi().getAccessRequestsStream(testProject);
820-
assertFalse(requests.anyMatch(r -> r.getId() == userId));
820+
assertFalse(requests.anyMatch(r -> r.getId().equals(userId)));
821821

822822
} finally {
823823
try {
@@ -852,12 +852,12 @@ public void testDenyRequestAccess() throws GitLabApiException {
852852
}
853853

854854
List<AccessRequest> requests = gitLabApi.getProjectApi().getAccessRequests(testProject);
855-
assertTrue(requests.stream().anyMatch(r -> r.getId() == userId));
855+
assertTrue(requests.stream().anyMatch(r -> r.getId().equals(userId)));
856856

857857
gitLabApi.getProjectApi().denyAccessRequest(testProject, userId);
858858

859859
requests = gitLabApi.getProjectApi().getAccessRequests(testProject);
860-
assertFalse(requests.stream().anyMatch(r -> r.getId() == userId));
860+
assertFalse(requests.stream().anyMatch(r -> r.getId().equals(userId)));
861861

862862
user = null;
863863

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"access_token": "7e6f7f02d5ce8e5c3d2123a210674ccea6b70547c77132b0dca502f8a4df760f",
33
"token_type": "bearer",
4+
"expires_in": 7200,
45
"refresh_token": "c29a678544ceb726a2a605e44502fa5cad8752cfab42aeae21413cacb703178f",
56
"scope": "api",
67
"created_at": 1514489666
7-
}
8+
}

0 commit comments

Comments
 (0)