Skip to content

Commit b39116c

Browse files
authored
Merge pull request #917 from amvanbaren/public-profile-agreement
Use public profile
2 parents c498245 + dc9387a commit b39116c

File tree

5 files changed

+79
-28
lines changed

5 files changed

+79
-28
lines changed

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,7 @@ public UserJson getUserData() {
112112
json.role = user.getRole();
113113
json.tokensUrl = createApiUrl(serverUrl, "user", "tokens");
114114
json.createTokenUrl = createApiUrl(serverUrl, "user", "token", "create");
115-
try {
116-
eclipse.enrichUserJson(json, user);
117-
} catch (ErrorResultException e) {
118-
logger.error("Failed to enrich UserJson", e);
119-
}
115+
eclipse.enrichUserJson(json, user);
120116
return json;
121117
}
122118

server/src/main/java/org/eclipse/openvsx/admin/AdminService.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -265,12 +265,7 @@ public UserPublishInfoJson getUserPublishInfo(String provider, String loginName)
265265

266266
var userPublishInfo = new UserPublishInfoJson();
267267
userPublishInfo.user = user.toUserJson();
268-
try {
269-
eclipse.enrichUserJson(userPublishInfo.user, user);
270-
} catch (ErrorResultException e) {
271-
userPublishInfo.user.error = e.getMessage();
272-
}
273-
268+
eclipse.adminEnrichUserJson(userPublishInfo.user, user);
274269
userPublishInfo.activeAccessTokenNum = (int) repositories.countActiveAccessTokens(user);
275270
var extVersions = repositories.findLatestVersions(user);
276271
var types = new String[]{DOWNLOAD, MANIFEST, ICON, README, LICENSE, CHANGELOG, VSIXMANIFEST};

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

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,47 @@ public void checkPublisherAgreement(UserData user) {
114114
if (personId == null) {
115115
throw new ErrorResultException("You must log in with an Eclipse Foundation account and sign a Publisher Agreement before publishing any extension.");
116116
}
117-
var agreement = getPublisherAgreement(user);
118-
if (agreement == null || agreement.version == null) {
117+
var profile = getPublicProfile(personId);
118+
if (profile.publisherAgreements == null || profile.publisherAgreements.openVsx == null
119+
|| profile.publisherAgreements.openVsx.version == null) {
119120
throw new ErrorResultException("You must sign a Publisher Agreement with the Eclipse Foundation before publishing any extension.");
120121
}
121-
if (!publisherAgreementVersion.equals(agreement.version)) {
122+
if (!publisherAgreementVersion.equals(profile.publisherAgreements.openVsx.version)) {
122123
throw new ErrorResultException("Your Publisher Agreement with the Eclipse Foundation is outdated (version "
123-
+ agreement.version + "). The current version is "
124+
+ profile.publisherAgreements.openVsx.version + "). The current version is "
124125
+ publisherAgreementVersion + ".");
125126
}
126127
});
127128
}
128129

130+
/**
131+
* Get the publicly available user profile.
132+
*/
133+
public EclipseProfile getPublicProfile(String personId) {
134+
checkApiUrl();
135+
var urlTemplate = eclipseApiUrl + "account/profile/{personId}";
136+
var uriVariables = Map.of("personId", personId);
137+
var headers = new HttpHeaders();
138+
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
139+
var request = new HttpEntity<Void>(headers);
140+
141+
try {
142+
var response = restTemplate.exchange(urlTemplate, HttpMethod.GET, request, String.class, uriVariables);
143+
return parseEclipseProfile(response);
144+
} catch (RestClientException exc) {
145+
if (exc instanceof HttpStatusCodeException) {
146+
var status = ((HttpStatusCodeException) exc).getStatusCode();
147+
if (status == HttpStatus.NOT_FOUND)
148+
throw new ErrorResultException("No Eclipse profile data available for user: " + personId);
149+
}
150+
151+
var url = UriComponentsBuilder.fromUriString(urlTemplate).build(uriVariables);
152+
logger.error("Get request failed with URL: " + url, exc);
153+
throw new ErrorResultException("Request for retrieving user profile failed: " + exc.getMessage(),
154+
HttpStatus.INTERNAL_SERVER_ERROR);
155+
}
156+
}
157+
129158
/**
130159
* Update the given user data with a profile obtained from Eclipse API.
131160
*/
@@ -151,7 +180,6 @@ public void enrichUserJson(UserJson json, UserData user) {
151180
}
152181

