From 2d00d9eb360e887d8d71179f150feba0237449f0 Mon Sep 17 00:00:00 2001 From: siddharth Date: Sat, 30 Apr 2022 21:13:20 -0400 Subject: lib: add SRPServer.RegisterUser --- lib/srp.go | 12 +++++++++++- lib/srp_test.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/srp.go b/lib/srp.go index 9fdd594..cdecc4f 100644 --- a/lib/srp.go +++ b/lib/srp.go @@ -30,7 +30,7 @@ import "math/big" // SRP Server. type SRPServer struct { - users []SRPUser + users []*SRPUser } // Registered user on the SRP server. @@ -90,6 +90,16 @@ type SRPClientSession struct { sk []byte } +func (server *SRPServer) RegisterUser(user *SRPUser) error { + for _, u := range server.users { + if u.ident == user.ident { + return CPError{"user already registered"} + } + } + server.users = append(server.users, user) + return nil +} + func NewSRPUser(n, g, k, ident, pass string) (*SRPUser, error) { var err error var ok bool diff --git a/lib/srp_test.go b/lib/srp_test.go index edcc588..06d1a27 100644 --- a/lib/srp_test.go +++ b/lib/srp_test.go @@ -8,6 +8,55 @@ import ( "testing" ) +func TestSRPServerRegisterUser(t *testing.T) { + n := "7" + g := "2" + k := "3" + + bobIdent := "bob@ricketyspace.net" + bobPass := "d59d6c93af0f37f272d924979" + bob, err := NewSRPUser(n, g, k, bobIdent, bobPass) + + aliceIdent := "alice@ricketyspace.net" + alicePass := "5ae262bcfaf6b4de5a3edc9ae" + alice, err := NewSRPUser(n, g, k, aliceIdent, alicePass) + + server := new(SRPServer) + err = server.RegisterUser(bob) + if err != nil { + t.Errorf("registering bob: %v", err) + } + if len(server.users) != 1 { + t.Errorf("server has %v users", len(server.users)) + } + err = server.RegisterUser(alice) + if err != nil { + t.Errorf("registering alice: %v", err) + } + if len(server.users) != 2 { + t.Errorf("server has %v users", len(server.users)) + } + + err = server.RegisterUser(bob) + if err == nil { + t.Errorf("server registered bob again") + return + } + if err.Error() != "user already registered" { + t.Errorf("registration message: %v", err) + return + } + err = server.RegisterUser(alice) + if err == nil { + t.Errorf("server registered alice again") + return + } + if err.Error() != "user already registered" { + t.Errorf("registration message: %v", err) + return + } +} + func TestNewSRPUser(t *testing.T) { n := StripSpaceChars( `ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024 -- cgit v1.2.3