From a7c36c999cfee1f9c2f237e2ba9451999b325685 Mon Sep 17 00:00:00 2001 From: puh Date: Fri, 29 Sep 2023 09:11:09 +0300 Subject: [PATCH] Forbid to create user without full UserInfo --- internal/server/usermanager/api.yaml | 2 +- internal/server/usermanager/localmanager.go | 8 ++++++++ internal/server/usermanager/localmanager_test.go | 14 ++++++++++---- internal/server/usermanager/usermanager.go | 1 + 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/internal/server/usermanager/api.yaml b/internal/server/usermanager/api.yaml index d49875d..30c12cc 100644 --- a/internal/server/usermanager/api.yaml +++ b/internal/server/usermanager/api.yaml @@ -149,4 +149,4 @@ externalDocs: host: 127.0.0.1:8080 basePath: / schemes: - - http \ No newline at end of file + - http diff --git a/internal/server/usermanager/localmanager.go b/internal/server/usermanager/localmanager.go index ed466cb..63552bb 100644 --- a/internal/server/usermanager/localmanager.go +++ b/internal/server/usermanager/localmanager.go @@ -222,6 +222,14 @@ func (manager *localManager) WriteUserInfo(u UserInfo) (err error) { if err != nil { return err } + k, v := bucket.Cursor().First() + // Registration required full UserInfo + if k == nil && v == nil { + if u.SessionsCap == nil || u.UpRate == nil || u.DownRate == nil || + u.UpCredit == nil || u.DownCredit == nil || u.ExpiryTime == nil { + return ErrInvalidUserInfo + } + } if u.SessionsCap != nil { if err = bucket.Put([]byte("SessionsCap"), i32ToB(*u.SessionsCap)); err != nil { return err diff --git a/internal/server/usermanager/localmanager_test.go b/internal/server/usermanager/localmanager_test.go index 6b28e8f..01d1b99 100644 --- a/internal/server/usermanager/localmanager_test.go +++ b/internal/server/usermanager/localmanager_test.go @@ -41,10 +41,16 @@ func TestLocalManager_WriteUserInfo(t *testing.T) { mgr, cleaner := makeManager(t) defer cleaner() - err := mgr.WriteUserInfo(mockUserInfo) - if err != nil { - t.Error(err) - } + /* New user with partial UserInfo */ + err := mgr.WriteUserInfo(UserInfo{ + UID: mockUID, + SessionsCap: JustInt32(*mockUserInfo.SessionsCap + 1), + }) + assert.Error(t, err, ErrInvalidUserInfo) + + /* New user with full UserInfo */ + err = mgr.WriteUserInfo(mockUserInfo) + assert.NoError(t, err) got, err := mgr.GetUserInfo(mockUID) assert.NoError(t, err) diff --git a/internal/server/usermanager/usermanager.go b/internal/server/usermanager/usermanager.go index bb5456e..1055c04 100644 --- a/internal/server/usermanager/usermanager.go +++ b/internal/server/usermanager/usermanager.go @@ -52,6 +52,7 @@ var ErrMangerIsVoid = errors.New("cannot perform operation with user manager as var ErrNoUpCredit = errors.New("No upload credit left") var ErrNoDownCredit = errors.New("No download credit left") var ErrUserExpired = errors.New("User has expired") +var ErrInvalidUserInfo = errors.New("Invalid user info") type UserManager interface { AuthenticateUser([]byte) (int64, int64, error)