2024-01-16 05:19:36 +00:00
|
|
|
package account
|
|
|
|
|
2024-01-16 18:30:36 +00:00
|
|
|
import (
|
2024-01-28 07:20:59 +00:00
|
|
|
"crypto/ed25519"
|
2024-02-14 00:07:24 +00:00
|
|
|
"errors"
|
2024-01-16 18:30:36 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/portal/db/models"
|
2024-01-28 07:20:59 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
"go.uber.org/fx"
|
2024-01-16 18:30:36 +00:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
2024-01-17 18:03:52 +00:00
|
|
|
"gorm.io/gorm"
|
2024-02-14 00:52:18 +00:00
|
|
|
"time"
|
2024-01-16 18:30:36 +00:00
|
|
|
)
|
2024-01-16 05:19:36 +00:00
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
type AccountServiceParams struct {
|
|
|
|
fx.In
|
|
|
|
Db *gorm.DB
|
|
|
|
Config *viper.Viper
|
|
|
|
Identity ed25519.PrivateKey
|
|
|
|
}
|
|
|
|
|
|
|
|
var Module = fx.Module("account",
|
|
|
|
fx.Options(
|
|
|
|
fx.Provide(NewAccountService),
|
|
|
|
),
|
2024-01-16 05:19:36 +00:00
|
|
|
)
|
|
|
|
|
2024-02-01 01:29:27 +00:00
|
|
|
type AccountServiceDefault struct {
|
2024-01-28 07:20:59 +00:00
|
|
|
db *gorm.DB
|
|
|
|
config *viper.Viper
|
|
|
|
identity ed25519.PrivateKey
|
2024-01-16 05:19:36 +00:00
|
|
|
}
|
|
|
|
|
2024-02-01 01:29:27 +00:00
|
|
|
func NewAccountService(params AccountServiceParams) *AccountServiceDefault {
|
|
|
|
return &AccountServiceDefault{db: params.Db, config: params.Config, identity: params.Identity}
|
2024-01-16 05:19:36 +00:00
|
|
|
}
|
2024-01-16 18:30:36 +00:00
|
|
|
|
2024-02-14 00:28:04 +00:00
|
|
|
func (s *AccountServiceDefault) EmailExists(email string) (bool, *models.User, error) {
|
|
|
|
user := &models.User{}
|
|
|
|
exists, model, err := s.exists(user, map[string]interface{}{"email": email})
|
|
|
|
if !exists || err != nil {
|
|
|
|
return false, nil, err
|
|
|
|
}
|
|
|
|
return true, model.(*models.User), nil // Type assertion since `exists` returns interface{}
|
2024-01-17 13:35:42 +00:00
|
|
|
}
|
2024-02-14 00:07:24 +00:00
|
|
|
|
2024-02-14 00:28:04 +00:00
|
|
|
func (s *AccountServiceDefault) PubkeyExists(pubkey string) (bool, *models.PublicKey, error) {
|
|
|
|
publicKey := &models.PublicKey{}
|
|
|
|
exists, model, err := s.exists(publicKey, map[string]interface{}{"key": pubkey})
|
|
|
|
if !exists || err != nil {
|
|
|
|
return false, nil, err
|
|
|
|
}
|
|
|
|
return true, model.(*models.PublicKey), nil // Type assertion is necessary
|
|
|
|
}
|
2024-02-14 00:07:24 +00:00
|
|
|
|
2024-02-14 00:28:04 +00:00
|
|
|
func (s *AccountServiceDefault) AccountExists(id uint) (bool, *models.User, error) {
|
|
|
|
user := &models.User{}
|
|
|
|
exists, model, err := s.exists(user, map[string]interface{}{"id": id})
|
|
|
|
if !exists || err != nil {
|
|
|
|
return false, nil, err
|
|
|
|
}
|
|
|
|
return true, model.(*models.User), nil // Ensure to assert the type correctly
|
2024-02-14 00:07:24 +00:00
|
|
|
}
|
|
|
|
|
2024-02-14 00:36:23 +00:00
|
|
|
func (s *AccountServiceDefault) HashPassword(password string) (string, error) {
|
2024-01-16 18:30:36 +00:00
|
|
|
bytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
2024-02-14 00:36:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(bytes), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *AccountServiceDefault) CreateAccount(email string, password string) (*models.User, error) {
|
|
|
|
passwordHash, err := s.HashPassword(password)
|
2024-01-16 18:30:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-02-14 00:36:23 +00:00
|
|
|
user := models.User{
|
|
|
|
Email: email,
|
|
|
|
PasswordHash: passwordHash,
|
|
|
|
}
|
2024-01-16 18:30:36 +00:00
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
result := s.db.Create(&user)
|
2024-01-16 18:30:36 +00:00
|
|
|
if result.Error != nil {
|
|
|
|
return nil, result.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
return &user, nil
|
|
|
|
}
|
2024-02-14 00:07:24 +00:00
|
|
|
|
|
|
|
func (s AccountServiceDefault) UpdateAccountName(userId uint, firstName string, lastName string) error {
|
2024-02-14 00:49:08 +00:00
|
|
|
return s.updateAccountInfo(userId, models.User{FirstName: firstName, LastName: lastName})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s AccountServiceDefault) updateAccountInfo(userId uint, info interface{}) error {
|
2024-02-14 00:07:24 +00:00
|
|
|
var user models.User
|
|
|
|
|
|
|
|
user.ID = userId
|
|
|
|
|
2024-02-14 00:49:08 +00:00
|
|
|
result := s.db.Model(&models.User{}).Where(&user).Updates(info)
|
2024-02-14 00:07:24 +00:00
|
|
|
|
|
|
|
if result.Error != nil {
|
|
|
|
return result.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-01 01:29:27 +00:00
|
|
|
func (s AccountServiceDefault) AddPubkeyToAccount(user models.User, pubkey string) error {
|
2024-01-16 18:30:36 +00:00
|
|
|
var model models.PublicKey
|
|
|
|
|
|
|
|
model.Key = pubkey
|
|
|
|
model.UserID = user.ID
|
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
result := s.db.Create(&model)
|
2024-01-16 18:30:36 +00:00
|
|
|
|
|
|
|
if result.Error != nil {
|
|
|
|
return result.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-02-14 00:52:18 +00:00
|
|
|
func (s AccountServiceDefault) LoginPassword(email string, password string, ip string) (string, *models.User, error) {
|
2024-02-14 00:07:24 +00:00
|
|
|
valid, user, err := s.ValidLogin(email, password)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !valid {
|
|
|
|
return "", nil, nil
|
|
|
|
}
|
|
|
|
|
2024-02-14 00:57:41 +00:00
|
|
|
token, err := s.doLogin(user, ip)
|
2024-02-14 00:52:18 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
2024-02-14 00:57:41 +00:00
|
|
|
return token, user, nil
|
2024-02-14 00:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s AccountServiceDefault) ValidLogin(email string, password string) (bool, *models.User, error) {
|
2024-01-16 18:30:36 +00:00
|
|
|
var user models.User
|
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
result := s.db.Model(&models.User{}).Where(&models.User{Email: email}).First(&user)
|
2024-01-16 18:30:36 +00:00
|
|
|
|
|
|
|
if result.RowsAffected == 0 || result.Error != nil {
|
2024-02-14 00:07:24 +00:00
|
|
|
return false, nil, result.Error
|
2024-01-16 18:30:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err := bcrypt.CompareHashAndPassword([]byte(user.PasswordHash), []byte(password))
|
|
|
|
if err != nil {
|
2024-02-14 00:07:24 +00:00
|
|
|
return false, nil, err
|
2024-01-16 18:30:36 +00:00
|
|
|
}
|
|
|
|
|
2024-02-14 00:07:24 +00:00
|
|
|
return true, nil, nil
|
2024-01-16 18:30:36 +00:00
|
|
|
}
|
|
|
|
|
2024-02-01 01:29:27 +00:00
|
|
|
func (s AccountServiceDefault) LoginPubkey(pubkey string) (string, error) {
|
2024-01-16 18:30:36 +00:00
|
|
|
var model models.PublicKey
|
|
|
|
|
2024-02-14 00:57:41 +00:00
|
|
|
result := s.db.Model(&models.PublicKey{}).Preload("User").Where(&models.PublicKey{Key: pubkey}).First(&model)
|
2024-01-16 18:30:36 +00:00
|
|
|
|
|
|
|
if result.RowsAffected == 0 || result.Error != nil {
|
|
|
|
return "", result.Error
|
|
|
|
}
|
|
|
|
|
2024-02-14 00:57:41 +00:00
|
|
|
user := model.User
|
|
|
|
|
|
|
|
token, err := s.doLogin(&user, "")
|
|
|
|
|
2024-01-16 18:30:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return token, nil
|
|
|
|
}
|
2024-01-17 17:32:50 +00:00
|
|
|
|
2024-02-01 01:29:27 +00:00
|
|
|
func (s AccountServiceDefault) AccountPins(id uint64, createdAfter uint64) ([]models.Pin, error) {
|
2024-01-17 17:32:50 +00:00
|
|
|
var pins []models.Pin
|
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
result := s.db.Model(&models.Pin{}).
|
2024-01-24 17:47:20 +00:00
|
|
|
Preload("Upload"). // Preload the related Upload for each Pin
|
|
|
|
Where(&models.Pin{UserID: uint(id)}).
|
|
|
|
Where("created_at > ?", createdAfter).
|
|
|
|
Order("created_at desc").
|
|
|
|
Find(&pins)
|
2024-01-17 17:32:50 +00:00
|
|
|
|
|
|
|
if result.Error != nil {
|
|
|
|
return nil, result.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
return pins, nil
|
|
|
|
}
|
2024-01-17 18:03:52 +00:00
|
|
|
|
2024-02-01 01:29:27 +00:00
|
|
|
func (s AccountServiceDefault) DeletePinByHash(hash string, accountID uint) error {
|
2024-01-17 18:03:52 +00:00
|
|
|
// Define a struct for the query condition
|
|
|
|
uploadQuery := models.Upload{Hash: hash}
|
|
|
|
|
|
|
|
// Retrieve the upload ID for the given hash
|
|
|
|
var uploadID uint
|
2024-01-28 07:20:59 +00:00
|
|
|
result := s.db.
|
2024-01-17 18:03:52 +00:00
|
|
|
Model(&models.Upload{}).
|
|
|
|
Where(&uploadQuery).
|
|
|
|
Select("id").
|
|
|
|
First(&uploadID)
|
|
|
|
|
|
|
|
if result.Error != nil {
|
|
|
|
if result.Error == gorm.ErrRecordNotFound {
|
|
|
|
// No record found, nothing to delete
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return result.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete pins with the retrieved upload ID and matching account ID
|
|
|
|
pinQuery := models.Pin{UploadID: uploadID, UserID: accountID}
|
2024-01-28 07:20:59 +00:00
|
|
|
result = s.db.
|
2024-01-17 18:03:52 +00:00
|
|
|
Where(&pinQuery).
|
|
|
|
Delete(&models.Pin{})
|
|
|
|
|
|
|
|
if result.Error != nil {
|
|
|
|
return result.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-02-01 01:29:27 +00:00
|
|
|
func (s AccountServiceDefault) PinByHash(hash string, accountID uint) error {
|
2024-01-17 18:13:37 +00:00
|
|
|
// Define a struct for the query condition
|
|
|
|
uploadQuery := models.Upload{Hash: hash}
|
|
|
|
|
|
|
|
// Retrieve the upload ID for the given hash
|
|
|
|
var uploadID uint
|
2024-01-28 07:20:59 +00:00
|
|
|
result := s.db.
|
2024-01-17 18:13:37 +00:00
|
|
|
Model(&models.Upload{}).
|
|
|
|
Where(&uploadQuery).
|
|
|
|
First(&uploadID)
|
|
|
|
|
|
|
|
if result.Error != nil {
|
|
|
|
return result.Error
|
|
|
|
}
|
|
|
|
|
2024-01-17 22:19:46 +00:00
|
|
|
return s.PinByID(uploadID, accountID)
|
2024-01-17 18:13:37 +00:00
|
|
|
}
|
2024-01-17 22:14:45 +00:00
|
|
|
|
2024-02-01 01:29:27 +00:00
|
|
|
func (s AccountServiceDefault) PinByID(uploadId uint, accountID uint) error {
|
2024-01-28 07:20:59 +00:00
|
|
|
result := s.db.Model(&models.Pin{}).Where(&models.Pin{UploadID: uploadId, UserID: accountID}).First(&models.Pin{})
|
2024-01-17 22:18:58 +00:00
|
|
|
|
|
|
|
if result.Error != nil && result.Error != gorm.ErrRecordNotFound {
|
|
|
|
return result.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
if result.RowsAffected > 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-17 22:14:45 +00:00
|
|
|
// Create a pin with the retrieved upload ID and matching account ID
|
|
|
|
pinQuery := models.Pin{UploadID: uploadId, UserID: accountID}
|
2024-01-28 07:20:59 +00:00
|
|
|
result = s.db.Create(&pinQuery)
|
2024-01-17 22:14:45 +00:00
|
|
|
|
|
|
|
if result.Error != nil {
|
|
|
|
return result.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-02-14 00:36:23 +00:00
|
|
|
|
2024-02-14 00:57:41 +00:00
|
|
|
func (s AccountServiceDefault) doLogin(user *models.User, ip string) (string, error) {
|
|
|
|
token, err := GenerateToken(s.config.GetString("core.domain"), s.identity, user.ID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
err = s.updateAccountInfo(user.ID, models.User{LastLoginIP: ip, LastLogin: &now})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return token, nil
|
|
|
|
}
|
|
|
|
func (s *AccountServiceDefault) exists(model interface{}, conditions map[string]interface{}) (bool, interface{}, error) {
|
|
|
|
// Conduct a query with the provided model and conditions
|
|
|
|
result := s.db.Model(model).Where(conditions).First(model)
|
|
|
|
|
|
|
|
// Check if any rows were found
|
|
|
|
exists := result.RowsAffected > 0
|
|
|
|
|
|
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
|
|
|
return false, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return exists, model, result.Error
|
|
|
|
}
|
|
|
|
|
2024-02-14 00:36:23 +00:00
|
|
|
func validateName(firstName, lastName string) error {
|
|
|
|
if len(firstName) == 0 || len(lastName) == 0 {
|
|
|
|
return errors.New("first name and last name cannot be empty")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|