31 lines
536 B
Go
31 lines
536 B
Go
package auth
|
|
|
|
import (
|
|
"crypto/subtle"
|
|
"net/http"
|
|
"os"
|
|
)
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func AdminAuth() gin.HandlerFunc {
|
|
|
|
token := os.Getenv("ADMIN_TOKEN")
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
// Without a configured token every admin request is refused, never allowed
|
|
provided := c.GetHeader("X-Admin-Token")
|
|
if token == "" || subtle.ConstantTimeCompare([]byte(provided), []byte(token)) != 1 {
|
|
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{
|
|
"error": "Unauthorized",
|
|
})
|
|
return
|
|
}
|
|
|
|
c.Next()
|
|
}
|
|
}
|