diff --git a/server/src/main/java/org/eclipse/openvsx/security/DefaultAuthUser.java b/server/src/main/java/org/eclipse/openvsx/security/DefaultAuthUser.java new file mode 100644 index 000000000..50b617553 --- /dev/null +++ b/server/src/main/java/org/eclipse/openvsx/security/DefaultAuthUser.java @@ -0,0 +1,68 @@ +/******************************************************************************** + * Copyright (c) 2023 Ericsson and others + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * SPDX-License-Identifier: EPL-2.0 + ********************************************************************************/ +package org.eclipse.openvsx.security; + +import org.springframework.security.oauth2.core.user.OAuth2User; + +public class DefaultAuthUser implements AuthUser { + + final String authId; + final String avatarUrl; + final String email; + final String fullName; + final String loginName; + final String providerId; + final String providerUrl; + + public DefaultAuthUser(String providerId, OAuth2User oauth2User) { + authId = oauth2User.getName(); + avatarUrl = oauth2User.getAttribute("avatar_url"); + email = oauth2User.getAttribute("email"); + fullName = oauth2User.getAttribute("name"); + loginName = oauth2User.getAttribute("login"); + this.providerId = providerId; + providerUrl = oauth2User.getAttribute("html_url"); + } + + @Override + public String getAuthId() { + return authId; + } + + @Override + public String getAvatarUrl() { + return avatarUrl; + } + + @Override + public String getEmail() { + return email; + } + + @Override + public String getFullName() { + return fullName; + } + + @Override + public String getLoginName() { + return loginName; + } + + @Override + public String getProviderId() { + return providerId; + } + + @Override + public String getProviderUrl() { + return providerUrl; + } +} diff --git a/server/src/main/java/org/eclipse/openvsx/security/OAuth2UserServices.java b/server/src/main/java/org/eclipse/openvsx/security/OAuth2UserServices.java index ea723d476..efd5cab4f 100644 --- a/server/src/main/java/org/eclipse/openvsx/security/OAuth2UserServices.java +++ b/server/src/main/java/org/eclipse/openvsx/security/OAuth2UserServices.java @@ -116,11 +116,11 @@ public IdPrincipal loadUser(OAuth2UserRequest userRequest) { } private IdPrincipal loadGitHubUser(OAuth2UserRequest userRequest) { - var authUser = new GithubAuthUser(delegate.loadUser(userRequest)); + var authUser = new DefaultAuthUser("github", delegate.loadUser(userRequest)); String loginName = authUser.getLoginName(); if (StringUtils.isEmpty(loginName)) throw new CodedAuthException("Invalid login: missing 'login' field.", INVALID_GITHUB_USER); - var userData = repositories.findUserByLoginName("github", loginName); + var userData = repositories.findUserByLoginName(authUser.getProviderId(), loginName); if (userData == null) { userData = users.registerNewUser(authUser); } else { @@ -173,31 +173,4 @@ private Collection getAuthorities(UserData userData) { return Collections.emptyList(); } } - - static class GithubAuthUser implements AuthUser { - - final String authId; - final String avatarUrl; - final String email; - final String fullName; - final String loginName; - final String providerUrl; - - @Override public String getAuthId() { return authId; } - @Override public String getAvatarUrl() { return avatarUrl; } - @Override public String getEmail() { return email; } - @Override public String getFullName() { return fullName; } - @Override public String getLoginName() { return loginName; } - @Override public String getProviderId() { return "github"; } - @Override public String getProviderUrl() { return providerUrl; } - - public GithubAuthUser(OAuth2User oauth2User) { - authId = oauth2User.getName(); - avatarUrl = oauth2User.getAttribute("avatar_url"); - email = oauth2User.getAttribute("email"); - fullName = oauth2User.getAttribute("name"); - loginName = oauth2User.getAttribute("login"); - providerUrl = oauth2User.getAttribute("html_url"); - } - } } diff --git a/server/src/test/java/org/eclipse/openvsx/cache/CacheServiceTest.java b/server/src/test/java/org/eclipse/openvsx/cache/CacheServiceTest.java index 272c1c01f..4010e5fe1 100644 --- a/server/src/test/java/org/eclipse/openvsx/cache/CacheServiceTest.java +++ b/server/src/test/java/org/eclipse/openvsx/cache/CacheServiceTest.java @@ -9,14 +9,35 @@ * ****************************************************************************** */ package org.eclipse.openvsx.cache; +import static org.eclipse.openvsx.cache.CacheService.CACHE_EXTENSION_JSON; +import static org.eclipse.openvsx.entities.FileResource.DOWNLOAD; +import static org.eclipse.openvsx.entities.FileResource.STORAGE_DB; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.nio.charset.StandardCharsets; +import java.time.LocalDateTime; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; + import org.eclipse.openvsx.ExtensionService; import org.eclipse.openvsx.LocalRegistryService; import org.eclipse.openvsx.UserService; import org.eclipse.openvsx.admin.AdminService; -import org.eclipse.openvsx.entities.*; +import org.eclipse.openvsx.entities.Extension; +import org.eclipse.openvsx.entities.ExtensionReview; +import org.eclipse.openvsx.entities.ExtensionVersion; +import org.eclipse.openvsx.entities.FileResource; +import org.eclipse.openvsx.entities.Namespace; +import org.eclipse.openvsx.entities.PersonalAccessToken; +import org.eclipse.openvsx.entities.UserData; import org.eclipse.openvsx.json.ExtensionJson; import org.eclipse.openvsx.json.ReviewJson; import org.eclipse.openvsx.repositories.RepositoryService; +import org.eclipse.openvsx.security.DefaultAuthUser; import org.eclipse.openvsx.security.IdPrincipal; import org.eclipse.openvsx.util.TimeUtil; import org.junit.jupiter.api.Test; @@ -34,16 +55,6 @@ import jakarta.persistence.EntityManager; import jakarta.transaction.Transactional; -import java.nio.charset.StandardCharsets; -import java.time.LocalDateTime; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; - -import static org.eclipse.openvsx.cache.CacheService.CACHE_EXTENSION_JSON; -import static org.eclipse.openvsx.entities.FileResource.DOWNLOAD; -import static org.eclipse.openvsx.entities.FileResource.STORAGE_DB; -import static org.junit.jupiter.api.Assertions.*; @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @ActiveProfiles("test") @@ -113,7 +124,8 @@ public void testUpdateExistingUser() { var user = extVersion.getPublishedWith().getUser(); var oauthUser = new DefaultOAuth2User(authorities, attributes, "name"); - users.updateExistingUser(user, oauthUser); + var authUser = new DefaultAuthUser(authority, oauthUser); + users.updateExistingUser(user, authUser); assertNull(cache.getCache(CACHE_EXTENSION_JSON).get(cacheKey, ExtensionJson.class)); var json = registry.getExtension(namespace.getName(), extension.getName(), extVersion.getTargetPlatform(), extVersion.getVersion());