2020-01-20 02:40:18 +01:00
|
|
|
package persistence
|
|
|
|
|
|
|
|
import (
|
2020-01-28 14:22:17 +01:00
|
|
|
"context"
|
2021-06-19 00:38:38 +02:00
|
|
|
"crypto/sha256"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
2020-01-20 02:40:18 +01:00
|
|
|
"time"
|
|
|
|
|
2020-01-28 14:22:17 +01:00
|
|
|
. "github.com/Masterminds/squirrel"
|
2022-07-30 18:43:48 +02:00
|
|
|
"github.com/beego/beego/v2/client/orm"
|
2020-01-20 04:36:15 +01:00
|
|
|
"github.com/deluan/rest"
|
2020-01-28 14:22:17 +01:00
|
|
|
"github.com/google/uuid"
|
2021-06-19 00:38:38 +02:00
|
|
|
"github.com/navidrome/navidrome/conf"
|
|
|
|
"github.com/navidrome/navidrome/consts"
|
|
|
|
"github.com/navidrome/navidrome/log"
|
2020-01-24 01:44:08 +01:00
|
|
|
"github.com/navidrome/navidrome/model"
|
2021-06-19 00:38:38 +02:00
|
|
|
"github.com/navidrome/navidrome/utils"
|
2020-01-20 02:40:18 +01:00
|
|
|
)
|
|
|
|
|
2020-01-20 04:36:15 +01:00
|
|
|
type userRepository struct {
|
2020-01-28 14:22:17 +01:00
|
|
|
sqlRepository
|
2020-03-22 01:00:46 +01:00
|
|
|
sqlRestful
|
2020-01-20 04:36:15 +01:00
|
|
|
}
|
|
|
|
|
2021-06-19 00:38:38 +02:00
|
|
|
var (
|
|
|
|
once sync.Once
|
|
|
|
encKey []byte
|
|
|
|
)
|
|
|
|
|
2022-07-30 18:43:48 +02:00
|
|
|
func NewUserRepository(ctx context.Context, o orm.QueryExecutor) model.UserRepository {
|
2020-01-28 14:22:17 +01:00
|
|
|
r := &userRepository{}
|
|
|
|
r.ctx = ctx
|
|
|
|
r.ormer = o
|
|
|
|
r.tableName = "user"
|
2021-06-19 00:38:38 +02:00
|
|
|
once.Do(func() {
|
|
|
|
_ = r.initPasswordEncryptionKey()
|
|
|
|
})
|
2020-01-20 15:54:29 +01:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2020-01-20 04:36:15 +01:00
|
|
|
func (r *userRepository) CountAll(qo ...model.QueryOptions) (int64, error) {
|
2020-01-28 14:22:17 +01:00
|
|
|
return r.count(Select(), qo...)
|
2020-01-20 04:36:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *userRepository) Get(id string) (*model.User, error) {
|
2020-01-28 14:22:17 +01:00
|
|
|
sel := r.newSelect().Columns("*").Where(Eq{"id": id})
|
|
|
|
var res model.User
|
|
|
|
err := r.queryOne(sel, &res)
|
|
|
|
return &res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *userRepository) GetAll(options ...model.QueryOptions) (model.Users, error) {
|
|
|
|
sel := r.newSelect(options...).Columns("*")
|
2020-01-31 22:47:13 +01:00
|
|
|
res := model.Users{}
|
2020-01-28 14:22:17 +01:00
|
|
|
err := r.queryAll(sel, &res)
|
|
|
|
return res, err
|
2020-01-20 04:36:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *userRepository) Put(u *model.User) error {
|
2020-01-28 14:22:17 +01:00
|
|
|
if u.ID == "" {
|
2021-02-01 07:22:31 +01:00
|
|
|
u.ID = uuid.NewString()
|
2020-01-28 14:22:17 +01:00
|
|
|
}
|
2020-02-01 00:33:53 +01:00
|
|
|
u.UpdatedAt = time.Now()
|
2021-07-01 22:09:49 +02:00
|
|
|
if u.NewPassword != "" {
|
|
|
|
_ = r.encryptPassword(u)
|
|
|
|
}
|
2020-01-28 14:22:17 +01:00
|
|
|
values, _ := toSqlArgs(*u)
|
2021-05-03 21:03:34 +02:00
|
|
|
delete(values, "current_password")
|
2020-01-28 14:22:17 +01:00
|
|
|
update := Update(r.tableName).Where(Eq{"id": u.ID}).SetMap(values)
|
|
|
|
count, err := r.executeSQL(update)
|
2020-01-20 04:36:15 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-28 14:22:17 +01:00
|
|
|
if count > 0 {
|
|
|
|
return nil
|
2020-01-20 04:36:15 +01:00
|
|
|
}
|
2020-02-01 00:33:53 +01:00
|
|
|
values["created_at"] = time.Now()
|
2020-01-28 14:22:17 +01:00
|
|
|
insert := Insert(r.tableName).SetMap(values)
|
|
|
|
_, err = r.executeSQL(insert)
|
|
|
|
return err
|
2020-01-20 04:36:15 +01:00
|
|
|
}
|
|
|
|
|
2020-07-11 20:38:17 +02:00
|
|
|
func (r *userRepository) FindFirstAdmin() (*model.User, error) {
|
|
|
|
sel := r.newSelect(model.QueryOptions{Sort: "updated_at", Max: 1}).Columns("*").Where(Eq{"is_admin": true})
|
|
|
|
var usr model.User
|
|
|
|
err := r.queryOne(sel, &usr)
|
|
|
|
return &usr, err
|
|
|
|
}
|
|
|
|
|
2020-01-20 15:54:29 +01:00
|
|
|
func (r *userRepository) FindByUsername(username string) (*model.User, error) {
|
2020-09-02 00:00:19 +02:00
|
|
|
sel := r.newSelect().Columns("*").Where(Like{"user_name": username})
|
2020-01-28 14:22:17 +01:00
|
|
|
var usr model.User
|
|
|
|
err := r.queryOne(sel, &usr)
|
|
|
|
return &usr, err
|
2020-01-20 15:54:29 +01:00
|
|
|
}
|
|
|
|
|
2021-06-19 00:38:38 +02:00
|
|
|
func (r *userRepository) FindByUsernameWithPassword(username string) (*model.User, error) {
|
|
|
|
usr, err := r.FindByUsername(username)
|
|
|
|
if err == nil {
|
|
|
|
_ = r.decryptPassword(usr)
|
|
|
|
}
|
|
|
|
return usr, err
|
|
|
|
}
|
|
|
|
|
2020-01-20 15:54:29 +01:00
|
|
|
func (r *userRepository) UpdateLastLoginAt(id string) error {
|
2020-01-28 14:22:17 +01:00
|
|
|
upd := Update(r.tableName).Where(Eq{"id": id}).Set("last_login_at", time.Now())
|
|
|
|
_, err := r.executeSQL(upd)
|
2020-01-20 15:54:29 +01:00
|
|
|
return err
|
2020-01-20 04:36:15 +01:00
|
|
|
}
|
|
|
|
|
2020-01-20 19:35:59 +01:00
|
|
|
func (r *userRepository) UpdateLastAccessAt(id string) error {
|
|
|
|
now := time.Now()
|
2020-01-28 14:22:17 +01:00
|
|
|
upd := Update(r.tableName).Where(Eq{"id": id}).Set("last_access_at", now)
|
|
|
|
_, err := r.executeSQL(upd)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *userRepository) Count(options ...rest.QueryOptions) (int64, error) {
|
2020-02-06 04:22:44 +01:00
|
|
|
usr := loggedUser(r.ctx)
|
|
|
|
if !usr.IsAdmin {
|
|
|
|
return 0, rest.ErrPermissionDenied
|
|
|
|
}
|
2020-01-28 14:22:17 +01:00
|
|
|
return r.CountAll(r.parseRestOptions(options...))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *userRepository) Read(id string) (interface{}, error) {
|
2020-02-06 04:22:44 +01:00
|
|
|
usr := loggedUser(r.ctx)
|
|
|
|
if !usr.IsAdmin && usr.ID != id {
|
|
|
|
return nil, rest.ErrPermissionDenied
|
|
|
|
}
|
2020-01-28 14:22:17 +01:00
|
|
|
usr, err := r.Get(id)
|
2022-10-01 00:54:25 +02:00
|
|
|
if errors.Is(err, model.ErrNotFound) {
|
2020-01-28 14:22:17 +01:00
|
|
|
return nil, rest.ErrNotFound
|
|
|
|
}
|
|
|
|
return usr, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *userRepository) ReadAll(options ...rest.QueryOptions) (interface{}, error) {
|
2020-02-06 04:22:44 +01:00
|
|
|
usr := loggedUser(r.ctx)
|
|
|
|
if !usr.IsAdmin {
|
|
|
|
return nil, rest.ErrPermissionDenied
|
|
|
|
}
|
2020-01-28 14:22:17 +01:00
|
|
|
return r.GetAll(r.parseRestOptions(options...))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *userRepository) EntityName() string {
|
|
|
|
return "user"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *userRepository) NewInstance() interface{} {
|
|
|
|
return &model.User{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *userRepository) Save(entity interface{}) (string, error) {
|
2020-02-06 04:22:44 +01:00
|
|
|
usr := loggedUser(r.ctx)
|
|
|
|
if !usr.IsAdmin {
|
|
|
|
return "", rest.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
u := entity.(*model.User)
|
2021-05-16 19:25:38 +02:00
|
|
|
if err := validateUsernameUnique(r, u); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2020-02-06 04:22:44 +01:00
|
|
|
err := r.Put(u)
|
2020-01-28 14:22:17 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2020-02-06 04:22:44 +01:00
|
|
|
return u.ID, err
|
2020-01-28 14:22:17 +01:00
|
|
|
}
|
|
|
|
|
2021-11-01 18:55:47 +01:00
|
|
|
func (r *userRepository) Update(id string, entity interface{}, cols ...string) error {
|
2020-02-06 04:22:44 +01:00
|
|
|
u := entity.(*model.User)
|
2021-11-01 18:55:47 +01:00
|
|
|
u.ID = id
|
2020-02-06 04:22:44 +01:00
|
|
|
usr := loggedUser(r.ctx)
|
|
|
|
if !usr.IsAdmin && usr.ID != u.ID {
|
|
|
|
return rest.ErrPermissionDenied
|
|
|
|
}
|
2021-04-29 04:35:25 +02:00
|
|
|
if !usr.IsAdmin {
|
2021-05-02 23:10:56 +02:00
|
|
|
if !conf.Server.EnableUserEditing {
|
|
|
|
return rest.ErrPermissionDenied
|
|
|
|
}
|
2021-04-29 04:35:25 +02:00
|
|
|
u.IsAdmin = false
|
|
|
|
u.UserName = usr.UserName
|
|
|
|
}
|
2021-06-28 22:36:14 +02:00
|
|
|
|
|
|
|
// Decrypt the user's existing password before validating. This is required otherwise the existing password entered by the user will never match.
|
|
|
|
if err := r.decryptPassword(usr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-03 21:03:34 +02:00
|
|
|
if err := validatePasswordChange(u, usr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-16 19:25:38 +02:00
|
|
|
if err := validateUsernameUnique(r, u); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-06 04:22:44 +01:00
|
|
|
err := r.Put(u)
|
2022-10-01 00:54:25 +02:00
|
|
|
if errors.Is(err, model.ErrNotFound) {
|
2020-01-28 14:22:17 +01:00
|
|
|
return rest.ErrNotFound
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-03 21:03:34 +02:00
|
|
|
func validatePasswordChange(newUser *model.User, logged *model.User) error {
|
|
|
|
err := &rest.ValidationError{Errors: map[string]string{}}
|
|
|
|
if logged.IsAdmin && newUser.ID != logged.ID {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if newUser.NewPassword != "" && newUser.CurrentPassword == "" {
|
|
|
|
err.Errors["currentPassword"] = "ra.validation.required"
|
|
|
|
}
|
|
|
|
if newUser.CurrentPassword != "" {
|
|
|
|
if newUser.NewPassword == "" {
|
|
|
|
err.Errors["password"] = "ra.validation.required"
|
|
|
|
}
|
|
|
|
if newUser.CurrentPassword != logged.Password {
|
|
|
|
err.Errors["currentPassword"] = "ra.validation.passwordDoesNotMatch"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(err.Errors) > 0 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-16 19:25:38 +02:00
|
|
|
func validateUsernameUnique(r model.UserRepository, u *model.User) error {
|
|
|
|
usr, err := r.FindByUsername(u.UserName)
|
2022-10-01 00:54:25 +02:00
|
|
|
if errors.Is(err, model.ErrNotFound) {
|
2021-05-16 19:25:38 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if usr.ID != u.ID {
|
|
|
|
return &rest.ValidationError{Errors: map[string]string{"userName": "ra.validation.unique"}}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-28 14:22:17 +01:00
|
|
|
func (r *userRepository) Delete(id string) error {
|
2020-02-06 04:22:44 +01:00
|
|
|
usr := loggedUser(r.ctx)
|
2021-04-29 04:35:25 +02:00
|
|
|
if !usr.IsAdmin {
|
2020-02-06 04:22:44 +01:00
|
|
|
return rest.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
err := r.delete(Eq{"id": id})
|
2022-10-01 00:54:25 +02:00
|
|
|
if errors.Is(err, model.ErrNotFound) {
|
2020-01-28 14:22:17 +01:00
|
|
|
return rest.ErrNotFound
|
|
|
|
}
|
2020-01-20 19:35:59 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-19 00:38:38 +02:00
|
|
|
func keyTo32Bytes(input string) []byte {
|
|
|
|
data := sha256.Sum256([]byte(input))
|
|
|
|
return data[0:]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *userRepository) initPasswordEncryptionKey() error {
|
|
|
|
encKey = keyTo32Bytes(consts.DefaultEncryptionKey)
|
|
|
|
if conf.Server.PasswordEncryptionKey == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
key := keyTo32Bytes(conf.Server.PasswordEncryptionKey)
|
|
|
|
keySum := fmt.Sprintf("%x", sha256.Sum256(key))
|
|
|
|
|
|
|
|
props := NewPropertyRepository(r.ctx, r.ormer)
|
|
|
|
savedKeySum, err := props.Get(consts.PasswordsEncryptedKey)
|
|
|
|
|
|
|
|
// If passwords are already encrypted
|
|
|
|
if err == nil {
|
|
|
|
if savedKeySum != keySum {
|
|
|
|
log.Error("Password Encryption Key changed! Users won't be able to login!")
|
|
|
|
return errors.New("passwordEncryptionKey changed")
|
|
|
|
}
|
|
|
|
encKey = key
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// if not, try to re-encrypt all current passwords with new encryption key,
|
|
|
|
// assuming they were encrypted with the DefaultEncryptionKey
|
|
|
|
sql := r.newSelect().Columns("id", "user_name", "password")
|
|
|
|
users := model.Users{}
|
|
|
|
err = r.queryAll(sql, &users)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Could not encrypt all passwords", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Warn("New PasswordEncryptionKey set. Encrypting all passwords", "numUsers", len(users))
|
|
|
|
if err = r.decryptAllPasswords(users); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
encKey = key
|
|
|
|
for i := range users {
|
|
|
|
u := users[i]
|
|
|
|
u.NewPassword = u.Password
|
|
|
|
if err := r.encryptPassword(&u); err == nil {
|
|
|
|
upd := Update(r.tableName).Set("password", u.NewPassword).Where(Eq{"id": u.ID})
|
|
|
|
_, err = r.executeSQL(upd)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Password NOT encrypted! This may cause problems!", "user", u.UserName, "id", u.ID, err)
|
|
|
|
} else {
|
|
|
|
log.Warn("Password encrypted successfully", "user", u.UserName, "id", u.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = props.Put(consts.PasswordsEncryptedKey, keySum)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Could not flag passwords as encrypted. It will cause login errors", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// encrypts u.NewPassword
|
|
|
|
func (r *userRepository) encryptPassword(u *model.User) error {
|
|
|
|
encPassword, err := utils.Encrypt(r.ctx, encKey, u.NewPassword)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(r.ctx, "Error encrypting user's password", "user", u.UserName, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
u.NewPassword = encPassword
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// decrypts u.Password
|
|
|
|
func (r *userRepository) decryptPassword(u *model.User) error {
|
|
|
|
plaintext, err := utils.Decrypt(r.ctx, encKey, u.Password)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(r.ctx, "Error decrypting user's password", "user", u.UserName, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
u.Password = plaintext
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *userRepository) decryptAllPasswords(users model.Users) error {
|
|
|
|
for i := range users {
|
|
|
|
if err := r.decryptPassword(&users[i]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-20 04:36:15 +01:00
|
|
|
var _ model.UserRepository = (*userRepository)(nil)
|
2020-01-28 14:22:17 +01:00
|
|
|
var _ rest.Repository = (*userRepository)(nil)
|
|
|
|
var _ rest.Persistable = (*userRepository)(nil)
|