From 083e467549a53d823668fd1edd7596dcee31177a Mon Sep 17 00:00:00 2001 From: Jun Nishimura Date: Sun, 17 Mar 2024 15:01:53 +0900 Subject: [PATCH 1/3] add new functions --- jwtauth.go | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/jwtauth.go b/jwtauth.go index a03731a..9e1a46e 100644 --- a/jwtauth.go +++ b/jwtauth.go @@ -251,10 +251,25 @@ func SetExpiryIn(claims map[string]interface{}, tm time.Duration) { claims["exp"] = ExpireIn(tm) } +const defaultCookieName = "jwt" + +// 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 "" } @@ -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. // @@ -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 From 9f0acdc701c851b71c067e61a190c0d0c6abdb87 Mon Sep 17 00:00:00 2001 From: Jun Nishimura Date: Sun, 22 Sep 2024 14:29:57 +0900 Subject: [PATCH 2/3] change function names to make it easier to understand the usage of of a function from the function name --- jwtauth.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/jwtauth.go b/jwtauth.go index 9e1a46e..7a5843b 100644 --- a/jwtauth.go +++ b/jwtauth.go @@ -253,9 +253,8 @@ func SetExpiryIn(claims map[string]interface{}, tm time.Duration) { const defaultCookieName = "jwt" -// TokenFromCookieWith tries to retreive the token string from a cookie with the -// specified name. -func TokenFromCookieWith(name string) func(r *http.Request) string { +// 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) } @@ -304,17 +303,17 @@ func TokenFromQuery(r *http.Request) string { return getTokenFromQuery(r, defaultQueryParam) } -// TokenFromQueryWith tries to retreive the token string from the specified +// 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, TokenFromQueryWith("token"), TokenFromHeader, TokenFromCookie)(next) +// return Verify(ja, TokenFromQueryByName("token"), TokenFromHeader, TokenFromCookie)(next) // } // } -func TokenFromQueryWith(param string) func(r *http.Request) string { +func TokenFromQueryByName(param string) func(r *http.Request) string { return func(r *http.Request) string { return getTokenFromQuery(r, param) } From 33dcb531b11979d470677e4719482f80cb46f4fa Mon Sep 17 00:00:00 2001 From: Jun Nishimura Date: Sun, 22 Sep 2024 14:34:04 +0900 Subject: [PATCH 3/3] combine multiple constants into one --- jwtauth.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/jwtauth.go b/jwtauth.go index 7a5843b..39bfbe4 100644 --- a/jwtauth.go +++ b/jwtauth.go @@ -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), @@ -251,8 +253,6 @@ func SetExpiryIn(claims map[string]interface{}, tm time.Duration) { claims["exp"] = ExpireIn(tm) } -const defaultCookieName = "jwt" - // 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 { @@ -263,7 +263,7 @@ func TokenFromCookieByName(name string) func(r *http.Request) string { // TokenFromCookie tries to retreive the token string from a cookie named // "jwt". func TokenFromCookie(r *http.Request) string { - return getTokenFromCookie(r, defaultCookieName) + return getTokenFromCookie(r, defaultTokenKeyName) } // get token from cookie @@ -286,8 +286,6 @@ func TokenFromHeader(r *http.Request) string { return "" } -const defaultQueryParam = "jwt" - // TokenFromQuery tries to retreive the token string from the "jwt" URI // query parameter. // @@ -300,7 +298,7 @@ const defaultQueryParam = "jwt" // } func TokenFromQuery(r *http.Request) string { // Get token from query param named "jwt". - return getTokenFromQuery(r, defaultQueryParam) + return getTokenFromQuery(r, defaultTokenKeyName) } // TokenFromQueryByName tries to retreive the token string from the specified