Skip to content
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

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

Open
wants to merge 1 commit 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
42 changes: 40 additions & 2 deletions jwtauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,25 @@ func SetExpiryIn(claims map[string]interface{}, tm time.Duration) {
claims["exp"] = ExpireIn(tm)
}

const defaultCookieName = "jwt"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a single const (for cookie/header/query param) would be enough


// TokenFromCookieWith tries to retreive the token string from a cookie with the
// specified name.
func TokenFromCookieWith(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, defaultCookieName)
}

// get token from cookie
func getTokenFromCookie(r *http.Request, name string) string {
cookie, err := r.Cookie(name)
if err != nil {
return ""
}
Expand All @@ -272,6 +287,8 @@ func TokenFromHeader(r *http.Request) string {
return ""
}

const defaultQueryParam = "jwt"

// TokenFromQuery tries to retreive the token string from the "jwt" URI
// query parameter.
//
Expand All @@ -284,7 +301,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, defaultQueryParam)
}

// TokenFromQueryWith 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, TokenFromQueryWith("token"), TokenFromHeader, TokenFromCookie)(next)
// }
// }
func TokenFromQueryWith(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