-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1abe659
commit bde567c
Showing
4 changed files
with
61 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 40 additions & 29 deletions
69
server/src/main/java/org/eclipse/openvsx/security/AuthUserFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,69 @@ | ||
package org.eclipse.openvsx.security; | ||
|
||
import java.util.NoSuchElementException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.eclipse.openvsx.OVSXConfig; | ||
import org.eclipse.openvsx.OVSXConfig.AuthConfig.AttributeNames; | ||
import org.springframework.security.oauth2.core.user.OAuth2User; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Service | ||
@Component | ||
public class AuthUserFactory { | ||
|
||
private static final AttributeNames GITHUB_ATTRIBUTES = new AttributeNames(); | ||
protected static final Map<String, AttributeNames> DEFAULTS = new HashMap<>(); | ||
|
||
public static class MissingProvider extends Exception { | ||
public MissingProvider(String provider) { super("Missing configuration: ovsx.auth.attribute-names." + provider); } | ||
} | ||
|
||
static { | ||
GITHUB_ATTRIBUTES.setAvatarUrl("avatar_url"); | ||
GITHUB_ATTRIBUTES.setEmail("email"); | ||
GITHUB_ATTRIBUTES.setFullName("name"); | ||
GITHUB_ATTRIBUTES.setLoginName("login"); | ||
GITHUB_ATTRIBUTES.setProviderUrl("html_url"); | ||
var github = new AttributeNames(); | ||
github.setAvatarUrl("avatar_url"); | ||
github.setEmail("email"); | ||
github.setFullName("name"); | ||
github.setLoginName("login"); | ||
github.setProviderUrl("html_url"); | ||
DEFAULTS.put("github", github); | ||
} | ||
|
||
private final OVSXConfig config; | ||
protected final OVSXConfig config; | ||
|
||
public AuthUserFactory( | ||
OVSXConfig config | ||
) { | ||
public AuthUserFactory(OVSXConfig config) { | ||
this.config = config; | ||
} | ||
|
||
public AuthUser createAuthUser(String providerId, OAuth2User oauth2User) { | ||
var attributeNames = getAttributeNames(providerId); | ||
/** | ||
* @param provider The configured OAuth2 provider from which the user object came from. | ||
* @param user The OAuth2 user object to get attributes from. | ||
* @return An {@link AuthUser} instance with attributes set according to the current configuration. | ||
* @throws MissingProvider if an attribute name mapping is missing for the given provider. | ||
*/ | ||
public AuthUser createAuthUser(String provider, OAuth2User user) throws MissingProvider { | ||
var attr = getAttributeNames(provider); | ||
return new DefaultAuthUser( | ||
oauth2User.getName(), | ||
oauth2User.getAttribute(attributeNames.getAvatarUrl()), | ||
oauth2User.getAttribute(attributeNames.getEmail()), | ||
oauth2User.getAttribute(attributeNames.getFullName()), | ||
oauth2User.getAttribute(attributeNames.getLoginName()), | ||
providerId, | ||
oauth2User.getAttribute(attributeNames.getProviderUrl()) | ||
user.getName(), | ||
getAttribute(user, attr.getAvatarUrl()), | ||
getAttribute(user, attr.getEmail()), | ||
getAttribute(user, attr.getFullName()), | ||
getAttribute(user, attr.getLoginName()), | ||
provider, | ||
getAttribute(user, attr.getProviderUrl()) | ||
); | ||
} | ||
|
||
protected <T> T getAttribute(OAuth2User oauth2User, String attribute) { | ||
return attribute == null ? null : oauth2User.getAttribute(attribute); | ||
} | ||
|
||
/** | ||
* @param provider The provider to get the attribute mappings for. | ||
* @return The relevant attribute mappings. | ||
*/ | ||
private AttributeNames getAttributeNames(String provider) { | ||
protected AttributeNames getAttributeNames(String provider) throws MissingProvider { | ||
var attributeNames = config.getAuth().getAttributeNames().get(provider); | ||
if (attributeNames == null) { | ||
return switch (provider) { | ||
case "github" -> GITHUB_ATTRIBUTES; | ||
default -> throw new NoSuchElementException("No attributes found for provider: " + provider); | ||
}; | ||
} | ||
if (attributeNames == null) attributeNames = DEFAULTS.get(provider); | ||
if (attributeNames == null) throw new MissingProvider(provider); | ||
return attributeNames; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters