From d4b7702bad423d3eaa2f1159b9894a36b11af0a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torjus=20H=C3=A5kestad?= Date: Thu, 20 Jan 2022 03:35:55 +0100 Subject: [PATCH] Create users package --- auth.go | 5 +++-- auth_test.go | 16 ++++++++++++++-- http.go | 9 +++++---- http_test.go | 3 ++- user.go => users/user.go | 2 +- user_test.go => users/user_test.go | 6 +++--- userstore_bolt.go => users/userstore_bolt.go | 2 +- .../userstore_bolt_test.go | 8 ++++---- userstore_memory.go => users/userstore_memory.go | 2 +- users/userstore_memory_test.go | 15 +++++++++++++++ userstore_test.go => users/userstore_test.go | 12 ++++++------ userstore_memory_test.go | 15 --------------- 12 files changed, 55 insertions(+), 40 deletions(-) rename user.go => users/user.go (98%) rename user_test.go => users/user_test.go (88%) rename userstore_bolt.go => users/userstore_bolt.go (98%) rename userstore_bolt_test.go => users/userstore_bolt_test.go (68%) rename userstore_memory.go => users/userstore_memory.go (97%) create mode 100644 users/userstore_memory_test.go rename userstore_test.go => users/userstore_test.go (79%) delete mode 100644 userstore_memory_test.go diff --git a/auth.go b/auth.go index 6e7da52..9e9748d 100644 --- a/auth.go +++ b/auth.go @@ -4,6 +4,7 @@ import ( "fmt" "time" + "git.t-juice.club/torjus/gpaste/users" "github.com/golang-jwt/jwt" "github.com/google/uuid" ) @@ -17,11 +18,11 @@ const ( ) type AuthService struct { - users UserStore + users users.UserStore hmacSecret []byte } -func NewAuthService(store UserStore, signingSecret []byte) *AuthService { +func NewAuthService(store users.UserStore, signingSecret []byte) *AuthService { return &AuthService{users: store, hmacSecret: signingSecret} } diff --git a/auth_test.go b/auth_test.go index 7685ce3..d4251f3 100644 --- a/auth_test.go +++ b/auth_test.go @@ -1,21 +1,23 @@ package gpaste_test import ( + "math/rand" "testing" "git.t-juice.club/torjus/gpaste" + "git.t-juice.club/torjus/gpaste/users" ) func TestAuth(t *testing.T) { t.Run("Token", func(t *testing.T) { - us := gpaste.NewMemoryUserStore() + us := users.NewMemoryUserStore() secret := []byte(randomString(16)) as := gpaste.NewAuthService(us, secret) username := randomString(8) password := randomString(16) - user := &gpaste.User{Username: username} + user := &users.User{Username: username} if err := user.SetPassword(password); err != nil { t.Fatalf("error setting user password: %s", err) } @@ -37,3 +39,13 @@ func TestAuth(t *testing.T) { } }) } + +func randomString(length int) string { + const charset = "abcdefghijklmnopqrstabcdefghijklmnopqrstuvwxyz" + + "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" + b := make([]byte, length) + for i := range b { + b[i] = charset[rand.Intn(len(charset))] + } + return string(b) +} diff --git a/http.go b/http.go index e61351a..cbf214a 100644 --- a/http.go +++ b/http.go @@ -6,6 +6,7 @@ import ( "net/http" "strings" + "git.t-juice.club/torjus/gpaste/users" "github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5/middleware" "github.com/google/uuid" @@ -14,7 +15,7 @@ import ( type HTTPServer struct { Files FileStore - Users UserStore + Users users.UserStore Auth *AuthService config *ServerConfig Logger *zap.SugaredLogger @@ -29,12 +30,12 @@ func NewHTTPServer(cfg *ServerConfig) *HTTPServer { AccessLogger: zap.NewNop().Sugar(), } srv.Files = NewMemoryFileStore() - srv.Users = NewMemoryUserStore() + srv.Users = users.NewMemoryUserStore() srv.Auth = NewAuthService(srv.Users, []byte(srv.config.SigningSecret)) // Create initial user // TODO: Do properly - user := &User{Username: "admin"} + user := &users.User{Username: "admin"} user.SetPassword("admin") srv.Users.Store(user) @@ -214,7 +215,7 @@ func (s *HTTPServer) HandlerAPIUserCreate(w http.ResponseWriter, r *http.Request } // TODO: Ensure user does not already exist - user := &User{Username: req.Username} + user := &users.User{Username: req.Username} if err := user.SetPassword(req.Password); err != nil { s.Logger.Warnw("Error setting user password.", "req_id", reqID, "error", err, "remote_addr", r.RemoteAddr) w.WriteHeader(http.StatusBadRequest) diff --git a/http_test.go b/http_test.go index 1b2005e..3093f31 100644 --- a/http_test.go +++ b/http_test.go @@ -11,6 +11,7 @@ import ( "testing" "git.t-juice.club/torjus/gpaste" + "git.t-juice.club/torjus/gpaste/users" ) func TestHandlers(t *testing.T) { @@ -101,7 +102,7 @@ func TestHandlers(t *testing.T) { // TODO: Add test username := "admin" password := "admin" - user := &gpaste.User{Username: username} + user := &users.User{Username: username} if err := user.SetPassword(password); err != nil { t.Fatalf("Error setting user password: %s", err) } diff --git a/user.go b/users/user.go similarity index 98% rename from user.go rename to users/user.go index 0018f89..c25fedd 100644 --- a/user.go +++ b/users/user.go @@ -1,4 +1,4 @@ -package gpaste +package users import "golang.org/x/crypto/bcrypt" diff --git a/user_test.go b/users/user_test.go similarity index 88% rename from user_test.go rename to users/user_test.go index 5855e68..f840c6a 100644 --- a/user_test.go +++ b/users/user_test.go @@ -1,10 +1,10 @@ -package gpaste_test +package users_test import ( "math/rand" "testing" - "git.t-juice.club/torjus/gpaste" + "git.t-juice.club/torjus/gpaste/users" ) func TestUser(t *testing.T) { @@ -15,7 +15,7 @@ func TestUser(t *testing.T) { } for username, password := range userMap { - user := &gpaste.User{Username: username} + user := &users.User{Username: username} if err := user.SetPassword(password); err != nil { t.Fatalf("Error setting password: %s", err) } diff --git a/userstore_bolt.go b/users/userstore_bolt.go similarity index 98% rename from userstore_bolt.go rename to users/userstore_bolt.go index c0c87e9..3ceff7c 100644 --- a/userstore_bolt.go +++ b/users/userstore_bolt.go @@ -1,4 +1,4 @@ -package gpaste +package users import ( "encoding/json" diff --git a/userstore_bolt_test.go b/users/userstore_bolt_test.go similarity index 68% rename from userstore_bolt_test.go rename to users/userstore_bolt_test.go index 4600443..b2f160b 100644 --- a/userstore_bolt_test.go +++ b/users/userstore_bolt_test.go @@ -1,18 +1,18 @@ -package gpaste_test +package users_test import ( "path/filepath" "testing" - "git.t-juice.club/torjus/gpaste" + "git.t-juice.club/torjus/gpaste/users" ) func TestBoltUserStore(t *testing.T) { tmpDir := t.TempDir() - newFunc := func() (func(), gpaste.UserStore) { + newFunc := func() (func(), users.UserStore) { tmpFile := filepath.Join(tmpDir, randomString(8)) - store, err := gpaste.NewBoltUserStore(tmpFile) + store, err := users.NewBoltUserStore(tmpFile) if err != nil { t.Fatalf("Error creating store: %s", err) } diff --git a/userstore_memory.go b/users/userstore_memory.go similarity index 97% rename from userstore_memory.go rename to users/userstore_memory.go index 33811a0..5da183d 100644 --- a/userstore_memory.go +++ b/users/userstore_memory.go @@ -1,4 +1,4 @@ -package gpaste +package users import ( "fmt" diff --git a/users/userstore_memory_test.go b/users/userstore_memory_test.go new file mode 100644 index 0000000..7397387 --- /dev/null +++ b/users/userstore_memory_test.go @@ -0,0 +1,15 @@ +package users_test + +import ( + "testing" + + "git.t-juice.club/torjus/gpaste/users" +) + +func TestMemoryUserStore(t *testing.T) { + newFunc := func() (func(), users.UserStore) { + return func() {}, users.NewMemoryUserStore() + } + + RunUserStoreTest(newFunc, t) +} diff --git a/userstore_test.go b/users/userstore_test.go similarity index 79% rename from userstore_test.go rename to users/userstore_test.go index b9d4371..6146067 100644 --- a/userstore_test.go +++ b/users/userstore_test.go @@ -1,26 +1,26 @@ -package gpaste_test +package users_test import ( "testing" - "git.t-juice.club/torjus/gpaste" + "git.t-juice.club/torjus/gpaste/users" "github.com/google/go-cmp/cmp" ) -func RunUserStoreTest(newFunc func() (func(), gpaste.UserStore), t *testing.T) { +func RunUserStoreTest(newFunc func() (func(), users.UserStore), t *testing.T) { t.Run("Basics", func(t *testing.T) { cleanup, s := newFunc() t.Cleanup(cleanup) - userMap := make(map[string]*gpaste.User) + userMap := make(map[string]*users.User) passwordMap := make(map[string]string) for i := 0; i < 10; i++ { username := randomString(8) password := randomString(16) passwordMap[username] = password - user := &gpaste.User{ + user := &users.User{ Username: username, - Roles: []gpaste.Role{gpaste.RoleAdmin}, + Roles: []users.Role{users.RoleAdmin}, } if err := user.SetPassword(password); err != nil { t.Fatalf("Error setting password: %s", err) diff --git a/userstore_memory_test.go b/userstore_memory_test.go deleted file mode 100644 index f044e82..0000000 --- a/userstore_memory_test.go +++ /dev/null @@ -1,15 +0,0 @@ -package gpaste_test - -import ( - "testing" - - "git.t-juice.club/torjus/gpaste" -) - -func TestMemoryUserStore(t *testing.T) { - newFunc := func() (func(), gpaste.UserStore) { - return func() {}, gpaste.NewMemoryUserStore() - } - - RunUserStoreTest(newFunc, t) -}