Skip to content

Commit

Permalink
exp/services/webauth: remove old convjwt2jwk command (#2666)
Browse files Browse the repository at this point in the history
### What
Remove the `convjwt2jwk` command.

### Why
The command was added to convert JWT keys generated with the old `genjwtkey` command into the same format as the new `genjwk` command. We no longer have any keys in the old format and do not need this command anymore.
  • Loading branch information
leighmcculloch authored Jun 5, 2020
1 parent fc044e8 commit 976f2f5
Show file tree
Hide file tree
Showing 5 changed files with 2 additions and 170 deletions.
8 changes: 2 additions & 6 deletions exp/services/webauth/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,8 @@ Usage:
webauth [command]
Available Commands:
convjwtkey2jwk Convert a JWT ECDSA private key ASN.1 DER Base64 encoded that was generated with the old genjwtkey command to a JSON Web Key
genjwk Generate a JSON Web Key (ECDSA/ES256) for JWT issuing
serve Run the SEP-10 Web Authentication server
Flags:
-h, --help help for webauth
genjwk Generate a JSON Web Key (ECDSA/ES256) for JWT issuing
serve Run the SEP-10 Web Authentication server
Use "webauth [command] --help" for more information about a command.
```
Expand Down
58 changes: 0 additions & 58 deletions exp/services/webauth/cmd/convjwt2jwk.go

This file was deleted.

1 change: 0 additions & 1 deletion exp/services/webauth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ func main() {

rootCmd.AddCommand((&cmd.ServeCommand{Logger: logger}).Command())
rootCmd.AddCommand((&cmd.GenJWKCommand{Logger: logger}).Command())
rootCmd.AddCommand((&cmd.ConvJWTKeyToJWKCommand{Logger: logger}).Command())

err := rootCmd.Execute()
if err != nil {
Expand Down
54 changes: 0 additions & 54 deletions exp/support/jwtkey/jwtkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"encoding/base64"

"github.com/stellar/go/support/errors"
)
Expand All @@ -26,55 +24,3 @@ func GenerateKey() (*ecdsa.PrivateKey, error) {
}
return k, nil
}

// PrivateKeyToString converts a ECDSA private key into a ASN.1 DER and base64
// encoded string.
func PrivateKeyToString(k *ecdsa.PrivateKey) (string, error) {
b, err := x509.MarshalECPrivateKey(k)
if err != nil {
return "", errors.Wrap(err, "marshaling ECDSA private key")
}
return base64.StdEncoding.EncodeToString(b), nil
}

// PublicKeyToString converts a ECDSA public key into a ASN.1 DER and base64
// encoded string.
func PublicKeyToString(k *ecdsa.PublicKey) (string, error) {
b, err := x509.MarshalPKIXPublicKey(k)
if err != nil {
return "", errors.Wrap(err, "marshaling ECDSA public key")
}
return base64.StdEncoding.EncodeToString(b), nil
}

// PrivateKeyFromString converts a ECDSA private key from a ASN.1 DER and
// base64 encoded string into a type.
func PrivateKeyFromString(s string) (*ecdsa.PrivateKey, error) {
keyBytes, err := base64.StdEncoding.DecodeString(s)
if err != nil {
return nil, errors.Wrap(err, "base64 decoding ECDSA private key")
}
key, err := x509.ParseECPrivateKey(keyBytes)
if err != nil {
return nil, errors.Wrap(err, "unmarshaling ECDSA private key")
}
return key, nil
}

// PublicKeyFromString converts a ECDSA public key from a ASN.1 DER and base64
// encoded string into a type.
func PublicKeyFromString(s string) (*ecdsa.PublicKey, error) {
keyBytes, err := base64.StdEncoding.DecodeString(s)
if err != nil {
return nil, errors.Wrap(err, "base64 decoding ECDSA public key")
}
keyI, err := x509.ParsePKIXPublicKey(keyBytes)
if err != nil {
return nil, errors.Wrap(err, "unmarshaling ECDSA public key")
}
key, ok := keyI.(*ecdsa.PublicKey)
if !ok {
return nil, errors.Wrap(err, "public key not ECDSA key")
}
return key, nil
}
51 changes: 0 additions & 51 deletions exp/support/jwtkey/jwtkey_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package jwtkey

import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"encoding/base64"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -16,51 +13,3 @@ func TestGenerate(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, elliptic.P256(), key.Curve)
}

func TestToFromStringRoundTrip(t *testing.T) {
testCases := []struct {
Name string
Curve elliptic.Curve
}{
{Name: "P224", Curve: elliptic.P224()},
{Name: "P256", Curve: elliptic.P256()},
{Name: "P384", Curve: elliptic.P384()},
{Name: "P521", Curve: elliptic.P521()},
}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
privateKey, err := ecdsa.GenerateKey(tc.Curve, rand.Reader)
require.NoError(t, err)

t.Run("private", func(t *testing.T) {
privateKeyStr, err := PrivateKeyToString(privateKey)
require.NoError(t, err)

// Private key as string should be valid standard base64
_, err = base64.StdEncoding.DecodeString(privateKeyStr)
require.NoError(t, err)

// Private key should decode back to the original
privateKeyRoundTripped, err := PrivateKeyFromString(privateKeyStr)
require.NoError(t, err)
assert.Equal(t, privateKey, privateKeyRoundTripped)
})

publicKey := &privateKey.PublicKey

t.Run("public", func(t *testing.T) {
publicKeyStr, err := PublicKeyToString(publicKey)
require.NoError(t, err)

// Public key as string should be valid standard base64
_, err = base64.StdEncoding.DecodeString(publicKeyStr)
require.NoError(t, err)

// Public key should decode back to the original
publicKeyRoundTripped, err := PublicKeyFromString(publicKeyStr)
require.NoError(t, err)
assert.Equal(t, publicKey, publicKeyRoundTripped)
})
})
}
}

0 comments on commit 976f2f5

Please sign in to comment.