Skip to content

Commit 401ed52

Browse files
authored
Merge pull request #1306 from apernet/wip-userpass-ignore-case
Make username of userpass case insensitive
2 parents 9466bc4 + 7652ddc commit 401ed52

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

app/cmd/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ func (c *serverConfig) fillAuthenticator(hyConfig *server.Config) error {
755755
if len(c.Auth.UserPass) == 0 {
756756
return configError{Field: "auth.userpass", Err: errors.New("empty auth userpass")}
757757
}
758-
hyConfig.Authenticator = &auth.UserPassAuthenticator{Users: c.Auth.UserPass}
758+
hyConfig.Authenticator = auth.NewUserPassAuthenticator(c.Auth.UserPass)
759759
return nil
760760
case "http", "https":
761761
if c.Auth.HTTP.URL == "" {

extras/auth/userpass.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,25 @@ var _ server.Authenticator = &UserPassAuthenticator{}
1616
// UserPassAuthenticator checks the provided auth string against a map of username/password pairs.
1717
// The format of the auth string must be "username:password".
1818
type UserPassAuthenticator struct {
19-
Users map[string]string
19+
users map[string]string
20+
}
21+
22+
func NewUserPassAuthenticator(users map[string]string) *UserPassAuthenticator {
23+
// Usernames are case-insensitive, as they are already lowercased by viper.
24+
// Lowercase it again on our own to make it explicit.
25+
lcUsers := make(map[string]string, len(users))
26+
for user, pass := range users {
27+
lcUsers[strings.ToLower(user)] = pass
28+
}
29+
return &UserPassAuthenticator{users: lcUsers}
2030
}
2131

2232
func (a *UserPassAuthenticator) Authenticate(addr net.Addr, auth string, tx uint64) (ok bool, id string) {
2333
u, p, ok := splitUserPass(auth)
2434
if !ok {
2535
return false, ""
2636
}
27-
rp, ok := a.Users[u]
37+
rp, ok := a.users[u]
2838
if !ok || rp != p {
2939
return false, ""
3040
}
@@ -36,5 +46,6 @@ func splitUserPass(auth string) (user, pass string, ok bool) {
3646
if len(rs) != 2 {
3747
return "", "", false
3848
}
39-
return rs[0], rs[1], true
49+
// Usernames are case-insensitive
50+
return strings.ToLower(rs[0]), rs[1], true
4051
}

extras/auth/userpass_test.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,26 @@ func TestUserPassAuthenticator(t *testing.T) {
8585
wantOk: false,
8686
wantId: "",
8787
},
88+
{
89+
name: "case insensitive username",
90+
fields: fields{
91+
Users: map[string]string{
92+
"gawR": "gura",
93+
"fubuki": "shirakami",
94+
},
95+
},
96+
args: args{
97+
addr: nil,
98+
auth: "Gawr:gura",
99+
tx: 0,
100+
},
101+
wantOk: true,
102+
wantId: "gawr",
103+
},
88104
}
89105
for _, tt := range tests {
90106
t.Run(tt.name, func(t *testing.T) {
91-
a := &UserPassAuthenticator{
92-
Users: tt.fields.Users,
93-
}
107+
a := NewUserPassAuthenticator(tt.fields.Users)
94108
gotOk, gotId := a.Authenticate(tt.args.addr, tt.args.auth, tt.args.tx)
95109
if gotOk != tt.wantOk {
96110
t.Errorf("Authenticate() gotOk = %v, want %v", gotOk, tt.wantOk)

0 commit comments

Comments
 (0)