Passport/pkg/internal/server/api/bot_token_api.go

220 lines
5.6 KiB
Go
Raw Normal View History

2024-08-24 12:28:10 +00:00
package api
import (
2024-08-25 12:51:58 +00:00
"fmt"
2024-08-24 12:28:10 +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/server/exts"
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
"github.com/gofiber/fiber/v2"
2024-08-25 16:33:57 +00:00
"gorm.io/gorm"
2024-08-24 12:28:10 +00:00
)
func listBotKeys(c *fiber.Ctx) error {
if err := exts.EnsureAuthenticated(c); err != nil {
return err
}
user := c.Locals("user").(models.Account)
2024-08-25 16:33:57 +00:00
var tx *gorm.DB
2024-08-25 12:51:58 +00:00
botId, _ := c.ParamsInt("botId", 0)
if botId > 0 {
var bot models.Account
if err := database.C.Where("automated_id = ? AND id = ?", user.ID, botId).First(&bot).Error; err != nil {
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("bot not found: %v", err))
}
tx = database.C.Where("account_id = ?", bot.ID)
} else {
tx = database.C.Where("account_id = ?", user.ID)
}
2024-08-25 09:03:06 +00:00
countTx := tx
var count int64
if err := countTx.Model(&models.ApiKey{}).Count(&count).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
2024-08-24 12:28:10 +00:00
var keys []models.ApiKey
2024-08-25 16:33:57 +00:00
if err := tx.Preload("Ticket").Find(&keys).Error; err != nil {
2024-08-24 12:28:10 +00:00
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
2024-08-25 09:03:06 +00:00
return c.JSON(fiber.Map{
"count": count,
"data": keys,
})
2024-08-24 12:28:10 +00:00
}
func getBotKey(c *fiber.Ctx) error {
if err := exts.EnsureAuthenticated(c); err != nil {
return err
}
user := c.Locals("user").(models.Account)
id, _ := c.ParamsInt("id", 0)
var key models.ApiKey
2024-08-25 15:43:15 +00:00
if err := database.C.
Where("id = ? AND account_id = ?", id, user.ID).
Preload("Ticket").
First(&key).Error; err != nil {
2024-08-24 12:28:10 +00:00
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
return c.JSON(key)
}
func createBotKey(c *fiber.Ctx) error {
if err := exts.EnsureAuthenticated(c); err != nil {
return err
}
user := c.Locals("user").(models.Account)
var data struct {
Name string `json:"name" validate:"required"`
Description string `json:"description"`
Lifecycle *int64 `json:"lifecycle"`
Claims []string `json:"claims"`
}
if err := exts.BindAndValidate(c, &data); err != nil {
return err
}
2024-08-25 12:51:58 +00:00
target := user
botId, _ := c.ParamsInt("botId", 0)
if botId > 0 {
var bot models.Account
if err := database.C.Where("automated_id = ? AND id = ?", user.ID, botId).First(&bot).Error; err != nil {
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("bot not found: %v", err))
}
target = bot
}
key, err := services.NewApiKey(target, models.ApiKey{
2024-08-24 12:28:10 +00:00
Name: data.Name,
Description: data.Description,
Lifecycle: data.Lifecycle,
}, c.IP(), c.Get(fiber.HeaderUserAgent), data.Claims)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
return c.JSON(key)
}
func editBotKey(c *fiber.Ctx) error {
if err := exts.EnsureAuthenticated(c); err != nil {
return err
}
user := c.Locals("user").(models.Account)
var data struct {
Name string `json:"name" validate:"required"`
Description string `json:"description"`
Lifecycle *int64 `json:"lifecycle"`
}
if err := exts.BindAndValidate(c, &data); err != nil {
return err
}
id, _ := c.ParamsInt("id", 0)
2024-08-25 16:44:10 +00:00
var tx *gorm.DB
botId, _ := c.ParamsInt("botId", 0)
if botId > 0 {
var bot models.Account
if err := database.C.Where("automated_id = ? AND id = ?", user.ID, botId).First(&bot).Error; err != nil {
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("bot not found: %v", err))
}
tx = database.C.Where("account_id = ?", bot.ID)
} else {
tx = database.C.Where("account_id = ?", user.ID)
}
2024-08-24 12:28:10 +00:00
var key models.ApiKey
2024-08-25 16:44:10 +00:00
if err := tx.Where("id = ?", id).First(&key).Error; err != nil {
2024-08-24 12:28:10 +00:00
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
key.Name = data.Name
key.Description = data.Description
key.Lifecycle = data.Lifecycle
if err := database.C.Save(&key).Error; err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
return c.JSON(key)
}
func rollBotKey(c *fiber.Ctx) error {
if err := exts.EnsureAuthenticated(c); err != nil {
return err
}
user := c.Locals("user").(models.Account)
id, _ := c.ParamsInt("id", 0)
2024-08-25 16:44:10 +00:00
var tx *gorm.DB
botId, _ := c.ParamsInt("botId", 0)
if botId > 0 {
var bot models.Account
if err := database.C.Where("automated_id = ? AND id = ?", user.ID, botId).First(&bot).Error; err != nil {
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("bot not found: %v", err))
}
tx = database.C.Where("account_id = ?", bot.ID)
} else {
tx = database.C.Where("account_id = ?", user.ID)
}
2024-08-24 12:28:10 +00:00
var key models.ApiKey
2024-08-25 16:44:10 +00:00
if err := tx.Where("id = ?", id).First(&key).Error; err != nil {
2024-08-24 12:28:10 +00:00
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
if key, err := services.RollApiKey(key); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else {
return c.JSON(key)
}
}
func revokeBotKey(c *fiber.Ctx) error {
if err := exts.EnsureAuthenticated(c); err != nil {
return err
}
user := c.Locals("user").(models.Account)
id, _ := c.ParamsInt("id", 0)
2024-08-25 16:44:10 +00:00
var tx *gorm.DB
botId, _ := c.ParamsInt("botId", 0)
if botId > 0 {
var bot models.Account
if err := database.C.Where("automated_id = ? AND id = ?", user.ID, botId).First(&bot).Error; err != nil {
return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("bot not found: %v", err))
}
tx = database.C.Where("account_id = ?", bot.ID)
} else {
tx = database.C.Where("account_id = ?", user.ID)
}
2024-08-24 12:28:10 +00:00
var key models.ApiKey
2024-08-25 16:44:10 +00:00
if err := tx.Where("id = ?", id).First(&key).Error; err != nil {
2024-08-24 12:28:10 +00:00
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
if err := database.C.Delete(&key).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
return c.JSON(key)
}