153182
var usableToken = true;
154-
ErrorResultException exception = null;
155183
try {
156184
// Add information on the publisher agreement
157185
var agreement = getPublisherAgreement(user);
@@ -167,7 +195,7 @@ else if (publisherAgreementVersion.equals(agreement.version))
167195
if(e.getStatus() == HttpStatus.FORBIDDEN) {
168196
usableToken = false;
169197
} else {
170-
exception = e;
198+
logger.info("Failed to enrich UserJson", e);
171199
}
172200
}
173201

@@ -182,10 +210,30 @@ else if (publisherAgreementVersion.equals(agreement.version))
182210
else
183211
json.additionalLogins.add(eclipseLogin);
184212
}
213+
}
214+
215+
public void adminEnrichUserJson(UserJson json, UserData user) {
216+
if (!isActive()) {
217+
return;
218+
}
219+
220+
json.publisherAgreement = new UserJson.PublisherAgreement();
221+
var personId = user.getEclipsePersonId();
222+
if (personId == null) {
223+
json.publisherAgreement.status = "none";
224+
return;
225+
}
185226

186-
// Throw exception at end of method, so that JSON data is fully enriched
187-
if(exception != null) {
188-
throw exception;
227+
try {
228+
var profile = getPublicProfile(personId);
229+
if (profile.publisherAgreements == null || profile.publisherAgreements.openVsx == null || StringUtils.isEmpty(profile.publisherAgreements.openVsx.version))
230+
json.publisherAgreement.status = "none";
231+
else if (publisherAgreementVersion.equals(profile.publisherAgreements.openVsx.version))
232+
json.publisherAgreement.status = "signed";
233+
else
234+
json.publisherAgreement.status = "outdated";
235+
} catch (ErrorResultException e) {
236+
logger.error("Failed to get public profile", e);
189237
}
190238
}
191239

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,10 @@ private boolean isCompliant(UserData user) {
8686
return false;
8787
}
8888

89-
var json = new UserJson();
90-
try {
91-
eclipseService.enrichUserJson(json, user);
92-
return json.publisherAgreement.status == null || !json.publisherAgreement.status.equals("none");
93-
} catch(ErrorResultException e) {
94-
// no way to determine whether the user has a publisher agreement
95-
return true;
96-
}
89+
var profile = eclipseService.getPublicProfile(user.getEclipsePersonId());
90+
return profile.publisherAgreements != null
91+
&& profile.publisherAgreements.openVsx != null
92+
&& profile.publisherAgreements.openVsx.version != null;
9793
}
9894

9995
private void deactivateExtensions(Streamable<PersonalAccessToken> accessTokens) {

server/src/test/java/org/eclipse/openvsx/eclipse/EclipseServiceTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,22 @@ public void setup() {
9090
eclipse.eclipseApiUrl = "https://test.openvsx.eclipse.org/";
9191
}
9292

93+
@Test
94+
public void testGetPublicProfile() throws Exception {
95+
var urlTemplate = "https://test.openvsx.eclipse.org/account/profile/{personId}";
96+
Mockito.when(restTemplate.exchange(eq(urlTemplate), eq(HttpMethod.GET), any(HttpEntity.class), eq(String.class), eq(Map.of("personId", "test"))))
97+
.thenReturn(mockProfileResponse());
98+
99+
var profile = eclipse.getPublicProfile("test");
100+
101+
assertThat(profile).isNotNull();
102+
assertThat(profile.name).isEqualTo("test");
103+
assertThat(profile.githubHandle).isEqualTo("test");
104+
assertThat(profile.publisherAgreements).isNotNull();
105+
assertThat(profile.publisherAgreements.openVsx).isNotNull();
106+
assertThat(profile.publisherAgreements.openVsx.version).isEqualTo("1");
107+
}
108+
93109
@Test
94110
public void testGetUserProfile() throws Exception {
95111
Mockito.when(restTemplate.exchange(any(RequestEntity.class), eq(String.class)))

0 commit comments

Comments
 (0)