From bfe701fdf5f92c3238e3e69a6578660ff6cc998b Mon Sep 17 00:00:00 2001 From: Alex Browne Date: Sat, 27 Dec 2014 14:14:23 -0500 Subject: [PATCH] When possible, instead of identifying signing methods by string, pass them in directly by name. This is less error-prone and avoids an unnecessary map lookup. Also encourage this type of usage by using it in the README. --- README.md | 2 +- jwt_test.go | 2 +- rsa_test.go | 6 ++---- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 99942d8..f0a8f9d 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ Parsing and verifying tokens is pretty straight forward. You pass in the token ```go // Create the token - token := jwt.New(jwt.GetSigningMethod("HS256")) + token := jwt.New(SigningMethodHS256) // Set some claims token.Claims["foo"] = "bar" token.Claims["exp"] = time.Now().Add(time.Hour * 72).Unix() diff --git a/jwt_test.go b/jwt_test.go index 557c850..a457c02 100644 --- a/jwt_test.go +++ b/jwt_test.go @@ -104,7 +104,7 @@ func makeSample(c map[string]interface{}) string { panic(e.Error()) } - token := New(GetSigningMethod("RS256")) + token := New(SigningMethodRS256) token.Claims = c s, e := token.SignedString(key) diff --git a/rsa_test.go b/rsa_test.go index 8d96fb6..a36dbd8 100644 --- a/rsa_test.go +++ b/rsa_test.go @@ -80,14 +80,13 @@ func TestRSASign(t *testing.T) { func TestRSAVerifyWithPreParsedPrivateKey(t *testing.T) { key, _ := ioutil.ReadFile("test/sample_key.pub") - method := GetSigningMethod("RS256").(*SigningMethodRSA) parsedKey, err := ParseRSAPublicKeyFromPEM(key) if err != nil { t.Fatal(err) } testData := rsaTestData[0] parts := strings.Split(testData.tokenString, ".") - err = method.Verify(strings.Join(parts[0:2], "."), parts[2], parsedKey) + err = SigningMethodRS256.Verify(strings.Join(parts[0:2], "."), parts[2], parsedKey) if err != nil { t.Errorf("[%v] Error while verifying key: %v", testData.name, err) } @@ -95,14 +94,13 @@ func TestRSAVerifyWithPreParsedPrivateKey(t *testing.T) { func TestRSAWithPreParsedPrivateKey(t *testing.T) { key, _ := ioutil.ReadFile("test/sample_key") - method := GetSigningMethod("RS256").(*SigningMethodRSA) parsedKey, err := ParseRSAPrivateKeyFromPEM(key) if err != nil { t.Fatal(err) } testData := rsaTestData[0] parts := strings.Split(testData.tokenString, ".") - sig, err := method.Sign(strings.Join(parts[0:2], "."), parsedKey) + sig, err := SigningMethodRS256.Sign(strings.Join(parts[0:2], "."), parsedKey) if err != nil { t.Errorf("[%v] Error signing token: %v", testData.name, err) }