forked from baron/baron-sso
golangci-lint gofmt/gofumpt 적용
This commit is contained in:
@@ -3,9 +3,9 @@ package handler
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"net/url"
|
||||
|
||||
"github.com/descope/go-sdk/descope"
|
||||
"github.com/descope/go-sdk/descope/client"
|
||||
@@ -50,7 +50,7 @@ func (h *AdminHandler) checkAuth(c *fiber.Ctx) error {
|
||||
if adminPass == "" {
|
||||
adminPass = "admin" // Default fallback
|
||||
}
|
||||
|
||||
|
||||
reqPass := c.Get("X-Admin-Password")
|
||||
if reqPass != adminPass {
|
||||
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"error": "Invalid Admin Password"})
|
||||
@@ -59,11 +59,11 @@ func (h *AdminHandler) checkAuth(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
type CreateUserRequest struct {
|
||||
LoginID string `json:"loginId"`
|
||||
Email string `json:"email"`
|
||||
Phone string `json:"phone"`
|
||||
DisplayName string `json:"displayName"`
|
||||
Roles []string `json:"roles"`
|
||||
LoginID string `json:"loginId"`
|
||||
Email string `json:"email"`
|
||||
Phone string `json:"phone"`
|
||||
DisplayName string `json:"displayName"`
|
||||
Roles []string `json:"roles"`
|
||||
Tenants map[string][]string `json:"tenants"` // tenantId -> roles
|
||||
}
|
||||
|
||||
@@ -76,18 +76,20 @@ func (h *AdminHandler) CheckAuth(c *fiber.Ctx) error {
|
||||
|
||||
// ListUsers - GET /api/v1/admin/users
|
||||
func (h *AdminHandler) ListUsers(c *fiber.Ctx) error {
|
||||
if err := h.checkAuth(c); err != nil { return err }
|
||||
if err := h.checkAuth(c); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
text := c.Query("text")
|
||||
// Limit is not directly supported in SearchAll options as a simple int in all SDK versions,
|
||||
// Limit is not directly supported in SearchAll options as a simple int in all SDK versions,
|
||||
// but let's check the options struct.
|
||||
// Based on previous inspection: SearchAll takes UserSearchOptions.
|
||||
|
||||
|
||||
var users []*descope.UserResponse
|
||||
var err error
|
||||
|
||||
if text != "" {
|
||||
options := &descope.UserSearchOptions{ Text: text, Limit: 50 }
|
||||
options := &descope.UserSearchOptions{Text: text, Limit: 50}
|
||||
users, _, err = h.DescopeClient.Management.User().SearchAll(context.Background(), options)
|
||||
} else {
|
||||
// Nil options means default search (usually returns all or default page)
|
||||
@@ -104,13 +106,15 @@ func (h *AdminHandler) ListUsers(c *fiber.Ctx) error {
|
||||
|
||||
// DeleteUser - DELETE /api/v1/admin/users/:loginId
|
||||
func (h *AdminHandler) DeleteUser(c *fiber.Ctx) error {
|
||||
if err := h.checkAuth(c); err != nil { return err }
|
||||
if err := h.checkAuth(c); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
loginID := c.Params("loginId")
|
||||
// Decode if necessary (Fiber usually decodes params, but let's be safe if it's double encoded)
|
||||
if decoded, err := url.QueryUnescape(loginID); err == nil {
|
||||
loginID = decoded
|
||||
}
|
||||
// Decode if necessary (Fiber usually decodes params, but let's be safe if it's double encoded)
|
||||
if decoded, err := url.QueryUnescape(loginID); err == nil {
|
||||
loginID = decoded
|
||||
}
|
||||
|
||||
slog.Info("[Admin] Deleting user", "loginID", loginID)
|
||||
if err := h.DescopeClient.Management.User().Delete(context.Background(), loginID); err != nil {
|
||||
@@ -123,12 +127,14 @@ func (h *AdminHandler) DeleteUser(c *fiber.Ctx) error {
|
||||
|
||||
// UpdateUserStatus - PATCH /api/v1/admin/users/:loginId/status
|
||||
func (h *AdminHandler) UpdateUserStatus(c *fiber.Ctx) error {
|
||||
if err := h.checkAuth(c); err != nil { return err }
|
||||
if err := h.checkAuth(c); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
loginID := c.Params("loginId")
|
||||
if decoded, err := url.QueryUnescape(loginID); err == nil {
|
||||
loginID = decoded
|
||||
}
|
||||
if decoded, err := url.QueryUnescape(loginID); err == nil {
|
||||
loginID = decoded
|
||||
}
|
||||
|
||||
var req struct {
|
||||
Status string `json:"status"` // "enabled" or "disabled"
|
||||
@@ -161,7 +167,9 @@ func (h *AdminHandler) UpdateUserStatus(c *fiber.Ctx) error {
|
||||
|
||||
// UpdateUser - PATCH /api/v1/admin/users/:loginId
|
||||
func (h *AdminHandler) UpdateUser(c *fiber.Ctx) error {
|
||||
if err := h.checkAuth(c); err != nil { return err }
|
||||
if err := h.checkAuth(c); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
loginID := c.Params("loginId")
|
||||
if decoded, err := url.QueryUnescape(loginID); err == nil {
|
||||
@@ -213,7 +221,9 @@ func (h *AdminHandler) UpdateUser(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
func (h *AdminHandler) CreateUser(c *fiber.Ctx) error {
|
||||
if err := h.checkAuth(c); err != nil { return err }
|
||||
if err := h.checkAuth(c); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if h.DescopeClient == nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Descope Client not configured"})
|
||||
@@ -247,7 +257,7 @@ func (h *AdminHandler) CreateUser(c *fiber.Ctx) error {
|
||||
VerifiedEmail: boolPtr(req.Email != ""),
|
||||
VerifiedPhone: boolPtr(normalizedPhone != ""),
|
||||
}
|
||||
|
||||
|
||||
// Add Roles if provided
|
||||
if len(req.Roles) > 0 {
|
||||
userObj.Roles = req.Roles
|
||||
@@ -278,4 +288,4 @@ func (h *AdminHandler) CreateUser(c *fiber.Ctx) error {
|
||||
"message": "User created successfully",
|
||||
"user": res,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user