Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RegisteredClaims #15

Merged
merged 2 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions claims.go
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"`
}
1 change: 1 addition & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ var (
ErrInvalidTokenAuth = errors.New("invalid token authentication")
ErrIncorrectTokenFormat = errors.New("incorrect token format")
ErrIncorrectTokenHeader = errors.New("incorrect token header")
ErrDateInvalidFormat = errors.New("date is not valid")
)
38 changes: 38 additions & 0 deletions numeric_date.go
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
}
71 changes: 71 additions & 0 deletions numeric_date_test.go
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))
}
Loading