Skip to content

Commit

Permalink
minor: add optional MaxAge field to the multiple keys signer
Browse files Browse the repository at this point in the history
  • Loading branch information
kataras committed Mar 21, 2022
1 parent 35647cc commit db37b5f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:

strategy:
matrix:
go_version: [1.16]
go_version: [1.18]
steps:

- name: Set up Go 1.x
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/kataras/jwt

go 1.15
go 1.18
16 changes: 14 additions & 2 deletions kid_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"strconv"
"strings"
"time"
)

var (
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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"`
}
)

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit db37b5f

Please sign in to comment.