2022-08-30 02:30:36 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2022-12-03 19:55:00 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/ent/authroles"
|
2022-10-30 04:05:38 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/repo"
|
2022-09-24 19:33:38 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/pkgs/hasher"
|
2022-09-27 23:52:13 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2022-08-30 02:30:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
type (
|
|
|
|
UserRegistration struct {
|
2022-10-07 02:54:09 +00:00
|
|
|
GroupToken string `json:"token"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Email string `json:"email"`
|
|
|
|
Password string `json:"password"`
|
2022-09-27 23:52:13 +00:00
|
|
|
}
|
|
|
|
UserAuthTokenDetail struct {
|
2022-12-03 19:55:00 +00:00
|
|
|
Raw string `json:"raw"`
|
|
|
|
AttachmentToken string `json:"attachmentToken"`
|
|
|
|
ExpiresAt time.Time `json:"expiresAt"`
|
2022-09-27 23:52:13 +00:00
|
|
|
}
|
|
|
|
LoginForm struct {
|
|
|
|
Username string `json:"username"`
|
|
|
|
Password string `json:"password"`
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-09-04 06:19:13 +00:00
|
|
|
// RegisterUser creates a new user and group in the data with the provided data. It also bootstraps the user's group
|
|
|
|
// with default Labels and Locations.
|
2022-09-27 23:52:13 +00:00
|
|
|
func (svc *UserService) RegisterUser(ctx context.Context, data UserRegistration) (repo.UserOut, error) {
|
|
|
|
log.Debug().
|
|
|
|
Str("name", data.Name).
|
|
|
|
Str("email", data.Email).
|
2022-10-07 02:54:09 +00:00
|
|
|
Str("groupToken", data.GroupToken).
|
2022-09-27 23:52:13 +00:00
|
|
|
Msg("Registering new user")
|
|
|
|
|
2022-10-07 02:54:09 +00:00
|
|
|
var (
|
2023-02-05 21:25:30 +00:00
|
|
|
err error
|
|
|
|
group repo.Group
|
|
|
|
token repo.GroupInvitation
|
|
|
|
|
|
|
|
// creatingGroup is true if the user is creating a new group.
|
|
|
|
creatingGroup = false
|
2022-10-07 02:54:09 +00:00
|
|
|
)
|
|
|
|
|
2022-10-09 17:44:13 +00:00
|
|
|
switch data.GroupToken {
|
|
|
|
case "":
|
2023-03-21 05:48:22 +00:00
|
|
|
log.Debug().Msg("creating new group")
|
2023-02-05 21:25:30 +00:00
|
|
|
creatingGroup = true
|
2022-10-09 13:03:24 +00:00
|
|
|
group, err = svc.repos.Groups.GroupCreate(ctx, "Home")
|
2022-10-07 02:54:09 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Msg("Failed to create group")
|
|
|
|
return repo.UserOut{}, err
|
|
|
|
}
|
2022-10-09 17:44:13 +00:00
|
|
|
default:
|
2023-03-21 05:48:22 +00:00
|
|
|
log.Debug().Msg("joining existing group")
|
2022-10-07 02:54:09 +00:00
|
|
|
token, err = svc.repos.Groups.InvitationGet(ctx, hasher.HashToken(data.GroupToken))
|
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Msg("Failed to get invitation token")
|
|
|
|
return repo.UserOut{}, err
|
|
|
|
}
|
|
|
|
group = token.Group
|
2022-08-30 18:05:11 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
hashed, _ := hasher.HashPassword(data.Password)
|
|
|
|
usrCreate := repo.UserCreate{
|
|
|
|
Name: data.Name,
|
|
|
|
Email: data.Email,
|
2022-08-30 18:05:11 +00:00
|
|
|
Password: hashed,
|
|
|
|
IsSuperuser: false,
|
|
|
|
GroupID: group.ID,
|
2023-02-05 21:25:30 +00:00
|
|
|
IsOwner: creatingGroup,
|
2022-08-30 18:05:11 +00:00
|
|
|
}
|
|
|
|
|
2022-09-04 06:19:13 +00:00
|
|
|
usr, err := svc.repos.Users.Create(ctx, usrCreate)
|
|
|
|
if err != nil {
|
2022-09-27 23:52:13 +00:00
|
|
|
return repo.UserOut{}, err
|
2022-09-04 06:19:13 +00:00
|
|
|
}
|
|
|
|
|
2023-02-05 21:25:30 +00:00
|
|
|
// Create the default labels and locations for the group.
|
|
|
|
if creatingGroup {
|
|
|
|
for _, label := range defaultLabels() {
|
2023-03-21 05:48:22 +00:00
|
|
|
_, err := svc.repos.Labels.Create(ctx, usr.GroupID, label)
|
2023-02-05 21:25:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return repo.UserOut{}, err
|
|
|
|
}
|
2022-09-04 06:19:13 +00:00
|
|
|
}
|
|
|
|
|
2023-02-05 21:25:30 +00:00
|
|
|
for _, location := range defaultLocations() {
|
2023-03-21 05:48:22 +00:00
|
|
|
_, err := svc.repos.Locations.Create(ctx, usr.GroupID, location)
|
2023-02-05 21:25:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return repo.UserOut{}, err
|
|
|
|
}
|
2022-09-04 06:19:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-05 21:25:30 +00:00
|
|
|
// Decrement the invitation token if it was used.
|
2022-10-07 02:54:09 +00:00
|
|
|
if token.ID != uuid.Nil {
|
|
|
|
err = svc.repos.Groups.InvitationUpdate(ctx, token.ID, token.Uses-1)
|
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Msg("Failed to update invitation token")
|
|
|
|
return repo.UserOut{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
return usr, nil
|
2022-08-30 18:05:11 +00:00
|
|
|
}
|
|
|
|
|
2022-08-30 02:30:36 +00:00
|
|
|
// GetSelf returns the user that is currently logged in based of the token provided within
|
2022-09-27 23:52:13 +00:00
|
|
|
func (svc *UserService) GetSelf(ctx context.Context, requestToken string) (repo.UserOut, error) {
|
2022-08-30 02:30:36 +00:00
|
|
|
hash := hasher.HashToken(requestToken)
|
2022-09-27 23:52:13 +00:00
|
|
|
return svc.repos.AuthTokens.GetUserFromToken(ctx, hash)
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
func (svc *UserService) UpdateSelf(ctx context.Context, ID uuid.UUID, data repo.UserUpdate) (repo.UserOut, error) {
|
2022-08-30 02:30:36 +00:00
|
|
|
err := svc.repos.Users.Update(ctx, ID, data)
|
|
|
|
if err != nil {
|
2022-09-27 23:52:13 +00:00
|
|
|
return repo.UserOut{}, err
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
return svc.repos.Users.GetOneId(ctx, ID)
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
// User Authentication
|
|
|
|
|
2022-12-03 19:55:00 +00:00
|
|
|
func (svc *UserService) createSessionToken(ctx context.Context, userId uuid.UUID) (UserAuthTokenDetail, error) {
|
|
|
|
attachmentToken := hasher.GenerateToken()
|
|
|
|
attachmentData := repo.UserAuthTokenCreate{
|
2022-08-30 02:30:36 +00:00
|
|
|
UserID: userId,
|
2022-12-03 19:55:00 +00:00
|
|
|
TokenHash: attachmentToken.Hash,
|
2022-08-30 02:30:36 +00:00
|
|
|
ExpiresAt: time.Now().Add(oneWeek),
|
2022-12-03 19:55:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err := svc.repos.AuthTokens.CreateToken(ctx, attachmentData, authroles.RoleAttachments)
|
|
|
|
if err != nil {
|
|
|
|
return UserAuthTokenDetail{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
userToken := hasher.GenerateToken()
|
|
|
|
data := repo.UserAuthTokenCreate{
|
|
|
|
UserID: userId,
|
|
|
|
TokenHash: userToken.Hash,
|
|
|
|
ExpiresAt: time.Now().Add(oneWeek),
|
|
|
|
}
|
|
|
|
|
|
|
|
created, err := svc.repos.AuthTokens.CreateToken(ctx, data, authroles.RoleUser)
|
|
|
|
if err != nil {
|
|
|
|
return UserAuthTokenDetail{}, err
|
|
|
|
}
|
2022-08-30 02:30:36 +00:00
|
|
|
|
2022-12-03 19:55:00 +00:00
|
|
|
return UserAuthTokenDetail{
|
|
|
|
Raw: userToken.Raw,
|
|
|
|
ExpiresAt: created.ExpiresAt,
|
|
|
|
AttachmentToken: attachmentToken.Raw,
|
|
|
|
}, nil
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
func (svc *UserService) Login(ctx context.Context, username, password string) (UserAuthTokenDetail, error) {
|
2022-08-30 02:30:36 +00:00
|
|
|
usr, err := svc.repos.Users.GetOneEmail(ctx, username)
|
2022-10-09 17:23:21 +00:00
|
|
|
if err != nil {
|
|
|
|
// SECURITY: Perform hash to ensure response times are the same
|
|
|
|
hasher.CheckPasswordHash("not-a-real-password", "not-a-real-password")
|
|
|
|
return UserAuthTokenDetail{}, ErrorInvalidLogin
|
|
|
|
}
|
|
|
|
|
|
|
|
if !hasher.CheckPasswordHash(password, usr.PasswordHash) {
|
2022-09-27 23:52:13 +00:00
|
|
|
return UserAuthTokenDetail{}, ErrorInvalidLogin
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
|
|
|
|
2022-12-03 19:55:00 +00:00
|
|
|
return svc.createSessionToken(ctx, usr.ID)
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (svc *UserService) Logout(ctx context.Context, token string) error {
|
|
|
|
hash := hasher.HashToken(token)
|
|
|
|
err := svc.repos.AuthTokens.DeleteToken(ctx, hash)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
func (svc *UserService) RenewToken(ctx context.Context, token string) (UserAuthTokenDetail, error) {
|
2022-08-30 02:30:36 +00:00
|
|
|
hash := hasher.HashToken(token)
|
|
|
|
|
|
|
|
dbToken, err := svc.repos.AuthTokens.GetUserFromToken(ctx, hash)
|
|
|
|
if err != nil {
|
2022-09-27 23:52:13 +00:00
|
|
|
return UserAuthTokenDetail{}, ErrorInvalidToken
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
|
|
|
|
2022-12-03 19:55:00 +00:00
|
|
|
return svc.createSessionToken(ctx, dbToken.ID)
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
2022-09-04 02:42:03 +00:00
|
|
|
|
|
|
|
// DeleteSelf deletes the user that is currently logged based of the provided UUID
|
|
|
|
// There is _NO_ protection against deleting the wrong user, as such this should only
|
|
|
|
// be used when the identify of the user has been confirmed.
|
|
|
|
func (svc *UserService) DeleteSelf(ctx context.Context, ID uuid.UUID) error {
|
|
|
|
return svc.repos.Users.Delete(ctx, ID)
|
|
|
|
}
|
2022-10-07 02:54:09 +00:00
|
|
|
|
2022-10-09 17:23:21 +00:00
|
|
|
func (svc *UserService) ChangePassword(ctx Context, current string, new string) (ok bool) {
|
|
|
|
usr, err := svc.repos.Users.GetOneId(ctx, ctx.UID)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !hasher.CheckPasswordHash(current, usr.PasswordHash) {
|
|
|
|
log.Err(errors.New("current password is incorrect")).Msg("Failed to change password")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
hashed, err := hasher.HashPassword(new)
|
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Msg("Failed to hash password")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
err = svc.repos.Users.ChangePassword(ctx.Context, ctx.UID, hashed)
|
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Msg("Failed to change password")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|