Skip to content

Commit ddd9ca1

Browse files
committed
returns - added better returns and error checks
1 parent 358059b commit ddd9ca1

File tree

6 files changed

+82
-27
lines changed

6 files changed

+82
-27
lines changed

claims.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,21 @@ import (
66
)
77

88
// Claims is the main container for our body information
9-
type Claims map[string]interface{}
9+
type Claims map[string]any
1010

1111
// New will initiate a new claims
1212
func New() *Claims {
1313
return &Claims{}
1414
}
1515

1616
// ToClaims takes in an interface and unmarshals it to claims
17-
func ToClaims(struc interface{}) (Claims, error) {
18-
strucBytes, _ := json.Marshal(struc)
17+
func ToClaims(struc any) (Claims, error) {
18+
strucBytes, err := json.Marshal(struc)
19+
if err != nil {
20+
return nil, err
21+
}
1922
var claims Claims
20-
err := json.Unmarshal(strucBytes, &claims)
23+
err = json.Unmarshal(strucBytes, &claims)
2124
if err != nil {
2225
return nil, err
2326
}
@@ -26,9 +29,13 @@ func ToClaims(struc interface{}) (Claims, error) {
2629
}
2730

2831
// ToStruct takes your claims and sets value to struct
29-
func (c Claims) ToStruct(struc interface{}) error {
30-
claimsBytes, _ := json.Marshal(c)
31-
err := json.Unmarshal(claimsBytes, struc)
32+
func (c Claims) ToStruct(struc any) error {
33+
claimsBytes, err := json.Marshal(c)
34+
if err != nil {
35+
return err
36+
}
37+
38+
err = json.Unmarshal(claimsBytes, struc)
3239
if err != nil {
3340
return err
3441
}

claims_pub.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
)
77

88
// Set adds/sets a name/value to claims
9-
func (c Claims) Set(name string, value interface{}) { c[name] = value }
9+
func (c Claims) Set(name string, value any) { c[name] = value }
1010

1111
// Del deletes a name/value from claims
1212
func (c Claims) Del(name string) { delete(c, name) }
@@ -15,7 +15,7 @@ func (c Claims) Del(name string) { delete(c, name) }
1515
func (c Claims) Has(name string) bool { _, ok := c[name]; return ok }
1616

1717
// Get gets claim value
18-
func (c Claims) Get(name string) (interface{}, error) {
18+
func (c Claims) Get(name string) (any, error) {
1919
if !c.Has(name) {
2020
return nil, ErrNotFound
2121
}

claims_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ func TestToStruct(t *testing.T) {
3333

3434
// Try to set claims into struct
3535
var test testStruc
36-
claims.ToStruct(&test)
36+
if err := claims.ToStruct(&test); err != nil {
37+
t.Fatalf("ToStruct returned error: %v", err)
38+
}
3739

3840
if test.FirstName != "Billy" {
3941
t.Error("Tried to get first name from test struct after running ToStruct and it failed")
@@ -52,7 +54,11 @@ func TestValidate(t *testing.T) {
5254
}
5355

5456
// Validate on parsed claims
55-
token := claims.Generate([]byte(secretKey))
57+
token, err := claims.Generate([]byte(secretKey))
58+
if err != nil {
59+
t.Fatalf("Generate returned error: %v", err)
60+
}
61+
5662
parsedClaims, err := Parse(token)
5763
if err != nil {
5864
t.Error("Error parsing token: ", err)

example_test.go

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,20 @@ func Example() {
1313

1414
// Generate jwt
1515
secretKey := []byte("secret_key_here")
16-
jwt := claims.Generate(secretKey)
16+
jwt, err := claims.Generate(secretKey)
17+
if err != nil {
18+
panic(err)
19+
}
1720
fmt.Println(jwt)
1821
}
1922

2023
func Example_parse() {
2124
// Parse jwt
2225
jwt := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
23-
claims, _ := Parse(jwt)
26+
claims, err := Parse(jwt)
27+
if err != nil {
28+
panic(err)
29+
}
2430

2531
// Get claims
2632
name, _ := claims.GetStr("name")
@@ -41,7 +47,10 @@ func Example_registeredClaims() {
4147

4248
// Generate jwt
4349
secretKey := []byte("secret_key_here")
44-
jwt := claims.Generate(secretKey)
50+
jwt, err := claims.Generate(secretKey)
51+
if err != nil {
52+
panic(err)
53+
}
4554
fmt.Println(jwt)
4655
}
4756

@@ -53,7 +62,10 @@ func Example_publicClaims() {
5362

5463
// Generate jwt
5564
secretKey := []byte("secret_key_here")
56-
jwt := claims.Generate(secretKey)
65+
jwt, err := claims.Generate(secretKey)
66+
if err != nil {
67+
panic(err)
68+
}
5769
fmt.Println(jwt)
5870
}
5971

@@ -64,11 +76,17 @@ func Example_structToClaims() {
6476

6577
// Marshal your struct into claims
6678
info := Info{Name: "Billy Mister"}
67-
claims, _ := ToClaims(info)
79+
claims, err := ToClaims(info)
80+
if err != nil {
81+
panic(err)
82+
}
6883

6984
// Generate jwt
7085
secretKey := []byte("secret_key_here")
71-
jwt := claims.Generate(secretKey)
86+
jwt, err := claims.Generate(secretKey)
87+
if err != nil {
88+
panic(err)
89+
}
7290
fmt.Println(jwt)
7391
// output: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiQmlsbHkgTWlzdGVyIn0.2FYrpCNy1tg_4UvimpSrgAy-nT9snh-l4w9VLz71b6Y
7492
}
@@ -80,11 +98,16 @@ func Example_claimsToStruct() {
8098

8199
// Parse jwt
82100
jwt := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiQmlsbHkgTWlzdGVyIn0.2FYrpCNy1tg_4UvimpSrgAy-nT9snh-l4w9VLz71b6Y"
83-
claims, _ := Parse(jwt)
101+
claims, err := Parse(jwt)
102+
if err != nil {
103+
panic(err)
104+
}
84105

85106
// Marshal your struct into claims
86107
info := Info{}
87-
claims.ToStruct(&info)
108+
if err := claims.ToStruct(&info); err != nil {
109+
panic(err)
110+
}
88111

89112
name, _ := claims.GetStr("name")
90113
fmt.Println(name)

sjwt.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,18 @@ import (
1010
)
1111

1212
// Generate takes in claims and a secret and outputs jwt token
13-
func (c Claims) Generate(secret []byte) string {
13+
func (c Claims) Generate(secret []byte) (string, error) {
1414
// Encode header and claims
15-
headerEnc, _ := json.Marshal(map[string]string{"typ": "JWT", "alg": "HS256"})
16-
claimsEnc, _ := json.Marshal(c)
15+
headerEnc, err := json.Marshal(map[string]string{"typ": "JWT", "alg": "HS256"})
16+
if err != nil {
17+
return "", err
18+
}
19+
20+
claimsEnc, err := json.Marshal(c)
21+
if err != nil {
22+
return "", err
23+
}
24+
1725
jwtStr := fmt.Sprintf(
1826
"%s.%s",
1927
base64.RawURLEncoding.EncodeToString(headerEnc),
@@ -24,7 +32,7 @@ func (c Claims) Generate(secret []byte) string {
2432
mac := hmac.New(sha256.New, secret)
2533
mac.Write([]byte(jwtStr))
2634

27-
return fmt.Sprintf("%s.%s", jwtStr, base64.RawURLEncoding.EncodeToString(mac.Sum(nil)))
35+
return fmt.Sprintf("%s.%s", jwtStr, base64.RawURLEncoding.EncodeToString(mac.Sum(nil))), nil
2836
}
2937

3038
// Parse will take in the token string grab the body and unmarshal into claims interface

sjwt_test.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ var secretKey = []byte("whats up yall")
99
func TestGenerate(t *testing.T) {
1010
claims := New()
1111
claims.Set("hello", "world")
12-
jwt := claims.Generate(secretKey)
12+
jwt, err := claims.Generate(secretKey)
13+
if err != nil {
14+
t.Fatalf("Generate returned error: %v", err)
15+
}
1316
if jwt == "" {
1417
t.Error("jwt is empty")
1518
}
@@ -19,14 +22,19 @@ func BenchmarkGenerate(b *testing.B) {
1922
for i := 0; i < b.N; i++ {
2023
claims := New()
2124
claims.Set("hello", "world")
22-
claims.Generate(secretKey)
25+
if _, err := claims.Generate(secretKey); err != nil {
26+
b.Fatalf("Generate returned error: %v", err)
27+
}
2328
}
2429
}
2530

2631
func TestParse(t *testing.T) {
2732
claims := New()
2833
claims.Set("hello", "world")
29-
jwt := claims.Generate(secretKey)
34+
jwt, err := claims.Generate(secretKey)
35+
if err != nil {
36+
t.Fatalf("Generate returned error: %v", err)
37+
}
3038

3139
newClaims, err := Parse(jwt)
3240
if err != nil {
@@ -59,7 +67,10 @@ func TestParseDecodeError(t *testing.T) {
5967
func TestVerify(t *testing.T) {
6068
claims := New()
6169
claims.Set("hello", "world")
62-
jwt := claims.Generate(secretKey)
70+
jwt, err := claims.Generate(secretKey)
71+
if err != nil {
72+
t.Fatalf("Generate returned error: %v", err)
73+
}
6374

6475
verified := Verify(jwt, secretKey)
6576
if !verified {

0 commit comments

Comments
 (0)