Skip to content

Commit

Permalink
chore: address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
CaioTeixeira95 committed May 24, 2024
1 parent d678757 commit 8297fca
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 23 deletions.
2 changes: 1 addition & 1 deletion cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (c *serveCmd) Command() *cobra.Command {
OptType: types.String,
ConfigKey: &cfg.ServerBaseURL,
FlagDefault: "http://localhost:8000",
Required: false,
Required: true,
},
{
Name: "wallet-signing-key",
Expand Down
2 changes: 1 addition & 1 deletion cmd/utils/custom_set_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func SetConfigOptionStellarPublicKey(co *config.ConfigOption) error {

kp, err := keypair.ParseAddress(publicKey)
if err != nil {
return fmt.Errorf("error validating public key in %s: %w", co.Name, err)
return fmt.Errorf("validating public key in %s: %w", co.Name, err)
}

key, ok := co.ConfigKey.(*string)
Expand Down
6 changes: 3 additions & 3 deletions cmd/utils/custom_set_value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,17 @@ func TestSetConfigOptionStellarPublicKey(t *testing.T) {
testCases := []customSetterTestCase[string]{
{
name: "returns an error if the public key is empty",
wantErrContains: "error validating public key in wallet-signing-key: strkey is 0 bytes long; minimum valid length is 5",
wantErrContains: "validating public key in wallet-signing-key: strkey is 0 bytes long; minimum valid length is 5",
},
{
name: "returns an error if the public key is invalid",
args: []string{"--wallet-signing-key", "invalid_public_key"},
wantErrContains: "error validating public key in wallet-signing-key: base32 decode failed: illegal base32 data at input byte 18",
wantErrContains: "validating public key in wallet-signing-key: base32 decode failed: illegal base32 data at input byte 18",
},
{
name: "returns an error if the public key is invalid (private key instead)",
args: []string{"--wallet-signing-key", "SDISQRUPIHAO5WIIGY4QRDCINZSA44TX3OIIUK3C63NUKN5DABKEQ276"},
wantErrContains: "error validating public key in wallet-signing-key: invalid version byte",
wantErrContains: "validating public key in wallet-signing-key: invalid version byte",
},
{
name: "handles Stellar public key through the CLI flag",
Expand Down
26 changes: 11 additions & 15 deletions internal/serve/auth/signature_verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@ var (
)

type ErrInvalidTimestampFormat struct {
TimestampString string
TimestampString string
timestampValueError bool
}

func (e ErrInvalidTimestampFormat) Error() string {
return fmt.Sprintf("signature format different than expected. expected unix seconds, got: %s", e.TimestampString)
if e.timestampValueError {
return fmt.Sprintf("signature format different than expected. expected unix seconds, got: %s", e.TimestampString)
}
return fmt.Sprintf("malformed timestamp: %s", e.TimestampString)
}

type ErrExpiredSignatureTimestamp struct {
Expand All @@ -53,21 +57,13 @@ func (sv *StellarSignatureVerifier) VerifySignature(ctx context.Context, signatu
t, s, err := ExtractTimestampedSignature(signatureHeaderContent)
if err != nil {
log.Ctx(ctx).Error(err)
var errTimestampFormat *ErrInvalidTimestampFormat
if errors.As(err, &errTimestampFormat) {
return err
}
return ErrStellarSignatureNotVerified
}

// 2 minutes in seconds
// 2 seconds
err = VerifyGracePeriodSeconds(t, 2*time.Second)
if err != nil {
log.Ctx(ctx).Error(err)
var errExpiredSignatureTimestamp *ErrExpiredSignatureTimestamp
if errors.As(err, &errExpiredSignatureTimestamp) {
return err
}
return ErrStellarSignatureNotVerified
}

Expand Down Expand Up @@ -97,20 +93,20 @@ func (sv *StellarSignatureVerifier) VerifySignature(ctx context.Context, signatu
func ExtractTimestampedSignature(signatureHeaderContent string) (t string, s string, err error) {
parts := strings.SplitN(signatureHeaderContent, ",", 2)
if len(parts) != 2 {
return "", "", fmt.Errorf("malformed header format: %s", signatureHeaderContent)
return "", "", fmt.Errorf("malformed header: %s", signatureHeaderContent)
}

tHeaderContent := parts[0]
timestampParts := strings.SplitN(tHeaderContent, "=", 2)
if len(timestampParts) != 2 || strings.TrimSpace(timestampParts[0]) != "t" {
return "", "", fmt.Errorf("malformed timestamp format: %s", timestampParts)
return "", "", &ErrInvalidTimestampFormat{TimestampString: tHeaderContent}
}
t = strings.TrimSpace(timestampParts[1])

sHeaderContent := parts[1]
signatureParts := strings.SplitN(sHeaderContent, "=", 2)
if len(signatureParts) != 2 || strings.TrimSpace(signatureParts[0]) != "s" {
return "", "", fmt.Errorf("malformed signature format: %s", signatureParts)
return "", "", fmt.Errorf("malformed signature: %s", signatureParts)
}
s = strings.TrimSpace(signatureParts[1])

Expand All @@ -120,7 +116,7 @@ func ExtractTimestampedSignature(signatureHeaderContent string) (t string, s str
func VerifyGracePeriodSeconds(timestampString string, gracePeriod time.Duration) error {
// Note: from Nov 20th, 2286 this RegEx will fail because of an extra digit
if ok, _ := regexp.MatchString(`^\d{10}$`, timestampString); !ok {
return &ErrInvalidTimestampFormat{TimestampString: timestampString}
return &ErrInvalidTimestampFormat{TimestampString: timestampString, timestampValueError: true}
}

timestampUnix, err := strconv.ParseInt(timestampString, 10, 64)
Expand Down
8 changes: 5 additions & 3 deletions internal/serve/auth/signature_verifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,19 @@ func TestSignatureVerifierVerifySignature(t *testing.T) {
func TestExtractTimestampedSignature(t *testing.T) {
t.Run("invalid_header_content", func(t *testing.T) {
ts, s, err := ExtractTimestampedSignature("")
assert.EqualError(t, err, "malformed header format: ")
assert.EqualError(t, err, "malformed header: ")
assert.Empty(t, ts)
assert.Empty(t, s)

ts, s, err = ExtractTimestampedSignature("a,b")
assert.EqualError(t, err, "malformed timestamp format: [a]")
var errTimestampFormat *ErrInvalidTimestampFormat
assert.ErrorAs(t, err, &errTimestampFormat)
assert.EqualError(t, err, "malformed timestamp: a")
assert.Empty(t, ts)
assert.Empty(t, s)

ts, s, err = ExtractTimestampedSignature("t=abc,b")
assert.EqualError(t, err, "malformed signature format: [b]")
assert.EqualError(t, err, "malformed signature: [b]")
assert.Empty(t, ts)
assert.Empty(t, s)
})
Expand Down

0 comments on commit 8297fca

Please sign in to comment.