diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 0352d6c..3251c1d 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -4,9 +4,11 @@
-
+
-
+
+
+
@@ -153,7 +155,6 @@
-
@@ -178,7 +179,8 @@
-
+
+
true
diff --git a/pkg/internal/server/api/bot_token_api.go b/pkg/internal/server/api/bot_token_api.go
index bf0252b..8fd3303 100644
--- a/pkg/internal/server/api/bot_token_api.go
+++ b/pkg/internal/server/api/bot_token_api.go
@@ -1,11 +1,13 @@
package api
import (
+ "fmt"
"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/gorm"
)
func listBotKeys(c *fiber.Ctx) error {
@@ -14,7 +16,18 @@ func listBotKeys(c *fiber.Ctx) error {
}
user := c.Locals("user").(models.Account)
- tx := database.C.Where("account_id = ?", user.ID)
+ 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)
+ }
countTx := tx
var count int64
@@ -66,7 +79,18 @@ func createBotKey(c *fiber.Ctx) error {
return err
}
- key, err := services.NewApiKey(user, models.ApiKey{
+ 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{
Name: data.Name,
Description: data.Description,
Lifecycle: data.Lifecycle,
diff --git a/pkg/internal/server/api/bots_api.go b/pkg/internal/server/api/bots_api.go
index f159e78..ff30815 100644
--- a/pkg/internal/server/api/bots_api.go
+++ b/pkg/internal/server/api/bots_api.go
@@ -86,7 +86,7 @@ func deleteBot(c *fiber.Ctx) error {
}
user := c.Locals("user").(models.Account)
- id, _ := c.ParamsInt("id", 0)
+ id, _ := c.ParamsInt("botId", 0)
var bot models.Account
if err := database.C.Where("id = ? AND automated_id = ?", id, user.ID).First(&bot).Error; err != nil {
diff --git a/pkg/internal/server/api/index.go b/pkg/internal/server/api/index.go
index a73b1de..ceabdfe 100644
--- a/pkg/internal/server/api/index.go
+++ b/pkg/internal/server/api/index.go
@@ -105,10 +105,16 @@ func MapAPIs(app *fiber.App, baseURL string) {
{
bots.Get("/", listBots)
bots.Post("/", createBot)
- bots.Delete("/:id", deleteBot)
+ bots.Delete("/:botId", deleteBot)
+
+ keys := bots.Group("/keys").Name("Bots' Keys")
+ {
+ keys.Get("/", listBotKeys)
+ keys.Post("/", createBotKey)
+ }
}
- keys := developers.Group("/keys").Name("Keys")
+ keys := developers.Group("/keys").Name("Own Bots' Keys")
{
keys.Get("/", listBotKeys)
keys.Get("/:id", getBotKey)