-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: introduce
AuthUser
interface
Replace the references to `OAuth2User` by `AuthUser`. This allows downstream extenders to more easily contribute alternative OAuth2 providers: If the expected data is stored in different attributes it will be possible to bridge it by implementing the proper `AuthUser`.
- Loading branch information
1 parent
0f5315d
commit 0e07da9
Showing
11 changed files
with
334 additions
and
129 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
48 changes: 48 additions & 0 deletions
48
server/src/main/java/org/eclipse/openvsx/security/AuthUser.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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/******************************************************************************** | ||
* 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; | ||
|
||
/** | ||
* Encapsulate information about freshly authenticated users. | ||
* | ||
* Different OAuth2 providers may return the same information with different | ||
* attribute keys. This interface allows bridging arbitrary providers. | ||
*/ | ||
public interface AuthUser { | ||
/** | ||
* @return Non-human readable unique identifier. | ||
*/ | ||
String getAuthId(); | ||
/** | ||
* @return The user's avatar URL. Some services require post-processing to get the actual value for it | ||
* (the value returned is a template and you need to remplace variables). | ||
*/ | ||
String getAvatarUrl(); | ||
/** | ||
* @return The user's email. | ||
*/ | ||
String getEmail(); | ||
/** | ||
* @return The user's full name (first and last names). | ||
*/ | ||
String getFullName(); | ||
/** | ||
* @return The login name for the user. Human-readable unique name. AKA username. | ||
*/ | ||
String getLoginName(); | ||
/** | ||
* @return The authentication provider unique name, e.g. `github`, `eclipse`, etc. | ||
*/ | ||
String getProviderId(); | ||
/** | ||
* @return The authentication provider URL. | ||
*/ | ||
String getProviderUrl(); | ||
} |
20 changes: 20 additions & 0 deletions
20
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package org.eclipse.openvsx.security; | ||
|
||
import org.springframework.security.oauth2.core.user.OAuth2User; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class AuthUserFactory { | ||
|
||
public AuthUser createAuthUser(String providerId, OAuth2User oauth2User) { | ||
return new DefaultAuthUser( | ||
oauth2User.getName(), | ||
oauth2User.getAttribute("avatar_url"), | ||
oauth2User.getAttribute("email"), | ||
oauth2User.getAttribute("name"), | ||
oauth2User.getAttribute("login"), | ||
providerId, | ||
oauth2User.getAttribute("html_url") | ||
); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
server/src/main/java/org/eclipse/openvsx/security/DefaultAuthUser.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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/******************************************************************************** | ||
* 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; | ||
|
||
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( | ||
final String authId, | ||
final String avatarUrl, | ||
final String email, | ||
final String fullName, | ||
final String loginName, | ||
final String providerId, | ||
final String providerUrl | ||
) { | ||
this.authId = authId; | ||
this.avatarUrl = avatarUrl; | ||
this.email = email; | ||
this.fullName = fullName; | ||
this.loginName = loginName; | ||
this.providerId = providerId; | ||
this.providerUrl = 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 providerId; | ||
} | ||
|
||
@Override | ||
public String getProviderUrl() { | ||
return providerUrl; | ||
} | ||
} |
Oops, something went wrong.