Skip to content

Commit

Permalink
feat: add auth hooks config (#1856)
Browse files Browse the repository at this point in the history
  • Loading branch information
J0 authored Jan 19, 2024
1 parent fefcfd9 commit b380dfb
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
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 @@ -200,6 +200,7 @@ var Config = config{
"workos": {},
"zoom": {},
},
Hook: hook{},
JwtExpiry: 3600,
JwtSecret: defaultJwtSecret,
},
Expand Down Expand Up @@ -312,6 +313,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 @@ -348,6 +350,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 @@ -640,6 +653,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
6 changes: 6 additions & 0 deletions internal/utils/templates/init_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ template = "Your code is {{ `{{ .Code }}` }} ."
[auth.sms.test_otp]
# 4152127777 = "123456"

# This hook runs before a token is issued and allows you to add additional claims based on the authentication method used.
[auth.hook.custom_access_token]
# 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

0 comments on commit b380dfb

Please sign in to comment.