mirror of
https://github.com/hay-kot/homebox.git
synced 2025-07-07 02:58:35 +00:00
Initial commit
This commit is contained in:
commit
29f583e936
135 changed files with 18463 additions and 0 deletions
15
backend/internal/services/all.go
Normal file
15
backend/internal/services/all.go
Normal file
|
@ -0,0 +1,15 @@
|
|||
package services
|
||||
|
||||
import "github.com/hay-kot/git-web-template/backend/internal/repo"
|
||||
|
||||
type AllServices struct {
|
||||
User *UserService
|
||||
Admin *AdminService
|
||||
}
|
||||
|
||||
func NewServices(repos *repo.AllRepos) *AllServices {
|
||||
return &AllServices{
|
||||
User: &UserService{repos},
|
||||
Admin: &AdminService{repos},
|
||||
}
|
||||
}
|
40
backend/internal/services/contexts.go
Normal file
40
backend/internal/services/contexts.go
Normal file
|
@ -0,0 +1,40 @@
|
|||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/hay-kot/git-web-template/backend/internal/types"
|
||||
)
|
||||
|
||||
type contextKeys struct {
|
||||
name string
|
||||
}
|
||||
|
||||
var (
|
||||
ContextUser = &contextKeys{name: "User"}
|
||||
ContextUserToken = &contextKeys{name: "UserToken"}
|
||||
)
|
||||
|
||||
// SetUserCtx is a helper function that sets the ContextUser and ContextUserToken
|
||||
// values within the context of a web request (or any context).
|
||||
func SetUserCtx(ctx context.Context, user *types.UserOut, token string) context.Context {
|
||||
ctx = context.WithValue(ctx, ContextUser, user)
|
||||
ctx = context.WithValue(ctx, ContextUserToken, token)
|
||||
return ctx
|
||||
}
|
||||
|
||||
// UseUserCtx is a helper function that returns the user from the context.
|
||||
func UseUserCtx(ctx context.Context) *types.UserOut {
|
||||
if val := ctx.Value(ContextUser); val != nil {
|
||||
return val.(*types.UserOut)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UseTokenCtx is a helper function that returns the user token from the context.
|
||||
func UseTokenCtx(ctx context.Context) string {
|
||||
if val := ctx.Value(ContextUserToken); val != nil {
|
||||
return val.(string)
|
||||
}
|
||||
return ""
|
||||
}
|
39
backend/internal/services/contexts_test.go
Normal file
39
backend/internal/services/contexts_test.go
Normal file
|
@ -0,0 +1,39 @@
|
|||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/hay-kot/git-web-template/backend/internal/types"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_SetAuthContext(t *testing.T) {
|
||||
user := &types.UserOut{
|
||||
ID: uuid.New(),
|
||||
}
|
||||
|
||||
token := uuid.New().String()
|
||||
|
||||
ctx := SetUserCtx(context.Background(), user, token)
|
||||
|
||||
ctxUser := UseUserCtx(ctx)
|
||||
|
||||
assert.NotNil(t, ctxUser)
|
||||
assert.Equal(t, user.ID, ctxUser.ID)
|
||||
|
||||
ctxUserToken := UseTokenCtx(ctx)
|
||||
assert.NotEmpty(t, ctxUserToken)
|
||||
}
|
||||
|
||||
func Test_SetAuthContext_Nulls(t *testing.T) {
|
||||
ctx := SetUserCtx(context.Background(), nil, "")
|
||||
|
||||
ctxUser := UseUserCtx(ctx)
|
||||
|
||||
assert.Nil(t, ctxUser)
|
||||
|
||||
ctxUserToken := UseTokenCtx(ctx)
|
||||
assert.Empty(t, ctxUserToken)
|
||||
}
|
47
backend/internal/services/service_admin.go
Normal file
47
backend/internal/services/service_admin.go
Normal file
|
@ -0,0 +1,47 @@
|
|||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/hay-kot/git-web-template/backend/internal/repo"
|
||||
"github.com/hay-kot/git-web-template/backend/internal/types"
|
||||
)
|
||||
|
||||
type AdminService struct {
|
||||
repos *repo.AllRepos
|
||||
}
|
||||
|
||||
func (svc *AdminService) Create(ctx context.Context, usr types.UserCreate) (types.UserOut, error) {
|
||||
return svc.repos.Users.Create(ctx, usr)
|
||||
}
|
||||
|
||||
func (svc *AdminService) GetAll(ctx context.Context) ([]types.UserOut, error) {
|
||||
return svc.repos.Users.GetAll(ctx)
|
||||
}
|
||||
|
||||
func (svc *AdminService) GetByID(ctx context.Context, id uuid.UUID) (types.UserOut, error) {
|
||||
return svc.repos.Users.GetOneId(ctx, id)
|
||||
}
|
||||
|
||||
func (svc *AdminService) GetByEmail(ctx context.Context, email string) (types.UserOut, error) {
|
||||
return svc.repos.Users.GetOneEmail(ctx, email)
|
||||
}
|
||||
|
||||
func (svc *AdminService) UpdateProperties(ctx context.Context, ID uuid.UUID, data types.UserUpdate) (types.UserOut, error) {
|
||||
err := svc.repos.Users.Update(ctx, ID, data)
|
||||
|
||||
if err != nil {
|
||||
return types.UserOut{}, err
|
||||
}
|
||||
|
||||
return svc.repos.Users.GetOneId(ctx, ID)
|
||||
}
|
||||
|
||||
func (svc *AdminService) Delete(ctx context.Context, id uuid.UUID) error {
|
||||
return svc.repos.Users.Delete(ctx, id)
|
||||
}
|
||||
|
||||
func (svc *AdminService) DeleteAll(ctx context.Context) error {
|
||||
return svc.repos.Users.DeleteAll(ctx)
|
||||
}
|
84
backend/internal/services/service_user.go
Normal file
84
backend/internal/services/service_user.go
Normal file
|
@ -0,0 +1,84 @@
|
|||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/hay-kot/git-web-template/backend/internal/repo"
|
||||
"github.com/hay-kot/git-web-template/backend/internal/types"
|
||||
"github.com/hay-kot/git-web-template/backend/pkgs/hasher"
|
||||
)
|
||||
|
||||
var (
|
||||
oneWeek = time.Hour * 24 * 7
|
||||
ErrorInvalidLogin = errors.New("invalid username or password")
|
||||
ErrorInvalidToken = errors.New("invalid token")
|
||||
ErrorTokenIdMismatch = errors.New("token id mismatch")
|
||||
)
|
||||
|
||||
type UserService struct {
|
||||
repos *repo.AllRepos
|
||||
}
|
||||
|
||||
// GetSelf returns the user that is currently logged in based of the token provided within
|
||||
func (svc *UserService) GetSelf(ctx context.Context, requestToken string) (types.UserOut, error) {
|
||||
hash := hasher.HashToken(requestToken)
|
||||
return svc.repos.AuthTokens.GetUserFromToken(ctx, hash)
|
||||
}
|
||||
|
||||
func (svc *UserService) UpdateSelf(ctx context.Context, ID uuid.UUID, data types.UserUpdate) (types.UserOut, error) {
|
||||
err := svc.repos.Users.Update(ctx, ID, data)
|
||||
|
||||
if err != nil {
|
||||
return types.UserOut{}, err
|
||||
}
|
||||
|
||||
return svc.repos.Users.GetOneId(ctx, ID)
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// User Authentication
|
||||
|
||||
func (svc *UserService) createToken(ctx context.Context, userId uuid.UUID) (types.UserAuthTokenDetail, error) {
|
||||
newToken := hasher.GenerateToken()
|
||||
|
||||
created, err := svc.repos.AuthTokens.CreateToken(ctx, types.UserAuthTokenCreate{
|
||||
UserID: userId,
|
||||
TokenHash: newToken.Hash,
|
||||
ExpiresAt: time.Now().Add(oneWeek),
|
||||
})
|
||||
|
||||
return types.UserAuthTokenDetail{Raw: newToken.Raw, ExpiresAt: created.ExpiresAt}, err
|
||||
}
|
||||
|
||||
func (svc *UserService) Login(ctx context.Context, username, password string) (types.UserAuthTokenDetail, error) {
|
||||
usr, err := svc.repos.Users.GetOneEmail(ctx, username)
|
||||
|
||||
if err != nil || !hasher.CheckPasswordHash(password, usr.Password) {
|
||||
return types.UserAuthTokenDetail{}, ErrorInvalidLogin
|
||||
}
|
||||
|
||||
return svc.createToken(ctx, usr.ID)
|
||||
}
|
||||
|
||||
func (svc *UserService) Logout(ctx context.Context, token string) error {
|
||||
hash := hasher.HashToken(token)
|
||||
err := svc.repos.AuthTokens.DeleteToken(ctx, hash)
|
||||
return err
|
||||
}
|
||||
|
||||
func (svc *UserService) RenewToken(ctx context.Context, token string) (types.UserAuthTokenDetail, error) {
|
||||
hash := hasher.HashToken(token)
|
||||
|
||||
dbToken, err := svc.repos.AuthTokens.GetUserFromToken(ctx, hash)
|
||||
|
||||
if err != nil {
|
||||
return types.UserAuthTokenDetail{}, ErrorInvalidToken
|
||||
}
|
||||
|
||||
newToken, _ := svc.createToken(ctx, dbToken.ID)
|
||||
|
||||
return newToken, nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue