package server import ( "crypto/sha256" "crypto/subtle" "encoding/base64" "html" "net/http" "strings" "github.com/gin-gonic/gin" ) const deployAuthCookieName = "nexttrace_deploy_auth" type deployAuth struct { Enabled bool Token string } func (a deployAuth) tokenConfigured() bool { return strings.TrimSpace(a.Token) != "" } func deployAuthMiddleware(auth deployAuth) gin.HandlerFunc { return func(c *gin.Context) { if !auth.Enabled || c.Request.Method == http.MethodOptions || isDeployAuthRoute(c.Request.URL.Path) { c.Next() return } if !auth.tokenConfigured() { writeDeployUnauthorized(c) c.Abort() return } if deployRequestAuthorized(c.Request, auth.Token) { c.Next() return } writeDeployUnauthorized(c) c.Abort() } } func isDeployAuthRoute(path string) bool { return path == "/auth/login" } func deployRequestAuthorized(r *http.Request, token string) bool { if deployTokenMatches(bearerToken(r.Header.Get("Authorization")), token) { return true } if deployTokenMatches(r.Header.Get("X-NextTrace-Token"), token) { return true } if cookie, err := r.Cookie(deployAuthCookieName); err == nil { return deployTokenMatches(cookie.Value, deployCookieValue(token)) } return false } func bearerToken(header string) string { parts := strings.Fields(strings.TrimSpace(header)) if len(parts) != 2 || !strings.EqualFold(parts[0], "Bearer") { return "" } return parts[1] } func deployTokenMatches(got, want string) bool { got = strings.TrimSpace(got) want = strings.TrimSpace(want) if got == "" || want == "" { return false } return subtle.ConstantTimeCompare([]byte(got), []byte(want)) == 1 } func deployCookieValue(token string) string { sum := sha256.Sum256([]byte("nexttrace-deploy-auth\x00" + token)) return base64.RawURLEncoding.EncodeToString(sum[:]) } func deployRequestIsHTTPS(r *http.Request) bool { if r == nil { return false } if r.TLS != nil { return true } if forwardedProtoIsHTTPS(r.Header.Get("X-Forwarded-Proto")) { return true } return forwardedHeaderProtoIsHTTPS(r.Header.Get("Forwarded")) } func forwardedProtoIsHTTPS(value string) bool { for _, part := range strings.Split(value, ",") { if strings.EqualFold(strings.TrimSpace(part), "https") { return true } } return false } func forwardedHeaderProtoIsHTTPS(value string) bool { for _, forwarded := range strings.Split(value, ",") { for _, part := range strings.Split(forwarded, ";") { key, val, ok := strings.Cut(strings.TrimSpace(part), "=") if !ok || !strings.EqualFold(key, "proto") { continue } if strings.EqualFold(strings.Trim(strings.TrimSpace(val), `"`), "https") { return true } } } return false } func registerDeployAuthRoutes(router *gin.Engine, auth deployAuth) { router.GET("/auth/login", func(c *gin.Context) { if !auth.Enabled { c.Redirect(http.StatusFound, "/") return } if !auth.tokenConfigured() { writeDeployAuthConfigError(c) return } c.Data(http.StatusOK, "text/html; charset=utf-8", []byte(deployLoginPage(""))) }) router.POST("/auth/login", func(c *gin.Context) { if !auth.Enabled { c.JSON(http.StatusOK, gin.H{"ok": true}) return } if !auth.tokenConfigured() { writeDeployAuthConfigError(c) return } token := strings.TrimSpace(c.PostForm("token")) if token == "" { var body struct { Token string `json:"token"` } _ = c.ShouldBindJSON(&body) token = strings.TrimSpace(body.Token) } if !deployTokenMatches(token, auth.Token) { if acceptsHTML(c.Request) { c.Data(http.StatusUnauthorized, "text/html; charset=utf-8", []byte(deployLoginPage("Invalid token"))) return } c.JSON(http.StatusUnauthorized, gin.H{"error": "invalid token"}) return } http.SetCookie(c.Writer, &http.Cookie{ Name: deployAuthCookieName, Value: deployCookieValue(auth.Token), Path: "/", HttpOnly: true, SameSite: http.SameSiteLaxMode, Secure: deployRequestIsHTTPS(c.Request), }) if acceptsHTML(c.Request) { c.Redirect(http.StatusFound, "/") return } c.JSON(http.StatusOK, gin.H{"ok": true}) }) } func writeDeployAuthConfigError(c *gin.Context) { if c.Request.Method == http.MethodGet && acceptsHTML(c.Request) { c.Data(http.StatusInternalServerError, "text/html; charset=utf-8", []byte(deployLoginPage("Deploy auth token is not configured"))) return } c.JSON(http.StatusInternalServerError, gin.H{"error": "deploy auth token is not configured"}) } func writeDeployUnauthorized(c *gin.Context) { if c.Request.Method == http.MethodGet && acceptsHTML(c.Request) { c.Data(http.StatusUnauthorized, "text/html; charset=utf-8", []byte(deployLoginPage(""))) return } c.JSON(http.StatusUnauthorized, gin.H{"error": "deploy token required"}) } func acceptsHTML(r *http.Request) bool { accept := r.Header.Get("Accept") return accept == "" || strings.Contains(accept, "text/html") } func deployLoginPage(message string) string { msg := "" if strings.TrimSpace(message) != "" { msg = `

` + html.EscapeString(message) + `

` } return ` NextTrace Login

NextTrace Web Console

` + msg + `
` }