diff options
author | Kevin Chabowski <kevin@kch42.de> | 2013-08-30 22:53:59 +0200 |
---|---|---|
committer | Kevin Chabowski <kevin@kch42.de> | 2013-08-30 22:53:59 +0200 |
commit | d4fe97a9a5437d71a5a29f413ba7be6efe5f7da3 (patch) | |
tree | 1df00e016300304318997e8f04053bf523b8d85e | |
parent | 11ec26feeabced25281b8637f928a8096690c54b (diff) | |
download | mailremind-d4fe97a9a5437d71a5a29f413ba7be6efe5f7da3.tar.gz mailremind-d4fe97a9a5437d71a5a29f413ba7be6efe5f7da3.tar.bz2 mailremind-d4fe97a9a5437d71a5a29f413ba7be6efe5f7da3.zip |
Password reset implemented
-rw-r--r-- | mails.go | 24 | ||||
-rw-r--r-- | mails/pwreset.tpl | 8 | ||||
-rw-r--r-- | main.go | 1 | ||||
-rw-r--r-- | pwreset.go | 36 | ||||
-rw-r--r-- | tpls.go | 2 | ||||
-rw-r--r-- | tpls/forgotpw.tpl | 16 | ||||
-rw-r--r-- | tpls/login.tpl | 1 |
7 files changed, 87 insertions, 1 deletions
@@ -18,7 +18,10 @@ func loadMailTpl(tplroot, name string) *template.Template { return tpl } -var mailActivationcode *template.Template +var ( + mailActivationcode *template.Template + mailPwreset *template.Template +) func initMails() { tplroot, err := conf.GetString("paths", "mailtpls") @@ -27,6 +30,7 @@ func initMails() { } mailActivationcode = loadMailTpl(tplroot, "activationcode") + mailPwreset = loadMailTpl(tplroot, "pwreset") } type activationcodeData struct { @@ -50,3 +54,21 @@ func SendActivationcode(to, acCode string, uid model.DBID) bool { return Mail(to, MailFrom, buf.Bytes()) } + +func SendPwresetLink(to, code string, uid model.DBID) bool { + buf := new(bytes.Buffer) + fmt.Fprintf(buf, "To: %s\n", to) + fmt.Fprintf(buf, "From: %s\n", MailFrom) + fmt.Fprintf(buf, "Subject: Password reset request for your mailremind account\n") + fmt.Fprintf(buf, "Date: %s\n", time.Now().Format(time.RFC822)) + + fmt.Fprintln(buf, "") + + url := fmt.Sprintf("%s/pwreset?U=%s&Code=%s", baseurl, uid, code) + if err := mailPwreset.Execute(buf, activationcodeData{url}); err != nil { + log.Printf("Error while executing mail template (pwreset): %s", err) + return false + } + + return Mail(to, MailFrom, buf.Bytes()) +} diff --git a/mails/pwreset.tpl b/mails/pwreset.tpl new file mode 100644 index 0000000..49ce391 --- /dev/null +++ b/mails/pwreset.tpl @@ -0,0 +1,8 @@ +Hi, + +You (or someone else) requested a password reset for your mailremind +account. Go to this URL, if you want to reset the password: + + {{.URL}} + +If you didn't request a reset, you can ignore this message. @@ -88,6 +88,7 @@ func main() { router.HandleFunc("/delete-acc/yes", mkHttpHandler(deleteacc, tplMsg)) router.HandleFunc("/delete-acc", mkHttpHandler(deleteask, tplReallyDelete)) router.HandleFunc("/pwreset", mkHttpHandler(pwreset, tplPwreset)) + router.HandleFunc("/forgotpw", mkHttpHandler(forgotpw, tplForgotpw)) http.Handle("/", router) @@ -75,3 +75,39 @@ func pwreset(user model.User, sess *sessions.Session, req *http.Request) interfa outdata.Success = "Password was changed" return outdata } + +type forgotpwTpldata struct { + Error, Success string +} + +func forgotpw(user model.User, sess *sessions.Session, req *http.Request) interface{} { + if req.Method != "POST" { + return &forgotpwTpldata{} + } + + if err := req.ParseForm(); err != nil { + return &forgotpwTpldata{Error: "Could not understand formdata."} + } + + email := req.FormValue("Mail") + if email == "" { + return &forgotpwTpldata{Error: "E-Mail must not be empty."} + } + + user, err := dbcon.UserByMail(email) + if err != nil { + return &forgotpwTpldata{Error: "E-Mail not found."} + } + + key := genAcCode() + if err := user.SetActivationCode(key); err != nil { + log.Printf("Could not store pwreset key: %s", err) + return &forgotpwTpldata{Error: "Could not store keyword reset code. If this happens again, please contact support."} + } + + if !SendPwresetLink(user.Email(), key, user.ID()) { + return &forgotpwTpldata{Error: "Could not send reset E-Mail. If this happens again, please contact support."} + } + + return &forgotpwTpldata{Success: "We sent you an E-Mail with further instructions."} +} @@ -22,6 +22,7 @@ var ( tplLogin *template.Template tplReallyDelete *template.Template tplPwreset *template.Template + tplForgotpw *template.Template ) func initTpls() { @@ -35,6 +36,7 @@ func initTpls() { tplLogin = loadTpl(tplpath, "login") tplReallyDelete = loadTpl(tplpath, "reallydelete") tplPwreset = loadTpl(tplpath, "pwreset") + tplForgotpw = loadTpl(tplpath, "forgotpw") } type msgTpldata struct { diff --git a/tpls/forgotpw.tpl b/tpls/forgotpw.tpl new file mode 100644 index 0000000..27dda6d --- /dev/null +++ b/tpls/forgotpw.tpl @@ -0,0 +1,16 @@ +{{define "title"}}Reset Password{{end}} + +{{define "content"}} + {{if .Success}} + <div class="success">{{.Success}}</div> + {{else}} + {{if .Error}} + <div class="error">{{.Error}}</div> + {{end}} + + <form action="/forgotpw" method="post" accept-charset="UTF-8"> + <p><strong>E-Mail:</strong> <input type="text" name="Mail" /></p> + <p><input type="submit" /></p> + </form> + {{end}} +{{end}}
\ No newline at end of file diff --git a/tpls/login.tpl b/tpls/login.tpl index 34541e4..b3a63e2 100644 --- a/tpls/login.tpl +++ b/tpls/login.tpl @@ -12,6 +12,7 @@ <p><strong>E-Mail</strong> <input type="text" name="Mail" /></p> <p><strong>Password</strong> <input type="password" name="Password" /></p> <p><input type="submit" /></p> + <p><a href="/forgotpw">Forgot your password?</a></p> </form> {{end}} {{end}}
\ No newline at end of file |