Skip to content

Commit

Permalink
fix: move location of GenerateSignature
Browse files Browse the repository at this point in the history
  • Loading branch information
J0 committed Mar 13, 2024
1 parent 6aa36dd commit fbce0d4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 31 deletions.
35 changes: 4 additions & 31 deletions internal/api/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net"
Expand All @@ -15,8 +14,8 @@ import (
"time"

"github.com/gofrs/uuid"
standardwebhooks "github.com/standard-webhooks/standard-webhooks/libraries/go"
"github.com/supabase/auth/internal/conf"
"github.com/supabase/auth/internal/crypto"

"github.com/sirupsen/logrus"
"github.com/supabase/auth/internal/hooks"
Expand All @@ -25,9 +24,8 @@ import (
)

const (
SymmetricSignaturePrefix = "v1,"
DefaultHTTPHookTimeout = 5 * time.Second
DefaultHTTPHookRetries = 3
DefaultHTTPHookTimeout = 5 * time.Second
DefaultHTTPHookRetries = 3
)

func (a *API) runPostgresHook(ctx context.Context, tx *storage.Connection, name string, input, output any) ([]byte, error) {
Expand Down Expand Up @@ -90,16 +88,13 @@ func (a *API) runHTTPHook(hookConfig conf.ExtensibilityPointConfiguration, input
return nil, err
}

if isOverSizeLimit(inputPayload) {
return nil, internalServerError("Over size limit")
}
startTime := time.Now()

for i := 0; i < DefaultHTTPHookRetries; i++ {
hookLog.Infof("invocation attempt: %d", i)
msgID := uuid.Must(uuid.NewV4())
currentTime := time.Now()
signatureList, err := generateSignatures(hookConfig.HTTPHookSecrets, msgID, currentTime, inputPayload)
signatureList, err := crypto.GenerateSignatures(hookConfig.HTTPHookSecrets, msgID, currentTime, inputPayload)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -169,28 +164,6 @@ func (a *API) runHTTPHook(hookConfig conf.ExtensibilityPointConfiguration, input
return nil, internalServerError("error executing hook")
}

func generateSignatures(secrets []string, msgID uuid.UUID, currentTime time.Time, inputPayload []byte) ([]string, error) {
// TODO(joel): Handle asymmetric case once library has been upgraded
var signatureList []string
for _, secret := range secrets {
if strings.HasPrefix(secret, SymmetricSignaturePrefix) {
trimmedSecret := strings.TrimPrefix(secret, SymmetricSignaturePrefix)
wh, err := standardwebhooks.NewWebhook(trimmedSecret)
if err != nil {
return nil, err
}
signature, err := wh.Sign(msgID.String(), currentTime, inputPayload)
if err != nil {
return nil, err
}
signatureList = append(signatureList, signature)
} else {
return nil, errors.New("invalid signature format")
}
}
return signatureList, nil
}

func watchForConnection(req *http.Request) (*connectionWatcher, *http.Request) {
w := new(connectionWatcher)
t := &httptrace.ClientTrace{
Expand Down
28 changes: 28 additions & 0 deletions internal/crypto/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import (
"math"
"math/big"
"strconv"
"strings"
"time"

"github.com/gofrs/uuid"
standardwebhooks "github.com/standard-webhooks/standard-webhooks/libraries/go"

"github.com/pkg/errors"
)
Expand Down Expand Up @@ -41,3 +46,26 @@ func GenerateOtp(digits int) (string, error) {
func GenerateTokenHash(emailOrPhone, otp string) string {
return fmt.Sprintf("%x", sha256.Sum224([]byte(emailOrPhone+otp)))
}

func GenerateSignatures(secrets []string, msgID uuid.UUID, currentTime time.Time, inputPayload []byte) ([]string, error) {
SymmetricSignaturePrefix := "v1,"
// TODO(joel): Handle asymmetric case once library has been upgraded
var signatureList []string
for _, secret := range secrets {
if strings.HasPrefix(secret, SymmetricSignaturePrefix) {
trimmedSecret := strings.TrimPrefix(secret, SymmetricSignaturePrefix)
wh, err := standardwebhooks.NewWebhook(trimmedSecret)
if err != nil {
return nil, err
}
signature, err := wh.Sign(msgID.String(), currentTime, inputPayload)
if err != nil {
return nil, err
}
signatureList = append(signatureList, signature)
} else {
return nil, errors.New("invalid signature format")
}
}
return signatureList, nil
}

0 comments on commit fbce0d4

Please sign in to comment.