Skip to content

Commit 7c2cb79

Browse files
committed
add expiresAfter to use a relative delay for expiration
1 parent 4f9db58 commit 7c2cb79

2 files changed

Lines changed: 30 additions & 14 deletions

File tree

config.go

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,27 @@ import (
1010

1111
// SignConfig contains additional configuration for the signer.
1212
type SignConfig struct {
13-
signAlg bool
14-
signCreated bool
15-
fakeCreated int64
16-
expires int64
17-
nonce string
18-
tag string
19-
keyID *string
13+
signAlg bool
14+
signCreated bool
15+
fakeCreated int64
16+
expires int64
17+
expiresAfter int64
18+
nonce string
19+
tag string
20+
keyID *string
2021
}
2122

2223
// NewSignConfig generates a default configuration.
2324
func NewSignConfig() *SignConfig {
2425
return &SignConfig{
25-
signAlg: true,
26-
signCreated: true,
27-
fakeCreated: 0,
28-
expires: 0,
29-
nonce: "",
30-
tag: "", // we disallow an empty tag
31-
keyID: nil,
26+
signAlg: true,
27+
signCreated: true,
28+
fakeCreated: 0,
29+
expires: 0,
30+
expiresAfter: 0,
31+
nonce: "",
32+
tag: "", // we disallow an empty tag
33+
keyID: nil,
3234
}
3335
}
3436

@@ -58,6 +60,14 @@ func (c *SignConfig) SetExpires(expires int64) *SignConfig {
5860
return c
5961
}
6062

63+
// SetExpiresAfter adds an "expires" parameter containing an expiration
64+
// deadline after the given delay, as Unix time.
65+
// Default: 0 (do not add the parameter).
66+
func (c *SignConfig) SetExpiresAfter(delay int64) *SignConfig {
67+
c.expiresAfter = delay
68+
return c
69+
}
70+
6171
// SetNonce adds a "nonce" string parameter whose content should be unique per signed message.
6272
// Default: empty string (do not add the parameter).
6373
func (c *SignConfig) SetNonce(nonce string) *SignConfig {

signatures.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,12 @@ func generateSigParams(config *SignConfig, alg string, foreignSigner interface{}
272272
if config.expires != 0 {
273273
p.Add("expires", config.expires)
274274
}
275+
if config.expiresAfter != 0 {
276+
if config.expires != 0 {
277+
return "", fmt.Errorf("cannot use both expires and expiresAfter")
278+
}
279+
p.Add("expires", config.expiresAfter+createdTime)
280+
}
275281
if config.nonce != "" {
276282
qNonce, err := quotedString(config.nonce)
277283
if err != nil {

0 commit comments

Comments
 (0)