package server import ( "encoding/json" "io" "log/slog" "net/http" "github.com/gin-gonic/gin" "git/palnet/slack-notifier/internal/gitea" "git/palnet/slack-notifier/internal/security" ) // handleGitea는 어떤 경우에도 Gitea에 에러를 응답하지 않는다(항상 200). // 검증 실패·파싱 실패는 내부 로그로만 남기고, Gitea Recent Deliveries에는 성공으로 보이게 한다. func (s *Server) handleGitea(c *gin.Context) { raw, err := io.ReadAll(c.Request.Body) if err != nil { slog.Warn("gitea: cannot read body", "err", err) c.JSON(http.StatusOK, gin.H{"status": "cannot read body"}) return } // 들어온 원본 페이로드 전체를 기록 (검증 전 — 모든 수신 요청 확인용). slog.Info("gitea webhook received", "event", c.GetHeader("X-Gitea-Event"), "delivery", c.GetHeader("X-Gitea-Delivery"), "signature", c.GetHeader("X-Gitea-Signature"), "body", string(raw)) if !security.VerifyHMACSHA256(s.cfg.GiteaWebhookSecret, raw, c.GetHeader("X-Gitea-Signature")) { slog.Warn("gitea signature verification failed") c.JSON(http.StatusOK, gin.H{"status": "invalid signature, ignored"}) return } event := c.GetHeader("X-Gitea-Event") // 모든 이벤트(push 포함)를 기본 Slack 채널로 브로드캐스트 (GITEA_BROADCAST=on 일 때만). if s.cfg.GiteaBroadcast && s.cfg.DefaultSlackChannel != "" { if msg, ok := gitea.BuildChannelMessage(event, raw); ok { s.slack.PostMessage(c.Request.Context(), s.cfg.DefaultSlackChannel, msg.Text, msg.Blocks) } } // 담당자가 정해지는 이벤트는 개인 DM도 함께 발송. notes, err := gitea.BuildNotifications(event, raw) if err != nil { slog.Warn("gitea: invalid payload", "err", err) c.JSON(http.StatusOK, gin.H{"status": "invalid payload"}) return } sent := s.deliver(c.Request.Context(), notes) c.JSON(http.StatusOK, gin.H{"event": event, "matched": len(notes), "sent": sent}) } func (s *Server) handleNotion(c *gin.Context) { raw, err := io.ReadAll(c.Request.Body) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "cannot read body"}) return } // 들어온 원본 페이로드 전체를 기록 (검증 전 — 모든 수신 요청 확인용). slog.Info("notion webhook received", "signature", c.GetHeader("X-Notion-Signature"), "body", string(raw)) // 1) Subscription verification handshake: Notion POSTs a verification_token once. // Capture it from logs and paste into NOTION_VERIFICATION_TOKEN. var probe struct { VerificationToken string `json:"verification_token"` } _ = json.Unmarshal(raw, &probe) if probe.VerificationToken != "" { slog.Warn("notion verification_token received — set this in NOTION_VERIFICATION_TOKEN", "verification_token", probe.VerificationToken) c.JSON(http.StatusOK, gin.H{"verification_token": probe.VerificationToken}) return } // 2) Normal events: verify signature against the verification token. if !security.VerifyHMACSHA256(s.cfg.NotionVerificationToken, raw, c.GetHeader("X-Notion-Signature")) { slog.Warn("notion signature verification failed") c.JSON(http.StatusUnauthorized, gin.H{"error": "invalid signature"}) return } notes, err := s.notion.BuildNotifications(c.Request.Context(), raw) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "invalid payload"}) return } sent := s.deliver(c.Request.Context(), notes) c.JSON(http.StatusOK, gin.H{"matched": len(notes), "sent": sent}) }