From cb50cfd8672d2f56534e0104d3f1438170fc14e7 Mon Sep 17 00:00:00 2001 From: joel Date: Wed, 14 Feb 2024 15:56:45 +0800 Subject: [PATCH] feat: initial commit --- internal/conf/configuration.go | 56 ++++++++++++++++++----------- internal/conf/configuration_test.go | 3 +- 2 files changed, 37 insertions(+), 22 deletions(-) diff --git a/internal/conf/configuration.go b/internal/conf/configuration.go index d33cbd9c72..3d26ae0387 100644 --- a/internal/conf/configuration.go +++ b/internal/conf/configuration.go @@ -444,12 +444,14 @@ type HookConfiguration struct { MFAVerificationAttempt ExtensibilityPointConfiguration `json:"mfa_verification_attempt" split_words:"true"` PasswordVerificationAttempt ExtensibilityPointConfiguration `json:"password_verification_attempt" split_words:"true"` CustomAccessToken ExtensibilityPointConfiguration `json:"custom_access_token" split_words:"true"` + CustomSMSProvider ExtensibilityPointConfiguration `json:"custom_sms_provider" split_words:"true"` } type ExtensibilityPointConfiguration struct { URI string `json:"uri"` Enabled bool `json:"enabled"` HookName string `json:"hook_name"` + Secret string `json:"secret"` } func (h *HookConfiguration) Validate() error { @@ -467,28 +469,40 @@ func (h *HookConfiguration) Validate() error { } func (e *ExtensibilityPointConfiguration) ValidateExtensibilityPoint() error { - if e.URI != "" { - u, err := url.Parse(e.URI) - if err != nil { - return err - } - pathParts := strings.Split(u.Path, "/") - if len(pathParts) < 3 { - return fmt.Errorf("URI path does not contain enough parts") - } - if u.Scheme != "pg-functions" { - return fmt.Errorf("only postgres hooks are supported at the moment") - } - schema := pathParts[1] - table := pathParts[2] - // Validate schema and table names - if !postgresNamesRegexp.MatchString(schema) { - return fmt.Errorf("invalid schema name: %s", schema) - } - if !postgresNamesRegexp.MatchString(table) { - return fmt.Errorf("invalid table name: %s", table) - } + u, err := url.Parse(e.URI) + if err != nil { + return err + } + switch u.Scheme { + case "pg-functions": + return validatePostgresPath(u) + case "https": + return validateHTTPSPath(u) + default: + return fmt.Errorf("only postgres hooks and HTTPS functions are supported at the moment") } +} + +func validatePostgresPath(u *url.URL) error { + pathParts := strings.Split(u.Path, "/") + if len(pathParts) < 3 { + return fmt.Errorf("URI path does not contain enough parts") + } + + schema := pathParts[1] + table := pathParts[2] + // Validate schema and table names + if !postgresNamesRegexp.MatchString(schema) { + return fmt.Errorf("invalid schema name: %s", schema) + } + if !postgresNamesRegexp.MatchString(table) { + return fmt.Errorf("invalid table name: %s", table) + } + return nil +} + +func validateHTTPSPath(u *url.URL) error { + // TOOD: add validation logi return nil } diff --git a/internal/conf/configuration_test.go b/internal/conf/configuration_test.go index 8017dd4dae..d6f7e52d7f 100644 --- a/internal/conf/configuration_test.go +++ b/internal/conf/configuration_test.go @@ -105,7 +105,8 @@ func TestValidateExtensibilityPoint(t *testing.T) { expectError bool }{ // Positive test cases - {desc: "Valid URI", uri: "pg-functions://postgres/auth/verification_hook_reject", expectError: false}, + {desc: "Valid Postgres URI", uri: "pg-functions://postgres/auth/verification_hook_reject", expectError: false}, + {desc: "Valid HTTPS URI", uri: "https://asdfgggqqwwerty.supabase.co/functions/v1/custom-sms-sender", expectError: false}, {desc: "Another Valid URI", uri: "pg-functions://postgres/user_management/add_user", expectError: false}, {desc: "Another Valid URI", uri: "pg-functions://postgres/MySpeCial/FUNCTION_THAT_YELLS_AT_YOU", expectError: false},