Chore: Remove public vars in setting package (#81018)

Removes the public variable setting.SecretKey plus some other ones. 
Introduces some new functions for creating setting.Cfg.
This commit is contained in:
Marcus Efraimsson
2024-01-23 12:36:22 +01:00
committed by GitHub
parent 147bf01745
commit 6768c6c059
131 changed files with 759 additions and 699 deletions

View File

@@ -19,7 +19,7 @@ const timeLimitCodeLength = timeLimitStartDateLength + timeLimitMinutesLength +
// create a time limit code
// code format: 12 length date time string + 6 minutes string + 64 HMAC-SHA256 encoded string
func createTimeLimitCode(payload string, minutes int, startStr string) (string, error) {
func createTimeLimitCode(secretKey string, payload string, minutes int, startStr string) (string, error) {
format := "200601021504"
var start, end time.Time
@@ -42,7 +42,7 @@ func createTimeLimitCode(payload string, minutes int, startStr string) (string,
endStr = end.Format(format)
// create HMAC-SHA256 encoded string
key := []byte(setting.SecretKey)
key := []byte(secretKey)
h := hmac.New(sha256.New, key)
if _, err := h.Write([]byte(payload + startStr + endStr)); err != nil {
return "", fmt.Errorf("cannot create hmac: %v", err)
@@ -71,7 +71,7 @@ func validateUserEmailCode(cfg *setting.Cfg, user *user.User, code string) (bool
// right active code
payload := strconv.FormatInt(user.ID, 10) + user.Email + user.Login + user.Password + user.Rands
expectedCode, err := createTimeLimitCode(payload, minutes, startStr)
expectedCode, err := createTimeLimitCode(cfg.SecretKey, payload, minutes, startStr)
if err != nil {
return false, err
}
@@ -104,7 +104,7 @@ func getLoginForEmailCode(code string) string {
func createUserEmailCode(cfg *setting.Cfg, user *user.User, startStr string) (string, error) {
minutes := cfg.EmailCodeValidMinutes
payload := strconv.FormatInt(user.ID, 10) + user.Email + user.Login + user.Password + user.Rands
code, err := createTimeLimitCode(payload, minutes, startStr)
code, err := createTimeLimitCode(cfg.SecretKey, payload, minutes, startStr)
if err != nil {
return "", err
}

View File

@@ -76,7 +76,7 @@ func TestTimeLimitCodes(t *testing.T) {
for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
code, err := createTimeLimitCode(test.payload, test.minutes, test.start.Format(format))
code, err := createTimeLimitCode(cfg.SecretKey, test.payload, test.minutes, test.start.Format(format))
require.NoError(t, err)
isValid, err := validateUserEmailCode(cfg, user, code)
@@ -86,7 +86,7 @@ func TestTimeLimitCodes(t *testing.T) {
}
t.Run("tampered minutes", func(t *testing.T) {
code, err := createTimeLimitCode(mailPayload, 5, tenMinutesAgo.Format(format))
code, err := createTimeLimitCode(cfg.SecretKey, mailPayload, 5, tenMinutesAgo.Format(format))
require.NoError(t, err)
// code is expired
@@ -102,7 +102,7 @@ func TestTimeLimitCodes(t *testing.T) {
})
t.Run("tampered start string", func(t *testing.T) {
code, err := createTimeLimitCode(mailPayload, 5, tenMinutesAgo.Format(format))
code, err := createTimeLimitCode(cfg.SecretKey, mailPayload, 5, tenMinutesAgo.Format(format))
require.NoError(t, err)
// code is expired

View File

@@ -25,7 +25,7 @@ type Message struct {
}
func setDefaultTemplateData(cfg *setting.Cfg, data map[string]any, u *user.User) {
data["AppUrl"] = setting.AppUrl
data["AppUrl"] = cfg.AppURL
data["BuildVersion"] = setting.BuildVersion
data["BuildStamp"] = setting.BuildStamp
data["EmailCodeValidHours"] = cfg.EmailCodeValidMinutes / 60

View File

@@ -258,7 +258,7 @@ func (ns *NotificationService) ValidateResetPasswordCode(ctx context.Context, qu
}
func (ns *NotificationService) signUpStartedHandler(ctx context.Context, evt *events.SignUpStarted) error {
if !setting.VerifyEmailEnabled {
if !ns.Cfg.VerifyEmailEnabled {
return nil
}

View File

@@ -159,12 +159,8 @@ func (sc *SmtpClient) createDialer() (*gomail.Dialer, error) {
d := gomail.NewDialer(host, iPort, sc.cfg.User, sc.cfg.Password)
d.TLSConfig = tlsconfig
d.StartTLSPolicy = getStartTLSPolicy(sc.cfg.StartTLSPolicy)
d.LocalName = sc.cfg.EhloIdentity
if sc.cfg.EhloIdentity != "" {
d.LocalName = sc.cfg.EhloIdentity
} else {
d.LocalName = setting.InstanceName
}
return d, nil
}