sshpoke/internal/api/rest/middleware/auth.go

32 lines
497 B
Go

package middleware
import (
"net/http"
"strings"
"github.com/gin-gonic/gin"
)
const (
AuthHeader = "Authorization"
bearerPrefix = "Bearer "
)
func Auth(token string) gin.HandlerFunc {
if token == "" {
return func(c *gin.Context) {}
}
return func(c *gin.Context) {
header := c.GetHeader(AuthHeader)
if strings.HasPrefix(header, bearerPrefix) {
header = header[len(bearerPrefix):]
}
if header != token {
c.AbortWithStatus(http.StatusUnauthorized)
}
c.Next()
}
}