You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ims/util/jwt/finder.go

51 lines
868 B

package jwt
import (
"strings"
"zestack.dev/slim"
)
type Finder func(c slim.Context) string
func DefaultFinder(c slim.Context) string {
if s := FromQuery(c); s != "" {
return s
}
if s := FromHeader(c); s != "" {
return s
}
return FromCookie(c)
}
func FromCookie(c slim.Context, keys ...string) string {
for _, key := range keys {
cookie, err := c.Cookie(key)
if err == nil {
return cookie.Value
}
}
cookie, err := c.Cookie("jwt")
if err != nil {
return ""
}
return cookie.Value
}
func FromHeader(c slim.Context) string {
bearer := c.Header("Authorization")
if len(bearer) > 7 && strings.ToUpper(bearer[0:6]) == "BEARER" {
return bearer[7:]
}
return ""
}
func FromQuery(c slim.Context, keys ...string) string {
for _, key := range keys {
s := c.QueryParam(key)
if s != "" {
return s
}
}
return c.QueryParam("jwt")
}