1
0
forked from baron/baron-sso

외부 OIDC IdP 연동 계획

This commit is contained in:
2026-01-29 10:28:13 +09:00
parent ec90853fe3
commit a8ac66b318
6 changed files with 196 additions and 133 deletions

View File

@@ -3,21 +3,80 @@ package handler
import (
"baron-sso-backend/internal/domain"
"baron-sso-backend/internal/repository"
"baron-sso-backend/internal/service"
"errors"
"github.com/gofiber/fiber/v2"
"gorm.io/gorm"
)
// FederationHandler handles API requests for IdP federation configurations.
// FederationHandler handles API requests for IdP federation.
type FederationHandler struct {
Repo *repository.FederationRepository
DB *gorm.DB // For tenant existence checks
fedSvc *service.FederationService
repo repository.FederationRepository // For IdP Config CRUD
db *gorm.DB // For tenant existence checks, etc. in CRUD
}
// NewFederationHandler creates a new FederationHandler.
func NewFederationHandler(repo *repository.FederationRepository, db *gorm.DB) *FederationHandler {
return &FederationHandler{Repo: repo, DB: db}
func NewFederationHandler(fedSvc *service.FederationService, repo repository.FederationRepository, db *gorm.DB) *FederationHandler {
return &FederationHandler{
fedSvc: fedSvc,
repo: repo,
db: db,
}
}
// InitiateOIDCLogin handles the start of the OIDC login flow.
// It expects `provider_id` and `login_challenge` as query parameters.
func (h *FederationHandler) InitiateOIDCLogin(c *fiber.Ctx) error {
providerID := c.Query("provider_id")
loginChallenge := c.Query("login_challenge")
if providerID == "" || loginChallenge == "" {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "provider_id and login_challenge are required"})
}
redirectURL, err := h.fedSvc.InitiateOIDCLogin(c.Context(), providerID, loginChallenge)
if err != nil {
// Log the error properly in a real application
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "failed to initiate OIDC login"})
}
return c.Redirect(redirectURL, fiber.StatusFound)
}
// HandleOIDCCallback handles the OIDC callback from the IdP.
func (h *FederationHandler) HandleOIDCCallback(c *fiber.Ctx) error {
code := c.Query("code")
state := c.Query("state")
if code == "" || state == "" {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "code and state are required"})
}
redirectURL, err := h.fedSvc.HandleOIDCCallback(c.Context(), code, state)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "failed to handle OIDC callback"})
}
return c.Redirect(redirectURL, fiber.StatusFound)
}
// ListIdpConfigsForTenant handles listing all IdP configurations for a tenant.
func (h *FederationHandler) ListIdpConfigsForTenant(c *fiber.Ctx) error {
tenantID := c.Params("tenantId")
if tenantID == "" {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "tenantId is required"})
}
// This is a temporary solution. We should create a proper method in the repository.
var configs []domain.IdentityProviderConfig
if err := h.db.Where("tenant_id = ?", tenantID).Find(&configs).Error; err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
}
return c.JSON(configs)
}
// CreateIdpConfig handles the creation of a new IdP configuration.
@@ -34,93 +93,18 @@ func (h *FederationHandler) CreateIdpConfig(c *fiber.Ctx) error {
// Check if tenant exists
var tenant domain.Tenant
if err := h.DB.First(&tenant, "id = ?", req.TenantID).Error; err != nil {
if err := h.db.First(&tenant, "id = ?", req.TenantID).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "tenant not found"})
}
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
}
if err := h.Repo.Create(&req); err != nil {
// Create in DB
if err := h.db.Create(&req).Error; err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
}
return c.Status(fiber.StatusCreated).JSON(req)
}
// GetIdpConfigByID handles retrieving a single IdP configuration.
func (h *FederationHandler) GetIdpConfigByID(c *fiber.Ctx) error {
id := c.Params("id")
config, err := h.Repo.GetByID(id)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
}
if config == nil {
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{"error": "identity provider configuration not found"})
}
return c.JSON(config)
}
// ListIdpConfigsForTenant handles listing all IdP configurations for a tenant.
func (h *FederationHandler) ListIdpConfigsForTenant(c *fiber.Ctx) error {
tenantID := c.Params("tenantId")
if tenantID == "" {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "tenantId is required"})
}
configs, err := h.Repo.ListByTenantID(tenantID)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
}
return c.JSON(configs)
}
// UpdateIdpConfig handles updating an IdP configuration.
func (h *FederationHandler) UpdateIdpConfig(c *fiber.Ctx) error {
id := c.Params("id")
existingConfig, err := h.Repo.GetByID(id)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
}
if existingConfig == nil {
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{"error": "identity provider configuration not found"})
}
var req domain.IdentityProviderConfig
if err := c.BodyParser(&req); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "invalid request body"})
}
// Overwrite fields
existingConfig.DisplayName = req.DisplayName
existingConfig.Status = req.Status
existingConfig.IssuerURL = req.IssuerURL
existingConfig.ClientID = req.ClientID
existingConfig.ClientSecret = req.ClientSecret
existingConfig.Scopes = req.Scopes
existingConfig.MetadataURL = req.MetadataURL
existingConfig.MetadataXML = req.MetadataXML
existingConfig.EntityID = req.EntityID
existingConfig.AcsURL = req.AcsURL
if err := h.Repo.Update(existingConfig); err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
}
return c.JSON(existingConfig)
}
// DeleteIdpConfig handles deleting an IdP configuration.
func (h *FederationHandler) DeleteIdpConfig(c *fiber.Ctx) error {
id := c.Params("id")
if err := h.Repo.Delete(id); err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
}
return c.SendStatus(fiber.StatusNoContent)
}
// TODO: Re-implement Update, Delete handlers for IdP Configs