mirror of
https://forgejo.merr.is/annika/jwtauth.git
synced 2025-12-11 13:47:41 -05:00
add MapClaims helper methods
This commit is contained in:
parent
ea7d7e213f
commit
fcf2c975be
4 changed files with 26 additions and 7 deletions
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
|
||||
}
|
||||
|
||||
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 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue