Skip to content

Allow users to retrieve JWT from a query and cookie with a specified name #89

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions jwtauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ var (
ErrAlgoInvalid = errors.New("algorithm mismatch")
)

const defaultTokenKeyName = "jwt"

func New(alg string, signKey interface{}, verifyKey interface{}, validateOptions ...jwt.ValidateOption) *JWTAuth {
ja := &JWTAuth{
alg: jwa.SignatureAlgorithm(alg),
Expand Down Expand Up @@ -251,10 +253,22 @@ func SetExpiryIn(claims map[string]interface{}, tm time.Duration) {
claims["exp"] = ExpireIn(tm)
}

// TokenFromCookieByName tries to retreive the token string from the specified cookie name.
func TokenFromCookieByName(name string) func(r *http.Request) string {
return func(r *http.Request) string {
return getTokenFromCookie(r, name)
}
}

// TokenFromCookie tries to retreive the token string from a cookie named
// "jwt".
func TokenFromCookie(r *http.Request) string {
cookie, err := r.Cookie("jwt")
return getTokenFromCookie(r, defaultTokenKeyName)
}

// get token from cookie
func getTokenFromCookie(r *http.Request, name string) string {
cookie, err := r.Cookie(name)
if err != nil {
return ""
}
Expand Down Expand Up @@ -284,7 +298,28 @@ func TokenFromHeader(r *http.Request) string {
// }
func TokenFromQuery(r *http.Request) string {
// Get token from query param named "jwt".
return r.URL.Query().Get("jwt")
return getTokenFromQuery(r, defaultTokenKeyName)
}

// TokenFromQueryByName tries to retreive the token string from the specified
// URI query parameter.
//
// To use it, build our own middleware handler, such as:
//
// func Verifier(ja *JWTAuth) func(http.Handler) http.Handler {
// return func(next http.Handler) http.Handler {
// return Verify(ja, TokenFromQueryByName("token"), TokenFromHeader, TokenFromCookie)(next)
// }
// }
func TokenFromQueryByName(param string) func(r *http.Request) string {
return func(r *http.Request) string {
return getTokenFromQuery(r, param)
}
}

// get token from query param
func getTokenFromQuery(r *http.Request, param string) string {
return r.URL.Query().Get(param)
}

// contextKey is a value for use with context.WithValue. It's used as
Expand Down