Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add auth hooks config #1856

Merged
merged 7 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions internal/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,29 @@ EOF
"GOTRUE_SMS_VONAGE_FROM="+utils.Config.Auth.Sms.Vonage.From,
)
}
if utils.Config.Auth.Hook.MFAVerificationAttempt.Enabled {
env = append(
env,
"GOTRUE_HOOK_MFA_VERIFICATION_ATTEMPT_ENABLED=true",
"GOTRUE_HOOK_MFA_VERIFICATION_ATTEMPT_URI="+utils.Config.Auth.Hook.MFAVerificationAttempt.URI,
)
}

if utils.Config.Auth.Hook.PasswordVerificationAttempt.Enabled {
env = append(
env,
"GOTRUE_HOOK_PASSWORD_VERIFICATION_ATTEMPT_ENABLED=true",
"GOTRUE_HOOK_PASSWORD_VERIFICATION_ATTEMPT_URI="+utils.Config.Auth.Hook.PasswordVerificationAttempt.URI,
)
}

if utils.Config.Auth.Hook.CustomAccessToken.Enabled {
env = append(
env,
"GOTRUE_HOOK_CUSTOM_ACCESS_TOKEN_ENABLED=true",
"GOTRUE_HOOK_CUSTOM_ACCESS_TOKEN_URI="+utils.Config.Auth.Hook.CustomAccessToken.URI,
)
}

for name, config := range utils.Config.Auth.External {
env = append(
Expand Down
32 changes: 32 additions & 0 deletions internal/utils/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ var Config = config{
"workos": {},
"zoom": {},
},
Hook: hook{},
JwtExpiry: 3600,
JwtSecret: defaultJwtSecret,
},
Expand Down Expand Up @@ -311,6 +312,7 @@ type (
EnableRefreshTokenRotation bool `toml:"enable_refresh_token_rotation"`
RefreshTokenReuseInterval uint `toml:"refresh_token_reuse_interval"`
EnableManualLinking bool `toml:"enable_manual_linking"`
Hook hook `toml:"hook"`

EnableSignup bool `toml:"enable_signup"`
Email email `toml:"email"`
Expand Down Expand Up @@ -347,6 +349,17 @@ type (
TestOTP map[string]string `toml:"test_otp"`
}

hook struct {
MFAVerificationAttempt hookConfig `toml:"mfa_verification_attempt"`
PasswordVerificationAttempt hookConfig `toml:"password_verification_attempt"`
CustomAccessToken hookConfig `toml:"custom_access_token"`
}

hookConfig struct {
Enabled bool `toml:"enabled"`
URI string `toml:"uri"`
}

twilioConfig struct {
Enabled bool `toml:"enabled"`
AccountSid string `toml:"account_sid"`
Expand Down Expand Up @@ -639,6 +652,25 @@ func LoadConfigFS(fsys afero.Fs) error {
return err
}
}

if Config.Auth.Hook.MFAVerificationAttempt.Enabled {
if Config.Auth.Hook.MFAVerificationAttempt.URI == "" {
return errors.New("Missing required field in config: auth.hook.mfa_verification_atempt.uri")
}
}

if Config.Auth.Hook.PasswordVerificationAttempt.Enabled {
if Config.Auth.Hook.PasswordVerificationAttempt.URI == "" {
return errors.New("Missing required field in config: auth.hook.password_verification_attempt.uri")
}
}

if Config.Auth.Hook.CustomAccessToken.Enabled {
if Config.Auth.Hook.CustomAccessToken.URI == "" {
return errors.New("Missing required field in config: auth.hook.custom_access_token.uri")
}
}

// Validate oauth config
for ext, provider := range Config.Auth.External {
if !provider.Enabled {
Expand Down
5 changes: 5 additions & 0 deletions internal/utils/templates/init_config.test.toml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ template = "Your code is {{ `{{ .Code }}` }} ."
[auth.sms.test_otp]
4152127777 = "123456"

[auth.hook.custom_access_token]
enabled = true
uri = "pg-functions://postgres/auth/custom-access-token-hook"


# Configure one of the supported SMS providers: `twilio`, `twilio_verify`, `messagebird`, `textlocal`, `vonage`.
[auth.sms.twilio]
enabled = true
Expand Down
5 changes: 5 additions & 0 deletions internal/utils/templates/init_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ template = "Your code is {{ `{{ .Code }}` }} ."
[auth.sms.test_otp]
# 4152127777 = "123456"

[auth.hook.custom_access_token]
J0 marked this conversation as resolved.
Show resolved Hide resolved
# enabled = true
# uri = "pg-functions://<database>/<schema>/<hook_name>"


# Configure one of the supported SMS providers: `twilio`, `twilio_verify`, `messagebird`, `textlocal`, `vonage`.
[auth.sms.twilio]
enabled = false
Expand Down