Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
111 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package urlutil | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
// BuildTimeParameters adds the issued and expiry timestamps to the query parameters. | ||
func BuildTimeParameters(params url.Values, expiry time.Duration) { | ||
now := time.Now() | ||
|
||
params.Set(QueryIssued, fmt.Sprint(now.UnixMilli())) | ||
params.Set(QueryExpiry, fmt.Sprint(now.Add(expiry).UnixMilli())) | ||
} | ||
|
||
// ValidateTimeParameters validates that the issued and expiry timestamps in the query parameters are valid. | ||
func ValidateTimeParameters(params url.Values) error { | ||
now := time.Now() | ||
|
||
issuedMS, err := strconv.ParseInt(params.Get(QueryIssued), 10, 64) | ||
if err != nil { | ||
return fmt.Errorf("invalid issued timestamp: %w", err) | ||
} | ||
issued := time.UnixMilli(issuedMS) | ||
|
||
if now.Add(DefaultLeeway).Before(issued) { | ||
return ErrIssuedInTheFuture | ||
} | ||
|
||
expiryMS, err := strconv.ParseInt(params.Get(QueryExpiry), 10, 64) | ||
if err != nil { | ||
return fmt.Errorf("invalid expiry timestamp: %w", err) | ||
} | ||
expiry := time.UnixMilli(expiryMS) | ||
|
||
if now.Add(-DefaultLeeway).After(expiry) { | ||
return ErrExpired | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package urlutil | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
"strconv" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestBuildTimeParameters(t *testing.T) { | ||
t.Parallel() | ||
|
||
params := make(url.Values) | ||
BuildTimeParameters(params, time.Minute) | ||
assert.True(t, params.Has(QueryIssued)) | ||
assert.True(t, params.Has(QueryExpiry)) | ||
|
||
ms1, _ := strconv.Atoi(params.Get(QueryIssued)) | ||
ms2, _ := strconv.Atoi(params.Get(QueryExpiry)) | ||
assert.Equal(t, 60000, ms2-ms1) | ||
} | ||
|
||
func TestValidateTimeParameters(t *testing.T) { | ||
t.Parallel() | ||
|
||
msNow := time.Now().UnixMilli() | ||
for _, tc := range []struct { | ||
name string | ||
params url.Values | ||
err string | ||
}{ | ||
{"empty", url.Values{}, "invalid issued timestamp"}, | ||
{"missing issued", url.Values{QueryExpiry: {fmt.Sprint(msNow + 10000)}}, "invalid issued timestamp"}, | ||
{"missing expiry", url.Values{QueryIssued: {fmt.Sprint(msNow + 10000)}}, "invalid expiry timestamp"}, | ||
{"invalid issued", url.Values{ | ||
QueryIssued: {fmt.Sprint(msNow + 120000)}, | ||
QueryExpiry: {fmt.Sprint(msNow + 240000)}, | ||
}, "issued in the future"}, | ||
{"invalid expiry", url.Values{ | ||
QueryIssued: {fmt.Sprint(msNow - 120000)}, | ||
QueryExpiry: {fmt.Sprint(msNow - 240000)}, | ||
}, "expired"}, | ||
{"valid", url.Values{ | ||
QueryIssued: {fmt.Sprint(msNow)}, | ||
QueryExpiry: {fmt.Sprint(msNow)}, | ||
}, ""}, | ||
} { | ||
tc := tc | ||
t.Run(tc.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
err := ValidateTimeParameters(tc.params) | ||
if tc.err == "" { | ||
assert.NoError(t, err) | ||
} else { | ||
assert.ErrorContains(t, err, tc.err) | ||
} | ||
}) | ||
} | ||
} |