2024-06-22 05:04:21 +00:00
|
|
|
package api
|
2024-01-06 17:56:32 +00:00
|
|
|
|
|
|
|
import (
|
2024-03-16 04:28:50 +00:00
|
|
|
"fmt"
|
|
|
|
"strconv"
|
2024-08-01 04:21:34 +00:00
|
|
|
"strings"
|
2024-03-16 04:28:50 +00:00
|
|
|
"time"
|
|
|
|
|
2024-10-27 04:50:07 +00:00
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/http/exts"
|
2024-07-23 15:50:05 +00:00
|
|
|
|
2024-06-17 14:21:34 +00:00
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
|
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
|
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
|
2024-01-26 17:11:32 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
2024-01-30 13:34:48 +00:00
|
|
|
jsoniter "github.com/json-iterator/go"
|
2024-01-29 08:11:59 +00:00
|
|
|
"github.com/spf13/viper"
|
2024-01-06 17:56:32 +00:00
|
|
|
)
|
|
|
|
|
2024-06-30 09:20:05 +00:00
|
|
|
func lookupAccount(c *fiber.Ctx) error {
|
|
|
|
probe := c.Query("probe")
|
|
|
|
if len(probe) == 0 {
|
2024-07-30 10:20:45 +00:00
|
|
|
return fiber.NewError(fiber.StatusBadRequest, "lookup probe is required")
|
2024-06-30 09:20:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
user, err := services.LookupAccount(probe)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(user)
|
|
|
|
}
|
|
|
|
|
2024-07-30 10:20:45 +00:00
|
|
|
func searchAccount(c *fiber.Ctx) error {
|
|
|
|
probe := c.Query("probe")
|
|
|
|
if len(probe) == 0 {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, "search probe is required")
|
|
|
|
}
|
|
|
|
|
|
|
|
users, err := services.SearchAccount(probe)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(users)
|
|
|
|
}
|
|
|
|
|
2024-01-31 13:16:54 +00:00
|
|
|
func getUserinfo(c *fiber.Ctx) error {
|
2024-06-22 05:04:21 +00:00
|
|
|
if err := exts.EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
user := c.Locals("user").(models.Account)
|
2024-01-28 08:17:38 +00:00
|
|
|
|
|
|
|
var data models.Account
|
2024-01-30 07:57:49 +00:00
|
|
|
if err := database.C.
|
|
|
|
Where(&models.Account{BaseModel: models.BaseModel{ID: user.ID}}).
|
|
|
|
Preload("Profile").
|
|
|
|
Preload("Contacts").
|
2024-06-02 12:15:04 +00:00
|
|
|
Preload("Badges").
|
2024-01-30 07:57:49 +00:00
|
|
|
First(&data).Error; err != nil {
|
2024-01-28 08:17:38 +00:00
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
2024-07-24 10:09:18 +00:00
|
|
|
} else {
|
|
|
|
data.PermNodes = c.Locals("permissions").(map[string]any)
|
2024-01-28 08:17:38 +00:00
|
|
|
}
|
|
|
|
|
2024-01-30 13:34:48 +00:00
|
|
|
var resp fiber.Map
|
|
|
|
raw, _ := jsoniter.Marshal(data)
|
|
|
|
jsoniter.Unmarshal(raw, &resp)
|
|
|
|
|
2024-01-30 14:09:07 +00:00
|
|
|
resp["sub"] = strconv.Itoa(int(data.ID))
|
2024-01-30 13:34:48 +00:00
|
|
|
resp["family_name"] = data.Profile.FirstName
|
|
|
|
resp["given_name"] = data.Profile.LastName
|
|
|
|
resp["name"] = data.Name
|
|
|
|
resp["email"] = data.GetPrimaryEmail().Content
|
|
|
|
resp["preferred_username"] = data.Nick
|
|
|
|
|
2024-05-20 14:41:54 +00:00
|
|
|
if data.Avatar != nil {
|
2024-05-22 15:45:43 +00:00
|
|
|
resp["picture"] = *data.GetAvatar()
|
2024-01-31 16:33:01 +00:00
|
|
|
}
|
|
|
|
|
2024-01-30 13:34:48 +00:00
|
|
|
return c.JSON(resp)
|
2024-01-28 08:17:38 +00:00
|
|
|
}
|
|
|
|
|
2024-01-30 09:57:23 +00:00
|
|
|
func getEvents(c *fiber.Ctx) error {
|
2024-06-22 05:04:21 +00:00
|
|
|
if err := exts.EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
user := c.Locals("user").(models.Account)
|
2024-01-30 09:57:23 +00:00
|
|
|
take := c.QueryInt("take", 0)
|
|
|
|
offset := c.QueryInt("offset", 0)
|
|
|
|
|
|
|
|
var count int64
|
|
|
|
var events []models.ActionEvent
|
|
|
|
if err := database.C.
|
|
|
|
Where(&models.ActionEvent{AccountID: user.ID}).
|
|
|
|
Model(&models.ActionEvent{}).
|
|
|
|
Count(&count).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := database.C.
|
2024-02-12 04:31:18 +00:00
|
|
|
Order("created_at desc").
|
2024-01-30 09:57:23 +00:00
|
|
|
Where(&models.ActionEvent{AccountID: user.ID}).
|
|
|
|
Limit(take).
|
|
|
|
Offset(offset).
|
|
|
|
Find(&events).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(fiber.Map{
|
|
|
|
"count": count,
|
|
|
|
"data": events,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-01-31 13:16:54 +00:00
|
|
|
func editUserinfo(c *fiber.Ctx) error {
|
2024-06-22 05:04:21 +00:00
|
|
|
if err := exts.EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
user := c.Locals("user").(models.Account)
|
2024-01-31 13:16:54 +00:00
|
|
|
|
|
|
|
var data struct {
|
2024-08-01 04:21:34 +00:00
|
|
|
Nick string `json:"nick" validate:"required"`
|
2024-02-28 15:30:29 +00:00
|
|
|
Description string `json:"description"`
|
|
|
|
FirstName string `json:"first_name"`
|
|
|
|
LastName string `json:"last_name"`
|
|
|
|
Birthday time.Time `json:"birthday"`
|
2024-01-31 13:16:54 +00:00
|
|
|
}
|
|
|
|
|
2024-06-22 05:04:21 +00:00
|
|
|
if err := exts.BindAndValidate(c, &data); err != nil {
|
2024-01-31 13:16:54 +00:00
|
|
|
return err
|
2024-08-01 04:21:34 +00:00
|
|
|
} else {
|
|
|
|
data.Nick = strings.TrimSpace(data.Nick)
|
|
|
|
}
|
|
|
|
if !services.ValidateAccountName(data.Nick, 4, 24) {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, "invalid account nick, length requires 4 to 24")
|
2024-01-31 13:16:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var account models.Account
|
|
|
|
if err := database.C.
|
|
|
|
Where(&models.Account{BaseModel: models.BaseModel{ID: user.ID}}).
|
|
|
|
Preload("Profile").
|
|
|
|
First(&account).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
account.Nick = data.Nick
|
2024-02-28 15:30:29 +00:00
|
|
|
account.Description = data.Description
|
2024-01-31 13:16:54 +00:00
|
|
|
account.Profile.FirstName = data.FirstName
|
|
|
|
account.Profile.LastName = data.LastName
|
|
|
|
account.Profile.Birthday = &data.Birthday
|
|
|
|
|
|
|
|
if err := database.C.Save(&account).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
} else if err := database.C.Save(&account.Profile).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
}
|
|
|
|
|
2024-10-14 14:23:51 +00:00
|
|
|
services.AddEvent(user.ID, "profile.edit", strconv.Itoa(int(user.ID)), c.IP(), c.Get(fiber.HeaderUserAgent))
|
2024-05-17 11:37:58 +00:00
|
|
|
services.InvalidAuthCacheWithUser(account.ID)
|
|
|
|
|
2024-01-31 13:16:54 +00:00
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
|
|
}
|
|
|
|
|
2024-01-26 17:11:32 +00:00
|
|
|
func doRegister(c *fiber.Ctx) error {
|
|
|
|
var data struct {
|
2024-08-01 04:21:34 +00:00
|
|
|
Name string `json:"name" validate:"required,lowercase,alphanum,min=4,max=16"`
|
|
|
|
Nick string `json:"nick" validate:"required"`
|
2024-01-31 13:16:54 +00:00
|
|
|
Email string `json:"email" validate:"required,email"`
|
|
|
|
Password string `json:"password" validate:"required,min=4,max=32"`
|
2024-01-29 08:11:59 +00:00
|
|
|
MagicToken string `json:"magic_token"`
|
2024-01-26 17:11:32 +00:00
|
|
|
}
|
|
|
|
|
2024-06-22 05:04:21 +00:00
|
|
|
if err := exts.BindAndValidate(c, &data); err != nil {
|
2024-01-26 17:11:32 +00:00
|
|
|
return err
|
2024-08-01 04:21:34 +00:00
|
|
|
} else {
|
|
|
|
data.Name = strings.TrimSpace(data.Name)
|
|
|
|
data.Nick = strings.TrimSpace(data.Nick)
|
|
|
|
data.Email = strings.TrimSpace(data.Email)
|
|
|
|
}
|
|
|
|
if !services.ValidateAccountName(data.Nick, 4, 24) {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, "invalid account nick, length requires 4 to 24")
|
|
|
|
}
|
|
|
|
if viper.GetBool("use_registration_magic_token") && len(data.MagicToken) <= 0 {
|
2024-01-29 08:11:59 +00:00
|
|
|
return fmt.Errorf("missing magic token in request")
|
2024-02-18 07:51:27 +00:00
|
|
|
} else if viper.GetBool("use_registration_magic_token") {
|
|
|
|
if tk, err := services.ValidateMagicToken(data.MagicToken, models.RegistrationMagicToken); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
database.C.Delete(&tk)
|
|
|
|
}
|
2024-01-26 17:11:32 +00:00
|
|
|
}
|
2024-01-06 17:56:32 +00:00
|
|
|
|
2024-01-28 08:17:38 +00:00
|
|
|
if user, err := services.CreateAccount(
|
|
|
|
data.Name,
|
|
|
|
data.Nick,
|
|
|
|
data.Email,
|
|
|
|
data.Password,
|
|
|
|
); err != nil {
|
2024-01-26 17:11:32 +00:00
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
2024-01-28 08:17:38 +00:00
|
|
|
} else {
|
|
|
|
return c.JSON(user)
|
2024-01-06 17:56:32 +00:00
|
|
|
}
|
|
|
|
}
|
2024-01-28 16:32:39 +00:00
|
|
|
|
|
|
|
func doRegisterConfirm(c *fiber.Ctx) error {
|
|
|
|
var data struct {
|
2024-01-31 13:16:54 +00:00
|
|
|
Code string `json:"code" validate:"required"`
|
2024-01-28 16:32:39 +00:00
|
|
|
}
|
|
|
|
|
2024-06-22 05:04:21 +00:00
|
|
|
if err := exts.BindAndValidate(c, &data); err != nil {
|
2024-01-28 16:32:39 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := services.ConfirmAccount(data.Code); err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
|
|
}
|
2024-09-19 14:18:22 +00:00
|
|
|
|
|
|
|
func requestDeleteAccount(c *fiber.Ctx) error {
|
|
|
|
if err := exts.EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
user := c.Locals("user").(models.Account)
|
|
|
|
|
|
|
|
if err := services.CheckAbleToDeleteAccount(user); err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
} else if err = services.RequestDeleteAccount(user); err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
func confirmDeleteAccount(c *fiber.Ctx) error {
|
|
|
|
var data struct {
|
|
|
|
Code string `json:"code" validate:"required"`
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := exts.BindAndValidate(c, &data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := services.ConfirmDeleteAccount(data.Code); err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
|
|
}
|