diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 52b7edb..9dde2a4 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -4,8 +4,13 @@
-
-
+
+
+
+
+
+
+
@@ -143,7 +148,6 @@
-
@@ -168,7 +172,8 @@
-
+
+
true
diff --git a/pkg/internal/models/tokens.go b/pkg/internal/models/tokens.go
index 9c432a1..760d7b4 100644
--- a/pkg/internal/models/tokens.go
+++ b/pkg/internal/models/tokens.go
@@ -14,6 +14,6 @@ type MagicToken struct {
Code string `json:"code"`
Type int8 `json:"type"`
- AssignTo *uint `json:"assign_to"`
+ AccountID *uint `json:"account_id"`
ExpiredAt *time.Time `json:"expired_at"`
}
diff --git a/pkg/internal/server/api/auth_api.go b/pkg/internal/server/api/auth_api.go
index 9d3763c..dd6f546 100644
--- a/pkg/internal/server/api/auth_api.go
+++ b/pkg/internal/server/api/auth_api.go
@@ -23,6 +23,8 @@ func doAuthenticate(c *fiber.Ctx) error {
user, err := services.LookupAccount(data.Username)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("account was not found: %v", err.Error()))
+ } else if user.ConfirmedAt == nil {
+ return fiber.NewError(fiber.StatusForbidden, "account was not confirmed")
}
ticket, err := services.NewTicket(user, c.IP(), c.Get(fiber.HeaderUserAgent))
diff --git a/pkg/internal/services/accounts.go b/pkg/internal/services/accounts.go
index a84d9f9..a4b23a0 100644
--- a/pkg/internal/services/accounts.go
+++ b/pkg/internal/services/accounts.go
@@ -2,6 +2,7 @@ package services
import (
"fmt"
+ "github.com/rs/zerolog/log"
"github.com/spf13/viper"
"gorm.io/datatypes"
"time"
@@ -93,7 +94,7 @@ func ConfirmAccount(code string) error {
var user models.Account
if err := database.C.Where(&models.Account{
- BaseModel: models.BaseModel{ID: *token.AssignTo},
+ BaseModel: models.BaseModel{ID: *token.AccountID},
}).First(&user).Error; err != nil {
return err
}
@@ -121,3 +122,49 @@ func ConfirmAccount(code string) error {
return nil
})
}
+
+func DeleteAccount(id uint) error {
+ tx := database.C.Begin()
+
+ for _, model := range []any{
+ &models.Badge{},
+ &models.RealmMember{},
+ &models.AccountContact{},
+ &models.AuthFactor{},
+ &models.AuthTicket{},
+ &models.MagicToken{},
+ &models.ThirdClient{},
+ &models.Notification{},
+ &models.NotificationSubscriber{},
+ &models.AccountFriendship{},
+ } {
+ if err := tx.Delete(model, "account_id = ?", id).Error; err != nil {
+ tx.Rollback()
+ return err
+ }
+ }
+
+ if err := tx.Delete(&models.Account{}, "id = ?", id).Error; err != nil {
+ tx.Rollback()
+ return err
+ }
+
+ return tx.Commit().Error
+}
+
+func RecycleUnConfirmAccount() {
+ var hitList []models.Account
+ if err := database.C.Where("confirmed_at IS NULL").Find(&hitList).Error; err != nil {
+ log.Error().Err(err).Msg("An error occurred while recycling accounts...")
+ return
+ }
+
+ if len(hitList) > 0 {
+ log.Info().Int("count", len(hitList)).Msg("Going to recycle those un-confirmed accounts...")
+ for _, entry := range hitList {
+ if err := DeleteAccount(entry.ID); err != nil {
+ log.Error().Err(err).Msg("An error occurred while recycling accounts...")
+ }
+ }
+ }
+}
diff --git a/pkg/internal/services/tokens.go b/pkg/internal/services/tokens.go
index f297444..9dd9419 100644
--- a/pkg/internal/services/tokens.go
+++ b/pkg/internal/services/tokens.go
@@ -47,7 +47,7 @@ func NewMagicToken(mode models.MagicTokenType, assignTo *models.Account, expired
token := models.MagicToken{
Code: strings.Replace(uuid.NewString(), "-", "", -1),
Type: mode,
- AssignTo: &uid,
+ AccountID: &uid,
ExpiredAt: expiredAt,
}
@@ -59,13 +59,13 @@ func NewMagicToken(mode models.MagicTokenType, assignTo *models.Account, expired
}
func NotifyMagicToken(token models.MagicToken) error {
- if token.AssignTo == nil {
+ if token.AccountID == nil {
return fmt.Errorf("could notify a non-assign magic token")
}
var user models.Account
if err := database.C.Where(&models.Account{
- BaseModel: models.BaseModel{ID: *token.AssignTo},
+ BaseModel: models.BaseModel{ID: *token.AccountID},
}).Preload("Contacts").First(&user).Error; err != nil {
return err
}
diff --git a/pkg/main.go b/pkg/main.go
index 636fc1f..dae1fd4 100644
--- a/pkg/main.go
+++ b/pkg/main.go
@@ -68,6 +68,7 @@ func main() {
quartz.AddFunc("@every 60m", services.DoAutoSignoff)
quartz.AddFunc("@every 60m", services.DoAutoDatabaseCleanup)
quartz.AddFunc("@every 60s", services.RecycleAuthContext)
+ quartz.AddFunc("@every 60m", services.RecycleUnConfirmAccount)
quartz.AddFunc("@every 5m", services.KexCleanup)
quartz.Start()