From 2149328bd9d7df9aa2bd343e6877269845a51ed1 Mon Sep 17 00:00:00 2001 From: Aymen Dhahri <41507665+DroidZed@users.noreply.github.com> Date: Tue, 5 Sep 2023 19:25:53 +0100 Subject: [PATCH 1/3] Added setSub to the library --- jwtauth.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/jwtauth.go b/jwtauth.go index bc42ae3..28059c6 100644 --- a/jwtauth.go +++ b/jwtauth.go @@ -238,6 +238,11 @@ func SetExpiryIn(claims map[string]interface{}, tm time.Duration) { claims["exp"] = ExpireIn(tm) } +// Set subject ("sub") in the claims +func setSub(claims map[string]interface{}, sub string) { + claims["sub"] = sub +} + // TokenFromCookie tries to retreive the token string from a cookie named // "jwt". func TokenFromCookie(r *http.Request) string { From 68aa1e470bdb487c52868a57f73d4fca27ac4025 Mon Sep 17 00:00:00 2001 From: Aymen Dhahri <41507665+DroidZed@users.noreply.github.com> Date: Wed, 6 Sep 2023 06:40:43 +0100 Subject: [PATCH 2/3] Added rest of the methods in RFC 7519 standard --- jwtauth.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/jwtauth.go b/jwtauth.go index 28059c6..bb53ead 100644 --- a/jwtauth.go +++ b/jwtauth.go @@ -243,6 +243,26 @@ func setSub(claims map[string]interface{}, sub string) { claims["sub"] = sub } +// Set audience ("aud") in the claims +func setAudience(claims map[string]interface{}, aud string) { + claims["aud"] = aud +} + +// Set JWT ID ("jti") in the claims +func setJwtId(claims map[string]interface{}, jti string) { + claims["jti"] = jti +} + +// Set not Before ("nbf") in the claims +func setNotBefore(claims map[string]interface{}, nbf time.Time) { + claims["nbf"] = nbf.UTC().Unix() +} + +// Add custom additional claim in the claims (For private/public claims, RFC 7519) +func setXClaim(claims map[string]interface{}, claimName string, claimValue interface{}) { + claims[claimName] = claimValue +} + // TokenFromCookie tries to retreive the token string from a cookie named // "jwt". func TokenFromCookie(r *http.Request) string { From 2676320d4019be918ddb8b409d16536f7beb886d Mon Sep 17 00:00:00 2001 From: Aymen Dhahri <41507665+DroidZed@users.noreply.github.com> Date: Tue, 18 Jun 2024 13:50:12 +0100 Subject: [PATCH 3/3] Exported methods jwtauth.go --- jwtauth.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/jwtauth.go b/jwtauth.go index bb53ead..024ebea 100644 --- a/jwtauth.go +++ b/jwtauth.go @@ -130,11 +130,11 @@ func (ja *JWTAuth) Decode(tokenString string) (jwt.Token, error) { return ja.parse([]byte(tokenString)) } -func (ja *JWTAuth) sign(token jwt.Token) ([]byte, error) { +func (ja *JWTAuth) Sign(token jwt.Token) ([]byte, error) { return jwt.Sign(token, jwt.WithKey(ja.alg, ja.signKey)) } -func (ja *JWTAuth) parse(payload []byte) (jwt.Token, error) { +func (ja *JWTAuth) Parse(payload []byte) (jwt.Token, error) { // we disable validation here because we use jwt.Validate to validate tokens return jwt.Parse(payload, ja.verifier, jwt.WithValidate(false)) } @@ -239,27 +239,27 @@ func SetExpiryIn(claims map[string]interface{}, tm time.Duration) { } // Set subject ("sub") in the claims -func setSub(claims map[string]interface{}, sub string) { +func SetSub(claims map[string]interface{}, sub string) { claims["sub"] = sub } // Set audience ("aud") in the claims -func setAudience(claims map[string]interface{}, aud string) { +func SetAudience(claims map[string]interface{}, aud string) { claims["aud"] = aud } // Set JWT ID ("jti") in the claims -func setJwtId(claims map[string]interface{}, jti string) { +func SetJwtId(claims map[string]interface{}, jti string) { claims["jti"] = jti } // Set not Before ("nbf") in the claims -func setNotBefore(claims map[string]interface{}, nbf time.Time) { +func SetNotBefore(claims map[string]interface{}, nbf time.Time) { claims["nbf"] = nbf.UTC().Unix() } // Add custom additional claim in the claims (For private/public claims, RFC 7519) -func setXClaim(claims map[string]interface{}, claimName string, claimValue interface{}) { +func SetXclaim(claims map[string]interface{}, claimName string, claimValue interface{}) { claims[claimName] = claimValue } @@ -302,7 +302,7 @@ func TokenFromQuery(r *http.Request) string { // contextKey is a value for use with context.WithValue. It's used as // a pointer so it fits in an interface{} without allocation. This technique // for defining context keys was copied from Go 1.7's new use of context in net/http. -type contextKey struct { +type ContextKey struct { name string }