Files
metatube-server/route/auth.go
T
2022-12-15 23:13:04 +08:00

29 lines
627 B
Go

package route
import (
"net/http"
"strings"
"github.com/gin-gonic/gin"
"github.com/javtube/metatube-sdk-go/errors"
"github.com/javtube/metatube-sdk-go/route/auth"
)
func authentication(v auth.Validator) gin.HandlerFunc {
return func(c *gin.Context) {
if v != nil /* auth enabled */ {
header := c.GetHeader("Authorization")
bearer, token, found := strings.Cut(header, " ")
hasInvalidHeader := bearer != "Bearer"
hasInvalidSecret := !found || !v.Valid(token)
if hasInvalidHeader || hasInvalidSecret {
abortWithError(c, errors.FromCode(http.StatusUnauthorized))
return
}
}
c.Next()
}
}