Skip to content

Commit

Permalink
server: rename config as ovsx.oauth2
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-marechal committed Dec 13, 2024
1 parent e2b4852 commit 960d8de
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 25 deletions.
29 changes: 18 additions & 11 deletions server/src/main/java/org/eclipse/openvsx/OVSXConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,35 @@
@ConfigurationProperties(prefix = "ovsx")
public class OVSXConfig {

private AuthConfig auth = new AuthConfig();
private OAuth2Config oauth2 = new OAuth2Config();

public AuthConfig getAuth() {
return auth;
public OAuth2Config getOauth2() {
return oauth2;
}

public void setAuth(AuthConfig authConfig) {
this.auth = authConfig;
public void setOauth2(OAuth2Config oauth2Config) {
this.oauth2 = oauth2Config;
}

public static class AuthConfig {
public static class OAuth2Config {

/**
* The user authentication provider to use.
*/
private String provider = "github";

/**
* Configuration example:
* <pre><code>
* ovsx:
* auth:
* attribute-names:
* github: # provider name
* login: field-returned-by-your-provider
*ovsx:
* oauth2:
* attribute-names:
* [provider-name]:
* avatar-url: string
* email: string
* full-name: string
* login-name: string
* provider-url: string
* </code></pre>
*/
private Map<String, AttributeNames> attributeNames = emptyMap();
Expand Down
44 changes: 32 additions & 12 deletions server/src/main/java/org/eclipse/openvsx/UserAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,31 @@
********************************************************************************/
package org.eclipse.openvsx;

import jakarta.servlet.http.HttpServletRequest;
import static org.eclipse.openvsx.entities.FileResource.CHANGELOG;
import static org.eclipse.openvsx.entities.FileResource.DOWNLOAD;
import static org.eclipse.openvsx.entities.FileResource.ICON;
import static org.eclipse.openvsx.entities.FileResource.LICENSE;
import static org.eclipse.openvsx.entities.FileResource.MANIFEST;
import static org.eclipse.openvsx.entities.FileResource.README;
import static org.eclipse.openvsx.entities.FileResource.VSIXMANIFEST;
import static org.eclipse.openvsx.util.UrlUtil.createApiUrl;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.eclipse.openvsx.eclipse.EclipseService;
import org.eclipse.openvsx.entities.NamespaceMembership;
import org.eclipse.openvsx.entities.UserData;
import org.eclipse.openvsx.json.*;
import org.eclipse.openvsx.json.AccessTokenJson;
import org.eclipse.openvsx.json.CsrfTokenJson;
import org.eclipse.openvsx.json.ErrorJson;
import org.eclipse.openvsx.json.ExtensionJson;
import org.eclipse.openvsx.json.NamespaceDetailsJson;
import org.eclipse.openvsx.json.NamespaceJson;
import org.eclipse.openvsx.json.NamespaceMembershipListJson;
import org.eclipse.openvsx.json.ResultJson;
import org.eclipse.openvsx.json.UserJson;
import org.eclipse.openvsx.repositories.RepositoryService;
import org.eclipse.openvsx.security.CodedAuthException;
import org.eclipse.openvsx.storage.StorageUtilService;
Expand All @@ -30,17 +50,17 @@
import org.springframework.security.web.WebAttributes;
import org.springframework.security.web.csrf.CsrfToken;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.servlet.ModelAndView;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static org.eclipse.openvsx.entities.FileResource.*;
import static org.eclipse.openvsx.util.UrlUtil.createApiUrl;
import jakarta.servlet.http.HttpServletRequest;

@RestController
public class UserAPI {
Expand Down Expand Up @@ -76,7 +96,7 @@ public UserAPI(
path = "/login"
)
public ModelAndView login(ModelMap model) {
return new ModelAndView("redirect:/oauth2/authorization/" + config.getAuth().getProvider(), model);
return new ModelAndView("redirect:/oauth2/authorization/" + config.getOauth2().getProvider(), model);
}

/**
Expand Down Expand Up @@ -289,7 +309,7 @@ public ResponseEntity<NamespaceMembershipListJson> getNamespaceMembers(@PathVari
membershipList.setNamespaceMemberships(memberships.stream().map(NamespaceMembership::toJson).toList());
return new ResponseEntity<>(membershipList, HttpStatus.OK);
} else {
return new ResponseEntity<>(NamespaceMembershipListJson.error("You don't have the permission to see this."), HttpStatus.FORBIDDEN);
return new ResponseEntity<>(NamespaceMembershipListJson.error("You don't have the permission to see this."), HttpStatus.FORBIDDEN);
}
}

Expand Down Expand Up @@ -342,4 +362,4 @@ public ResponseEntity<UserJson> signPublisherAgreement() {
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.util.Map;

import org.eclipse.openvsx.OVSXConfig;
import org.eclipse.openvsx.OVSXConfig.AuthConfig.AttributeNames;
import org.eclipse.openvsx.OVSXConfig.OAuth2Config.AttributeNames;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.stereotype.Component;

Expand Down Expand Up @@ -61,7 +61,7 @@ protected <T> T getAttribute(OAuth2User oauth2User, String attribute) {
* @return The relevant attribute mappings.
*/
protected AttributeNames getAttributeNames(String provider) throws MissingProvider {
var attributeNames = config.getAuth().getAttributeNames().get(provider);
var attributeNames = config.getOauth2().getAttributeNames().get(provider);
if (attributeNames == null) attributeNames = DEFAULTS.get(provider);
if (attributeNames == null) throw new MissingProvider(provider);
return attributeNames;
Expand Down

0 comments on commit 960d8de

Please sign in to comment.