47 lines
974 B
Go
47 lines
974 B
Go
package jwt
|
|
|
|
import (
|
|
"git.0x7f.app/WOJ/woj-server/internal/e"
|
|
"git.0x7f.app/WOJ/woj-server/internal/model"
|
|
"github.com/gin-gonic/gin"
|
|
"strings"
|
|
)
|
|
|
|
func (s *service) Handler(forced bool) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
claim, status := func() (*model.Claim, e.Status) {
|
|
tokenHeader := c.GetHeader("Authorization")
|
|
if tokenHeader == "" {
|
|
return nil, e.TokenEmpty
|
|
}
|
|
|
|
token := tokenHeader
|
|
const tokenPrefix = "bearer "
|
|
if strings.HasPrefix(strings.ToLower(tokenHeader), tokenPrefix) {
|
|
// don't force "bearer" prefix
|
|
token = tokenHeader[len(tokenPrefix):]
|
|
}
|
|
|
|
claim, status := s.ParseToken(token)
|
|
if status != e.Success {
|
|
return nil, status
|
|
}
|
|
|
|
if !s.Validate(claim) {
|
|
return nil, e.TokenRevoked
|
|
}
|
|
return claim, e.Success
|
|
}()
|
|
|
|
if status == e.Success {
|
|
c.Set("claim", claim)
|
|
}
|
|
if forced && status != e.Success {
|
|
e.Pong[any](c, status, nil)
|
|
c.Abort()
|
|
} else {
|
|
c.Next()
|
|
}
|
|
}
|
|
}
|