2024-01-16 05:19:36 +00:00
|
|
|
package account
|
|
|
|
|
2024-01-16 18:30:36 +00:00
|
|
|
import (
|
2024-03-03 11:16:01 +00:00
|
|
|
"context"
|
2024-01-28 07:20:59 +00:00
|
|
|
"crypto/ed25519"
|
2024-02-26 13:14:30 +00:00
|
|
|
"crypto/rand"
|
2024-02-14 00:07:24 +00:00
|
|
|
"errors"
|
2024-02-14 05:24:22 +00:00
|
|
|
"time"
|
|
|
|
|
2024-03-03 11:16:01 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/portal/metadata"
|
|
|
|
|
2024-02-26 13:14:30 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/portal/mailer"
|
|
|
|
|
2024-02-25 14:48:26 +00:00
|
|
|
"gorm.io/gorm/clause"
|
|
|
|
|
2024-02-22 08:41:28 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/portal/config"
|
|
|
|
|
2024-01-16 18:30:36 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/portal/db/models"
|
2024-01-28 07:20:59 +00:00
|
|
|
"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-01-16 18:30:36 +00:00
|
|
|
)
|
2024-01-16 05:19:36 +00:00
|
|
|
|
2024-02-14 04:23:45 +00:00
|
|
|
var (
|
|
|
|
ErrInvalidOTPCode = errors.New("Invalid OTP code")
|
|
|
|
)
|
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
type AccountServiceParams struct {
|
|
|
|
fx.In
|
|
|
|
Db *gorm.DB
|
2024-02-22 08:41:28 +00:00
|
|
|
Config *config.Manager
|
2024-01-28 07:20:59 +00:00
|
|
|
Identity ed25519.PrivateKey
|
2024-02-26 13:14:30 +00:00
|
|
|
Mailer *mailer.Mailer
|
2024-03-03 11:16:01 +00:00
|
|
|
Metadata metadata.MetadataService
|
2024-01-28 07:20:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2024-02-22 08:41:28 +00:00
|
|
|
config *config.Manager
|
2024-01-28 07:20:59 +00:00
|
|
|
identity ed25519.PrivateKey
|
2024-02-26 13:14:30 +00:00
|
|
|
mailer *mailer.Mailer
|
2024-03-03 11:16:01 +00:00
|
|
|
metadata metadata.MetadataService
|
2024-01-16 05:19:36 +00:00
|
|
|
}
|
|
|
|
|
2024-02-01 01:29:27 +00:00
|
|
|
func NewAccountService(params AccountServiceParams) *AccountServiceDefault {
|
2024-03-03 11:16:01 +00:00
|
|
|
return &AccountServiceDefault{db: params.Db, config: params.Config, identity: params.Identity, mailer: params.Mailer, metadata: params.Metadata}
|
2024-01-16 05:19:36 +00:00
|
|
|
}
|
2024-01-16 18:30:36 +00:00
|
|
|
|
2024-02-17 13:14:58 +00:00
|
|
|
func (s *AccountServiceDefault) EmailExists(email string) (bool, *models.User, error) {
|
2024-02-14 00:28:04 +00:00
|
|
|
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-17 13:14:58 +00:00
|
|
|
func (s *AccountServiceDefault) PubkeyExists(pubkey string) (bool, *models.PublicKey, error) {
|
2024-02-14 00:28:04 +00:00
|
|
|
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-17 13:14:58 +00:00
|
|
|
func (s *AccountServiceDefault) AccountExists(id uint) (bool, *models.User, error) {
|
2024-02-14 00:28:04 +00:00
|
|
|
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-17 13:14:58 +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 {
|
2024-02-14 05:24:22 +00:00
|
|
|
return "", NewAccountError(ErrKeyHashingFailed, err)
|
2024-02-14 00:36:23 +00:00
|
|
|
}
|
|
|
|
return string(bytes), nil
|
|
|
|
}
|
|
|
|
|
2024-02-26 13:15:10 +00:00
|
|
|
func (s *AccountServiceDefault) CreateAccount(email string, password string, verifyEmail bool) (*models.User, error) {
|
2024-02-14 00:36:23 +00:00
|
|
|
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 {
|
2024-03-13 22:53:45 +00:00
|
|
|
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
|
|
|
|
return nil, NewAccountError(ErrKeyEmailAlreadyExists, nil)
|
|
|
|
}
|
2024-02-14 05:24:22 +00:00
|
|
|
return nil, NewAccountError(ErrKeyAccountCreationFailed, result.Error)
|
2024-01-16 18:30:36 +00:00
|
|
|
}
|
|
|
|
|
2024-02-26 13:15:10 +00:00
|
|
|
if verifyEmail {
|
|
|
|
err = s.SendEmailVerification(&user)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-16 18:30:36 +00:00
|
|
|
return &user, nil
|
|
|
|
}
|
2024-02-14 00:07:24 +00:00
|
|
|
|
2024-02-26 13:14:30 +00:00
|
|
|
func (s *AccountServiceDefault) SendEmailVerification(user *models.User) error {
|
|
|
|
token := GenerateSecurityToken()
|
|
|
|
|
|
|
|
var verification models.EmailVerification
|
|
|
|
|
|
|
|
verification.UserID = user.ID
|
|
|
|
verification.Token = token
|
|
|
|
verification.ExpiresAt = time.Now().Add(time.Hour)
|
|
|
|
|
|
|
|
err := s.db.Create(&verification).Error
|
|
|
|
if err != nil {
|
|
|
|
return NewAccountError(ErrKeyDatabaseOperationFailed, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
vars := map[string]interface{}{
|
|
|
|
"FirstName": user.FirstName,
|
|
|
|
"Email": user.Email,
|
|
|
|
"VerificationCode": token,
|
|
|
|
"ExpireTime": verification.ExpiresAt,
|
|
|
|
"PortalName": s.config.Config().Core.PortalName,
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.mailer.TemplateSend(mailer.TPL_VERIFY_EMAIL, vars, vars, user.Email)
|
|
|
|
}
|
|
|
|
|
2024-02-26 15:55:26 +00:00
|
|
|
func (s AccountServiceDefault) SendPasswordReset(user *models.User) error {
|
|
|
|
token := GenerateSecurityToken()
|
|
|
|
|
|
|
|
var reset models.PasswordReset
|
|
|
|
|
|
|
|
reset.UserID = user.ID
|
|
|
|
reset.Token = token
|
|
|
|
reset.ExpiresAt = time.Now().Add(time.Hour)
|
|
|
|
|
|
|
|
err := s.db.Create(&reset).Error
|
|
|
|
if err != nil {
|
|
|
|
return NewAccountError(ErrKeyDatabaseOperationFailed, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
vars := map[string]interface{}{
|
|
|
|
"FirstName": user.FirstName,
|
|
|
|
"Email": user.Email,
|
|
|
|
"ResetCode": token,
|
|
|
|
"ExpireTime": reset.ExpiresAt,
|
|
|
|
"PortalName": s.config.Config().Core.PortalName,
|
|
|
|
"PortalDomain": s.config.Config().Core.Domain,
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.mailer.TemplateSend(mailer.TPL_PASSWORD_RESET, vars, vars, user.Email)
|
|
|
|
}
|
|
|
|
|
2024-02-26 13:14:30 +00:00
|
|
|
func (s AccountServiceDefault) VerifyEmail(email string, token string) error {
|
|
|
|
var verification models.EmailVerification
|
|
|
|
|
|
|
|
verification.Token = token
|
|
|
|
|
|
|
|
result := s.db.Model(&verification).
|
|
|
|
Preload("User").
|
|
|
|
Where(&verification).
|
|
|
|
First(&verification)
|
|
|
|
|
|
|
|
if result.Error != nil {
|
|
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
|
|
|
return NewAccountError(ErrKeyUserNotFound, result.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewAccountError(ErrKeyDatabaseOperationFailed, result.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
if verification.ExpiresAt.Before(time.Now()) {
|
|
|
|
return NewAccountError(ErrKeySecurityTokenExpired, nil)
|
|
|
|
}
|
|
|
|
|
2024-02-26 15:35:57 +00:00
|
|
|
if len(verification.NewEmail) > 0 && verification.NewEmail != email {
|
|
|
|
return NewAccountError(ErrKeySecurityInvalidToken, nil)
|
|
|
|
} else if verification.User.Email != email {
|
2024-02-26 13:14:30 +00:00
|
|
|
return NewAccountError(ErrKeySecurityInvalidToken, nil)
|
|
|
|
}
|
|
|
|
|
2024-02-26 15:35:57 +00:00
|
|
|
var update models.User
|
|
|
|
|
|
|
|
doUpdate := false
|
|
|
|
|
2024-02-26 15:30:22 +00:00
|
|
|
if !verification.User.Verified {
|
2024-02-26 15:35:57 +00:00
|
|
|
update.Verified = true
|
|
|
|
doUpdate = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(verification.NewEmail) > 0 {
|
|
|
|
update.Email = verification.NewEmail
|
|
|
|
doUpdate = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if doUpdate {
|
|
|
|
err := s.updateAccountInfo(verification.UserID, update)
|
2024-02-26 15:30:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-02-26 13:14:30 +00:00
|
|
|
}
|
|
|
|
|
2024-02-26 15:39:09 +00:00
|
|
|
verification = models.EmailVerification{
|
|
|
|
UserID: verification.UserID,
|
|
|
|
}
|
|
|
|
|
|
|
|
if result := s.db.Where(&verification).Delete(&verification); result.Error != nil {
|
|
|
|
return NewAccountError(ErrKeyDatabaseOperationFailed, result.Error)
|
|
|
|
}
|
2024-02-26 13:14:30 +00:00
|
|
|
|
2024-02-26 15:39:09 +00:00
|
|
|
return nil
|
2024-02-26 13:14:30 +00:00
|
|
|
}
|
|
|
|
|
2024-02-26 15:55:26 +00:00
|
|
|
func (s AccountServiceDefault) ResetPassword(email string, token string, password string) error {
|
|
|
|
var reset models.PasswordReset
|
|
|
|
|
|
|
|
reset.Token = token
|
|
|
|
|
|
|
|
result := s.db.Model(&reset).
|
|
|
|
Preload("User").
|
|
|
|
Where(&reset).
|
|
|
|
First(&reset)
|
|
|
|
|
|
|
|
if result.Error != nil {
|
|
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
|
|
|
return NewAccountError(ErrKeyUserNotFound, result.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewAccountError(ErrKeyDatabaseOperationFailed, result.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
if reset.ExpiresAt.Before(time.Now()) {
|
|
|
|
return NewAccountError(ErrKeySecurityTokenExpired, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
if reset.User.Email != email {
|
|
|
|
return NewAccountError(ErrKeySecurityInvalidToken, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
passwordHash, err := s.HashPassword(password)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = s.updateAccountInfo(reset.UserID, models.User{PasswordHash: passwordHash})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
reset = models.PasswordReset{
|
|
|
|
UserID: reset.UserID,
|
|
|
|
}
|
|
|
|
|
|
|
|
if result := s.db.Where(&reset).Delete(&reset); result.Error != nil {
|
|
|
|
return NewAccountError(ErrKeyDatabaseOperationFailed, result.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-17 13:14:58 +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})
|
|
|
|
}
|
|
|
|
|
2024-02-17 13:14:58 +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 {
|
2024-02-14 05:24:22 +00:00
|
|
|
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
|
|
|
|
return NewAccountError(ErrKeyPublicKeyExists, result.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewAccountError(ErrKeyDatabaseOperationFailed, result.Error)
|
2024-01-16 18:30:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-02-17 13:14:58 +00:00
|
|
|
func (s AccountServiceDefault) LoginPassword(email string, password string, ip string) (string, *models.User, error) {
|
2024-02-14 04:23:45 +00:00
|
|
|
valid, user, err := s.ValidLoginByEmail(email, password)
|
2024-02-14 00:07:24 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !valid {
|
|
|
|
return "", nil, nil
|
|
|
|
}
|
|
|
|
|
2024-02-26 13:26:59 +00:00
|
|
|
token, err := s.doLogin(user, ip, false)
|
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
|
|
|
}
|
|
|
|
|
2024-02-17 13:14:58 +00:00
|
|
|
func (s AccountServiceDefault) LoginOTP(userId uint, code string) (string, error) {
|
2024-02-14 04:23:45 +00:00
|
|
|
valid, err := s.OTPVerify(userId, code)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !valid {
|
2024-02-14 05:24:22 +00:00
|
|
|
return "", NewAccountError(ErrKeyInvalidOTPCode, nil)
|
2024-02-14 04:23:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var user models.User
|
|
|
|
user.ID = userId
|
|
|
|
|
2024-02-22 08:41:28 +00:00
|
|
|
token, tokenErr := JWTGenerateToken(s.config.Config().Core.Domain, s.identity, user.ID, JWTPurposeLogin)
|
2024-02-14 05:24:22 +00:00
|
|
|
if tokenErr != nil {
|
2024-02-14 04:23:45 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return token, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s AccountServiceDefault) ValidLoginByUserObj(user *models.User, password string) bool {
|
|
|
|
return s.validPassword(user, password)
|
|
|
|
}
|
|
|
|
|
2024-02-17 13:14:58 +00:00
|
|
|
func (s AccountServiceDefault) ValidLoginByEmail(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 05:24:22 +00:00
|
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
|
|
|
return false, nil, NewAccountError(ErrKeyInvalidLogin, result.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil, NewAccountError(ErrKeyDatabaseOperationFailed, result.Error)
|
2024-01-16 18:30:36 +00:00
|
|
|
}
|
|
|
|
|
2024-02-14 04:23:45 +00:00
|
|
|
valid := s.ValidLoginByUserObj(&user, password)
|
|
|
|
|
|
|
|
if !valid {
|
|
|
|
return false, nil, nil
|
2024-01-16 18:30:36 +00:00
|
|
|
}
|
|
|
|
|
2024-03-13 18:09:17 +00:00
|
|
|
return true, &user, nil
|
2024-01-16 18:30:36 +00:00
|
|
|
}
|
|
|
|
|
2024-02-17 13:14:58 +00:00
|
|
|
func (s AccountServiceDefault) ValidLoginByUserID(id uint, password string) (bool, *models.User, error) {
|
2024-02-14 04:23:45 +00:00
|
|
|
var user models.User
|
|
|
|
|
|
|
|
user.ID = id
|
|
|
|
|
|
|
|
result := s.db.Model(&user).Where(&user).First(&user)
|
|
|
|
|
|
|
|
if result.RowsAffected == 0 || result.Error != nil {
|
2024-02-14 05:24:22 +00:00
|
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
|
|
|
return false, nil, NewAccountError(ErrKeyInvalidLogin, result.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil, NewAccountError(ErrKeyDatabaseOperationFailed, result.Error)
|
2024-02-14 04:23:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
valid := s.ValidLoginByUserObj(&user, password)
|
|
|
|
|
|
|
|
if !valid {
|
|
|
|
return false, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, &user, nil
|
|
|
|
}
|
|
|
|
|
2024-02-26 13:28:22 +00:00
|
|
|
func (s AccountServiceDefault) LoginPubkey(pubkey string, ip 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 {
|
2024-02-14 05:24:22 +00:00
|
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
|
|
|
return "", NewAccountError(ErrKeyInvalidLogin, result.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", NewAccountError(ErrKeyDatabaseOperationFailed, result.Error)
|
2024-01-16 18:30:36 +00:00
|
|
|
}
|
|
|
|
|
2024-02-14 00:57:41 +00:00
|
|
|
user := model.User
|
|
|
|
|
2024-02-26 13:28:22 +00:00
|
|
|
token, err := s.doLogin(&user, ip, true)
|
2024-02-14 00:57:41 +00:00
|
|
|
|
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-17 13:14:58 +00:00
|
|
|
func (s AccountServiceDefault) AccountPins(id uint, 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
|
2024-02-14 04:29:36 +00:00
|
|
|
Where(&models.Pin{UserID: id}).
|
2024-01-24 17:47:20 +00:00
|
|
|
Where("created_at > ?", createdAfter).
|
|
|
|
Order("created_at desc").
|
|
|
|
Find(&pins)
|
2024-01-17 17:32:50 +00:00
|
|
|
|
|
|
|
if result.Error != nil {
|
2024-02-14 05:24:22 +00:00
|
|
|
return nil, NewAccountError(ErrKeyPinsRetrievalFailed, result.Error)
|
2024-01-17 17:32:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return pins, nil
|
|
|
|
}
|
2024-01-17 18:03:52 +00:00
|
|
|
|
2024-02-18 04:06:27 +00:00
|
|
|
func (s AccountServiceDefault) DeletePinByHash(hash []byte, userId 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
|
2024-02-14 04:29:36 +00:00
|
|
|
pinQuery := models.Pin{UploadID: uploadID, UserID: userId}
|
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-17 02:57:20 +00:00
|
|
|
func (s AccountServiceDefault) PinByHash(hash []byte, userId uint) error {
|
2024-01-17 18:13:37 +00:00
|
|
|
// Define a struct for the query condition
|
2024-02-18 04:06:27 +00:00
|
|
|
uploadQuery := models.Upload{Hash: hash}
|
2024-01-17 18:13:37 +00:00
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
result := s.db.
|
2024-02-18 08:21:52 +00:00
|
|
|
Model(&uploadQuery).
|
2024-01-17 18:13:37 +00:00
|
|
|
Where(&uploadQuery).
|
2024-02-18 08:21:52 +00:00
|
|
|
First(&uploadQuery)
|
2024-01-17 18:13:37 +00:00
|
|
|
|
|
|
|
if result.Error != nil {
|
|
|
|
return result.Error
|
|
|
|
}
|
|
|
|
|
2024-02-18 08:21:52 +00:00
|
|
|
return s.PinByID(uploadQuery.ID, userId)
|
2024-01-17 18:13:37 +00:00
|
|
|
}
|
2024-01-17 22:14:45 +00:00
|
|
|
|
2024-02-14 04:31:08 +00:00
|
|
|
func (s AccountServiceDefault) PinByID(uploadId uint, userId uint) error {
|
|
|
|
result := s.db.Model(&models.Pin{}).Where(&models.Pin{UploadID: uploadId, UserID: userId}).First(&models.Pin{})
|
2024-01-17 22:18:58 +00:00
|
|
|
|
2024-02-18 08:24:06 +00:00
|
|
|
if result.Error != nil && !errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
2024-01-17 22:18:58 +00:00
|
|
|
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
|
2024-02-14 04:31:08 +00:00
|
|
|
pinQuery := models.Pin{UploadID: uploadId, UserID: userId}
|
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-17 13:14:58 +00:00
|
|
|
func (s AccountServiceDefault) OTPGenerate(userId uint) (string, error) {
|
2024-02-14 04:23:45 +00:00
|
|
|
exists, user, err := s.AccountExists(userId)
|
|
|
|
|
|
|
|
if !exists || err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2024-02-22 08:41:28 +00:00
|
|
|
otp, otpErr := TOTPGenerate(user.Email, s.config.Config().Core.Domain)
|
2024-02-14 05:24:22 +00:00
|
|
|
if otpErr != nil {
|
|
|
|
return "", NewAccountError(ErrKeyOTPGenerationFailed, otpErr)
|
2024-02-14 04:23:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = s.updateAccountInfo(user.ID, models.User{OTPSecret: otp})
|
|
|
|
return otp, nil
|
|
|
|
}
|
|
|
|
|
2024-02-17 13:14:58 +00:00
|
|
|
func (s AccountServiceDefault) OTPVerify(userId uint, code string) (bool, error) {
|
2024-02-14 04:23:45 +00:00
|
|
|
exists, user, err := s.AccountExists(userId)
|
|
|
|
|
|
|
|
if !exists || err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
valid := TOTPValidate(user.OTPSecret, code)
|
|
|
|
if !valid {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s AccountServiceDefault) OTPEnable(userId uint, code string) error {
|
|
|
|
verify, err := s.OTPVerify(userId, code)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !verify {
|
|
|
|
return ErrInvalidOTPCode
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.updateAccountInfo(userId, models.User{OTPEnabled: true})
|
|
|
|
}
|
|
|
|
|
2024-02-17 13:14:58 +00:00
|
|
|
func (s AccountServiceDefault) OTPDisable(userId uint) error {
|
2024-02-14 04:23:45 +00:00
|
|
|
return s.updateAccountInfo(userId, models.User{OTPEnabled: false, OTPSecret: ""})
|
|
|
|
}
|
|
|
|
|
2024-02-25 14:47:40 +00:00
|
|
|
func (s AccountServiceDefault) DNSLinkExists(hash []byte) (bool, *models.DNSLink, error) {
|
2024-03-03 11:16:01 +00:00
|
|
|
upload, err := s.metadata.GetUpload(context.Background(), hash)
|
|
|
|
if err != nil {
|
|
|
|
return false, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
exists, model, err := s.exists(&models.DNSLink{}, map[string]interface{}{"upload_id": upload.ID})
|
2024-02-25 14:47:40 +00:00
|
|
|
if !exists || err != nil {
|
|
|
|
return false, nil, err
|
|
|
|
}
|
2024-03-03 11:16:01 +00:00
|
|
|
|
|
|
|
pinned, err := s.UploadPinned(hash)
|
|
|
|
if err != nil {
|
|
|
|
return false, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !pinned {
|
|
|
|
return false, nil, nil
|
|
|
|
}
|
|
|
|
|
2024-02-25 14:47:40 +00:00
|
|
|
return true, model.(*models.DNSLink), nil
|
|
|
|
}
|
|
|
|
|
2024-03-03 11:16:01 +00:00
|
|
|
func (s AccountServiceDefault) UploadPinned(hash []byte) (bool, error) {
|
|
|
|
upload, err := s.metadata.GetUpload(context.Background(), hash)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var pin models.Pin
|
|
|
|
result := s.db.Model(&models.Pin{}).Where(&models.Pin{UploadID: upload.ID}).First(&pin)
|
|
|
|
|
|
|
|
if result.Error != nil {
|
|
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, result.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2024-02-26 13:14:30 +00:00
|
|
|
func GenerateSecurityToken() string {
|
|
|
|
const charset = "abcdefghijklmnopqrstuvwxyz0123456789"
|
|
|
|
b := make([]byte, 6)
|
|
|
|
_, err := rand.Read(b)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
for i := 0; i < 6; i++ {
|
|
|
|
b[i] = charset[b[i]%byte(len(charset))]
|
|
|
|
}
|
|
|
|
return string(b)
|
|
|
|
}
|
|
|
|
|
2024-02-26 13:26:59 +00:00
|
|
|
func (s AccountServiceDefault) doLogin(user *models.User, ip string, bypassSecurity bool) (string, error) {
|
2024-02-14 04:23:45 +00:00
|
|
|
purpose := JWTPurposeLogin
|
|
|
|
|
2024-02-26 13:26:59 +00:00
|
|
|
if user.OTPEnabled && !bypassSecurity {
|
2024-02-14 04:23:45 +00:00
|
|
|
purpose = JWTPurpose2FA
|
|
|
|
}
|
|
|
|
|
2024-02-22 08:41:28 +00:00
|
|
|
token, jwtErr := JWTGenerateToken(s.config.Config().Core.Domain, s.identity, user.ID, purpose)
|
2024-02-14 05:24:22 +00:00
|
|
|
if jwtErr != nil {
|
|
|
|
return "", NewAccountError(ErrKeyJWTGenerationFailed, jwtErr)
|
2024-02-14 00:57:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
|
2024-02-14 05:24:22 +00:00
|
|
|
err := s.updateAccountInfo(user.ID, models.User{LastLoginIP: ip, LastLogin: &now})
|
2024-02-14 00:57:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return token, nil
|
|
|
|
}
|
2024-02-14 01:00:16 +00:00
|
|
|
|
2024-02-17 14:02:04 +00:00
|
|
|
func (s AccountServiceDefault) updateAccountInfo(userId uint, info models.User) error {
|
2024-02-14 01:00:16 +00:00
|
|
|
var user models.User
|
|
|
|
|
|
|
|
user.ID = userId
|
|
|
|
|
|
|
|
result := s.db.Model(&models.User{}).Where(&user).Updates(info)
|
|
|
|
|
|
|
|
if result.Error != nil {
|
2024-02-14 05:24:22 +00:00
|
|
|
return NewAccountError(ErrKeyDatabaseOperationFailed, result.Error)
|
2024-02-14 01:00:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-17 13:14:58 +00:00
|
|
|
func (s AccountServiceDefault) exists(model interface{}, conditions map[string]interface{}) (bool, interface{}, error) {
|
2024-02-14 00:57:41 +00:00
|
|
|
// Conduct a query with the provided model and conditions
|
2024-02-25 14:48:26 +00:00
|
|
|
result := s.db.Preload(clause.Associations).Model(model).Where(conditions).First(model)
|
2024-02-14 00:57:41 +00:00
|
|
|
|
|
|
|
// Check if any rows were found
|
|
|
|
exists := result.RowsAffected > 0
|
|
|
|
|
|
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
|
|
|
return false, nil, nil
|
|
|
|
}
|
|
|
|
|
2024-02-18 01:19:00 +00:00
|
|
|
if exists {
|
|
|
|
return true, model, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, model, NewAccountError(ErrKeyDatabaseOperationFailed, result.Error)
|
2024-02-14 00:57:41 +00:00
|
|
|
}
|
2024-02-14 04:23:45 +00:00
|
|
|
|
|
|
|
func (s AccountServiceDefault) validPassword(user *models.User, password string) bool {
|
|
|
|
err := bcrypt.CompareHashAndPassword([]byte(user.PasswordHash), []byte(password))
|
|
|
|
|
|
|
|
return err == nil
|
|
|
|
}
|