mirror of
https://forgejo.merr.is/annika/jwtauth.git
synced 2025-12-13 11:36:46 -05:00
Implement custom search functions (#20)
* Implement custom search functions * Improve function and parameter names * Preserve original search order
This commit is contained in:
parent
050a030f1c
commit
e6503c425b
1 changed files with 39 additions and 31 deletions
70
jwtauth.go
70
jwtauth.go
|
|
@ -21,6 +21,34 @@ var (
|
||||||
ErrExpired = errors.New("jwtauth: token is expired")
|
ErrExpired = errors.New("jwtauth: token is expired")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// TokenFromCookie tries to retreive the token string from a cookie named
|
||||||
|
// "jwt".
|
||||||
|
TokenFromCookie = func(r *http.Request) string {
|
||||||
|
cookie, err := r.Cookie("jwt")
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return cookie.Value
|
||||||
|
}
|
||||||
|
// TokenFromHeader tries to retreive the token string from the
|
||||||
|
// "Authorization" reqeust header: "Authorization: BEARER T".
|
||||||
|
TokenFromHeader = func(r *http.Request) string {
|
||||||
|
// Get token from authorization header.
|
||||||
|
bearer := r.Header.Get("Authorization")
|
||||||
|
if len(bearer) > 7 && strings.ToUpper(bearer[0:6]) == "BEARER" {
|
||||||
|
return bearer[7:]
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
// TokenFromQuery tries to retreive the token string from the "jwt" URI
|
||||||
|
// query parameter.
|
||||||
|
TokenFromQuery = func(r *http.Request) string {
|
||||||
|
// Get token from query param named "jwt".
|
||||||
|
return r.URL.Query().Get("jwt")
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
type JwtAuth struct {
|
type JwtAuth struct {
|
||||||
signKey interface{}
|
signKey interface{}
|
||||||
verifyKey interface{}
|
verifyKey interface{}
|
||||||
|
|
@ -68,15 +96,15 @@ func NewWithParser(alg string, parser *jwt.Parser, signKey interface{}, verifyKe
|
||||||
// http response.
|
// http response.
|
||||||
func Verifier(ja *JwtAuth) func(http.Handler) http.Handler {
|
func Verifier(ja *JwtAuth) func(http.Handler) http.Handler {
|
||||||
return func(next http.Handler) http.Handler {
|
return func(next http.Handler) http.Handler {
|
||||||
return Verify(ja, "")(next)
|
return Verify(ja, TokenFromQuery, TokenFromHeader, TokenFromCookie)(next)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Verify(ja *JwtAuth, paramAliases ...string) func(http.Handler) http.Handler {
|
func Verify(ja *JwtAuth, findTokenFns ...func(r *http.Request) string) func(http.Handler) http.Handler {
|
||||||
return func(next http.Handler) http.Handler {
|
return func(next http.Handler) http.Handler {
|
||||||
hfn := func(w http.ResponseWriter, r *http.Request) {
|
hfn := func(w http.ResponseWriter, r *http.Request) {
|
||||||
ctx := r.Context()
|
ctx := r.Context()
|
||||||
token, err := VerifyRequest(ja, r, paramAliases...)
|
token, err := VerifyRequest(ja, r, findTokenFns...)
|
||||||
ctx = NewContext(ctx, token, err)
|
ctx = NewContext(ctx, token, err)
|
||||||
next.ServeHTTP(w, r.WithContext(ctx))
|
next.ServeHTTP(w, r.WithContext(ctx))
|
||||||
}
|
}
|
||||||
|
|
@ -84,37 +112,17 @@ func Verify(ja *JwtAuth, paramAliases ...string) func(http.Handler) http.Handler
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func VerifyRequest(ja *JwtAuth, r *http.Request, paramAliases ...string) (*jwt.Token, error) {
|
func VerifyRequest(ja *JwtAuth, r *http.Request, findTokenFns ...func(r *http.Request) string) (*jwt.Token, error) {
|
||||||
var tokenStr string
|
var tokenStr string
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
// Get token from query params
|
// Extract token string from the request by calling token find functions in
|
||||||
tokenStr = r.URL.Query().Get("jwt")
|
// the order they where provided. Further extraction stops if a function
|
||||||
|
// returns a non-empty string.
|
||||||
// Get token from other param aliases
|
for _, fn := range findTokenFns {
|
||||||
if tokenStr == "" && paramAliases != nil && len(paramAliases) > 0 {
|
tokenStr = fn(r)
|
||||||
for _, p := range paramAliases {
|
if tokenStr != "" {
|
||||||
tokenStr = r.URL.Query().Get(p)
|
break
|
||||||
if tokenStr != "" {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get token from authorization header
|
|
||||||
if tokenStr == "" {
|
|
||||||
bearer := r.Header.Get("Authorization")
|
|
||||||
if len(bearer) > 7 && strings.ToUpper(bearer[0:6]) == "BEARER" {
|
|
||||||
tokenStr = bearer[7:]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get token from cookie
|
|
||||||
if tokenStr == "" {
|
|
||||||
// TODO: paramAliases should apply to cookies too..
|
|
||||||
cookie, err := r.Cookie("jwt")
|
|
||||||
if err == nil {
|
|
||||||
tokenStr = cookie.Value
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue