mirror of
https://forgejo.merr.is/annika/jwtauth.git
synced 2025-12-11 12:13:14 -05:00
add MapClaims helper methods
This commit is contained in:
parent
ea7d7e213f
commit
fcf2c975be
4 changed files with 26 additions and 7 deletions
|
|
@ -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"
|
|
||||||
3
go.mod
Normal file
3
go.mod
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
module github.com/go-chi/jwtauth
|
||||||
|
|
||||||
|
require github.com/dgrijalva/jwt-go v3.2.0+incompatible
|
||||||
2
go.sum
Normal file
2
go.sum
Normal file
|
|
@ -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=
|
||||||
22
jwtauth.go
22
jwtauth.go
|
|
@ -131,7 +131,7 @@ func VerifyRequest(ja *JWTAuth, r *http.Request, findTokenFns ...func(r *http.Re
|
||||||
return token, nil
|
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 = jwt.New(ja.signer)
|
||||||
t.Claims = claims
|
t.Claims = claims
|
||||||
tokenString, err = t.SignedString(ja.signKey)
|
tokenString, err = t.SignedString(ja.signKey)
|
||||||
|
|
@ -218,6 +218,26 @@ func ExpireIn(tm time.Duration) int64 {
|
||||||
return EpochNow() + int64(tm.Seconds())
|
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
|
// TokenFromCookie tries to retreive the token string from a cookie named
|
||||||
// "jwt".
|
// "jwt".
|
||||||
func TokenFromCookie(r *http.Request) string {
|
func TokenFromCookie(r *http.Request) string {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue