forked from go-gitea/gitea
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Blender ID allows a wider range of characters in the nickname than Gitea allows. Before using a Blender ID nickname as a Gitea username, it needs to be massaged into a valid form. This was already done in the Blender ID-to-Gitea webhook for username changes, and this PR introduces it in Gitea itself for the registration flow. The implementation follows what the webhook code[1] does, except it's simpler because it can use built-in Gitea functionality. This fixes https://projects.blender.org/blender/blender/issues/111937 [1]: https://projects.blender.org/infrastructure/gitea-blenderid-webhook/src/branch/main/gitea_blenderid_webhook/gitea_users.py Reviewed on: #3
- Loading branch information
Showing
6 changed files
with
117 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
65 changes: 65 additions & 0 deletions
65
services/auth/source/oauth2/blenderid/gitealize_usernames.go
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,65 @@ | ||
// Copyright 2023 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
package blenderid | ||
|
||
import ( | ||
"regexp" | ||
"strings" | ||
|
||
"code.gitea.io/gitea/models/user" | ||
|
||
"github.com/mozillazg/go-unidecode" | ||
) | ||
|
||
var ( | ||
reInvalidCharsPattern = regexp.MustCompile(`[^\da-zA-Z.\w-]+`) | ||
|
||
// Consecutive non-alphanumeric at start: | ||
reConsPrefix = regexp.MustCompile(`^[._-]+`) | ||
reConsSuffix = regexp.MustCompile(`[._-]+$`) | ||
reConsInfix = regexp.MustCompile(`[._-]{2,}`) | ||
) | ||
|
||
// gitealizeUsername turns a valid Blender ID nickname into a valid Gitea username. | ||
func gitealizeUsername(bidNickname string) string { | ||
// Remove accents and other non-ASCIIness. | ||
asciiUsername := unidecode.Unidecode(bidNickname) | ||
asciiUsername = strings.TrimSpace(asciiUsername) | ||
asciiUsername = strings.ReplaceAll(asciiUsername, " ", "_") | ||
|
||
err := user.IsUsableUsername(asciiUsername) | ||
if err == nil && len(asciiUsername) <= 40 { | ||
return asciiUsername | ||
} | ||
|
||
newUsername := asciiUsername | ||
newUsername = reInvalidCharsPattern.ReplaceAllString(newUsername, "_") | ||
newUsername = reConsPrefix.ReplaceAllString(newUsername, "") | ||
newUsername = reConsSuffix.ReplaceAllString(newUsername, "") | ||
newUsername = reConsInfix.ReplaceAllStringFunc( | ||
newUsername, | ||
func(match string) string { | ||
firstRune := []rune(match)[0] | ||
return string(firstRune) | ||
}) | ||
|
||
if newUsername == "" { | ||
// Everything was stripped and nothing was left. Better to keep as-is and | ||
// just let Gitea bork on it. | ||
return asciiUsername | ||
} | ||
|
||
// This includes a test for reserved names, which are easily circumvented by | ||
// appending another character. | ||
if user.IsUsableUsername(newUsername) != nil { | ||
if len(newUsername) > 39 { | ||
return newUsername[:39] + "2" | ||
} | ||
return newUsername + "2" | ||
} | ||
|
||
if len(newUsername) > 40 { | ||
return newUsername[:40] | ||
} | ||
return newUsername | ||
} |
43 changes: 43 additions & 0 deletions
43
services/auth/source/oauth2/blenderid/gitealize_usernames_test.go
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,43 @@ | ||
// Copyright 2023 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
package blenderid | ||
|
||
import "testing" | ||
|
||
func Test_gitealizeUsername(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
bidNickname string | ||
want string | ||
}{ | ||
{"empty", "", ""}, | ||
{"underscore", "_", "_"}, | ||
{"reserved-name", "ghost", "ghost2"}, // Reserved name in Gitea. | ||
{"short", "x", "x"}, | ||
{"simple", "simple", "simple"}, | ||
{"start-bad", "____startbad", "startbad"}, | ||
{"end-bad", "endbad___", "endbad"}, | ||
{"mid-bad-1", "mid__bad", "mid_bad"}, | ||
{"mid-bad-2", "user_.-name", "user_name"}, | ||
{"plus-mid-single", "RT2+356", "RT2_356"}, | ||
{"plus-mid-many", "RT2+++356", "RT2_356"}, | ||
{"plus-end", "RT2356+", "RT2356"}, | ||
{ | ||
"too-long", // # Max username length is 40: | ||
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", | ||
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", | ||
}, | ||
{"accented-latin", "Ümlaut-Đenja", "Umlaut-Denja"}, | ||
{"thai", "แบบไทย", "aebbaithy"}, | ||
{"mandarin", "普通话", "Pu_Tong_Hua"}, | ||
{"cyrillic", "ћирилица", "tshirilitsa"}, | ||
{"all-bad", "------", "------"}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := gitealizeUsername(tt.bidNickname); got != tt.want { | ||
t.Errorf("gitealizeUsername() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |