Skip to content

Commit

Permalink
feat: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
joel authored and joel committed Feb 14, 2024
1 parent 9fc699e commit cb50cfd
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 22 deletions.
56 changes: 35 additions & 21 deletions internal/conf/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}

Expand Down
3 changes: 2 additions & 1 deletion internal/conf/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},

Expand Down

0 comments on commit cb50cfd

Please sign in to comment.