Skip to content

Commit 04316db

Browse files
author
amvanbaren
committed
Hande Eclipse API expired token
1 parent 4a4193d commit 04316db

File tree

7 files changed

+66
-62
lines changed

7 files changed

+66
-62
lines changed

server/src/main/java/org/eclipse/openvsx/ExtensionService.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@
3030
import java.io.IOException;
3131
import java.io.InputStream;
3232
import java.nio.file.Files;
33-
import java.nio.file.StandardCopyOption;
3433
import java.time.LocalDateTime;
3534
import java.util.LinkedHashSet;
36-
import java.util.concurrent.atomic.AtomicLong;
3735

3836
@Component
3937
public class ExtensionService {

server/src/main/java/org/eclipse/openvsx/eclipse/EclipseService.java

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,30 @@ public void enrichUserJson(UserJson json, UserData user) {
150150
return;
151151
}
152152

153-
// Report user as logged in only if there is a usabe token:
153+
var usableToken = true;
154+
ErrorResultException exception = null;
155+
try {
156+
// Add information on the publisher agreement
157+
var agreement = getPublisherAgreement(user);
158+
if (agreement == null || !agreement.isActive || agreement.version == null)
159+
json.publisherAgreement.status = "none";
160+
else if (publisherAgreementVersion.equals(agreement.version))
161+
json.publisherAgreement.status = "signed";
162+
else
163+
json.publisherAgreement.status = "outdated";
164+
if (agreement != null && agreement.timestamp != null)
165+
json.publisherAgreement.timestamp = TimeUtil.toUTCString(agreement.timestamp);
166+
} catch (ErrorResultException e) {
167+
if(e.getStatus() == HttpStatus.FORBIDDEN) {
168+
usableToken = false;
169+
} else {
170+
exception = e;
171+
}
172+
}
173+
174+
// Report user as logged in only if there is a usable token:
154175
// we need the token to access the Eclipse REST API
155-
if (tokens.isUsable(user.getEclipseToken())) {
176+
if(usableToken) {
156177
var eclipseLogin = new UserJson();
157178
eclipseLogin.provider = "eclipse";
158179
eclipseLogin.loginName = personId;
@@ -162,16 +183,10 @@ public void enrichUserJson(UserJson json, UserData user) {
162183
json.additionalLogins.add(eclipseLogin);
163184
}
164185

165-
// Add information on the publisher agreement
166-
var agreement = getPublisherAgreement(user);
167-
if (agreement == null || !agreement.isActive || agreement.version == null)
168-
json.publisherAgreement.status = "none";
169-
else if (publisherAgreementVersion.equals(agreement.version))
170-
json.publisherAgreement.status = "signed";
171-
else
172-
json.publisherAgreement.status = "outdated";
173-
if (agreement != null && agreement.timestamp != null)
174-
json.publisherAgreement.timestamp = TimeUtil.toUTCString(agreement.timestamp);
186+
// Throw exception at end of method, so that JSON data is fully enriched
187+
if(exception != null) {
188+
throw exception;
189+
}
175190
}
176191

177192
/**
@@ -240,8 +255,9 @@ public PublisherAgreement getPublisherAgreement(UserData user) {
240255
var json = restTemplate.exchange(urlTemplate, HttpMethod.GET, request, String.class, uriVariables);
241256
return parseAgreementResponse(json);
242257
} catch (RestClientException exc) {
258+
HttpStatusCode status = HttpStatus.INTERNAL_SERVER_ERROR;
243259
if (exc instanceof HttpStatusCodeException) {
244-
var status = ((HttpStatusCodeException) exc).getStatusCode();
260+
status = ((HttpStatusCodeException) exc).getStatusCode();
245261
// The endpoint yields 404 if the specified user has not signed a publisher agreement
246262
if (status == HttpStatus.NOT_FOUND)
247263
return null;
@@ -250,7 +266,7 @@ public PublisherAgreement getPublisherAgreement(UserData user) {
250266
var url = UriComponentsBuilder.fromUriString(urlTemplate).build(uriVariables);
251267
logger.error("Get request failed with URL: " + url, exc);
252268
throw new ErrorResultException("Request for retrieving publisher agreement failed: " + exc.getMessage(),
253-
HttpStatus.INTERNAL_SERVER_ERROR);
269+
status);
254270
}
255271
}
256272

server/src/main/java/org/eclipse/openvsx/eclipse/PublisherComplianceChecker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ private boolean isCompliant(UserData user) {
8989
var json = new UserJson();
9090
try {
9191
eclipseService.enrichUserJson(json, user);
92-
return !json.publisherAgreement.status.equals("none");
92+
return json.publisherAgreement.status == null || !json.publisherAgreement.status.equals("none");
9393
} catch(ErrorResultException e) {
9494
// no way to determine whether the user has a publisher agreement
9595
return true;

server/src/main/java/org/eclipse/openvsx/security/TokenService.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ public AuthToken updateTokens(long userId, String registrationId, OAuth2AccessTo
8080
token.scopes = accessToken.getScopes();
8181
token.issuedAt = accessToken.getIssuedAt();
8282
token.expiresAt = accessToken.getExpiresAt();
83-
8483
if (refreshToken != null) {
8584
token.refreshToken = refreshToken.getTokenValue();
8685
token.refreshExpiresAt = refreshToken.getExpiresAt();
@@ -145,16 +144,6 @@ public AuthToken getActiveToken(UserData userData, String registrationId) {
145144
return null;
146145
}
147146

148-
public boolean isUsable(AuthToken token) {
149-
if (token == null)
150-
return false;
151-
if (token.accessToken != null && !isExpired(token.expiresAt))
152-
return true;
153-
if (token.refreshToken != null && !isExpired(token.refreshExpiresAt))
154-
return true;
155-
return false;
156-
}
157-
158147
private boolean isExpired(Instant instant) {
159148
return instant != null && Instant.now().isAfter(instant);
160149
}

server/src/main/java/org/eclipse/openvsx/util/ErrorResultException.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import org.eclipse.openvsx.json.ResultJson;
1313
import org.springframework.http.HttpStatus;
14+
import org.springframework.http.HttpStatusCode;
1415
import org.springframework.http.ResponseEntity;
1516

1617
/**
@@ -26,7 +27,7 @@ public class ErrorResultException extends RuntimeException {
2627

2728
private static final long serialVersionUID = 147466147310091931L;
2829

29-
private final HttpStatus status;
30+
private final HttpStatusCode status;
3031

3132
public ErrorResultException(String message) {
3233
super(message);
@@ -38,12 +39,12 @@ public ErrorResultException(String message, Throwable cause) {
3839
this.status = null;
3940
}
4041

41-
public ErrorResultException(String message, HttpStatus status) {
42+
public ErrorResultException(String message, HttpStatusCode status) {
4243
super(message);
4344
this.status = status;
4445
}
4546

46-
public HttpStatus getStatus() {
47+
public HttpStatusCode getStatus() {
4748
return status;
4849
}
4950

server/src/main/jooq-gen/org/eclipse/openvsx/jooq/tables/UserData.java

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/src/main/jooq-gen/org/eclipse/openvsx/jooq/tables/records/UserDataRecord.java

Lines changed: 26 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)