♻️ Single table to store auth preferences
This commit is contained in:
@ -27,6 +27,7 @@ var AutoMaintainRange = []any{
|
||||
&models.ApiKey{},
|
||||
&models.SignRecord{},
|
||||
&models.PreferenceNotification{},
|
||||
&models.PreferenceAuth{},
|
||||
&models.AbuseReport{},
|
||||
}
|
||||
|
||||
|
@ -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"`
|
||||
|
@ -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
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user