-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #77 from cerberauth/jwt-signed-with-same-method
feat: use the same jwt alg as the token
- Loading branch information
Showing
4 changed files
with
144 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package jwt | ||
|
||
import ( | ||
"crypto/ecdsa" | ||
"crypto/elliptic" | ||
"crypto/rand" | ||
"crypto/rsa" | ||
"errors" | ||
|
||
"github.com/golang-jwt/jwt/v5" | ||
) | ||
|
||
func generateKey(method jwt.SigningMethod) (interface{}, error) { | ||
switch method.Alg() { | ||
case jwt.SigningMethodRS256.Alg(), | ||
jwt.SigningMethodRS384.Alg(), | ||
jwt.SigningMethodRS512.Alg(), | ||
jwt.SigningMethodPS256.Alg(), | ||
jwt.SigningMethodPS384.Alg(), | ||
jwt.SigningMethodPS512.Alg(): | ||
privateKeyRS, err := rsa.GenerateKey(rand.Reader, 2048) | ||
if err != nil { | ||
return "", err | ||
} | ||
return privateKeyRS, nil | ||
|
||
case jwt.SigningMethodES256.Alg(): | ||
privateKeyES256, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) | ||
if err != nil { | ||
return "", err | ||
} | ||
return privateKeyES256, nil | ||
|
||
case jwt.SigningMethodES384.Alg(): | ||
privateKeyES384, err := ecdsa.GenerateKey(elliptic.P384(), rand.Reader) | ||
if err != nil { | ||
return "", err | ||
} | ||
return privateKeyES384, nil | ||
|
||
case jwt.SigningMethodES512.Alg(): | ||
privateKeyES512, err := ecdsa.GenerateKey(elliptic.P521(), rand.Reader) | ||
if err != nil { | ||
return "", err | ||
} | ||
return privateKeyES512, nil | ||
|
||
case jwt.SigningMethodHS256.Alg(), | ||
jwt.SigningMethodHS384.Alg(), | ||
jwt.SigningMethodHS512.Alg(): | ||
keyHS := make([]byte, 64) | ||
_, err := rand.Read(keyHS) | ||
if err != nil { | ||
return "", err | ||
} | ||
return keyHS, nil | ||
|
||
case jwt.SigningMethodNone.Alg(): | ||
return nil, nil | ||
} | ||
|
||
return "", errors.New("unsupported signing method") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package jwt_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cerberauth/vulnapi/jwt" | ||
libjwt "github.com/golang-jwt/jwt/v5" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestJWTWriter_SignWithMethodAndRandomKey_WhenSigningMethodIsHS256(t *testing.T) { | ||
token := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c" | ||
writer, _ := jwt.NewJWTWriter(token) | ||
|
||
token, err := writer.SignWithMethodAndRandomKey(libjwt.SigningMethodHS256) | ||
assert.NoError(t, err) | ||
assert.NotEmpty(t, token) | ||
} | ||
|
||
func TestJWTWriter_SignWithMethodAndRandomKey_WhenSigningMethodIsRS256(t *testing.T) { | ||
token := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c" | ||
writer, _ := jwt.NewJWTWriter(token) | ||
|
||
token, err := writer.SignWithMethodAndRandomKey(libjwt.SigningMethodRS256) | ||
assert.NoError(t, err) | ||
assert.NotEmpty(t, token) | ||
} | ||
|
||
func TestJWTWriter_SignWithMethodAndRandomKey_WhenSigningMethodIsRS384(t *testing.T) { | ||
token := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c" | ||
writer, _ := jwt.NewJWTWriter(token) | ||
|
||
token, err := writer.SignWithMethodAndRandomKey(libjwt.SigningMethodRS384) | ||
assert.NoError(t, err) | ||
assert.NotEmpty(t, token) | ||
} | ||
|
||
func TestJWTWriter_SignWithMethodAndRandomKey_WhenSigningMethodIsRS512(t *testing.T) { | ||
token := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c" | ||
writer, _ := jwt.NewJWTWriter(token) | ||
|
||
token, err := writer.SignWithMethodAndRandomKey(libjwt.SigningMethodRS512) | ||
assert.NoError(t, err) | ||
assert.NotEmpty(t, token) | ||
} | ||
|
||
func TestJWTWriter_SignWithMethodAndRandomKey_WhenSigningMethodIsES256(t *testing.T) { | ||
token := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c" | ||
writer, _ := jwt.NewJWTWriter(token) | ||
|
||
token, err := writer.SignWithMethodAndRandomKey(libjwt.SigningMethodES256) | ||
assert.NoError(t, err) | ||
assert.NotEmpty(t, token) | ||
} | ||
|
||
func TestJWTWriter_SignWithMethodAndRandomKey_WhenSigningMethodIsES384(t *testing.T) { | ||
token := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c" | ||
writer, _ := jwt.NewJWTWriter(token) | ||
|
||
token, err := writer.SignWithMethodAndRandomKey(libjwt.SigningMethodES384) | ||
assert.NoError(t, err) | ||
assert.NotEmpty(t, token) | ||
} | ||
|
||
func TestJWTWriter_SignWithMethodAndRandomKey_WhenSigningMethodIsES512(t *testing.T) { | ||
token := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c" | ||
writer, _ := jwt.NewJWTWriter(token) | ||
|
||
token, err := writer.SignWithMethodAndRandomKey(libjwt.SigningMethodES512) | ||
assert.NoError(t, err) | ||
assert.NotEmpty(t, token) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters