Skip to content

Commit

Permalink
fix: refactor out mixed hook invocation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
joel authored and joel committed Mar 7, 2024
1 parent fe7c8cb commit 02eb176
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
38 changes: 33 additions & 5 deletions internal/api/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net"
"net/http"
"net/http/httptrace"
"net/url"
"strings"
"time"

Expand All @@ -22,8 +23,7 @@ import (
"github.com/supabase/auth/internal/storage"
)

func (a *API) runHook(ctx context.Context, tx *storage.Connection, name string, input, output any) ([]byte, error) {

func (a *API) runPostgresHook(ctx context.Context, tx *storage.Connection, name string, input, output any) ([]byte, error) {
db := a.db.WithContext(ctx)

request, err := json.Marshal(input)
Expand Down Expand Up @@ -218,7 +218,32 @@ func isOverSizeLimit(payload []byte) bool {
return len(payload) > maxSizeKB
}

func validateHTTPHook(uri string) error {
u, err := url.Parse(uri)
if err != nil {
return err
}
if !(strings.ToLower(u.Scheme) == "http" || strings.ToLower(u.Scheme) == "https") {
return fmt.Errorf("invalid http hook")
}
return nil
}

func validatePostgresHook(uri string) error {
u, err := url.Parse(uri)
if err != nil {
return err
}
if !(strings.ToLower(u.Scheme) == "pg-functions") {
return fmt.Errorf("invalid postgres hook")
}
return nil
}

func (a *API) invokeHTTPHook(input, output any, hookURI string) error {
if err := validateHTTPHook(hookURI); err != nil {
return err
}
switch input.(type) {
case *hooks.CustomSMSProviderInput:
hookOutput, ok := output.(*hooks.CustomSMSProviderOutput)
Expand Down Expand Up @@ -252,6 +277,9 @@ func (a *API) invokeHTTPHook(input, output any, hookURI string) error {
// pass the current transaciton, as pool-exhaustion deadlocks are very easy to
// trigger.
func (a *API) invokePostgresHook(ctx context.Context, conn *storage.Connection, input, output any, hookURI string) error {
if err := validatePostgresHook(hookURI); err != nil {
return err
}
config := a.config
// Switch based on hook type
switch input.(type) {
Expand All @@ -261,7 +289,7 @@ func (a *API) invokePostgresHook(ctx context.Context, conn *storage.Connection,
panic("output should be *hooks.MFAVerificationAttemptOutput")
}

if _, err := a.runHook(ctx, conn, config.Hook.MFAVerificationAttempt.HookName, input, output); err != nil {
if _, err := a.runPostgresHook(ctx, conn, config.Hook.MFAVerificationAttempt.HookName, input, output); err != nil {
return internalServerError("Error invoking MFA verification hook.").WithInternalError(err)
}

Expand All @@ -287,7 +315,7 @@ func (a *API) invokePostgresHook(ctx context.Context, conn *storage.Connection,
panic("output should be *hooks.PasswordVerificationAttemptOutput")
}

if _, err := a.runHook(ctx, conn, config.Hook.PasswordVerificationAttempt.HookName, input, output); err != nil {
if _, err := a.runPostgresHook(ctx, conn, config.Hook.PasswordVerificationAttempt.HookName, input, output); err != nil {
return internalServerError("Error invoking password verification hook.").WithInternalError(err)
}

Expand All @@ -313,7 +341,7 @@ func (a *API) invokePostgresHook(ctx context.Context, conn *storage.Connection,
panic("output should be *hooks.CustomAccessTokenOutput")
}

if _, err := a.runHook(ctx, conn, config.Hook.CustomAccessToken.HookName, input, output); err != nil {
if _, err := a.runPostgresHook(ctx, conn, config.Hook.CustomAccessToken.HookName, input, output); err != nil {
return internalServerError("Error invoking access token hook.").WithInternalError(err)
}

Expand Down
5 changes: 0 additions & 5 deletions internal/api/phone.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,10 @@ func (a *API) sendPhoneConfirmation(tx *storage.Connection, user *models.User, p
OTP: otp,
}
output := hooks.CustomSMSProviderOutput{}
// TODO: Fix this by either passing down the context ore removing the need for it
err := a.invokeHTTPHook(&input, &output, config.Hook.CustomSMSProvider.URI)
if err != nil {
return "", err
}
// if !output.Success {
// // TODO: adjust this
// return "", internalServerError("error sending sms using custom provider")
// }
} else {

messageID, err = smsProvider.SendMessage(phone, message, channel, otp)
Expand Down

0 comments on commit 02eb176

Please sign in to comment.