diff options
Diffstat (limited to 'pkg/components')
-rw-r--r-- | pkg/components/auth/controller.go | 77 | ||||
-rw-r--r-- | pkg/components/auth/controller_test.go | 78 | ||||
-rw-r--r-- | pkg/components/auth/mock_test.go | 121 | ||||
-rw-r--r-- | pkg/components/errors.go | 8 | ||||
-rw-r--r-- | pkg/components/filesystem/controller.go | 91 | ||||
-rw-r--r-- | pkg/components/user/controller.go | 1 |
6 files changed, 0 insertions, 376 deletions
diff --git a/pkg/components/auth/controller.go b/pkg/components/auth/controller.go deleted file mode 100644 index 0b08fcc..0000000 --- a/pkg/components/auth/controller.go +++ /dev/null @@ -1,77 +0,0 @@ -package auth - -import ( - "context" - - "golang.org/x/crypto/bcrypt" - - "git.sr.ht/~gabrielgio/img/pkg/components" - "git.sr.ht/~gabrielgio/img/pkg/database/repository" - "git.sr.ht/~gabrielgio/img/pkg/ext" -) - -type Controller struct { - authRepository repository.AuthRepository - userRepository repository.UserRepository - key []byte -} - -func NewController( - authRepository repository.AuthRepository, - userRepository repository.UserRepository, - key []byte, -) *Controller { - return &Controller{ - authRepository: authRepository, - userRepository: userRepository, - key: key, - } -} - -func (c *Controller) Login(ctx context.Context, username, password []byte) ([]byte, error) { - id, err := c.authRepository.GetIDByUsername(ctx, string(username)) - if err != nil { - return nil, err - } - - hashedPassword, err := c.authRepository.GetPassword(ctx, id) - if err != nil { - return nil, err - } - - if err := bcrypt.CompareHashAndPassword(hashedPassword, password); err != nil { - return nil, err - } - - token := &ext.Token{ - UserID: id, - Username: string(username), - } - return ext.WriteToken(token, c.key) -} - -// InitialRegister register a initial user, it will validate if there is another -// user stored already. If so an error `InvlidaInput` will be returned -func (c *Controller) InitialRegister(ctx context.Context, username, password []byte, path []byte) error { - exist, err := c.userRepository.Any(ctx) - if err != nil { - return err - } - - if exist { - return components.InvlidaInput - } - - hash, err := bcrypt.GenerateFromPassword(password, bcrypt.MinCost) - if err != nil { - return err - } - - _, err = c.userRepository.Create(ctx, &repository.CreateUser{ - Username: string(username), - Password: hash, - Path: string(path), - }) - - return err -} diff --git a/pkg/components/auth/controller_test.go b/pkg/components/auth/controller_test.go deleted file mode 100644 index b1ca065..0000000 --- a/pkg/components/auth/controller_test.go +++ /dev/null @@ -1,78 +0,0 @@ -//go:build unit - -package auth - -import ( - "context" - "testing" - - "git.sr.ht/~gabrielgio/img/pkg/database/repository" - "git.sr.ht/~gabrielgio/img/pkg/ext" - "git.sr.ht/~gabrielgio/img/pkg/testkit" -) - -type ( - scene struct { - ctx context.Context - authRepository repository.AuthRepository - userRepository repository.UserRepository - controller *Controller - } -) - -var ( - key = []byte("6368616e676520746869732070617373") -) - -func setUp() *scene { - userRepository := NewUserRepository() - return &scene{ - ctx: context.Background(), - authRepository: userRepository, - userRepository: userRepository, - controller: NewController(userRepository, userRepository, key), - } -} - -func TestInitialRegisterAndLogin(t *testing.T) { - testCases := []struct { - name string - username string - password []byte - }{ - { - name: "Normal register", - username: "username", - password: []byte("this is an password"), - }, - } - - for _, tc := range testCases { - t.Run(tc.name, func(t *testing.T) { - scene := setUp() - - err := scene.controller.InitialRegister(scene.ctx, []byte(tc.username), tc.password, []byte("/")) - testkit.TestFatalError(t, "Register", err) - - users, err := scene.userRepository.List(scene.ctx) - userID := users[0].ID - - user, err := scene.userRepository.Get(scene.ctx, userID) - testkit.TestFatalError(t, "Get", err) - testkit.TestValue(t, "Register", tc.username, user.Username) - - auth, err := scene.controller.Login(scene.ctx, []byte(tc.username), tc.password) - testkit.TestFatalError(t, "Login", err) - - token, err := ext.ReadToken(auth, key) - testkit.TestFatalError(t, "Login", err) - - testkit.TestValue(t, "Login", tc.username, token.Username) - testkit.TestValue(t, "Login", userID, token.UserID) - }) - } -} - -func remove[T any](slice []T, s int) []T { - return append(slice[:s], slice[s+1:]...) -} diff --git a/pkg/components/auth/mock_test.go b/pkg/components/auth/mock_test.go deleted file mode 100644 index 885f643..0000000 --- a/pkg/components/auth/mock_test.go +++ /dev/null @@ -1,121 +0,0 @@ -//go:build unit - -package auth - -import ( - "context" - "errors" - - "git.sr.ht/~gabrielgio/img/pkg/database/repository" -) - -type ( - User struct { - ID uint - Username string - Name string - Password []byte - IsAdmin bool - Path string - } - - Users map[uint]*User - - UserRepository struct { - icount uint - users Users - } -) - -var _ repository.UserRepository = &UserRepository{} -var _ repository.AuthRepository = &UserRepository{} - -func NewUserRepository() *UserRepository { - return &UserRepository{ - users: make(map[uint]*User), - } -} - -func (u *User) ToModel() *repository.User { - return &repository.User{ - ID: u.ID, - Username: u.Username, - Name: u.Name, - IsAdmin: u.IsAdmin, - Path: u.Path, - } -} - -func (u Users) ToModels() []*repository.User { - users := make([]*repository.User, 0, len(u)) - for _, i := range u { - users = append(users, i.ToModel()) - } - return users -} - -func (u *UserRepository) Get(ctx context.Context, id uint) (*repository.User, error) { - if user, ok := u.users[id]; ok { - return user.ToModel(), nil - } - - return nil, errors.New("Not Found") -} - -func (u *UserRepository) List(_ context.Context) ([]*repository.User, error) { - return u.users.ToModels(), nil -} - -func (u *UserRepository) Create(_ context.Context, createUser *repository.CreateUser) (uint, error) { - id := u.furtherID() - u.users[id] = &User{ - ID: id, - Name: createUser.Name, - Username: createUser.Username, - Path: createUser.Path, - Password: createUser.Password, - } - return id, nil -} - -func (u *UserRepository) Update(_ context.Context, id uint, updateUser *repository.UpdateUser) error { - user, ok := u.users[id] - if !ok { - return errors.New("Invalid ID") - } - - user.Name = updateUser.Name - user.Username = updateUser.Username - if updateUser.Password != "" { - user.Password = []byte(updateUser.Password) - } - - return nil -} - -func (u *UserRepository) Any(_ context.Context) (bool, error) { - return len(u.users) > 0, nil -} - -func (u *UserRepository) GetIDByUsername(ctx context.Context, username string) (uint, error) { - for id, u := range u.users { - if u.Username == username { - return id, nil - } - } - - return 0, errors.New("Not Found") -} - -func (u *UserRepository) GetPassword(ctx context.Context, id uint) ([]byte, error) { - if user, ok := u.users[id]; ok { - return []byte(user.Password), nil - } - - return nil, errors.New("Not Found") -} - -func (u *UserRepository) furtherID() uint { - u.icount++ - return u.icount -} diff --git a/pkg/components/errors.go b/pkg/components/errors.go deleted file mode 100644 index aedbe88..0000000 --- a/pkg/components/errors.go +++ /dev/null @@ -1,8 +0,0 @@ -package components - -import "errors" - -var ( - NotFound = errors.New("Not found") - InvlidaInput = errors.New("Invalid Input") -) diff --git a/pkg/components/filesystem/controller.go b/pkg/components/filesystem/controller.go deleted file mode 100644 index 6c613a3..0000000 --- a/pkg/components/filesystem/controller.go +++ /dev/null @@ -1,91 +0,0 @@ -package filesystem - -import ( - "io/fs" - "net/url" - "path" - "strings" - - "git.sr.ht/~gabrielgio/img/pkg/database/repository" -) - -type ( - Controller struct { - repository repository.FileSystemRepository - } - - DirectoryParam struct { - Name string - UrlEncodedPath string - } - - FileParam struct { - UrlEncodedPath string - Info fs.FileInfo - } - - Page struct { - History []*DirectoryParam - Files []*FileParam - } -) - -func NewController(repository repository.FileSystemRepository) *Controller { - return &Controller{ - repository: repository, - } -} - -func getHistory(filepath string) []*DirectoryParam { - var ( - paths = strings.Split(filepath, "/") - result = make([]*DirectoryParam, 0, len(paths)) - acc = "" - ) - - // add root folder - result = append(result, &DirectoryParam{ - Name: "...", - UrlEncodedPath: "", - }) - - if len(paths) == 1 && paths[0] == "" { - return result - } - - for _, p := range paths { - acc = path.Join(acc, p) - result = append(result, &DirectoryParam{ - Name: p, - UrlEncodedPath: url.QueryEscape(acc), - }) - } - return result -} - -func (self *Controller) GetPage(filepath string) (*Page, error) { - decodedPath, err := url.QueryUnescape(filepath) - if err != nil { - return nil, err - } - - files, err := self.repository.List(decodedPath) - if err != nil { - return nil, err - } - - params := make([]*FileParam, 0, len(files)) - for _, info := range files { - fullPath := path.Join(decodedPath, info.Name()) - scapedFullPath := url.QueryEscape(fullPath) - params = append(params, &FileParam{ - Info: info, - UrlEncodedPath: scapedFullPath, - }) - } - - return &Page{ - Files: params, - History: getHistory(decodedPath), - }, nil -} diff --git a/pkg/components/user/controller.go b/pkg/components/user/controller.go deleted file mode 100644 index a00006b..0000000 --- a/pkg/components/user/controller.go +++ /dev/null @@ -1 +0,0 @@ -package user |