diff --git a/server/src/main/java/org/eclipse/openvsx/ExtensionService.java b/server/src/main/java/org/eclipse/openvsx/ExtensionService.java index 8f0a5d544..5533d38dc 100644 --- a/server/src/main/java/org/eclipse/openvsx/ExtensionService.java +++ b/server/src/main/java/org/eclipse/openvsx/ExtensionService.java @@ -30,10 +30,8 @@ import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; -import java.nio.file.StandardCopyOption; import java.time.LocalDateTime; import java.util.LinkedHashSet; -import java.util.concurrent.atomic.AtomicLong; @Component public class ExtensionService { diff --git a/server/src/main/java/org/eclipse/openvsx/eclipse/EclipseService.java b/server/src/main/java/org/eclipse/openvsx/eclipse/EclipseService.java index ad4dfec1d..bd00033da 100644 --- a/server/src/main/java/org/eclipse/openvsx/eclipse/EclipseService.java +++ b/server/src/main/java/org/eclipse/openvsx/eclipse/EclipseService.java @@ -150,9 +150,30 @@ public void enrichUserJson(UserJson json, UserData user) { return; } - // Report user as logged in only if there is a usabe token: + var usableToken = true; + ErrorResultException exception = null; + try { + // Add information on the publisher agreement + var agreement = getPublisherAgreement(user); + if (agreement == null || !agreement.isActive || agreement.version == null) + json.publisherAgreement.status = "none"; + else if (publisherAgreementVersion.equals(agreement.version)) + json.publisherAgreement.status = "signed"; + else + json.publisherAgreement.status = "outdated"; + if (agreement != null && agreement.timestamp != null) + json.publisherAgreement.timestamp = TimeUtil.toUTCString(agreement.timestamp); + } catch (ErrorResultException e) { + if(e.getStatus() == HttpStatus.FORBIDDEN) { + usableToken = false; + } else { + exception = e; + } + } + + // Report user as logged in only if there is a usable token: // we need the token to access the Eclipse REST API - if (tokens.isUsable(user.getEclipseToken())) { + if(usableToken) { var eclipseLogin = new UserJson(); eclipseLogin.provider = "eclipse"; eclipseLogin.loginName = personId; @@ -162,16 +183,10 @@ public void enrichUserJson(UserJson json, UserData user) { json.additionalLogins.add(eclipseLogin); } - // Add information on the publisher agreement - var agreement = getPublisherAgreement(user); - if (agreement == null || !agreement.isActive || agreement.version == null) - json.publisherAgreement.status = "none"; - else if (publisherAgreementVersion.equals(agreement.version)) - json.publisherAgreement.status = "signed"; - else - json.publisherAgreement.status = "outdated"; - if (agreement != null && agreement.timestamp != null) - json.publisherAgreement.timestamp = TimeUtil.toUTCString(agreement.timestamp); + // Throw exception at end of method, so that JSON data is fully enriched + if(exception != null) { + throw exception; + } } /** @@ -240,8 +255,9 @@ public PublisherAgreement getPublisherAgreement(UserData user) { var json = restTemplate.exchange(urlTemplate, HttpMethod.GET, request, String.class, uriVariables); return parseAgreementResponse(json); } catch (RestClientException exc) { + HttpStatusCode status = HttpStatus.INTERNAL_SERVER_ERROR; if (exc instanceof HttpStatusCodeException) { - var status = ((HttpStatusCodeException) exc).getStatusCode(); + status = ((HttpStatusCodeException) exc).getStatusCode(); // The endpoint yields 404 if the specified user has not signed a publisher agreement if (status == HttpStatus.NOT_FOUND) return null; @@ -250,7 +266,7 @@ public PublisherAgreement getPublisherAgreement(UserData user) { var url = UriComponentsBuilder.fromUriString(urlTemplate).build(uriVariables); logger.error("Get request failed with URL: " + url, exc); throw new ErrorResultException("Request for retrieving publisher agreement failed: " + exc.getMessage(), - HttpStatus.INTERNAL_SERVER_ERROR); + status); } } diff --git a/server/src/main/java/org/eclipse/openvsx/eclipse/PublisherComplianceChecker.java b/server/src/main/java/org/eclipse/openvsx/eclipse/PublisherComplianceChecker.java index dc2df00f2..786181450 100644 --- a/server/src/main/java/org/eclipse/openvsx/eclipse/PublisherComplianceChecker.java +++ b/server/src/main/java/org/eclipse/openvsx/eclipse/PublisherComplianceChecker.java @@ -89,7 +89,7 @@ private boolean isCompliant(UserData user) { var json = new UserJson(); try { eclipseService.enrichUserJson(json, user); - return !json.publisherAgreement.status.equals("none"); + return json.publisherAgreement.status == null || !json.publisherAgreement.status.equals("none"); } catch(ErrorResultException e) { // no way to determine whether the user has a publisher agreement return true; diff --git a/server/src/main/java/org/eclipse/openvsx/security/TokenService.java b/server/src/main/java/org/eclipse/openvsx/security/TokenService.java index bf30087da..a15e7c68f 100644 --- a/server/src/main/java/org/eclipse/openvsx/security/TokenService.java +++ b/server/src/main/java/org/eclipse/openvsx/security/TokenService.java @@ -80,7 +80,6 @@ public AuthToken updateTokens(long userId, String registrationId, OAuth2AccessTo token.scopes = accessToken.getScopes(); token.issuedAt = accessToken.getIssuedAt(); token.expiresAt = accessToken.getExpiresAt(); - if (refreshToken != null) { token.refreshToken = refreshToken.getTokenValue(); token.refreshExpiresAt = refreshToken.getExpiresAt(); @@ -145,16 +144,6 @@ public AuthToken getActiveToken(UserData userData, String registrationId) { return null; } - public boolean isUsable(AuthToken token) { - if (token == null) - return false; - if (token.accessToken != null && !isExpired(token.expiresAt)) - return true; - if (token.refreshToken != null && !isExpired(token.refreshExpiresAt)) - return true; - return false; - } - private boolean isExpired(Instant instant) { return instant != null && Instant.now().isAfter(instant); } diff --git a/server/src/main/java/org/eclipse/openvsx/util/ErrorResultException.java b/server/src/main/java/org/eclipse/openvsx/util/ErrorResultException.java index 126516bad..f24f90c71 100644 --- a/server/src/main/java/org/eclipse/openvsx/util/ErrorResultException.java +++ b/server/src/main/java/org/eclipse/openvsx/util/ErrorResultException.java @@ -11,6 +11,7 @@ import org.eclipse.openvsx.json.ResultJson; import org.springframework.http.HttpStatus; +import org.springframework.http.HttpStatusCode; import org.springframework.http.ResponseEntity; /** @@ -26,7 +27,7 @@ public class ErrorResultException extends RuntimeException { private static final long serialVersionUID = 147466147310091931L; - private final HttpStatus status; + private final HttpStatusCode status; public ErrorResultException(String message) { super(message); @@ -38,12 +39,12 @@ public ErrorResultException(String message, Throwable cause) { this.status = null; } - public ErrorResultException(String message, HttpStatus status) { + public ErrorResultException(String message, HttpStatusCode status) { super(message); this.status = status; } - public HttpStatus getStatus() { + public HttpStatusCode getStatus() { return status; } diff --git a/server/src/main/jooq-gen/org/eclipse/openvsx/jooq/tables/UserData.java b/server/src/main/jooq-gen/org/eclipse/openvsx/jooq/tables/UserData.java index 4226a606d..5bb4f55a3 100644 --- a/server/src/main/jooq-gen/org/eclipse/openvsx/jooq/tables/UserData.java +++ b/server/src/main/jooq-gen/org/eclipse/openvsx/jooq/tables/UserData.java @@ -95,11 +95,6 @@ public Class getRecordType() { */ public final TableField ROLE = createField(DSL.name("role"), SQLDataType.VARCHAR(32), this, ""); - /** - * The column public.user_data.eclipse_data. - */ - public final TableField ECLIPSE_DATA = createField(DSL.name("eclipse_data"), SQLDataType.VARCHAR(4096), this, ""); - /** * The column public.user_data.eclipse_token. */ @@ -110,6 +105,11 @@ public Class getRecordType() { */ public final TableField GITHUB_TOKEN = createField(DSL.name("github_token"), SQLDataType.VARCHAR(4096), this, ""); + /** + * The column public.user_data.eclipse_person_id. + */ + public final TableField ECLIPSE_PERSON_ID = createField(DSL.name("eclipse_person_id"), SQLDataType.VARCHAR(255), this, ""); + private UserData(Name alias, Table aliased) { this(alias, aliased, null); } diff --git a/server/src/main/jooq-gen/org/eclipse/openvsx/jooq/tables/records/UserDataRecord.java b/server/src/main/jooq-gen/org/eclipse/openvsx/jooq/tables/records/UserDataRecord.java index 6f849a003..ca0c6f48e 100644 --- a/server/src/main/jooq-gen/org/eclipse/openvsx/jooq/tables/records/UserDataRecord.java +++ b/server/src/main/jooq-gen/org/eclipse/openvsx/jooq/tables/records/UserDataRecord.java @@ -147,44 +147,44 @@ public String getRole() { } /** - * Setter for public.user_data.eclipse_data. + * Setter for public.user_data.eclipse_token. */ - public void setEclipseData(String value) { + public void setEclipseToken(String value) { set(9, value); } /** - * Getter for public.user_data.eclipse_data. + * Getter for public.user_data.eclipse_token. */ - public String getEclipseData() { + public String getEclipseToken() { return (String) get(9); } /** - * Setter for public.user_data.eclipse_token. + * Setter for public.user_data.github_token. */ - public void setEclipseToken(String value) { + public void setGithubToken(String value) { set(10, value); } /** - * Getter for public.user_data.eclipse_token. + * Getter for public.user_data.github_token. */ - public String getEclipseToken() { + public String getGithubToken() { return (String) get(10); } /** - * Setter for public.user_data.github_token. + * Setter for public.user_data.eclipse_person_id. */ - public void setGithubToken(String value) { + public void setEclipsePersonId(String value) { set(11, value); } /** - * Getter for public.user_data.github_token. + * Getter for public.user_data.eclipse_person_id. */ - public String getGithubToken() { + public String getEclipsePersonId() { return (String) get(11); } @@ -258,17 +258,17 @@ public Field field9() { @Override public Field field10() { - return UserData.USER_DATA.ECLIPSE_DATA; + return UserData.USER_DATA.ECLIPSE_TOKEN; } @Override public Field field11() { - return UserData.USER_DATA.ECLIPSE_TOKEN; + return UserData.USER_DATA.GITHUB_TOKEN; } @Override public Field field12() { - return UserData.USER_DATA.GITHUB_TOKEN; + return UserData.USER_DATA.ECLIPSE_PERSON_ID; } @Override @@ -318,17 +318,17 @@ public String component9() { @Override public String component10() { - return getEclipseData(); + return getEclipseToken(); } @Override public String component11() { - return getEclipseToken(); + return getGithubToken(); } @Override public String component12() { - return getGithubToken(); + return getEclipsePersonId(); } @Override @@ -378,17 +378,17 @@ public String value9() { @Override public String value10() { - return getEclipseData(); + return getEclipseToken(); } @Override public String value11() { - return getEclipseToken(); + return getGithubToken(); } @Override public String value12() { - return getGithubToken(); + return getEclipsePersonId(); } @Override @@ -447,19 +447,19 @@ public UserDataRecord value9(String value) { @Override public UserDataRecord value10(String value) { - setEclipseData(value); + setEclipseToken(value); return this; } @Override public UserDataRecord value11(String value) { - setEclipseToken(value); + setGithubToken(value); return this; } @Override public UserDataRecord value12(String value) { - setGithubToken(value); + setEclipsePersonId(value); return this; } @@ -494,7 +494,7 @@ public UserDataRecord() { /** * Create a detached, initialised UserDataRecord */ - public UserDataRecord(Long id, String avatarUrl, String email, String fullName, String loginName, String provider, String authId, String providerUrl, String role, String eclipseData, String eclipseToken, String githubToken) { + public UserDataRecord(Long id, String avatarUrl, String email, String fullName, String loginName, String provider, String authId, String providerUrl, String role, String eclipseToken, String githubToken, String eclipsePersonId) { super(UserData.USER_DATA); setId(id); @@ -506,9 +506,9 @@ public UserDataRecord(Long id, String avatarUrl, String email, String fullName, setAuthId(authId); setProviderUrl(providerUrl); setRole(role); - setEclipseData(eclipseData); setEclipseToken(eclipseToken); setGithubToken(githubToken); + setEclipsePersonId(eclipsePersonId); resetChangedOnNotNull(); } }