Skip to content

Commit b14be0f

Browse files
committed
fix username sanitation #278
1 parent 1473bf7 commit b14be0f

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

internal/common-operation.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"github.com/spf13/viper"
2424
)
2525

26-
var invalidClientIDCharactersRegExp = regexp.MustCompile(`[^a-zA-Z0-9_-]`)
26+
var invalidClientIDCharactersRegExp = regexp.MustCompile(`[^a-zA-Z0-9-]+`)
2727

2828
type TokenProvider struct {
2929
PluginName string
@@ -374,7 +374,7 @@ func sanitizeUsername(u string) string {
374374
u = s[len(s)-1]
375375
// Windows account can contain spaces or other special characters not supported
376376
// in client ID. Keep the bare minimum and ditch the rest.
377-
return invalidClientIDCharactersRegExp.ReplaceAllString(u, "")
377+
return invalidClientIDCharactersRegExp.ReplaceAllString(u, "-")
378378
}
379379

380380
// setupTlsConfig takes the paths to a tls certificate, CA, and certificate key in

internal/common-operation_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,35 @@ func TestListConfigsFromEntries(t *testing.T) {
8181
})
8282
}
8383
}
84+
85+
func TestSanitizeUsername(t *testing.T) {
86+
testCases := []struct {
87+
descriptions string
88+
username string
89+
want string
90+
}{
91+
{
92+
descriptions: "windows user with domain",
93+
username: "DOMAIN|MACHINE\\username",
94+
want: "username",
95+
},
96+
{
97+
descriptions: "user with email",
98+
username: "[email protected]",
99+
want: "user-domain-com",
100+
},
101+
{
102+
descriptions: "user with underscores",
103+
username: "user_with__underscores",
104+
want: "user-with-underscores",
105+
},
106+
}
107+
108+
for _, tc := range testCases {
109+
t.Run(tc.descriptions, func(t *testing.T) {
110+
if tc.want != sanitizeUsername(tc.username) {
111+
t.Fatalf("expected:\n--\n%s\n--\nactual:\n--\n%s\n--", tc.want, sanitizeUsername(tc.username))
112+
}
113+
})
114+
}
115+
}

0 commit comments

Comments
 (0)