2024-01-06 17:56:32 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
2024-01-28 08:17:38 +00:00
|
|
|
"fmt"
|
2024-05-17 09:13:11 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
"gorm.io/datatypes"
|
2024-03-20 12:56:43 +00:00
|
|
|
"time"
|
|
|
|
|
2024-06-17 14:21:34 +00:00
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
|
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
|
2024-01-29 08:11:59 +00:00
|
|
|
"github.com/google/uuid"
|
2024-01-28 16:32:39 +00:00
|
|
|
"github.com/samber/lo"
|
|
|
|
"gorm.io/gorm"
|
2024-01-06 17:56:32 +00:00
|
|
|
)
|
|
|
|
|
2024-01-07 07:52:23 +00:00
|
|
|
func GetAccount(id uint) (models.Account, error) {
|
|
|
|
var account models.Account
|
|
|
|
if err := database.C.Where(models.Account{
|
|
|
|
BaseModel: models.BaseModel{ID: id},
|
|
|
|
}).First(&account).Error; err != nil {
|
|
|
|
return account, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return account, nil
|
|
|
|
}
|
|
|
|
|
2024-04-20 11:04:33 +00:00
|
|
|
func LookupAccount(probe string) (models.Account, error) {
|
2024-01-06 17:56:32 +00:00
|
|
|
var account models.Account
|
2024-04-20 11:04:33 +00:00
|
|
|
if err := database.C.Where(models.Account{Name: probe}).First(&account).Error; err == nil {
|
2024-01-06 17:56:32 +00:00
|
|
|
return account, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var contact models.AccountContact
|
2024-04-20 11:04:33 +00:00
|
|
|
if err := database.C.Where(models.AccountContact{Content: probe}).First(&contact).Error; err == nil {
|
2024-01-06 17:56:32 +00:00
|
|
|
if err := database.C.
|
|
|
|
Where(models.Account{
|
|
|
|
BaseModel: models.BaseModel{ID: contact.AccountID},
|
2024-01-27 16:05:19 +00:00
|
|
|
}).First(&account).Error; err == nil {
|
2024-01-06 17:56:32 +00:00
|
|
|
return account, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return account, fmt.Errorf("account was not found")
|
|
|
|
}
|
2024-01-28 08:17:38 +00:00
|
|
|
|
|
|
|
func CreateAccount(name, nick, email, password string) (models.Account, error) {
|
|
|
|
user := models.Account{
|
2024-02-18 07:51:27 +00:00
|
|
|
Name: name,
|
|
|
|
Nick: nick,
|
2024-01-28 08:17:38 +00:00
|
|
|
Profile: models.AccountProfile{
|
|
|
|
Experience: 100,
|
|
|
|
},
|
|
|
|
Factors: []models.AuthFactor{
|
|
|
|
{
|
|
|
|
Type: models.PasswordAuthFactor,
|
2024-04-20 11:04:33 +00:00
|
|
|
Secret: HashPassword(password),
|
2024-01-28 08:17:38 +00:00
|
|
|
},
|
2024-01-29 08:11:59 +00:00
|
|
|
{
|
|
|
|
Type: models.EmailPasswordFactor,
|
|
|
|
Secret: uuid.NewString()[:8],
|
|
|
|
},
|
2024-01-28 08:17:38 +00:00
|
|
|
},
|
|
|
|
Contacts: []models.AccountContact{
|
|
|
|
{
|
|
|
|
Type: models.EmailAccountContact,
|
|
|
|
Content: email,
|
2024-01-29 08:11:59 +00:00
|
|
|
IsPrimary: true,
|
2024-01-28 08:17:38 +00:00
|
|
|
VerifiedAt: nil,
|
|
|
|
},
|
|
|
|
},
|
2024-05-17 09:13:11 +00:00
|
|
|
PermNodes: datatypes.JSONMap(viper.GetStringMap("permissions.default")),
|
2024-01-28 08:17:38 +00:00
|
|
|
ConfirmedAt: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := database.C.Create(&user).Error; err != nil {
|
|
|
|
return user, err
|
|
|
|
}
|
2024-01-28 16:32:39 +00:00
|
|
|
|
|
|
|
if tk, err := NewMagicToken(models.ConfirmMagicToken, &user, nil); err != nil {
|
|
|
|
return user, err
|
|
|
|
} else if err := NotifyMagicToken(tk); err != nil {
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ConfirmAccount(code string) error {
|
2024-01-29 08:11:59 +00:00
|
|
|
token, err := ValidateMagicToken(code, models.ConfirmMagicToken)
|
|
|
|
if err != nil {
|
2024-01-28 16:32:39 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var user models.Account
|
|
|
|
if err := database.C.Where(&models.Account{
|
|
|
|
BaseModel: models.BaseModel{ID: *token.AssignTo},
|
|
|
|
}).First(&user).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return database.C.Transaction(func(tx *gorm.DB) error {
|
|
|
|
user.ConfirmedAt = lo.ToPtr(time.Now())
|
2024-05-17 09:13:11 +00:00
|
|
|
|
|
|
|
for k, v := range viper.GetStringMap("permissions.verified") {
|
|
|
|
if val, ok := user.PermNodes[k]; !ok {
|
|
|
|
user.PermNodes[k] = v
|
2024-05-17 11:24:14 +00:00
|
|
|
} else if !ComparePermNode(val, v) {
|
2024-05-17 09:13:11 +00:00
|
|
|
user.PermNodes[k] = v
|
|
|
|
}
|
|
|
|
}
|
2024-01-28 16:32:39 +00:00
|
|
|
|
|
|
|
if err := database.C.Delete(&token).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := database.C.Save(&user).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-17 11:37:58 +00:00
|
|
|
InvalidAuthCacheWithUser(user.ID)
|
|
|
|
|
2024-01-28 16:32:39 +00:00
|
|
|
return nil
|
|
|
|
})
|
2024-01-28 08:17:38 +00:00
|
|
|
}
|