fix: allow phone number as basic login

This commit is contained in:
richjohnrambo
2026-08-17 14:28:24 +08:00
parent cf590a1c78
commit 4ec84f961b
2 changed files with 45 additions and 3 deletions
+4 -3
View File
@@ -23,9 +23,10 @@ const (
defaultMinPasswordLength = 3
)
// Token suitable as a login: starts and ends with a Unicode letter (class L) or number (class N),
// contains Unicode letters, numbers, dot and underscore.
var loginPattern = regexp.MustCompile(`^[\pL\pN][_.\pL\pN]*[\pL\pN]+$`)
// Token suitable as a login: either an E.164 phone number or a name which starts
// and ends with a Unicode letter (class L) or number (class N), and contains
// Unicode letters, numbers, dot and underscore.
var loginPattern = regexp.MustCompile(`^(\+[\pN]+|[\pL\pN][_.\pL\pN]*[\pL\pN]+)$`)
// authenticator is the type to map authentication methods to.
type authenticator struct {
+41
View File
@@ -53,3 +53,44 @@ func TestAuthenticateRejectsMissingTenant(t *testing.T) {
t.Fatalf("got %v, want %v", err, types.ErrMalformed)
}
}
func TestCheckLoginPolicyAcceptsE164Phone(t *testing.T) {
a := &authenticator{name: "basic", minLoginLength: defaultMinLoginLength,
minPasswordLength: defaultMinPasswordLength}
if err := a.checkLoginPolicy("+8615521120255"); err != nil {
t.Fatalf("E.164 phone login rejected: %v", err)
}
}
func TestAddRecordAcceptsE164PhoneLogin(t *testing.T) {
ctrl := gomock.NewController(t)
users := mock_store.NewMockUsersPersistenceInterface(ctrl)
oldUsers := store.Users
store.Users = users
t.Cleanup(func() {
store.Users = oldUsers
ctrl.Finish()
})
users.EXPECT().AddAuthRecord(types.TenantID(7), types.Uid(42), auth.LevelAuth,
"basic", "+8615521120255", gomock.Any(), time.Time{}).Return(nil)
a := &authenticator{name: "basic", addToTags: true, minLoginLength: defaultMinLoginLength,
minPasswordLength: 6}
rec, err := a.AddRecord(auth.AuthContext{TenantID: 7}, &auth.Rec{TenantID: 7, Uid: 42},
[]byte("+8615521120255:123456"))
if err != nil {
t.Fatal(err)
}
if rec == nil || rec.AuthLevel != auth.LevelAuth || len(rec.Tags) != 1 || rec.Tags[0] != "basic:+8615521120255" {
t.Fatalf("unexpected auth record: %#v", rec)
}
}
func TestCheckLoginPolicyRejectsMalformedPhone(t *testing.T) {
a := &authenticator{name: "basic", minLoginLength: defaultMinLoginLength,
minPasswordLength: defaultMinPasswordLength}
if err := a.checkLoginPolicy("+86-15521120255"); err != types.ErrPolicy {
t.Fatalf("got %v, want %v", err, types.ErrPolicy)
}
}