2016-08-19 00:45:39 +00:00
|
|
|
package kolide
|
2016-08-12 19:20:29 +00:00
|
|
|
|
|
|
|
import (
|
2016-09-15 14:52:17 +00:00
|
|
|
"bytes"
|
|
|
|
"html/template"
|
2016-08-12 19:20:29 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2016-09-15 14:52:17 +00:00
|
|
|
// PasswordResetStore manages password resets in the Datastore
|
|
|
|
type PasswordResetStore interface {
|
|
|
|
NewPasswordResetRequest(req *PasswordResetRequest) (*PasswordResetRequest, error)
|
|
|
|
SavePasswordResetRequest(req *PasswordResetRequest) error
|
2016-08-19 00:45:39 +00:00
|
|
|
DeletePasswordResetRequest(req *PasswordResetRequest) error
|
2016-09-15 14:52:17 +00:00
|
|
|
DeletePasswordResetRequestsForUser(userID uint) error
|
2016-08-19 00:45:39 +00:00
|
|
|
FindPassswordResetByID(id uint) (*PasswordResetRequest, error)
|
2016-08-20 06:56:49 +00:00
|
|
|
FindPassswordResetsByUserID(id uint) ([]*PasswordResetRequest, error)
|
2016-08-19 00:45:39 +00:00
|
|
|
FindPassswordResetByToken(token string) (*PasswordResetRequest, error)
|
|
|
|
FindPassswordResetByTokenAndUserID(token string, id uint) (*PasswordResetRequest, error)
|
|
|
|
}
|
|
|
|
|
2016-09-15 14:52:17 +00:00
|
|
|
// Mailer is an email campaign
|
|
|
|
// Types which implement the Campaign interface
|
|
|
|
// can be marshalled into an email body
|
|
|
|
type Mailer interface {
|
|
|
|
Message() ([]byte, error)
|
2016-08-12 19:20:29 +00:00
|
|
|
}
|
|
|
|
|
2016-09-15 14:52:17 +00:00
|
|
|
type Email struct {
|
|
|
|
To []string
|
|
|
|
From string
|
|
|
|
Msg Mailer
|
2016-08-12 19:20:29 +00:00
|
|
|
}
|
|
|
|
|
2016-09-15 14:52:17 +00:00
|
|
|
type MailService interface {
|
|
|
|
SendEmail(e Email) error
|
2016-08-12 19:20:29 +00:00
|
|
|
}
|
2016-08-19 00:45:39 +00:00
|
|
|
|
|
|
|
// PasswordResetRequest represents a database table for
|
|
|
|
// Password Reset Requests
|
|
|
|
type PasswordResetRequest struct {
|
2016-11-16 13:47:49 +00:00
|
|
|
UpdateCreateTimestamps
|
|
|
|
ID uint
|
|
|
|
ExpiresAt time.Time `db:"expires_at"`
|
|
|
|
UserID uint `db:"user_id"`
|
|
|
|
Token string
|
2016-08-19 00:45:39 +00:00
|
|
|
}
|
|
|
|
|
2016-09-15 14:52:17 +00:00
|
|
|
const passwordResetTemplate = `
|
2016-10-20 19:09:51 +00:00
|
|
|
You requested a password reset,
|
2016-09-15 14:52:17 +00:00
|
|
|
Follow the link below to reset your password:
|
2016-10-20 19:09:51 +00:00
|
|
|
http://localhost:8080/login/reset?token={{.Token}}
|
2016-09-15 14:52:17 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
func (r PasswordResetRequest) Message() ([]byte, error) {
|
|
|
|
var msg bytes.Buffer
|
|
|
|
var err error
|
|
|
|
t := template.New(passwordResetTemplate)
|
|
|
|
if t, err = t.Parse(passwordResetTemplate); err != nil {
|
2016-08-19 00:45:39 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-09-15 14:52:17 +00:00
|
|
|
if err = t.Execute(&msg, r); err != nil {
|
2016-08-19 00:45:39 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-09-15 14:52:17 +00:00
|
|
|
return msg.Bytes(), nil
|
2016-08-19 00:45:39 +00:00
|
|
|
}
|