Skip to content

Commit ada6f94

Browse files
authored
Console user and authentication improvements. (#978)
1 parent d1e894f commit ada6f94

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

server/console_authenticate.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ func (s *ConsoleServer) lookupConsoleUser(ctx context.Context, unameOrEmail, pas
163163
}
164164
err = status.Error(codes.Unauthenticated, "Invalid credentials.")
165165
}
166+
// Call hash function to help obfuscate response time when user does not exist.
167+
var dummyHash = []byte("$2y$10$x8B0hPVxYGDq7bZiYC9jcuwA0B9m4J6vYITYIv0nf.IfYuM1kGI3W")
168+
_ = bcrypt.CompareHashAndPassword(dummyHash, []byte(password))
166169
return
167170
}
168171

server/console_user.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ import (
2020
"database/sql"
2121
"encoding/json"
2222
"errors"
23-
"github.com/jackc/pgconn"
2423
"net/http"
2524
"regexp"
25+
"strings"
2626
"unicode"
2727

2828
"github.com/gofrs/uuid"
2929
"github.com/heroiclabs/nakama/v3/console"
30+
"github.com/jackc/pgconn"
3031
"go.uber.org/zap"
3132
"golang.org/x/crypto/bcrypt"
3233
"google.golang.org/grpc/codes"
@@ -43,6 +44,7 @@ func (s *ConsoleServer) AddUser(ctx context.Context, in *console.AddUserRequest)
4344
} else if len(in.Username) < 3 || len(in.Username) > 20 || !usernameRegex.MatchString(in.Username) {
4445
return nil, status.Error(codes.InvalidArgument, "Username must be 3-20 long sequence of alphanumeric characters _ or . and cannot start and end with _ or .")
4546
}
47+
in.Username = strings.ToLower(in.Username)
4648

4749
if in.Username == "admin" || in.Username == s.config.GetConsole().Username {
4850
return nil, status.Error(codes.InvalidArgument, "Username cannot be the console configured username")
@@ -53,11 +55,12 @@ func (s *ConsoleServer) AddUser(ctx context.Context, in *console.AddUserRequest)
5355
} else if len(in.Email) < 3 || len(in.Email) > 254 || !emailRegex.MatchString(in.Email) || invalidCharsRegex.MatchString(in.Email) {
5456
return nil, status.Error(codes.InvalidArgument, "Not a valid email address")
5557
}
58+
in.Email = strings.ToLower(in.Email)
5659

5760
if in.Password == "" {
5861
return nil, status.Error(codes.InvalidArgument, "Password is required")
5962
} else if !isValidPassword(in.Password) {
60-
return nil, status.Error(codes.InvalidArgument, "Password must be at least 6 characters long and contain 1 number and 1 upper case character")
63+
return nil, status.Error(codes.InvalidArgument, "Password must be at least 8 characters long and contain 1 number and 1 upper case character")
6164
}
6265

6366
inviterUsername := ctx.Value(ctxConsoleUsernameKey{}).(string)
@@ -168,7 +171,7 @@ func (s *ConsoleServer) dbDeleteConsoleUser(ctx context.Context, username string
168171
}
169172

170173
func isValidPassword(pwd string) bool {
171-
if len(pwd) < 6 {
174+
if len(pwd) < 8 {
172175
return false
173176
}
174177
var number bool

0 commit comments

Comments
 (0)