diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 11ae1da..50a5096 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -4,12 +4,14 @@
-
+
+
-
+
+
@@ -144,7 +146,7 @@
-
+
@@ -157,7 +159,6 @@
-
@@ -182,7 +183,8 @@
-
+
+
true
diff --git a/pkg/internal/database/migrator.go b/pkg/internal/database/migrator.go
index dece2ef..f70f450 100644
--- a/pkg/internal/database/migrator.go
+++ b/pkg/internal/database/migrator.go
@@ -27,6 +27,7 @@ var AutoMaintainRange = []any{
&models.ApiKey{},
&models.SignRecord{},
&models.PreferenceNotification{},
+ &models.PreferenceAuth{},
&models.AbuseReport{},
}
diff --git a/pkg/internal/models/accounts.go b/pkg/internal/models/accounts.go
index 79505e8..418d441 100644
--- a/pkg/internal/models/accounts.go
+++ b/pkg/internal/models/accounts.go
@@ -12,15 +12,14 @@ import (
type Account struct {
BaseModel
- Name string `json:"name" gorm:"uniqueIndex"`
- Nick string `json:"nick"`
- Description string `json:"description"`
- Avatar *string `json:"avatar"`
- Banner *string `json:"banner"`
- ConfirmedAt *time.Time `json:"confirmed_at"`
- SuspendedAt *time.Time `json:"suspended_at"`
- PermNodes datatypes.JSONMap `json:"perm_nodes"`
- AuthConfig datatypes.JSONType[AuthConfig] `json:"auth_config"`
+ Name string `json:"name" gorm:"uniqueIndex"`
+ Nick string `json:"nick"`
+ Description string `json:"description"`
+ Avatar *string `json:"avatar"`
+ Banner *string `json:"banner"`
+ ConfirmedAt *time.Time `json:"confirmed_at"`
+ SuspendedAt *time.Time `json:"suspended_at"`
+ PermNodes datatypes.JSONMap `json:"perm_nodes"`
AutomatedBy *Account `json:"automated_by" gorm:"foreignKey:AutomatedID"`
AutomatedID *uint `json:"automated_id"`
diff --git a/pkg/internal/models/preferences.go b/pkg/internal/models/preferences.go
index 9e87e09..7d67286 100644
--- a/pkg/internal/models/preferences.go
+++ b/pkg/internal/models/preferences.go
@@ -2,6 +2,14 @@ package models
import "gorm.io/datatypes"
+type PreferenceAuth struct {
+ BaseModel
+
+ Config datatypes.JSONType[AuthConfig] `json:"config"`
+ AccountID uint `json:"account_id"`
+ Account Account `json:"account"`
+}
+
type PreferenceNotification struct {
BaseModel
diff --git a/pkg/internal/server/api/index.go b/pkg/internal/server/api/index.go
index 953d3ec..1e4cb7f 100644
--- a/pkg/internal/server/api/index.go
+++ b/pkg/internal/server/api/index.go
@@ -26,8 +26,8 @@ func MapAPIs(app *fiber.App, baseURL string) {
preferences := api.Group("/preferences").Name("Preferences API")
{
- preferences.Get("/auth", getAuthConfig)
- preferences.Put("/auth", updateAuthConfig)
+ preferences.Get("/auth", getAuthPreference)
+ preferences.Put("/auth", updateAuthPreference)
preferences.Get("/notifications", getNotificationPreference)
preferences.Put("/notifications", updateNotificationPreference)
}
diff --git a/pkg/internal/server/api/preferences_api.go b/pkg/internal/server/api/preferences_api.go
index bbc2145..2d0164b 100644
--- a/pkg/internal/server/api/preferences_api.go
+++ b/pkg/internal/server/api/preferences_api.go
@@ -1,24 +1,27 @@
package api
import (
- "git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/server/exts"
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
"github.com/gofiber/fiber/v2"
- "gorm.io/datatypes"
)
-func getAuthConfig(c *fiber.Ctx) error {
+func getAuthPreference(c *fiber.Ctx) error {
if err := exts.EnsureAuthenticated(c); err != nil {
return err
}
user := c.Locals("user").(models.Account)
- return c.JSON(user.AuthConfig)
+ cfg, err := services.GetAuthPreference(user)
+ if err != nil {
+ return fiber.NewError(fiber.StatusNotFound, err.Error())
+ }
+
+ return c.JSON(cfg.Config)
}
-func updateAuthConfig(c *fiber.Ctx) error {
+func updateAuthPreference(c *fiber.Ctx) error {
if err := exts.EnsureAuthenticated(c); err != nil {
return err
}
@@ -29,15 +32,12 @@ func updateAuthConfig(c *fiber.Ctx) error {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
- user.AuthConfig = datatypes.NewJSONType(data)
-
- if err := database.C.Save(&user).Error; err != nil {
- return fiber.NewError(fiber.StatusInternalServerError, err.Error())
- } else {
- services.InvalidAuthCacheWithUser(user.ID)
+ cfg, err := services.UpdateAuthPreference(user, data)
+ if err != nil {
+ return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
- return c.JSON(user.AuthConfig)
+ return c.JSON(cfg.Config)
}
func getNotificationPreference(c *fiber.Ctx) error {
diff --git a/pkg/internal/services/preferences.go b/pkg/internal/services/preferences.go
index 15e7674..e2f8801 100644
--- a/pkg/internal/services/preferences.go
+++ b/pkg/internal/services/preferences.go
@@ -17,6 +17,31 @@ import (
"gorm.io/gorm"
)
+func GetAuthPreference(account models.Account) (models.PreferenceAuth, error) {
+ var auth models.PreferenceAuth
+ if err := database.C.Where("account_id = ?", account.ID).First(&auth).Error; err != nil {
+ return auth, err
+ }
+
+ return auth, nil
+}
+
+func UpdateAuthPreference(account models.Account, config models.AuthConfig) (models.PreferenceAuth, error) {
+ var auth models.PreferenceAuth
+ var err error
+ if auth, err = GetAuthPreference(account); err != nil {
+ auth = models.PreferenceAuth{
+ AccountID: account.ID,
+ Config: datatypes.NewJSONType(config),
+ }
+ } else {
+ auth.Config = datatypes.NewJSONType(config)
+ }
+
+ err = database.C.Save(&auth).Error
+ return auth, err
+}
+
func GetNotificationPreferenceCacheKey(accountId uint) string {
return fmt.Sprintf("notification-preference#%d", accountId)
}
diff --git a/pkg/internal/services/ticket.go b/pkg/internal/services/ticket.go
index b8cb054..beda37a 100644
--- a/pkg/internal/services/ticket.go
+++ b/pkg/internal/services/ticket.go
@@ -32,8 +32,8 @@ func DetectRisk(user models.Account, ip, ua string) int {
return 2
}
-// PickTicketAttempt is trying to pick up the ticket that haven't completed but created by a same client (identify by ip address).
-// Then the client can continue their journey to get ticket actived.
+// PickTicketAttempt is trying to pick up the ticket that hasn't completed but created by a same client (identify by ip address).
+// Then the client can continue their journey to get ticket activated.
func PickTicketAttempt(user models.Account, ip string) (models.AuthTicket, error) {
var ticket models.AuthTicket
if err := database.C.
@@ -54,10 +54,11 @@ func NewTicket(user models.Account, ip, ua string) (models.AuthTicket, error) {
if count := CountUserFactor(user.ID); count <= 0 {
return ticket, fmt.Errorf("specified user didn't enable sign in")
} else {
- cfg := user.AuthConfig.Data()
steps = min(steps, int(count))
- if cfg.MaximumAuthSteps >= 1 {
- steps = min(steps, cfg.MaximumAuthSteps)
+
+ cfg, err := GetAuthPreference(user)
+ if err == nil && cfg.Config.Data().MaximumAuthSteps >= 1 {
+ steps = min(steps, cfg.Config.Data().MaximumAuthSteps)
}
}