-
Notifications
You must be signed in to change notification settings - Fork 142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
server: refactor: introduce AuthUser
interface
#838
Open
paul-marechal
wants to merge
7
commits into
eclipse:master
Choose a base branch
from
paul-marechal:auth-user
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
736cbf4
refactor: introduce `AuthUser` interface
paul-marechal 0f8c930
fix tests
paul-marechal 64b943f
add AuthUserFactory
paul-marechal 3c02760
update tests
paul-marechal 9ce6fe8
add test bean for AuthUserFactory
paul-marechal 7480927
move AuthUserFactory bean around
paul-marechal 8f65be8
finally fix test suite
paul-marechal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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; | ||
} | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@HeikoBoettger this class should make it easier to customize
AuthUser
instances. Right now it is hardcoded but you can later make this factory depend on some Spring configuration to customize attribute access.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@paul-marechal I am not so familiar with spring, do you know how to map the "login" in the spring configuration? I only know about the "spring.security.oauth2.client.provider.my-oauth-provider.user-name-attribute=name". May be short hint somewhere in the openvsx documentation on how to customize the attribute access would be good.
Great work by the way.