pkg/services: Check errors (#19712)
* pkg/services: Check errors * pkg/services: Don't treat context.Canceled|context.DeadlineExceeded as error
This commit is contained in:
@@ -16,7 +16,7 @@ const timeLimitCodeLength = 12 + 6 + 40
|
||||
|
||||
// create a time limit code
|
||||
// code format: 12 length date time string + 6 minutes string + 40 sha1 encoded string
|
||||
func createTimeLimitCode(data string, minutes int, startInf interface{}) string {
|
||||
func createTimeLimitCode(data string, minutes int, startInf interface{}) (string, error) {
|
||||
format := "200601021504"
|
||||
|
||||
var start, end time.Time
|
||||
@@ -38,17 +38,20 @@ func createTimeLimitCode(data string, minutes int, startInf interface{}) string
|
||||
|
||||
// create sha1 encode string
|
||||
sh := sha1.New()
|
||||
sh.Write([]byte(data + setting.SecretKey + startStr + endStr + com.ToStr(minutes)))
|
||||
if _, err := sh.Write([]byte(data + setting.SecretKey + startStr + endStr +
|
||||
com.ToStr(minutes))); err != nil {
|
||||
return "", err
|
||||
}
|
||||
encoded := hex.EncodeToString(sh.Sum(nil))
|
||||
|
||||
code := fmt.Sprintf("%s%06d%s", startStr, minutes, encoded)
|
||||
return code
|
||||
return code, nil
|
||||
}
|
||||
|
||||
// verify time limit code
|
||||
func validateUserEmailCode(user *m.User, code string) bool {
|
||||
func validateUserEmailCode(user *m.User, code string) (bool, error) {
|
||||
if len(code) <= 18 {
|
||||
return false
|
||||
return false, nil
|
||||
}
|
||||
|
||||
minutes := setting.EmailCodeValidMinutes
|
||||
@@ -63,18 +66,21 @@ func validateUserEmailCode(user *m.User, code string) bool {
|
||||
|
||||
// right active code
|
||||
data := com.ToStr(user.Id) + user.Email + user.Login + user.Password + user.Rands
|
||||
retCode := createTimeLimitCode(data, minutes, start)
|
||||
retCode, err := createTimeLimitCode(data, minutes, start)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
fmt.Printf("code : %s\ncode2: %s", retCode, code)
|
||||
if retCode == code && minutes > 0 {
|
||||
// check time is expired or not
|
||||
before, _ := time.ParseInLocation("200601021504", start, time.Local)
|
||||
now := time.Now()
|
||||
if before.Add(time.Minute*time.Duration(minutes)).Unix() > now.Unix() {
|
||||
return true
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func getLoginForEmailCode(code string) string {
|
||||
@@ -88,12 +94,15 @@ func getLoginForEmailCode(code string) string {
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func createUserEmailCode(u *m.User, startInf interface{}) string {
|
||||
func createUserEmailCode(u *m.User, startInf interface{}) (string, error) {
|
||||
minutes := setting.EmailCodeValidMinutes
|
||||
data := com.ToStr(u.Id) + u.Email + u.Login + u.Password + u.Rands
|
||||
code := createTimeLimitCode(data, minutes, startInf)
|
||||
code, err := createTimeLimitCode(data, minutes, startInf)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// add tail hex username
|
||||
code += hex.EncodeToString([]byte(u.Login))
|
||||
return code
|
||||
return code, nil
|
||||
}
|
||||
|
||||
@@ -14,7 +14,8 @@ func TestEmailCodes(t *testing.T) {
|
||||
setting.EmailCodeValidMinutes = 120
|
||||
|
||||
user := &m.User{Id: 10, Email: "t@a.com", Login: "asd", Password: "1", Rands: "2"}
|
||||
code := createUserEmailCode(user, nil)
|
||||
code, err := createUserEmailCode(user, nil)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
Convey("getLoginForCode should return login", func() {
|
||||
login := getLoginForEmailCode(code)
|
||||
@@ -22,12 +23,16 @@ func TestEmailCodes(t *testing.T) {
|
||||
})
|
||||
|
||||
Convey("Can verify valid code", func() {
|
||||
So(validateUserEmailCode(user, code), ShouldBeTrue)
|
||||
isValid, err := validateUserEmailCode(user, code)
|
||||
So(err, ShouldBeNil)
|
||||
So(isValid, ShouldBeTrue)
|
||||
})
|
||||
|
||||
Convey("Cannot verify in-valid code", func() {
|
||||
code = "ASD"
|
||||
So(validateUserEmailCode(user, code), ShouldBeFalse)
|
||||
isValid, err := validateUserEmailCode(user, code)
|
||||
So(err, ShouldBeNil)
|
||||
So(isValid, ShouldBeFalse)
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
@@ -147,11 +147,15 @@ func (ns *NotificationService) sendEmailCommandHandler(cmd *m.SendEmailCommand)
|
||||
}
|
||||
|
||||
func (ns *NotificationService) sendResetPasswordEmail(cmd *m.SendResetPasswordEmailCommand) error {
|
||||
code, err := createUserEmailCode(cmd.User, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return ns.sendEmailCommandHandler(&m.SendEmailCommand{
|
||||
To: []string{cmd.User.Email},
|
||||
Template: tmplResetPassword,
|
||||
Data: map[string]interface{}{
|
||||
"Code": createUserEmailCode(cmd.User, nil),
|
||||
"Code": code,
|
||||
"Name": cmd.User.NameOrFallback(),
|
||||
},
|
||||
})
|
||||
@@ -168,7 +172,11 @@ func (ns *NotificationService) validateResetPasswordCode(query *m.ValidateResetP
|
||||
return err
|
||||
}
|
||||
|
||||
if !validateUserEmailCode(userQuery.Result, query.Code) {
|
||||
validEmailCode, err := validateUserEmailCode(userQuery.Result, query.Code)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !validEmailCode {
|
||||
return m.ErrInvalidEmailCode
|
||||
}
|
||||
|
||||
|
||||
@@ -61,7 +61,8 @@ func TestEmailIntegrationTest(t *testing.T) {
|
||||
sentMsg := <-ns.mailQueue
|
||||
So(sentMsg.From, ShouldEqual, "Grafana Admin <from@address.com>")
|
||||
So(sentMsg.To[0], ShouldEqual, "asdf@asdf.com")
|
||||
ioutil.WriteFile("../../../tmp/test_email.html", []byte(sentMsg.Body), 0777)
|
||||
err = ioutil.WriteFile("../../../tmp/test_email.html", []byte(sentMsg.Body), 0777)
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
@@ -77,7 +77,9 @@ func (ns *NotificationService) sendWebRequestSync(ctx context.Context, webhook *
|
||||
|
||||
if resp.StatusCode/100 == 2 {
|
||||
// flushing the body enables the transport to reuse the same connection
|
||||
io.Copy(ioutil.Discard, resp.Body)
|
||||
if _, err := io.Copy(ioutil.Discard, resp.Body); err != nil {
|
||||
ns.log.Error("Failed to copy resp.Body to ioutil.Discard", "err", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user