diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 72a7abc..77cc555 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -14,7 +14,7 @@ jobs: strategy: matrix: - go_version: [1.16] + go_version: [1.18] steps: - name: Set up Go 1.x diff --git a/go.mod b/go.mod index cba9f58..4a51c6b 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module github.com/kataras/jwt -go 1.15 +go 1.18 diff --git a/kid_keys.go b/kid_keys.go index a347fc8..0c53765 100644 --- a/kid_keys.go +++ b/kid_keys.go @@ -5,6 +5,7 @@ import ( "fmt" "strconv" "strings" + "time" ) var ( @@ -32,6 +33,7 @@ type ( Alg Alg Public PublicKey Private PrivateKey + MaxAge time.Duration // optional. } // Keys is a map which holds the key id and a key pair. @@ -73,6 +75,11 @@ type ( Alg string `json:"alg" yaml:"Alg" toml:"Alg" ini:"alg"` Private string `json:"private" yaml:"Private" toml:"Private" ini:"private"` Public string `json:"public" yaml:"Public" toml:"Public" ini:"public"` + // Token expiration. Optional. + // If greater than zero then the MaxAge token validation + // will be appended to the "VerifyToken" and the token is invalid + // after expiration of its sign time. + MaxAge time.Duration `json:"max_age" yaml:"MaxAge" toml:"MaxAge" ini:"max_age"` } ) @@ -101,8 +108,9 @@ func (c KeysConfiguration) Load() (Keys, error) { } p := &Key{ - ID: entry.ID, - Alg: alg, + ID: entry.ID, + Alg: alg, + MaxAge: entry.MaxAge, } if public, err := strconv.Unquote(entry.Public); err == nil { @@ -183,6 +191,10 @@ func (keys Keys) SignToken(kid string, claims interface{}, opts ...SignOption) ( return nil, ErrUnknownKid } + if k.MaxAge > 0 { + opts = append([]SignOption{MaxAge(k.MaxAge)}, opts...) + } + return SignWithHeader(k.Alg, k.Private, claims, HeaderWithKid{ Kid: kid, Alg: k.Alg.Name(),