-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
51901bf
commit 6eb1a2f
Showing
4 changed files
with
121 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package paseto | ||
|
||
type RegisteredClaims struct { | ||
ID string `json:"jti,omitempty"` | ||
Audience string `json:"aud,omitempty"` | ||
Issuer string `json:"iss,omitempty"` | ||
Subject string `json:"sub,omitempty"` | ||
ExpiresAt *NumericDate `json:"exp,omitempty"` | ||
IssuedAt *NumericDate `json:"iat,omitempty"` | ||
NotBefore *NumericDate `json:"nbf,omitempty"` | ||
} |
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,38 @@ | ||
package paseto | ||
|
||
import ( | ||
"encoding/json" | ||
"math" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
type NumericDate struct { | ||
time.Time | ||
} | ||
|
||
func NewNumericDate(t time.Time) *NumericDate { | ||
return &NumericDate{t} | ||
} | ||
|
||
func (t NumericDate) MarshalJSON() ([]byte, error) { | ||
if t.IsZero() { | ||
return []byte("null"), nil | ||
} | ||
return []byte(strconv.FormatInt(t.Unix(), 10)), nil | ||
} | ||
|
||
func (t *NumericDate) UnmarshalJSON(data []byte) error { | ||
var value json.Number | ||
if err := json.Unmarshal(data, &value); err != nil { | ||
return ErrDateInvalidFormat | ||
} | ||
f, err := value.Float64() | ||
if err != nil { | ||
return ErrDateInvalidFormat | ||
} | ||
sec, dec := math.Modf(f) | ||
ts := time.Unix(int64(sec), int64(dec*1e9)) | ||
*t = NumericDate{ts} | ||
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,71 @@ | ||
package paseto | ||
|
||
import ( | ||
"strconv" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestNumericDateMarshal(t *testing.T) { | ||
now := time.Now() | ||
nowTS := now.Unix() | ||
|
||
testCases := []struct { | ||
value *NumericDate | ||
want string | ||
}{ | ||
{NewNumericDate(time.Time{}), `null`}, | ||
{NewNumericDate(now), strconv.Itoa(int(nowTS))}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
raw, err := tc.value.MarshalJSON() | ||
mustOk(t, err) | ||
mustEqual(t, string(raw), tc.want) | ||
} | ||
} | ||
|
||
func TestNumericDateUnmarshal(t *testing.T) { | ||
testCases := []struct { | ||
s string | ||
want NumericDate | ||
}{ | ||
{`1588707274`, asNumericDate(1588707274)}, | ||
{`1588707274.3769999`, asNumericDate(1588707274)}, | ||
{`"12345"`, asNumericDate(12345)}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
var have NumericDate | ||
err := have.UnmarshalJSON([]byte(tc.s)) | ||
mustOk(t, err) | ||
mustEqual(t, have.Unix(), tc.want.Unix()) | ||
} | ||
} | ||
|
||
func TestNumericDateUnmarshalMalformed(t *testing.T) { | ||
testCases := []struct { | ||
value string | ||
}{ | ||
{``}, | ||
{`{}`}, | ||
{`[{}]`}, | ||
{`abc12`}, | ||
{`"abc"`}, | ||
{`["admin",{}]`}, | ||
{`["admin",123]`}, | ||
{`{}`}, | ||
{`[]`}, | ||
{`1e+309`}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
var nd NumericDate | ||
err := nd.UnmarshalJSON([]byte(tc.value)) | ||
mustFail(t, err) | ||
} | ||
} | ||
|
||
func asNumericDate(n int64) NumericDate { | ||
return *NewNumericDate(time.Unix(n, 0)) | ||
} |