34 lines
737 B
Go
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)
|
|
}
|
|
}
|
|
}
|