feat: validate personal and enterprise auth tenants

This commit is contained in:
richjohnrambo
2026-09-21 21:24:25 +08:00
parent fc393cd685
commit 186cfa21c8
9 changed files with 689 additions and 53 deletions
+60 -16
View File
@@ -19,6 +19,10 @@ type testTenantStore struct {
err error
}
func ptrInt(v int) *int {
return &v
}
type testLoginDeviceStore struct {
devices map[types.Uid][]types.DeviceDef
updates int
@@ -365,9 +369,11 @@ func TestDispatchLogin(t *testing.T) {
msg := &ClientComMessage{
Login: &MsgClientLogin{
Id: "123",
Scheme: "basic",
Secret: []byte(secret),
Id: "123",
Scheme: "basic",
Secret: []byte(secret),
LoginType: ptrInt(registrationTypeEnterprise),
TenantCode: "test",
},
}
@@ -425,7 +431,7 @@ func TestDispatchLoginRejectsAuthenticatorTenantMismatch(t *testing.T) {
s := &Session{send: make(chan any, 1), ver: 16, tenantID: 1, tenantCode: "acme"}
s.login(&ClientComMessage{
Login: &MsgClientLogin{Id: "login-1", Scheme: "basic", Secret: secret},
Login: &MsgClientLogin{Id: "login-1", Scheme: "basic", Secret: secret, LoginType: ptrInt(registrationTypeEnterprise), TenantCode: "acme"},
Timestamp: time.Now(),
})
@@ -438,6 +444,40 @@ func TestDispatchLoginRejectsAuthenticatorTenantMismatch(t *testing.T) {
}
}
func TestDispatchLoginRejectsEnterpriseDefaultTenant(t *testing.T) {
s := &Session{send: make(chan any, 1), ver: 16, tenantID: 1, tenantCode: imDefaultTenantCode}
s.login(&ClientComMessage{
Login: &MsgClientLogin{Id: "login-1", Scheme: "basic", Secret: []byte("alice:password"), LoginType: ptrInt(registrationTypeEnterprise), TenantCode: imDefaultTenantCode},
Timestamp: time.Now(),
})
resp := (<-s.send).(*ServerComMessage)
if resp.Ctrl == nil || resp.Ctrl.Code != http.StatusUnprocessableEntity {
t.Fatalf("unexpected response: %+v", resp)
}
params, _ := resp.Ctrl.Params.(map[string]any)
if params["reason"] != "TENANT_CODE_FORBIDDEN" {
t.Fatalf("unexpected params: %+v", resp.Ctrl.Params)
}
}
func TestDispatchLoginRequiresTypeForBasic(t *testing.T) {
s := &Session{send: make(chan any, 1), ver: 16, tenantID: 1, tenantCode: imDefaultTenantCode}
s.login(&ClientComMessage{
Login: &MsgClientLogin{Id: "login-1", Scheme: "basic", Secret: []byte("alice:password")},
Timestamp: time.Now(),
})
resp := (<-s.send).(*ServerComMessage)
if resp.Ctrl == nil || resp.Ctrl.Code != http.StatusUnprocessableEntity {
t.Fatalf("unexpected response: %+v", resp)
}
params, _ := resp.Ctrl.Params.(map[string]any)
if params["reason"] != "LOGIN_TYPE_REQUIRED" {
t.Fatalf("unexpected params: %+v", resp.Ctrl.Params)
}
}
func TestPublishSysRequiresRoot(t *testing.T) {
s := &Session{
send: make(chan any, 1),
@@ -1420,12 +1460,14 @@ func TestDispatchAccNew(t *testing.T) {
public := "public name"
msg := &ClientComMessage{
Acc: &MsgClientAcc{
Id: "123",
User: "newXYZ",
Scheme: "basic",
Username: "alice",
Phone: "13800138000",
Secret: []byte(secret),
Id: "123",
User: "newXYZ",
Scheme: "basic",
Username: "alice",
Phone: "13800138000",
RegistrationType: ptrInt(registrationTypeEnterprise),
TenantCode: "test",
Secret: []byte(secret),
Cred: []MsgCredClient{{
Method: "tel", Value: "+8613800138000", Response: "AbCdE",
Params: map[string]any{"challenge_id": "challenge-1"},
@@ -1540,12 +1582,14 @@ func TestDispatchAccNewUsernameOnlyDoesNotRequireInvite(t *testing.T) {
msg := &ClientComMessage{
Acc: &MsgClientAcc{
Id: "username-only",
User: "new",
Scheme: "basic",
Username: "alice",
Secret: []byte(secret),
Desc: &MsgSetDesc{Public: map[string]any{"fn": "Alice"}},
Id: "username-only",
User: "new",
Scheme: "basic",
Username: "alice",
RegistrationType: ptrInt(registrationTypeEnterprise),
TenantCode: "test",
Secret: []byte(secret),
Desc: &MsgSetDesc{Public: map[string]any{"fn": "Alice"}},
},
}