Skip to content

Commit

Permalink
Add RegisteredClaims (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg authored Apr 15, 2024
1 parent 51901bf commit 6eb1a2f
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 0 deletions.
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))
}

0 comments on commit 6eb1a2f

Please sign in to comment.