Repository navigation
feat: add support for oauth email-to-username mapping #1185
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
Closed
blesswinsamuel
wants to merge
1
commit into
tinyauthapp:main
from
blesswinsamuel:feat/oauth-user-mapping
Closed
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,96 @@ | ||
| package service | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/tinyauthapp/tinyauth/internal/model" | ||
| "golang.org/x/oauth2" | ||
| ) | ||
|
|
||
| func TestOAuthService_UserMapping(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| userMapping []string | ||
| extractedClaims model.Claims | ||
| expectedUsername string | ||
| }{ | ||
| { | ||
| name: "Map user email to custom username", | ||
| userMapping: []string{ | ||
| "user@example.com:customuser", | ||
| }, | ||
| extractedClaims: model.Claims{ | ||
| Email: "user@example.com", | ||
| PreferredUsername: "", | ||
| }, | ||
| expectedUsername: "customuser", | ||
| }, | ||
| { | ||
| name: "Case insensitive email matching", | ||
| userMapping: []string{ | ||
| "Alice@example.com:alice", | ||
| }, | ||
| extractedClaims: model.Claims{ | ||
| Email: "alice@EXAMPLE.COM", | ||
| PreferredUsername: "", | ||
| }, | ||
| expectedUsername: "alice", | ||
| }, | ||
| { | ||
| name: "Override existing preferred username if mapped", | ||
| userMapping: []string{ | ||
| "bob@example.com:robert", | ||
| }, | ||
| extractedClaims: model.Claims{ | ||
| Email: "bob@example.com", | ||
| PreferredUsername: "bob_github", | ||
| }, | ||
| expectedUsername: "robert", | ||
| }, | ||
| { | ||
| name: "Keep original preferred username when not in mapping", | ||
| userMapping: []string{ | ||
| "other@example.com:other", | ||
| }, | ||
| extractedClaims: model.Claims{ | ||
| Email: "unmapped@example.com", | ||
| PreferredUsername: "unmapped_login", | ||
| }, | ||
| expectedUsername: "unmapped_login", | ||
| }, | ||
| { | ||
| name: "Leave preferred username empty when unmapped and absent", | ||
| userMapping: []string{}, | ||
| extractedClaims: model.Claims{ | ||
| Email: "unmapped@example.com", | ||
| PreferredUsername: "", | ||
| }, | ||
| expectedUsername: "", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| cfg := model.OAuthServiceConfig{ | ||
| UserMapping: tt.userMapping, | ||
| } | ||
| svc := NewOAuthService(cfg, "test", context.Background()) | ||
| svc.WithUserinfoExtractor(func(client *http.Client, ctx context.Context, url string, mapClaims MapClaims) (*model.Claims, error) { | ||
| claims := tt.extractedClaims | ||
| return &claims, nil | ||
| }) | ||
|
|
||
| // Dummy token | ||
| token := &oauth2.Token{ | ||
| AccessToken: "dummy-token", | ||
| } | ||
| claims, err := svc.GetUserinfo(token) | ||
| assert.NoError(t, err) | ||
| assert.NotNil(t, claims) | ||
| assert.Equal(t, tt.expectedUsername, claims.PreferredUsername) | ||
| }) | ||
| } | ||
| } |
This file contains hidden or 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,26 @@ | ||
| package utils | ||
|
|
||
| import ( | ||
| "strings" | ||
| ) | ||
|
|
||
| // ParseUserMapping parses a list of "email:username" or "email=username" strings into a map of email -> username. | ||
| // Email keys are normalized to lowercase. Empty lines and comment lines (starting with #) are ignored. | ||
| func ParseUserMapping(mappings []string) map[string]string { | ||
| result := make(map[string]string) | ||
| for _, entry := range mappings { | ||
| entry = strings.TrimSpace(entry) | ||
| if entry == "" || strings.HasPrefix(entry, "#") { | ||
| continue | ||
| } | ||
| var email, username string | ||
| if idx := strings.IndexAny(entry, ":="); idx != -1 { | ||
| email = strings.TrimSpace(entry[:idx]) | ||
| username = strings.TrimSpace(entry[idx+1:]) | ||
| } | ||
| if email != "" && username != "" { | ||
| result[strings.ToLower(email)] = username | ||
| } | ||
| } | ||
| return result | ||
| } |
This file contains hidden or 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,75 @@ | ||
| package utils | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestParseUserMapping(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| input []string | ||
| expected map[string]string | ||
| }{ | ||
| { | ||
| name: "Empty list", | ||
| input: []string{}, | ||
| expected: map[string]string{}, | ||
| }, | ||
| { | ||
| name: "Colon separated mappings with whitespace and case normalization", | ||
| input: []string{ | ||
| " Alice@example.com:alice ", | ||
| "bob@example.com:robert", | ||
| }, | ||
| expected: map[string]string{ | ||
| "alice@example.com": "alice", | ||
| "bob@example.com": "robert", | ||
| }, | ||
| }, | ||
| { | ||
| name: "Equal separated mappings and comments", | ||
| input: []string{ | ||
| "# this is a comment", | ||
| "", | ||
| "carol@example.com=carol", | ||
| " ", | ||
| "dave@example.com:david", | ||
| }, | ||
| expected: map[string]string{ | ||
| "carol@example.com": "carol", | ||
| "dave@example.com": "david", | ||
| }, | ||
| }, | ||
| { | ||
| name: "Later entries override earlier entries", | ||
| input: []string{ | ||
| "user@example.com:first", | ||
| "user@example.com:second", | ||
| }, | ||
| expected: map[string]string{ | ||
| "user@example.com": "second", | ||
| }, | ||
| }, | ||
| { | ||
| name: "Ignore entries without separator or missing parts", | ||
| input: []string{ | ||
| "invalid-entry", | ||
| ":empty-email", | ||
| "empty-username:", | ||
| "valid@example.com:user", | ||
| }, | ||
| expected: map[string]string{ | ||
| "valid@example.com": "user", | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| result := ParseUserMapping(tt.input) | ||
| assert.Equal(t, tt.expected, result) | ||
| }) | ||
| } | ||
| } |
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.
🔒 Security & Privacy | 🟠 Major | 🏗️ Heavy lift
🔎 Supported by static analysis
🏁 Script executed:
Repository: tinyauthapp/tinyauth
Length of output: 22163
🏁 Script executed:
Repository: tinyauthapp/tinyauth
Length of output: 41835
Require verified email ownership before applying the mapping.
defaultExtractormaps any provideremailclaim intomodel.Claimswithout checkingemail_verified.GetUserinfothen assigns the mapped username for every non-empty email. A configured provider can therefore produce a mappedPreferredUsernamefor an unverified address.Carry the verification result through
model.Claimsand apply the mapping only when the email is verified. Fail closed when the provider does not supply a verification result.🤖 Prompt for AI Agents