| 
									
										
										
										
											2022-11-26 20:42:45 +08:00
										 |  |  | package settings | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"encoding/json" | 
					
						
							|  |  |  | 	"errors" | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"strings" | 
					
						
							|  |  |  | 	"sync" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	validation "github.com/go-ozzo/ozzo-validation/v4" | 
					
						
							|  |  |  | 	"github.com/go-ozzo/ozzo-validation/v4/is" | 
					
						
							|  |  |  | 	"github.com/pocketbase/pocketbase/tools/auth" | 
					
						
							| 
									
										
										
										
											2022-12-13 17:45:59 +08:00
										 |  |  | 	"github.com/pocketbase/pocketbase/tools/mailer" | 
					
						
							| 
									
										
										
										
											2022-08-15 00:30:45 +08:00
										 |  |  | 	"github.com/pocketbase/pocketbase/tools/rest" | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 	"github.com/pocketbase/pocketbase/tools/security" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-24 03:07:00 +08:00
										 |  |  | // SecretMask is the default settings secrets replacement value
 | 
					
						
							|  |  |  | // (see Settings.RedactClone()).
 | 
					
						
							|  |  |  | const SecretMask string = "******" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | // Settings defines common app configuration options.
 | 
					
						
							|  |  |  | type Settings struct { | 
					
						
							|  |  |  | 	mux sync.RWMutex | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-15 00:30:45 +08:00
										 |  |  | 	Meta MetaConfig `form:"meta" json:"meta"` | 
					
						
							|  |  |  | 	Logs LogsConfig `form:"logs" json:"logs"` | 
					
						
							|  |  |  | 	Smtp SmtpConfig `form:"smtp" json:"smtp"` | 
					
						
							|  |  |  | 	S3   S3Config   `form:"s3" json:"s3"` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-30 16:28:14 +08:00
										 |  |  | 	AdminAuthToken           TokenConfig `form:"adminAuthToken" json:"adminAuthToken"` | 
					
						
							|  |  |  | 	AdminPasswordResetToken  TokenConfig `form:"adminPasswordResetToken" json:"adminPasswordResetToken"` | 
					
						
							| 
									
										
										
										
											2023-04-05 01:33:35 +08:00
										 |  |  | 	AdminFileToken           TokenConfig `form:"adminFileToken" json:"adminFileToken"` | 
					
						
							| 
									
										
										
										
											2022-10-30 16:28:14 +08:00
										 |  |  | 	RecordAuthToken          TokenConfig `form:"recordAuthToken" json:"recordAuthToken"` | 
					
						
							|  |  |  | 	RecordPasswordResetToken TokenConfig `form:"recordPasswordResetToken" json:"recordPasswordResetToken"` | 
					
						
							|  |  |  | 	RecordEmailChangeToken   TokenConfig `form:"recordEmailChangeToken" json:"recordEmailChangeToken"` | 
					
						
							|  |  |  | 	RecordVerificationToken  TokenConfig `form:"recordVerificationToken" json:"recordVerificationToken"` | 
					
						
							| 
									
										
										
										
											2023-04-05 01:33:35 +08:00
										 |  |  | 	RecordFileToken          TokenConfig `form:"recordFileToken" json:"recordFileToken"` | 
					
						
							| 
									
										
										
										
											2022-10-30 16:28:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-06 04:57:39 +08:00
										 |  |  | 	// Deprecated: Will be removed in v0.9+
 | 
					
						
							| 
									
										
										
										
											2022-10-30 16:28:14 +08:00
										 |  |  | 	EmailAuth EmailAuthConfig `form:"emailAuth" json:"emailAuth"` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-01 03:17:10 +08:00
										 |  |  | 	GoogleAuth    AuthProviderConfig `form:"googleAuth" json:"googleAuth"` | 
					
						
							|  |  |  | 	FacebookAuth  AuthProviderConfig `form:"facebookAuth" json:"facebookAuth"` | 
					
						
							|  |  |  | 	GithubAuth    AuthProviderConfig `form:"githubAuth" json:"githubAuth"` | 
					
						
							|  |  |  | 	GitlabAuth    AuthProviderConfig `form:"gitlabAuth" json:"gitlabAuth"` | 
					
						
							|  |  |  | 	DiscordAuth   AuthProviderConfig `form:"discordAuth" json:"discordAuth"` | 
					
						
							|  |  |  | 	TwitterAuth   AuthProviderConfig `form:"twitterAuth" json:"twitterAuth"` | 
					
						
							|  |  |  | 	MicrosoftAuth AuthProviderConfig `form:"microsoftAuth" json:"microsoftAuth"` | 
					
						
							| 
									
										
										
										
											2022-11-01 23:06:06 +08:00
										 |  |  | 	SpotifyAuth   AuthProviderConfig `form:"spotifyAuth" json:"spotifyAuth"` | 
					
						
							| 
									
										
										
										
											2022-11-13 19:05:06 +08:00
										 |  |  | 	KakaoAuth     AuthProviderConfig `form:"kakaoAuth" json:"kakaoAuth"` | 
					
						
							| 
									
										
										
										
											2022-11-13 20:20:11 +08:00
										 |  |  | 	TwitchAuth    AuthProviderConfig `form:"twitchAuth" json:"twitchAuth"` | 
					
						
							| 
									
										
										
										
											2022-12-31 08:21:41 +08:00
										 |  |  | 	StravaAuth    AuthProviderConfig `form:"stravaAuth" json:"stravaAuth"` | 
					
						
							| 
									
										
										
										
											2022-12-31 17:46:36 +08:00
										 |  |  | 	GiteeAuth     AuthProviderConfig `form:"giteeAuth" json:"giteeAuth"` | 
					
						
							| 
									
										
										
										
											2023-01-13 04:12:34 +08:00
										 |  |  | 	LivechatAuth  AuthProviderConfig `form:"livechatAuth" json:"livechatAuth"` | 
					
						
							| 
									
										
										
										
											2023-01-20 16:17:57 +08:00
										 |  |  | 	GiteaAuth     AuthProviderConfig `form:"giteaAuth" json:"giteaAuth"` | 
					
						
							| 
									
										
										
										
											2023-02-24 03:07:00 +08:00
										 |  |  | 	OIDCAuth      AuthProviderConfig `form:"oidcAuth" json:"oidcAuth"` | 
					
						
							|  |  |  | 	OIDC2Auth     AuthProviderConfig `form:"oidc2Auth" json:"oidc2Auth"` | 
					
						
							|  |  |  | 	OIDC3Auth     AuthProviderConfig `form:"oidc3Auth" json:"oidc3Auth"` | 
					
						
							| 
									
										
										
										
											2023-03-02 05:29:45 +08:00
										 |  |  | 	AppleAuth     AuthProviderConfig `form:"appleAuth" json:"appleAuth"` | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-26 20:42:45 +08:00
										 |  |  | // New creates and returns a new default Settings instance.
 | 
					
						
							|  |  |  | func New() *Settings { | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 	return &Settings{ | 
					
						
							|  |  |  | 		Meta: MetaConfig{ | 
					
						
							| 
									
										
										
										
											2022-08-15 00:30:45 +08:00
										 |  |  | 			AppName:                    "Acme", | 
					
						
							|  |  |  | 			AppUrl:                     "http://localhost:8090", | 
					
						
							| 
									
										
										
										
											2022-08-18 22:10:42 +08:00
										 |  |  | 			HideControls:               false, | 
					
						
							| 
									
										
										
										
											2022-08-15 00:30:45 +08:00
										 |  |  | 			SenderName:                 "Support", | 
					
						
							|  |  |  | 			SenderAddress:              "support@example.com", | 
					
						
							|  |  |  | 			VerificationTemplate:       defaultVerificationTemplate, | 
					
						
							|  |  |  | 			ResetPasswordTemplate:      defaultResetPasswordTemplate, | 
					
						
							|  |  |  | 			ConfirmEmailChangeTemplate: defaultConfirmEmailChangeTemplate, | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		Logs: LogsConfig{ | 
					
						
							| 
									
										
										
										
											2022-10-30 16:28:14 +08:00
										 |  |  | 			MaxDays: 5, | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		Smtp: SmtpConfig{ | 
					
						
							|  |  |  | 			Enabled:  false, | 
					
						
							|  |  |  | 			Host:     "smtp.example.com", | 
					
						
							|  |  |  | 			Port:     587, | 
					
						
							|  |  |  | 			Username: "", | 
					
						
							|  |  |  | 			Password: "", | 
					
						
							|  |  |  | 			Tls:      false, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		AdminAuthToken: TokenConfig{ | 
					
						
							|  |  |  | 			Secret:   security.RandomString(50), | 
					
						
							| 
									
										
										
										
											2023-04-05 01:33:35 +08:00
										 |  |  | 			Duration: 1209600, // 14 days
 | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		AdminPasswordResetToken: TokenConfig{ | 
					
						
							|  |  |  | 			Secret:   security.RandomString(50), | 
					
						
							| 
									
										
										
										
											2023-04-05 01:33:35 +08:00
										 |  |  | 			Duration: 1800, // 30 minutes
 | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		AdminFileToken: TokenConfig{ | 
					
						
							|  |  |  | 			Secret:   security.RandomString(50), | 
					
						
							| 
									
										
										
										
											2023-04-05 01:47:03 +08:00
										 |  |  | 			Duration: 300, // 5 minutes
 | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2022-10-30 16:28:14 +08:00
										 |  |  | 		RecordAuthToken: TokenConfig{ | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 			Secret:   security.RandomString(50), | 
					
						
							| 
									
										
										
										
											2023-04-05 01:33:35 +08:00
										 |  |  | 			Duration: 1209600, // 14 days
 | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2022-10-30 16:28:14 +08:00
										 |  |  | 		RecordPasswordResetToken: TokenConfig{ | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 			Secret:   security.RandomString(50), | 
					
						
							| 
									
										
										
										
											2023-04-05 01:33:35 +08:00
										 |  |  | 			Duration: 1800, // 30 minutes
 | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2022-10-30 16:28:14 +08:00
										 |  |  | 		RecordVerificationToken: TokenConfig{ | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 			Secret:   security.RandomString(50), | 
					
						
							| 
									
										
										
										
											2023-04-05 01:33:35 +08:00
										 |  |  | 			Duration: 604800, // 7 days
 | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		RecordFileToken: TokenConfig{ | 
					
						
							|  |  |  | 			Secret:   security.RandomString(50), | 
					
						
							| 
									
										
										
										
											2023-04-05 01:47:03 +08:00
										 |  |  | 			Duration: 300, // 5 minutes
 | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2022-10-30 16:28:14 +08:00
										 |  |  | 		RecordEmailChangeToken: TokenConfig{ | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 			Secret:   security.RandomString(50), | 
					
						
							| 
									
										
										
										
											2023-04-05 01:33:35 +08:00
										 |  |  | 			Duration: 1800, // 30 minutes
 | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		GoogleAuth: AuthProviderConfig{ | 
					
						
							| 
									
										
										
										
											2022-10-30 16:28:14 +08:00
										 |  |  | 			Enabled: false, | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		FacebookAuth: AuthProviderConfig{ | 
					
						
							| 
									
										
										
										
											2022-10-30 16:28:14 +08:00
										 |  |  | 			Enabled: false, | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		GithubAuth: AuthProviderConfig{ | 
					
						
							| 
									
										
										
										
											2022-10-30 16:28:14 +08:00
										 |  |  | 			Enabled: false, | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		GitlabAuth: AuthProviderConfig{ | 
					
						
							| 
									
										
										
										
											2022-10-30 16:28:14 +08:00
										 |  |  | 			Enabled: false, | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2022-08-22 00:38:42 +08:00
										 |  |  | 		DiscordAuth: AuthProviderConfig{ | 
					
						
							| 
									
										
										
										
											2022-10-30 16:28:14 +08:00
										 |  |  | 			Enabled: false, | 
					
						
							| 
									
										
										
										
											2022-08-22 00:38:42 +08:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2022-09-01 21:46:06 +08:00
										 |  |  | 		TwitterAuth: AuthProviderConfig{ | 
					
						
							| 
									
										
										
										
											2022-10-30 16:28:14 +08:00
										 |  |  | 			Enabled: false, | 
					
						
							| 
									
										
										
										
											2022-09-01 21:46:06 +08:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2022-11-01 03:17:10 +08:00
										 |  |  | 		MicrosoftAuth: AuthProviderConfig{ | 
					
						
							|  |  |  | 			Enabled: false, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2022-11-01 23:06:06 +08:00
										 |  |  | 		SpotifyAuth: AuthProviderConfig{ | 
					
						
							|  |  |  | 			Enabled: false, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2022-11-13 19:05:06 +08:00
										 |  |  | 		KakaoAuth: AuthProviderConfig{ | 
					
						
							|  |  |  | 			Enabled: false, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2022-11-13 20:20:11 +08:00
										 |  |  | 		TwitchAuth: AuthProviderConfig{ | 
					
						
							|  |  |  | 			Enabled: false, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2022-12-31 08:21:41 +08:00
										 |  |  | 		StravaAuth: AuthProviderConfig{ | 
					
						
							|  |  |  | 			Enabled: false, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2022-12-31 17:46:36 +08:00
										 |  |  | 		GiteeAuth: AuthProviderConfig{ | 
					
						
							|  |  |  | 			Enabled: false, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2023-01-13 04:12:34 +08:00
										 |  |  | 		LivechatAuth: AuthProviderConfig{ | 
					
						
							|  |  |  | 			Enabled: false, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2023-02-24 03:07:00 +08:00
										 |  |  | 		GiteaAuth: AuthProviderConfig{ | 
					
						
							| 
									
										
										
										
											2023-01-16 17:47:08 +08:00
										 |  |  | 			Enabled: false, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2023-02-24 03:07:00 +08:00
										 |  |  | 		OIDCAuth: AuthProviderConfig{ | 
					
						
							|  |  |  | 			Enabled: false, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		OIDC2Auth: AuthProviderConfig{ | 
					
						
							|  |  |  | 			Enabled: false, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		OIDC3Auth: AuthProviderConfig{ | 
					
						
							| 
									
										
										
										
											2023-01-20 16:17:57 +08:00
										 |  |  | 			Enabled: false, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2023-03-02 05:29:45 +08:00
										 |  |  | 		AppleAuth: AuthProviderConfig{ | 
					
						
							|  |  |  | 			Enabled: false, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Validate makes Settings validatable by implementing [validation.Validatable] interface.
 | 
					
						
							|  |  |  | func (s *Settings) Validate() error { | 
					
						
							|  |  |  | 	s.mux.Lock() | 
					
						
							|  |  |  | 	defer s.mux.Unlock() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return validation.ValidateStruct(s, | 
					
						
							|  |  |  | 		validation.Field(&s.Meta), | 
					
						
							|  |  |  | 		validation.Field(&s.Logs), | 
					
						
							|  |  |  | 		validation.Field(&s.AdminAuthToken), | 
					
						
							|  |  |  | 		validation.Field(&s.AdminPasswordResetToken), | 
					
						
							| 
									
										
										
										
											2023-04-05 01:47:03 +08:00
										 |  |  | 		validation.Field(&s.AdminFileToken), | 
					
						
							| 
									
										
										
										
											2022-10-30 16:28:14 +08:00
										 |  |  | 		validation.Field(&s.RecordAuthToken), | 
					
						
							|  |  |  | 		validation.Field(&s.RecordPasswordResetToken), | 
					
						
							|  |  |  | 		validation.Field(&s.RecordEmailChangeToken), | 
					
						
							|  |  |  | 		validation.Field(&s.RecordVerificationToken), | 
					
						
							| 
									
										
										
										
											2023-04-05 01:33:35 +08:00
										 |  |  | 		validation.Field(&s.RecordFileToken), | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 		validation.Field(&s.Smtp), | 
					
						
							|  |  |  | 		validation.Field(&s.S3), | 
					
						
							|  |  |  | 		validation.Field(&s.GoogleAuth), | 
					
						
							|  |  |  | 		validation.Field(&s.FacebookAuth), | 
					
						
							|  |  |  | 		validation.Field(&s.GithubAuth), | 
					
						
							|  |  |  | 		validation.Field(&s.GitlabAuth), | 
					
						
							| 
									
										
										
										
											2022-08-22 00:38:42 +08:00
										 |  |  | 		validation.Field(&s.DiscordAuth), | 
					
						
							| 
									
										
										
										
											2022-09-01 21:46:06 +08:00
										 |  |  | 		validation.Field(&s.TwitterAuth), | 
					
						
							| 
									
										
										
										
											2022-11-01 03:17:10 +08:00
										 |  |  | 		validation.Field(&s.MicrosoftAuth), | 
					
						
							| 
									
										
										
										
											2022-11-01 23:06:06 +08:00
										 |  |  | 		validation.Field(&s.SpotifyAuth), | 
					
						
							| 
									
										
										
										
											2022-11-13 19:05:06 +08:00
										 |  |  | 		validation.Field(&s.KakaoAuth), | 
					
						
							| 
									
										
										
										
											2022-11-13 20:20:11 +08:00
										 |  |  | 		validation.Field(&s.TwitchAuth), | 
					
						
							| 
									
										
										
										
											2022-12-31 08:21:41 +08:00
										 |  |  | 		validation.Field(&s.StravaAuth), | 
					
						
							| 
									
										
										
										
											2022-12-31 17:46:36 +08:00
										 |  |  | 		validation.Field(&s.GiteeAuth), | 
					
						
							| 
									
										
										
										
											2023-01-13 04:12:34 +08:00
										 |  |  | 		validation.Field(&s.LivechatAuth), | 
					
						
							| 
									
										
										
										
											2023-01-20 16:17:57 +08:00
										 |  |  | 		validation.Field(&s.GiteaAuth), | 
					
						
							| 
									
										
										
										
											2023-02-24 03:07:00 +08:00
										 |  |  | 		validation.Field(&s.OIDCAuth), | 
					
						
							|  |  |  | 		validation.Field(&s.OIDC2Auth), | 
					
						
							|  |  |  | 		validation.Field(&s.OIDC3Auth), | 
					
						
							| 
									
										
										
										
											2023-03-02 05:29:45 +08:00
										 |  |  | 		validation.Field(&s.AppleAuth), | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 	) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Merge merges `other` settings into the current one.
 | 
					
						
							|  |  |  | func (s *Settings) Merge(other *Settings) error { | 
					
						
							|  |  |  | 	s.mux.Lock() | 
					
						
							|  |  |  | 	defer s.mux.Unlock() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bytes, err := json.Marshal(other) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return json.Unmarshal(bytes, s) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Clone creates a new deep copy of the current settings.
 | 
					
						
							| 
									
										
										
										
											2022-07-10 14:13:44 +08:00
										 |  |  | func (s *Settings) Clone() (*Settings, error) { | 
					
						
							| 
									
										
										
										
											2022-11-26 20:42:45 +08:00
										 |  |  | 	clone := &Settings{} | 
					
						
							|  |  |  | 	if err := clone.Merge(s); err != nil { | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-11-26 20:42:45 +08:00
										 |  |  | 	return clone, nil | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // RedactClone creates a new deep copy of the current settings,
 | 
					
						
							|  |  |  | // while replacing the secret values with `******`.
 | 
					
						
							|  |  |  | func (s *Settings) RedactClone() (*Settings, error) { | 
					
						
							|  |  |  | 	clone, err := s.Clone() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	sensitiveFields := []*string{ | 
					
						
							|  |  |  | 		&clone.Smtp.Password, | 
					
						
							|  |  |  | 		&clone.S3.Secret, | 
					
						
							|  |  |  | 		&clone.AdminAuthToken.Secret, | 
					
						
							|  |  |  | 		&clone.AdminPasswordResetToken.Secret, | 
					
						
							| 
									
										
										
										
											2023-04-05 01:47:03 +08:00
										 |  |  | 		&clone.AdminFileToken.Secret, | 
					
						
							| 
									
										
										
										
											2022-10-30 16:28:14 +08:00
										 |  |  | 		&clone.RecordAuthToken.Secret, | 
					
						
							|  |  |  | 		&clone.RecordPasswordResetToken.Secret, | 
					
						
							|  |  |  | 		&clone.RecordEmailChangeToken.Secret, | 
					
						
							|  |  |  | 		&clone.RecordVerificationToken.Secret, | 
					
						
							| 
									
										
										
										
											2023-04-05 01:33:35 +08:00
										 |  |  | 		&clone.RecordFileToken.Secret, | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 		&clone.GoogleAuth.ClientSecret, | 
					
						
							|  |  |  | 		&clone.FacebookAuth.ClientSecret, | 
					
						
							|  |  |  | 		&clone.GithubAuth.ClientSecret, | 
					
						
							|  |  |  | 		&clone.GitlabAuth.ClientSecret, | 
					
						
							| 
									
										
										
										
											2022-08-22 00:38:42 +08:00
										 |  |  | 		&clone.DiscordAuth.ClientSecret, | 
					
						
							| 
									
										
										
										
											2022-09-01 21:46:06 +08:00
										 |  |  | 		&clone.TwitterAuth.ClientSecret, | 
					
						
							| 
									
										
										
										
											2022-11-01 03:17:10 +08:00
										 |  |  | 		&clone.MicrosoftAuth.ClientSecret, | 
					
						
							| 
									
										
										
										
											2022-11-01 23:06:06 +08:00
										 |  |  | 		&clone.SpotifyAuth.ClientSecret, | 
					
						
							| 
									
										
										
										
											2022-11-13 19:05:06 +08:00
										 |  |  | 		&clone.KakaoAuth.ClientSecret, | 
					
						
							| 
									
										
										
										
											2022-11-13 20:20:11 +08:00
										 |  |  | 		&clone.TwitchAuth.ClientSecret, | 
					
						
							| 
									
										
										
										
											2022-12-31 08:21:41 +08:00
										 |  |  | 		&clone.StravaAuth.ClientSecret, | 
					
						
							| 
									
										
										
										
											2022-12-31 17:46:36 +08:00
										 |  |  | 		&clone.GiteeAuth.ClientSecret, | 
					
						
							| 
									
										
										
										
											2023-01-13 04:12:34 +08:00
										 |  |  | 		&clone.LivechatAuth.ClientSecret, | 
					
						
							| 
									
										
										
										
											2023-01-20 16:17:57 +08:00
										 |  |  | 		&clone.GiteaAuth.ClientSecret, | 
					
						
							| 
									
										
										
										
											2023-02-24 03:07:00 +08:00
										 |  |  | 		&clone.OIDCAuth.ClientSecret, | 
					
						
							|  |  |  | 		&clone.OIDC2Auth.ClientSecret, | 
					
						
							|  |  |  | 		&clone.OIDC3Auth.ClientSecret, | 
					
						
							| 
									
										
										
										
											2023-03-02 05:29:45 +08:00
										 |  |  | 		&clone.AppleAuth.ClientSecret, | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// mask all sensitive fields
 | 
					
						
							|  |  |  | 	for _, v := range sensitiveFields { | 
					
						
							|  |  |  | 		if v != nil && *v != "" { | 
					
						
							| 
									
										
										
										
											2023-02-24 03:07:00 +08:00
										 |  |  | 			*v = SecretMask | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return clone, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // NamedAuthProviderConfigs returns a map with all registered OAuth2
 | 
					
						
							|  |  |  | // provider configurations (indexed by their name identifier).
 | 
					
						
							|  |  |  | func (s *Settings) NamedAuthProviderConfigs() map[string]AuthProviderConfig { | 
					
						
							| 
									
										
										
										
											2022-08-15 00:30:45 +08:00
										 |  |  | 	s.mux.RLock() | 
					
						
							|  |  |  | 	defer s.mux.RUnlock() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 	return map[string]AuthProviderConfig{ | 
					
						
							| 
									
										
										
										
											2023-02-24 03:07:00 +08:00
										 |  |  | 		auth.NameGoogle:     s.GoogleAuth, | 
					
						
							|  |  |  | 		auth.NameFacebook:   s.FacebookAuth, | 
					
						
							|  |  |  | 		auth.NameGithub:     s.GithubAuth, | 
					
						
							|  |  |  | 		auth.NameGitlab:     s.GitlabAuth, | 
					
						
							|  |  |  | 		auth.NameDiscord:    s.DiscordAuth, | 
					
						
							|  |  |  | 		auth.NameTwitter:    s.TwitterAuth, | 
					
						
							|  |  |  | 		auth.NameMicrosoft:  s.MicrosoftAuth, | 
					
						
							|  |  |  | 		auth.NameSpotify:    s.SpotifyAuth, | 
					
						
							|  |  |  | 		auth.NameKakao:      s.KakaoAuth, | 
					
						
							|  |  |  | 		auth.NameTwitch:     s.TwitchAuth, | 
					
						
							|  |  |  | 		auth.NameStrava:     s.StravaAuth, | 
					
						
							|  |  |  | 		auth.NameGitee:      s.GiteeAuth, | 
					
						
							|  |  |  | 		auth.NameLivechat:   s.LivechatAuth, | 
					
						
							|  |  |  | 		auth.NameGitea:      s.GiteaAuth, | 
					
						
							|  |  |  | 		auth.NameOIDC:       s.OIDCAuth, | 
					
						
							|  |  |  | 		auth.NameOIDC + "2": s.OIDC2Auth, | 
					
						
							|  |  |  | 		auth.NameOIDC + "3": s.OIDC3Auth, | 
					
						
							| 
									
										
										
										
											2023-03-02 05:29:45 +08:00
										 |  |  | 		auth.NameApple:      s.AppleAuth, | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // -------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type TokenConfig struct { | 
					
						
							|  |  |  | 	Secret   string `form:"secret" json:"secret"` | 
					
						
							|  |  |  | 	Duration int64  `form:"duration" json:"duration"` | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Validate makes TokenConfig validatable by implementing [validation.Validatable] interface.
 | 
					
						
							|  |  |  | func (c TokenConfig) Validate() error { | 
					
						
							|  |  |  | 	return validation.ValidateStruct(&c, | 
					
						
							|  |  |  | 		validation.Field(&c.Secret, validation.Required, validation.Length(30, 300)), | 
					
						
							| 
									
										
										
										
											2022-08-25 15:57:50 +08:00
										 |  |  | 		validation.Field(&c.Duration, validation.Required, validation.Min(5), validation.Max(63072000)), | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 	) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // -------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type SmtpConfig struct { | 
					
						
							|  |  |  | 	Enabled  bool   `form:"enabled" json:"enabled"` | 
					
						
							|  |  |  | 	Host     string `form:"host" json:"host"` | 
					
						
							|  |  |  | 	Port     int    `form:"port" json:"port"` | 
					
						
							|  |  |  | 	Username string `form:"username" json:"username"` | 
					
						
							|  |  |  | 	Password string `form:"password" json:"password"` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-13 17:45:59 +08:00
										 |  |  | 	// SMTP AUTH - PLAIN (default) or LOGIN
 | 
					
						
							|  |  |  | 	AuthMethod string `form:"authMethod" json:"authMethod"` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 	// Whether to enforce TLS encryption for the mail server connection.
 | 
					
						
							|  |  |  | 	//
 | 
					
						
							|  |  |  | 	// When set to false StartTLS command is send, leaving the server
 | 
					
						
							|  |  |  | 	// to decide whether to upgrade the connection or not.
 | 
					
						
							|  |  |  | 	Tls bool `form:"tls" json:"tls"` | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Validate makes SmtpConfig validatable by implementing [validation.Validatable] interface.
 | 
					
						
							|  |  |  | func (c SmtpConfig) Validate() error { | 
					
						
							|  |  |  | 	return validation.ValidateStruct(&c, | 
					
						
							| 
									
										
										
										
											2022-12-13 17:45:59 +08:00
										 |  |  | 		validation.Field( | 
					
						
							|  |  |  | 			&c.Host, | 
					
						
							|  |  |  | 			validation.When(c.Enabled, validation.Required), | 
					
						
							|  |  |  | 			is.Host, | 
					
						
							|  |  |  | 		), | 
					
						
							|  |  |  | 		validation.Field( | 
					
						
							|  |  |  | 			&c.Port, | 
					
						
							|  |  |  | 			validation.When(c.Enabled, validation.Required), | 
					
						
							|  |  |  | 			validation.Min(0), | 
					
						
							|  |  |  | 		), | 
					
						
							|  |  |  | 		validation.Field( | 
					
						
							|  |  |  | 			&c.AuthMethod, | 
					
						
							|  |  |  | 			// don't require it for backward compatibility
 | 
					
						
							|  |  |  | 			// (fallback internally to PLAIN)
 | 
					
						
							|  |  |  | 			// validation.When(c.Enabled, validation.Required),
 | 
					
						
							|  |  |  | 			validation.In(mailer.SmtpAuthLogin, mailer.SmtpAuthPlain), | 
					
						
							|  |  |  | 		), | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 	) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // -------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type S3Config struct { | 
					
						
							| 
									
										
										
										
											2022-07-19 15:45:38 +08:00
										 |  |  | 	Enabled        bool   `form:"enabled" json:"enabled"` | 
					
						
							|  |  |  | 	Bucket         string `form:"bucket" json:"bucket"` | 
					
						
							|  |  |  | 	Region         string `form:"region" json:"region"` | 
					
						
							|  |  |  | 	Endpoint       string `form:"endpoint" json:"endpoint"` | 
					
						
							|  |  |  | 	AccessKey      string `form:"accessKey" json:"accessKey"` | 
					
						
							|  |  |  | 	Secret         string `form:"secret" json:"secret"` | 
					
						
							|  |  |  | 	ForcePathStyle bool   `form:"forcePathStyle" json:"forcePathStyle"` | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Validate makes S3Config validatable by implementing [validation.Validatable] interface.
 | 
					
						
							|  |  |  | func (c S3Config) Validate() error { | 
					
						
							|  |  |  | 	return validation.ValidateStruct(&c, | 
					
						
							| 
									
										
										
										
											2022-07-12 02:00:17 +08:00
										 |  |  | 		validation.Field(&c.Endpoint, is.URL, validation.When(c.Enabled, validation.Required)), | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 		validation.Field(&c.Bucket, validation.When(c.Enabled, validation.Required)), | 
					
						
							|  |  |  | 		validation.Field(&c.Region, validation.When(c.Enabled, validation.Required)), | 
					
						
							|  |  |  | 		validation.Field(&c.AccessKey, validation.When(c.Enabled, validation.Required)), | 
					
						
							|  |  |  | 		validation.Field(&c.Secret, validation.When(c.Enabled, validation.Required)), | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // -------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type MetaConfig struct { | 
					
						
							| 
									
										
										
										
											2022-08-15 00:30:45 +08:00
										 |  |  | 	AppName                    string        `form:"appName" json:"appName"` | 
					
						
							|  |  |  | 	AppUrl                     string        `form:"appUrl" json:"appUrl"` | 
					
						
							| 
									
										
										
										
											2022-08-18 22:10:42 +08:00
										 |  |  | 	HideControls               bool          `form:"hideControls" json:"hideControls"` | 
					
						
							| 
									
										
										
										
											2022-08-15 00:30:45 +08:00
										 |  |  | 	SenderName                 string        `form:"senderName" json:"senderName"` | 
					
						
							|  |  |  | 	SenderAddress              string        `form:"senderAddress" json:"senderAddress"` | 
					
						
							|  |  |  | 	VerificationTemplate       EmailTemplate `form:"verificationTemplate" json:"verificationTemplate"` | 
					
						
							|  |  |  | 	ResetPasswordTemplate      EmailTemplate `form:"resetPasswordTemplate" json:"resetPasswordTemplate"` | 
					
						
							|  |  |  | 	ConfirmEmailChangeTemplate EmailTemplate `form:"confirmEmailChangeTemplate" json:"confirmEmailChangeTemplate"` | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Validate makes MetaConfig validatable by implementing [validation.Validatable] interface.
 | 
					
						
							|  |  |  | func (c MetaConfig) Validate() error { | 
					
						
							|  |  |  | 	return validation.ValidateStruct(&c, | 
					
						
							|  |  |  | 		validation.Field(&c.AppName, validation.Required, validation.Length(1, 255)), | 
					
						
							|  |  |  | 		validation.Field(&c.AppUrl, validation.Required, is.URL), | 
					
						
							|  |  |  | 		validation.Field(&c.SenderName, validation.Required, validation.Length(1, 255)), | 
					
						
							| 
									
										
										
										
											2022-09-01 22:08:55 +08:00
										 |  |  | 		validation.Field(&c.SenderAddress, is.EmailFormat, validation.Required), | 
					
						
							| 
									
										
										
										
											2022-08-15 00:30:45 +08:00
										 |  |  | 		validation.Field(&c.VerificationTemplate, validation.Required), | 
					
						
							|  |  |  | 		validation.Field(&c.ResetPasswordTemplate, validation.Required), | 
					
						
							|  |  |  | 		validation.Field(&c.ConfirmEmailChangeTemplate, validation.Required), | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type EmailTemplate struct { | 
					
						
							|  |  |  | 	Body      string `form:"body" json:"body"` | 
					
						
							|  |  |  | 	Subject   string `form:"subject" json:"subject"` | 
					
						
							|  |  |  | 	ActionUrl string `form:"actionUrl" json:"actionUrl"` | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Validate makes EmailTemplate validatable by implementing [validation.Validatable] interface.
 | 
					
						
							|  |  |  | func (t EmailTemplate) Validate() error { | 
					
						
							|  |  |  | 	return validation.ValidateStruct(&t, | 
					
						
							|  |  |  | 		validation.Field(&t.Subject, validation.Required), | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 		validation.Field( | 
					
						
							| 
									
										
										
										
											2022-08-15 00:30:45 +08:00
										 |  |  | 			&t.Body, | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 			validation.Required, | 
					
						
							| 
									
										
										
										
											2022-08-15 00:30:45 +08:00
										 |  |  | 			validation.By(checkPlaceholderParams(EmailPlaceholderActionUrl)), | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 		), | 
					
						
							|  |  |  | 		validation.Field( | 
					
						
							| 
									
										
										
										
											2022-08-15 00:30:45 +08:00
										 |  |  | 			&t.ActionUrl, | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 			validation.Required, | 
					
						
							| 
									
										
										
										
											2022-08-15 00:30:45 +08:00
										 |  |  | 			validation.By(checkPlaceholderParams(EmailPlaceholderToken)), | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 		), | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-15 00:30:45 +08:00
										 |  |  | func checkPlaceholderParams(params ...string) validation.RuleFunc { | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 	return func(value any) error { | 
					
						
							|  |  |  | 		v, _ := value.(string) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for _, param := range params { | 
					
						
							|  |  |  | 			if !strings.Contains(v, param) { | 
					
						
							| 
									
										
										
										
											2022-08-15 00:30:45 +08:00
										 |  |  | 				return validation.NewError( | 
					
						
							|  |  |  | 					"validation_missing_required_param", | 
					
						
							|  |  |  | 					fmt.Sprintf("Missing required parameter %q", param), | 
					
						
							|  |  |  | 				) | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-15 00:30:45 +08:00
										 |  |  | // Resolve replaces the placeholder parameters in the current email
 | 
					
						
							|  |  |  | // template and returns its components as ready-to-use strings.
 | 
					
						
							|  |  |  | func (t EmailTemplate) Resolve( | 
					
						
							|  |  |  | 	appName string, | 
					
						
							|  |  |  | 	appUrl, | 
					
						
							|  |  |  | 	token string, | 
					
						
							|  |  |  | ) (subject, body, actionUrl string) { | 
					
						
							|  |  |  | 	// replace action url placeholder params (if any)
 | 
					
						
							|  |  |  | 	actionUrlParams := map[string]string{ | 
					
						
							|  |  |  | 		EmailPlaceholderAppName: appName, | 
					
						
							|  |  |  | 		EmailPlaceholderAppUrl:  appUrl, | 
					
						
							|  |  |  | 		EmailPlaceholderToken:   token, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	actionUrl = t.ActionUrl | 
					
						
							|  |  |  | 	for k, v := range actionUrlParams { | 
					
						
							|  |  |  | 		actionUrl = strings.ReplaceAll(actionUrl, k, v) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	actionUrl, _ = rest.NormalizeUrl(actionUrl) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// replace body placeholder params (if any)
 | 
					
						
							|  |  |  | 	bodyParams := map[string]string{ | 
					
						
							|  |  |  | 		EmailPlaceholderAppName:   appName, | 
					
						
							|  |  |  | 		EmailPlaceholderAppUrl:    appUrl, | 
					
						
							|  |  |  | 		EmailPlaceholderToken:     token, | 
					
						
							|  |  |  | 		EmailPlaceholderActionUrl: actionUrl, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	body = t.Body | 
					
						
							|  |  |  | 	for k, v := range bodyParams { | 
					
						
							|  |  |  | 		body = strings.ReplaceAll(body, k, v) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// replace subject placeholder params (if any)
 | 
					
						
							|  |  |  | 	subjectParams := map[string]string{ | 
					
						
							|  |  |  | 		EmailPlaceholderAppName: appName, | 
					
						
							|  |  |  | 		EmailPlaceholderAppUrl:  appUrl, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	subject = t.Subject | 
					
						
							|  |  |  | 	for k, v := range subjectParams { | 
					
						
							|  |  |  | 		subject = strings.ReplaceAll(subject, k, v) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return subject, body, actionUrl | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | // -------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type LogsConfig struct { | 
					
						
							|  |  |  | 	MaxDays int `form:"maxDays" json:"maxDays"` | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Validate makes LogsConfig validatable by implementing [validation.Validatable] interface.
 | 
					
						
							|  |  |  | func (c LogsConfig) Validate() error { | 
					
						
							|  |  |  | 	return validation.ValidateStruct(&c, | 
					
						
							|  |  |  | 		validation.Field(&c.MaxDays, validation.Min(0)), | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // -------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type AuthProviderConfig struct { | 
					
						
							| 
									
										
										
										
											2023-03-26 05:34:00 +08:00
										 |  |  | 	Enabled      bool   `form:"enabled" json:"enabled"` | 
					
						
							|  |  |  | 	ClientId     string `form:"clientId" json:"clientId"` | 
					
						
							|  |  |  | 	ClientSecret string `form:"clientSecret" json:"clientSecret"` | 
					
						
							|  |  |  | 	AuthUrl      string `form:"authUrl" json:"authUrl"` | 
					
						
							|  |  |  | 	TokenUrl     string `form:"tokenUrl" json:"tokenUrl"` | 
					
						
							|  |  |  | 	UserApiUrl   string `form:"userApiUrl" json:"userApiUrl"` | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Validate makes `ProviderConfig` validatable by implementing [validation.Validatable] interface.
 | 
					
						
							|  |  |  | func (c AuthProviderConfig) Validate() error { | 
					
						
							|  |  |  | 	return validation.ValidateStruct(&c, | 
					
						
							|  |  |  | 		validation.Field(&c.ClientId, validation.When(c.Enabled, validation.Required)), | 
					
						
							|  |  |  | 		validation.Field(&c.ClientSecret, validation.When(c.Enabled, validation.Required)), | 
					
						
							|  |  |  | 		validation.Field(&c.AuthUrl, is.URL), | 
					
						
							|  |  |  | 		validation.Field(&c.TokenUrl, is.URL), | 
					
						
							|  |  |  | 		validation.Field(&c.UserApiUrl, is.URL), | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // SetupProvider loads the current AuthProviderConfig into the specified provider.
 | 
					
						
							|  |  |  | func (c AuthProviderConfig) SetupProvider(provider auth.Provider) error { | 
					
						
							|  |  |  | 	if !c.Enabled { | 
					
						
							|  |  |  | 		return errors.New("The provider is not enabled.") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if c.ClientId != "" { | 
					
						
							| 
									
										
										
										
											2022-07-09 22:17:41 +08:00
										 |  |  | 		provider.SetClientId(c.ClientId) | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if c.ClientSecret != "" { | 
					
						
							| 
									
										
										
										
											2022-07-09 22:17:41 +08:00
										 |  |  | 		provider.SetClientSecret(c.ClientSecret) | 
					
						
							| 
									
										
										
										
											2022-07-07 05:19:05 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if c.AuthUrl != "" { | 
					
						
							|  |  |  | 		provider.SetAuthUrl(c.AuthUrl) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if c.UserApiUrl != "" { | 
					
						
							|  |  |  | 		provider.SetUserApiUrl(c.UserApiUrl) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if c.TokenUrl != "" { | 
					
						
							|  |  |  | 		provider.SetTokenUrl(c.TokenUrl) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-10-30 16:28:14 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // -------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-06 04:57:39 +08:00
										 |  |  | // Deprecated: Will be removed in v0.9+
 | 
					
						
							| 
									
										
										
										
											2022-10-30 16:28:14 +08:00
										 |  |  | type EmailAuthConfig struct { | 
					
						
							|  |  |  | 	Enabled           bool     `form:"enabled" json:"enabled"` | 
					
						
							|  |  |  | 	ExceptDomains     []string `form:"exceptDomains" json:"exceptDomains"` | 
					
						
							|  |  |  | 	OnlyDomains       []string `form:"onlyDomains" json:"onlyDomains"` | 
					
						
							|  |  |  | 	MinPasswordLength int      `form:"minPasswordLength" json:"minPasswordLength"` | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-06 04:57:39 +08:00
										 |  |  | // Deprecated: Will be removed in v0.9+
 | 
					
						
							| 
									
										
										
										
											2022-10-30 16:28:14 +08:00
										 |  |  | func (c EmailAuthConfig) Validate() error { | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } |