Skip to content

Commit

Permalink
rs256 test case to verify only
Browse files Browse the repository at this point in the history
  • Loading branch information
pkieltyka committed Dec 14, 2023
1 parent 4e5a7fd commit a77c650
Showing 1 changed file with 79 additions and 47 deletions.
126 changes: 79 additions & 47 deletions jwtauth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,53 +51,6 @@ func init() {
// Tests
//

func TestSimpleRSA(t *testing.T) {
privateKeyBlock, _ := pem.Decode([]byte(PrivateKeyRS256String))

privateKey, err := x509.ParsePKCS1PrivateKey(privateKeyBlock.Bytes)

if err != nil {
t.Fatalf(err.Error())
}

publicKeyBlock, _ := pem.Decode([]byte(PublicKeyRS256String))

publicKey, err := x509.ParsePKIXPublicKey(publicKeyBlock.Bytes)

if err != nil {
t.Fatalf(err.Error())
}

TokenAuthRS256 = jwtauth.New(jwa.RS256.String(), privateKey, publicKey)

claims := map[string]interface{}{
"key": "val",
"key2": "val2",
"key3": "val3",
}

_, tokenString, err := TokenAuthRS256.Encode(claims)

if err != nil {
t.Fatalf("Failed to encode claims %s\n", err.Error())
}

token, err := TokenAuthRS256.Decode(tokenString)

if err != nil {
t.Fatalf("Failed to decode token string %s\n", err.Error())
}

tokenClaims, err := token.AsMap(context.Background())
if err != nil {
t.Fatal(err.Error())
}

if !reflect.DeepEqual(claims, tokenClaims) {
t.Fatalf("The decoded claims don't match the original ones\n")
}
}

func TestSimple(t *testing.T) {
r := chi.NewRouter()

Expand Down Expand Up @@ -157,6 +110,85 @@ func TestSimple(t *testing.T) {
}
}

func TestSimpleRSA(t *testing.T) {
privateKeyBlock, _ := pem.Decode([]byte(PrivateKeyRS256String))

privateKey, err := x509.ParsePKCS1PrivateKey(privateKeyBlock.Bytes)
if err != nil {
t.Fatalf(err.Error())
}

publicKeyBlock, _ := pem.Decode([]byte(PublicKeyRS256String))

publicKey, err := x509.ParsePKIXPublicKey(publicKeyBlock.Bytes)
if err != nil {
t.Fatalf(err.Error())
}

TokenAuthRS256 = jwtauth.New(jwa.RS256.String(), privateKey, publicKey)

claims := map[string]interface{}{
"key": "val",
"key2": "val2",
"key3": "val3",
}

_, tokenString, err := TokenAuthRS256.Encode(claims)
if err != nil {
t.Fatalf("Failed to encode claims %s\n", err.Error())
}

token, err := TokenAuthRS256.Decode(tokenString)
if err != nil {
t.Fatalf("Failed to decode token string %s\n", err.Error())
}

tokenClaims, err := token.AsMap(context.Background())
if err != nil {
t.Fatal(err.Error())
}

if !reflect.DeepEqual(claims, tokenClaims) {
t.Fatalf("The decoded claims don't match the original ones\n")
}
}

func TestSimpleRSAVerifyOnly(t *testing.T) {
tokenString := "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJrZXkiOiJ2YWwiLCJrZXkyIjoidmFsMiIsImtleTMiOiJ2YWwzIn0.kLEK3FZZPsAlQNKR5yHyjRyrlCJFhvKmrh7o-GqDT_zaGQgvb0Dufp8uNSMeOFAlLGK5FbKX7BckjJqfvEyrTQ"
claims := map[string]interface{}{
"key": "val",
"key2": "val2",
"key3": "val3",
}

publicKeyBlock, _ := pem.Decode([]byte(PublicKeyRS256String))
publicKey, err := x509.ParsePKIXPublicKey(publicKeyBlock.Bytes)
if err != nil {
t.Fatalf(err.Error())
}

TokenAuthRS256 = jwtauth.New(jwa.RS256.String(), nil, publicKey)

_, _, err = TokenAuthRS256.Encode(claims)
if err == nil {
t.Fatalf("Expecting error when encoding claims without signing key")
}

token, err := TokenAuthRS256.Decode(tokenString)
if err != nil {
t.Fatalf("Failed to decode token string %s\n", err.Error())
}

tokenClaims, err := token.AsMap(context.Background())
if err != nil {
t.Fatal(err.Error())
}

if !reflect.DeepEqual(claims, tokenClaims) {
t.Fatalf("The decoded claims don't match the original ones\n")
}
}

func TestMore(t *testing.T) {
r := chi.NewRouter()

Expand Down

0 comments on commit a77c650

Please sign in to comment.