-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
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"` | ||
} |
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 | ||
Check failure on line 28 in numeric_date.go GitHub Actions / vuln / Vuln
Check failure on line 28 in numeric_date.go GitHub Actions / vuln / Vuln
Check failure on line 28 in numeric_date.go GitHub Actions / vuln / Vuln
Check failure on line 28 in numeric_date.go GitHub Actions / vuln / Vuln
Check failure on line 28 in numeric_date.go GitHub Actions / build / Build (stable)
|
||
} | ||
f, err := value.Float64() | ||
if err != nil { | ||
return ErrDateInvalidFormat | ||
Check failure on line 32 in numeric_date.go GitHub Actions / vuln / Vuln
Check failure on line 32 in numeric_date.go GitHub Actions / vuln / Vuln
Check failure on line 32 in numeric_date.go GitHub Actions / vuln / Vuln
Check failure on line 32 in numeric_date.go GitHub Actions / vuln / Vuln
Check failure on line 32 in numeric_date.go GitHub Actions / build / Build (stable)
|
||
} | ||
sec, dec := math.Modf(f) | ||
ts := time.Unix(int64(sec), int64(dec*1e9)) | ||
*t = NumericDate{ts} | ||
return nil | ||
} |
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)) | ||
} |