Skip to content

Commit 7de3beb

Browse files
author
khanh.nguyen
committed
Updated implementation and test cases
1 parent d117afc commit 7de3beb

File tree

3 files changed

+178
-311
lines changed

3 files changed

+178
-311
lines changed

signature/claims.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ type Claims struct {
2020
// immediately following call to Valid() by jwt-go has *all* necessary information to
2121
// determine whether JWT is valid. These fields should not be overwritten by JSON
2222
// unmarshal.
23-
receivedTime time.Time `json:"-"`
24-
correctPayloadHash string `json:"-"`
25-
correctURLHash string `json:"-"`
23+
receivedTime time.Time
24+
correctPayloadHash string
25+
correctURLHash string
2626

2727
Issuer string `json:"iss"`
28-
IssuedAt int64 `json:"iat"`
28+
NotBefore int64 `json:"nbf"`
2929
ExpirationTime int64 `json:"exp"`
3030
JWTID string `json:"jti"`
3131
URLHash string `json:"url_hash"`
@@ -38,11 +38,11 @@ func (c Claims) Valid() error {
3838
var errs []string
3939

4040
if c.Issuer != "MessageBird" {
41-
errs = append(errs, "wrong iss")
41+
errs = append(errs, "claim iss has wrong value")
4242
}
4343

44-
if iat := time.Unix(c.IssuedAt, int64(c.receivedTime.Nanosecond())).Add(-maxSkew); c.receivedTime.Before(iat) {
45-
errs = append(errs, "claim iat is in the future")
44+
if iat := time.Unix(c.NotBefore, int64(c.receivedTime.Nanosecond())).Add(-maxSkew); c.receivedTime.Before(iat) {
45+
errs = append(errs, "claim nbf is in the future")
4646
}
4747

4848
if exp := time.Unix(c.ExpirationTime, int64(c.receivedTime.Nanosecond())).Add(maxSkew); c.receivedTime.After(exp) {

signature/signature_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ func TestValidSignature(t *testing.T) {
9696
Payload string `json:"payload"`
9797
Timestamp string `json:"timestamp"`
9898
Token string `json:"token"`
99-
Outcome string `json:"outcome"`
99+
Valid bool `json:"valid"`
100+
Reason string `json:"reason"`
100101
}
101102
if err := json.Unmarshal(testData, &tcs); err != nil {
102103
assert.NoError(t, err)
@@ -109,13 +110,13 @@ func TestValidSignature(t *testing.T) {
109110
return r
110111
}
111112

112-
v := NewValidator(testSecret)
113+
v := NewValidator([]byte(tc.Secret))
113114
err := v.ValidSignature(tc.Token, tc.Url, []byte(tc.Payload))
114-
if tc.Outcome == "valid" {
115+
if tc.Valid {
115116
assert.NoError(t, err)
116117
return
117118
}
118-
assert.EqualError(t, err, tc.Outcome)
119+
assert.EqualError(t, err, tc.Reason)
119120
})
120121
}
121122
}

0 commit comments

Comments
 (0)