From fcf2c975beb3edf5fd627d4904f693ebfc3c4363 Mon Sep 17 00:00:00 2001 From: Peter Kieltyka Date: Wed, 26 Sep 2018 13:55:45 -0400 Subject: [PATCH] add MapClaims helper methods --- Gopkg.toml | 6 ------ go.mod | 3 +++ go.sum | 2 ++ jwtauth.go | 22 +++++++++++++++++++++- 4 files changed, 26 insertions(+), 7 deletions(-) delete mode 100644 Gopkg.toml create mode 100644 go.mod create mode 100644 go.sum diff --git a/Gopkg.toml b/Gopkg.toml deleted file mode 100644 index b495b7b..0000000 --- a/Gopkg.toml +++ /dev/null @@ -1,6 +0,0 @@ -[[constraint]] - name = "github.com/dgrijalva/jwt-go" - version = "^3.1.0" -[[constraint]] - name = "github.com/go-chi/chi" - version = "^3.0.0" diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..5998327 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/go-chi/jwtauth + +require github.com/dgrijalva/jwt-go v3.2.0+incompatible diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..6a8f140 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= +github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= diff --git a/jwtauth.go b/jwtauth.go index 4f1130d..1118add 100644 --- a/jwtauth.go +++ b/jwtauth.go @@ -131,7 +131,7 @@ func VerifyRequest(ja *JWTAuth, r *http.Request, findTokenFns ...func(r *http.Re return token, nil } -func (ja *JWTAuth) Encode(claims jwt.MapClaims) (t *jwt.Token, tokenString string, err error) { +func (ja *JWTAuth) Encode(claims jwt.Claims) (t *jwt.Token, tokenString string, err error) { t = jwt.New(ja.signer) t.Claims = claims tokenString, err = t.SignedString(ja.signKey) @@ -218,6 +218,26 @@ func ExpireIn(tm time.Duration) int64 { return EpochNow() + int64(tm.Seconds()) } +// Set issued at ("iat") to specified time in the claims +func SetIssuedAt(claims jwt.MapClaims, tm time.Time) { + claims["iat"] = tm.UTC().Unix() +} + +// Set issued at ("iat") to present time in the claims +func SetIssuedNow(claims jwt.MapClaims) { + claims["iat"] = EpochNow() +} + +// Set expiry ("exp") in the claims +func SetExpiry(claims jwt.MapClaims, tm time.Time) { + claims["exp"] = tm.UTC().Unix() +} + +// Set expiry ("exp") in the claims to some duration from the present time +func SetExpiryIn(claims jwt.MapClaims, tm time.Duration) { + claims["exp"] = ExpireIn(tm) +} + // TokenFromCookie tries to retreive the token string from a cookie named // "jwt". func TokenFromCookie(r *http.Request) string {