add "valid until" option to limit lifetime of signed requests

Closes #222

Co-authored-by: Will Norris <will@willnorris.com>
This commit is contained in:
sl 2020-03-04 18:18:47 +01:00 committed by Will Norris
parent b98b3455a1
commit fe35d19c3e
5 changed files with 58 additions and 5 deletions

View file

@ -110,8 +110,11 @@ func TestAllowed(t *testing.T) {
return req
}
now := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
tests := []struct {
url string
now time.Time
options Options
allowHosts []string
denyHosts []string
@ -153,6 +156,11 @@ func TestAllowed(t *testing.T) {
{url: "http://test/image", options: Options{Signature: "NDx5zZHx7QfE8E-ijowRreq6CJJBZjwiRfOVk_mkfQQ="}, denyHosts: []string{"test"}, keys: key, allowed: false},
{url: "http://127.0.0.1/image", denyHosts: []string{"127.0.0.0/8"}, allowed: false},
{url: "http://127.0.0.1:3000/image", denyHosts: []string{"127.0.0.0/8"}, allowed: false},
// valid until options
{url: "http://test/image", now: now, options: Options{ValidUntil: now.Add(time.Second)}, allowed: true},
{url: "http://test/image", now: now, options: Options{ValidUntil: now.Add(-time.Second)}, allowed: false},
{url: "http://test/image", now: now, options: Options{ValidUntil: now}, allowed: false},
}
for _, tt := range tests {
@ -161,6 +169,7 @@ func TestAllowed(t *testing.T) {
p.DenyHosts = tt.denyHosts
p.SignatureKeys = tt.keys
p.Referrers = tt.referrers
p.timeNow = tt.now
u, err := url.Parse(tt.url)
if err != nil {