gocommon/ginutils/auth.go
Matthieu 'JP' DERASSE 77b4351ef1
All checks were successful
continuous-integration/drone/push Build is passing
feat(error): Create api error. WIP
2023-08-22 19:59:44 +00:00

34 lines
737 B
Go

package ginutils
import (
"git.dev.m-and-m.ovh/mderasse/gocommon/aerr"
"git.dev.m-and-m.ovh/mderasse/gocommon/constant"
"github.com/gin-gonic/gin"
)
// SimpleTokens is a middleware that will just check if a token match X-TOKEN header.
func SimpleTokens(tokens []string, forbiddenHandler gin.HandlerFunc) gin.HandlerFunc {
if forbiddenHandler == nil {
forbiddenHandler = func(c *gin.Context) {
errorHandler(c, aerr.NewForbidden())
}
}
return func(c *gin.Context) {
requestToken := c.GetHeader(string(constant.HeaderKey_Token))
isAuthorized := false
for _, key := range tokens {
if key == requestToken {
isAuthorized = true
}
}
if isAuthorized {
c.Next()
} else {
forbiddenHandler(c)
}
}
}