chore: sync local updates
This commit is contained in:
23
mengyamonitor-backend-server/internal/middleware/admin.go
Normal file
23
mengyamonitor-backend-server/internal/middleware/admin.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"crypto/subtle"
|
||||
"net/http"
|
||||
|
||||
"mengyamonitor-backend-server/internal/config"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// AdminToken rejects requests without a valid X-Admin-Token header.
|
||||
func AdminToken(cfg *config.Config) gin.HandlerFunc {
|
||||
tok := []byte(cfg.AdminToken)
|
||||
return func(c *gin.Context) {
|
||||
given := []byte(c.GetHeader("X-Admin-Token"))
|
||||
if subtle.ConstantTimeCompare(given, tok) != 1 {
|
||||
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
|
||||
return
|
||||
}
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user