Skip to content
This repository was archived by the owner on Apr 22, 2024. It is now read-only.

Commit 5481a4c

Browse files
author
Rowley
committed
fixing issues
1 parent 952dbad commit 5481a4c

File tree

4 files changed

+12
-12
lines changed

4 files changed

+12
-12
lines changed

algorithms.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func (a *Algorithm) Decode(encoded string) (*Claims, error) {
9090

9191
b64Payload := encryptedComponents[1]
9292

93-
var claims map[string]string
93+
var claims map[string]interface{}
9494
payload, err := base64.StdEncoding.DecodeString(b64Payload)
9595
if err != nil {
9696
return nil, errors.Wrap(err, "unable to decode base64 payload")
@@ -149,6 +149,7 @@ func (a *Algorithm) validateSignature(encoded string) error {
149149
return nil
150150
}
151151

152+
152153
func (a *Algorithm) validateExp(claims *Claims) error {
153154
if claims.HasClaim("exp") {
154155
exp, err := claims.GetTime("exp")

claims.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package jwt
22

33
import (
4-
"fmt"
54
"time"
65

76
"github.com/pkg/errors"
@@ -39,7 +38,7 @@ func (c *Claims) Set(key string, value interface{}) {
3938

4039
// SetTime sets the claim given to the specified time.
4140
func (c *Claims) SetTime(key string, value time.Time) {
42-
c.Set(key, fmt.Sprintf("%d", value.Unix()))
41+
c.Set(key, value.Unix())
4342
}
4443

4544
// Get returns the claim in string form and returns an error if the specified claim doesn't exist.

example/example.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@ func main() {
3737
panic(err)
3838
}
3939

40-
if strings.Compare(role, "Admin") == 0 {
40+
roleString, ok := role.(string)
41+
if !ok {
42+
panic(err)
43+
}
44+
45+
if strings.Compare(roleString, "Admin") == 0 {
4146
//user is an admin
4247
fmt.Println("User is an admin")
4348
}

jwt_test.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,8 @@ func RunTest(t *testing.T, command func(Algorithm)) {
2424
func TestEncodeAndValidateToken(t *testing.T) {
2525
RunTest(t, func(algorithm Algorithm) {
2626
payload := NewClaim()
27-
payload.SetTime("nbf", time.Now().Add(-1*time.Hour))
28-
payload.SetTime("exp", time.Now().Add(1*time.Hour))
29-
30-
err := json.Unmarshal([]byte(`{"sub":"1234567890","name":"John Doe","admin":true}`), &payload)
31-
if err != nil {
32-
t.Fatal(err)
33-
}
27+
payload.SetTime("nbf", time.Now().Add(time.Duration(-1) * time.Hour))
28+
payload.SetTime("exp", time.Now().Add(time.Duration(100) * time.Hour))
3429

3530
token, err := algorithm.Encode(payload)
3631
if err != nil {
@@ -95,7 +90,7 @@ func TestVerifyTokenNbf(t *testing.T) {
9590
RunTest(t, func(algorithm Algorithm) {
9691

9792
payload := NewClaim()
98-
payload.Set("nbf", fmt.Sprintf("%d", time.Now().Add(1*time.Hour).Unix()))
93+
payload.SetTime("nbf", time.Now().Add(time.Duration(1) * time.Hour))
9994

10095
err := json.Unmarshal([]byte(`{"sub":"1234567890","name":"John Doe","admin":true}`), &payload)
10196
if err != nil {

0 commit comments

Comments
 (0)