-
Notifications
You must be signed in to change notification settings - Fork 324
/
configuration.go
366 lines (319 loc) · 11.3 KB
/
configuration.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package conf
import (
"database/sql/driver"
"encoding/json"
"errors"
"os"
"time"
"github.com/joho/godotenv"
"github.com/kelseyhightower/envconfig"
)
// OAuthProviderConfiguration holds all config related to external account providers.
type OAuthProviderConfiguration struct {
ClientID string `json:"client_id" split_words:"true"`
Secret string `json:"secret"`
RedirectURI string `json:"redirect_uri" split_words:"true"`
URL string `json:"url"`
Enabled bool `json:"enabled"`
}
type EmailProviderConfiguration struct {
Enabled bool `json:"enabled" default:"true"`
}
type SamlProviderConfiguration struct {
Enabled bool `json:"enabled"`
MetadataURL string `json:"metadata_url" envconfig:"METADATA_URL"`
APIBase string `json:"api_base" envconfig:"API_BASE"`
Name string `json:"name"`
SigningCert string `json:"signing_cert" envconfig:"SIGNING_CERT"`
SigningKey string `json:"signing_key" envconfig:"SIGNING_KEY"`
}
// DBConfiguration holds all the database related configuration.
type DBConfiguration struct {
Driver string `json:"driver" required:"true"`
URL string `json:"url" envconfig:"DATABASE_URL" required:"true"`
MigrationsPath string `json:"migrations_path" split_words:"true" default:"./migrations"`
}
// JWTConfiguration holds all the JWT related configuration.
type JWTConfiguration struct {
Secret string `json:"secret" required:"true"`
Exp int `json:"exp"`
Aud string `json:"aud"`
AdminGroupName string `json:"admin_group_name" split_words:"true"`
AdminRoles []string `json:"admin_roles" split_words:"true"`
DefaultGroupName string `json:"default_group_name" split_words:"true"`
}
// GlobalConfiguration holds all the configuration that applies to all instances.
type GlobalConfiguration struct {
API struct {
Host string
Port int `envconfig:"PORT" default:"8081"`
Endpoint string
RequestIDHeader string `envconfig:"REQUEST_ID_HEADER"`
ExternalURL string `json:"external_url" envconfig:"API_EXTERNAL_URL"`
}
DB DBConfiguration
External ProviderConfiguration
Logging LoggingConfig `envconfig:"LOG"`
OperatorToken string `split_words:"true" required:"false"`
MultiInstanceMode bool
Tracing TracingConfig
SMTP SMTPConfiguration
RateLimitHeader string `split_words:"true"`
RateLimitEmailSent float64 `split_words:"true" default:"30"`
}
// EmailContentConfiguration holds the configuration for emails, both subjects and template URLs.
type EmailContentConfiguration struct {
Invite string `json:"invite"`
Confirmation string `json:"confirmation"`
Recovery string `json:"recovery"`
EmailChange string `json:"email_change" split_words:"true"`
MagicLink string `json:"magic_link" split_words:"true"`
}
type ProviderConfiguration struct {
Apple OAuthProviderConfiguration `json:"apple"`
Azure OAuthProviderConfiguration `json:"azure"`
Bitbucket OAuthProviderConfiguration `json:"bitbucket"`
Discord OAuthProviderConfiguration `json:"discord"`
Facebook OAuthProviderConfiguration `json:"facebook"`
Github OAuthProviderConfiguration `json:"github"`
Gitlab OAuthProviderConfiguration `json:"gitlab"`
Google OAuthProviderConfiguration `json:"google"`
Spotify OAuthProviderConfiguration `json:"spotify"`
Slack OAuthProviderConfiguration `json:"slack"`
Twitter OAuthProviderConfiguration `json:"twitter"`
Twitch OAuthProviderConfiguration `json:"twitch"`
Email EmailProviderConfiguration `json:"email"`
Phone PhoneProviderConfiguration `json:"phone"`
Saml SamlProviderConfiguration `json:"saml"`
IosBundleId string `json:"ios_bundle_id" split_words:"true"`
RedirectURL string `json:"redirect_url"`
}
type SMTPConfiguration struct {
MaxFrequency time.Duration `json:"max_frequency" split_words:"true"`
Host string `json:"host"`
Port int `json:"port,omitempty" default:"587"`
User string `json:"user"`
Pass string `json:"pass,omitempty"`
AdminEmail string `json:"admin_email" split_words:"true"`
SenderName string `json:"sender_name" split_words:"true"`
}
type MailerConfiguration struct {
Autoconfirm bool `json:"autoconfirm"`
Subjects EmailContentConfiguration `json:"subjects"`
Templates EmailContentConfiguration `json:"templates"`
URLPaths EmailContentConfiguration `json:"url_paths"`
SecureEmailChangeEnabled bool `json:"secure_email_change_enabled" split_words:"true" default:"true"`
}
type PhoneProviderConfiguration struct {
Enabled bool `json:"enabled"`
}
type SmsProviderConfiguration struct {
Autoconfirm bool `json:"autoconfirm"`
MaxFrequency time.Duration `json:"max_frequency" split_words:"true"`
OtpExp uint `json:"otp_exp" split_words:"true"`
OtpLength int `json:"otp_length" split_words:"true"`
Provider string `json:"provider"`
Template string `json:"template"`
Twilio TwilioProviderConfiguration `json:"twilio"`
Messagebird MessagebirdProviderConfiguration `json:"messagebird"`
}
type TwilioProviderConfiguration struct {
AccountSid string `json:"account_sid" split_words:"true"`
AuthToken string `json:"auth_token" split_words:"true"`
MessageServiceSid string `json:"message_service_sid" split_words:"true"`
}
type MessagebirdProviderConfiguration struct {
AccessKey string `json:"access_key" split_words:"true"`
Originator string `json:"originator" split_words:"true"`
}
type CaptchaConfiguration struct {
Enabled bool `json:"enabled" default:"false"`
Provider string `json:"provider" default:"hcaptcha"`
Secret string `json:"provider_secret"`
}
type SecurityConfiguration struct {
Captcha CaptchaConfiguration `json:"captcha"`
}
// Configuration holds all the per-instance configuration.
type Configuration struct {
SiteURL string `json:"site_url" split_words:"true" required:"true"`
URIAllowList []string `json:"uri_allow_list" split_words:"true"`
PasswordMinLength int `json:"password_min_length" default:"6"`
JWT JWTConfiguration `json:"jwt"`
SMTP SMTPConfiguration `json:"smtp"`
Mailer MailerConfiguration `json:"mailer"`
External ProviderConfiguration `json:"external"`
Sms SmsProviderConfiguration `json:"sms"`
DisableSignup bool `json:"disable_signup" split_words:"true"`
Webhook WebhookConfig `json:"webhook" split_words:"true"`
Security SecurityConfiguration `json:"security"`
Cookie struct {
Key string `json:"key"`
Duration int `json:"duration"`
} `json:"cookies"`
}
func loadEnvironment(filename string) error {
var err error
if filename != "" {
err = godotenv.Load(filename)
} else {
err = godotenv.Load()
// handle if .env file does not exist, this is OK
if os.IsNotExist(err) {
return nil
}
}
return err
}
type WebhookConfig struct {
URL string `json:"url"`
Retries int `json:"retries"`
TimeoutSec int `json:"timeout_sec"`
Secret string `json:"secret"`
Events []string `json:"events"`
}
func (w *WebhookConfig) HasEvent(event string) bool {
for _, name := range w.Events {
if event == name {
return true
}
}
return false
}
// LoadGlobal loads configuration from file and environment variables.
func LoadGlobal(filename string) (*GlobalConfiguration, error) {
if err := loadEnvironment(filename); err != nil {
return nil, err
}
config := new(GlobalConfiguration)
if err := envconfig.Process("gotrue", config); err != nil {
return nil, err
}
if _, err := ConfigureLogging(&config.Logging); err != nil {
return nil, err
}
ConfigureTracing(&config.Tracing)
if config.SMTP.MaxFrequency == 0 {
config.SMTP.MaxFrequency = 1 * time.Minute
}
return config, nil
}
// LoadConfig loads per-instance configuration.
func LoadConfig(filename string) (*Configuration, error) {
if err := loadEnvironment(filename); err != nil {
return nil, err
}
config := new(Configuration)
if err := envconfig.Process("gotrue", config); err != nil {
return nil, err
}
config.ApplyDefaults()
return config, nil
}
// ApplyDefaults sets defaults for a Configuration
func (config *Configuration) ApplyDefaults() {
if config.JWT.AdminGroupName == "" {
config.JWT.AdminGroupName = "admin"
}
if config.JWT.AdminRoles == nil || len(config.JWT.AdminRoles) == 0 {
config.JWT.AdminRoles = []string{"service_role", "supabase_admin"}
}
if config.JWT.Exp == 0 {
config.JWT.Exp = 3600
}
if config.Mailer.URLPaths.Invite == "" {
config.Mailer.URLPaths.Invite = "/"
}
if config.Mailer.URLPaths.Confirmation == "" {
config.Mailer.URLPaths.Confirmation = "/"
}
if config.Mailer.URLPaths.Recovery == "" {
config.Mailer.URLPaths.Recovery = "/"
}
if config.Mailer.URLPaths.EmailChange == "" {
config.Mailer.URLPaths.EmailChange = "/"
}
if config.SMTP.MaxFrequency == 0 {
config.SMTP.MaxFrequency = 1 * time.Minute
}
if config.Sms.MaxFrequency == 0 {
config.Sms.MaxFrequency = 1 * time.Minute
}
if config.Sms.OtpExp == 0 {
config.Sms.OtpExp = 60
}
if config.Sms.OtpLength == 0 || config.Sms.OtpLength < 6 || config.Sms.OtpLength > 10 {
// 6-digit otp by default
config.Sms.OtpLength = 6
}
if len(config.Sms.Template) == 0 {
config.Sms.Template = ""
}
if config.Cookie.Key == "" {
config.Cookie.Key = "nf_jwt"
}
if config.Cookie.Duration == 0 {
config.Cookie.Duration = 86400
}
if config.URIAllowList == nil {
config.URIAllowList = []string{}
}
}
func (config *Configuration) Value() (driver.Value, error) {
data, err := json.Marshal(config)
if err != nil {
return driver.Value(""), err
}
return driver.Value(string(data)), nil
}
func (config *Configuration) Scan(src interface{}) error {
var source []byte
switch v := src.(type) {
case string:
source = []byte(v)
case []byte:
source = v
default:
return errors.New("Invalid data type for Configuration")
}
if len(source) == 0 {
source = []byte("{}")
}
return json.Unmarshal(source, &config)
}
func (o *OAuthProviderConfiguration) Validate() error {
if !o.Enabled {
return errors.New("Provider is not enabled")
}
if o.ClientID == "" {
return errors.New("Missing Oauth client ID")
}
if o.Secret == "" {
return errors.New("Missing Oauth secret")
}
if o.RedirectURI == "" {
return errors.New("Missing redirect URI")
}
return nil
}
func (t *TwilioProviderConfiguration) Validate() error {
if t.AccountSid == "" {
return errors.New("Missing Twilio account SID")
}
if t.AuthToken == "" {
return errors.New("Missing Twilio auth token")
}
if t.MessageServiceSid == "" {
return errors.New("Missing Twilio message service SID or Twilio phone number")
}
return nil
}
func (t *MessagebirdProviderConfiguration) Validate() error {
if t.AccessKey == "" {
return errors.New("Missing Messagebird access key")
}
if t.Originator == "" {
return errors.New("Missing Messagebird originator")
}
return nil
}