2015-02-07 14:27:12 +02:00
|
|
|
// Package confirm implements user confirming after N bad sign-in attempts.
|
|
|
|
package confirm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/md5"
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/base64"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2015-02-10 10:43:45 +02:00
|
|
|
"net/url"
|
2015-02-07 14:27:12 +02:00
|
|
|
|
|
|
|
"gopkg.in/authboss.v0"
|
|
|
|
"gopkg.in/authboss.v0/internal/views"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-02-16 23:27:29 +02:00
|
|
|
StoreConfirmToken = "confirm_token"
|
|
|
|
StoreConfirmed = "confirmed"
|
2015-02-07 14:27:12 +02:00
|
|
|
|
|
|
|
FormValueConfirm = "cnf"
|
|
|
|
|
|
|
|
tplConfirmHTML = "confirm_email.html.tpl"
|
2015-02-11 02:29:52 +02:00
|
|
|
tplConfirmText = "confirm_email.txt.tpl"
|
2015-02-07 14:27:12 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrNotConfirmed happens when the account is there, but
|
|
|
|
// not yet confirmed.
|
|
|
|
ErrNotConfirmed = errors.New("Account is not confirmed.")
|
|
|
|
)
|
|
|
|
|
|
|
|
// C is the singleton instance of the confirm module which will have been
|
|
|
|
// configured and ready to use after authboss.Init()
|
|
|
|
var C *Confirm
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
C = &Confirm{}
|
|
|
|
authboss.RegisterModule("confirm", C)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Confirm struct {
|
|
|
|
emailTemplates views.Templates
|
|
|
|
}
|
|
|
|
|
2015-02-16 06:35:32 +02:00
|
|
|
func (c *Confirm) Initialize() (err error) {
|
2015-02-10 10:43:45 +02:00
|
|
|
var ok bool
|
2015-02-16 06:35:32 +02:00
|
|
|
storer, ok := authboss.Cfg.Storer.(authboss.ConfirmStorer)
|
|
|
|
if storer == nil || !ok {
|
2015-02-10 10:43:45 +02:00
|
|
|
return errors.New("confirm: Need a ConfirmStorer.")
|
2015-02-07 14:27:12 +02:00
|
|
|
}
|
|
|
|
|
2015-02-16 06:35:32 +02:00
|
|
|
c.emailTemplates, err = views.Get(authboss.Cfg.LayoutEmail, authboss.Cfg.ViewsPath, tplConfirmHTML, tplConfirmText)
|
2015-02-07 14:27:12 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-02-16 06:35:32 +02:00
|
|
|
authboss.Cfg.Callbacks.Before(authboss.EventGet, c.BeforeGet)
|
|
|
|
authboss.Cfg.Callbacks.After(authboss.EventRegister, c.AfterRegister)
|
2015-02-07 14:27:12 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Confirm) Routes() authboss.RouteTable {
|
|
|
|
return authboss.RouteTable{
|
|
|
|
"/confirm": c.confirmHandler,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Confirm) Storage() authboss.StorageOptions {
|
|
|
|
return authboss.StorageOptions{
|
2015-02-16 23:27:29 +02:00
|
|
|
StoreConfirmToken: authboss.String,
|
|
|
|
StoreConfirmed: authboss.Bool,
|
2015-02-07 14:27:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Confirm) BeforeGet(ctx *authboss.Context) error {
|
2015-02-16 23:27:29 +02:00
|
|
|
if intf, ok := ctx.User[StoreConfirmed]; ok {
|
|
|
|
if confirmed, ok := intf.(bool); ok && confirmed {
|
|
|
|
return nil
|
2015-02-07 14:27:12 +02:00
|
|
|
}
|
|
|
|
}
|
2015-02-10 10:43:45 +02:00
|
|
|
|
2015-02-16 23:27:29 +02:00
|
|
|
return ErrNotConfirmed
|
2015-02-07 14:27:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// AfterRegister ensures the account is not activated.
|
|
|
|
func (c *Confirm) AfterRegister(ctx *authboss.Context) {
|
|
|
|
if ctx.User == nil {
|
2015-02-16 06:35:32 +02:00
|
|
|
fmt.Fprintln(authboss.Cfg.LogWriter, "confirm: user not loaded in AfterRegister callback")
|
2015-02-16 23:27:29 +02:00
|
|
|
return
|
2015-02-07 14:27:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
token := make([]byte, 32)
|
|
|
|
if _, err := rand.Read(token); err != nil {
|
2015-02-16 06:35:32 +02:00
|
|
|
fmt.Fprintln(authboss.Cfg.LogWriter, "confirm: failed to produce random token:", err)
|
2015-02-07 14:27:12 +02:00
|
|
|
}
|
|
|
|
sum := md5.Sum(token)
|
|
|
|
|
2015-02-16 23:27:29 +02:00
|
|
|
ctx.User[StoreConfirmToken] = base64.StdEncoding.EncodeToString(sum[:])
|
2015-02-07 14:27:12 +02:00
|
|
|
|
2015-02-16 23:27:29 +02:00
|
|
|
username, ok := ctx.User.String(authboss.StoreUsername)
|
|
|
|
if !ok {
|
|
|
|
fmt.Fprintln(authboss.Cfg.LogWriter, "confirm: failed to save confirm token, username doesn't exist")
|
|
|
|
}
|
2015-02-10 10:43:45 +02:00
|
|
|
|
2015-02-16 06:35:32 +02:00
|
|
|
if err := ctx.SaveUser(username, authboss.Cfg.Storer); err != nil {
|
|
|
|
fmt.Fprintln(authboss.Cfg.LogWriter, "confirm: failed to save user's token:", err)
|
2015-02-10 10:43:45 +02:00
|
|
|
return
|
2015-02-07 14:27:12 +02:00
|
|
|
}
|
|
|
|
|
2015-02-16 23:27:29 +02:00
|
|
|
if email, ok := ctx.User.String(authboss.StoreEmail); !ok {
|
2015-02-16 06:35:32 +02:00
|
|
|
fmt.Fprintln(authboss.Cfg.LogWriter, "confirm: user has no e-mail address to send to, could not send confirm e-mail")
|
2015-02-07 14:27:12 +02:00
|
|
|
} else {
|
2015-02-10 10:43:45 +02:00
|
|
|
goConfirmEmail(c, email, base64.URLEncoding.EncodeToString(sum[:]))
|
2015-02-07 14:27:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-10 10:43:45 +02:00
|
|
|
var goConfirmEmail = func(c *Confirm, to, token string) {
|
|
|
|
go c.confirmEmail(to, token)
|
|
|
|
}
|
|
|
|
|
2015-02-07 14:27:12 +02:00
|
|
|
// confirmEmail sends a confirmation e-mail.
|
|
|
|
func (c *Confirm) confirmEmail(to, token string) {
|
2015-02-16 06:35:32 +02:00
|
|
|
url := fmt.Sprintf("%s/confirm?%s=%s", authboss.Cfg.HostName, url.QueryEscape(FormValueConfirm), url.QueryEscape(token))
|
2015-02-07 14:27:12 +02:00
|
|
|
|
2015-02-10 10:43:45 +02:00
|
|
|
var htmlEmailBody, textEmailBody *bytes.Buffer
|
|
|
|
var err error
|
|
|
|
if htmlEmailBody, err = c.emailTemplates.ExecuteTemplate(tplConfirmHTML, url); err != nil {
|
2015-02-16 06:35:32 +02:00
|
|
|
fmt.Fprintln(authboss.Cfg.LogWriter, "confirm: failed to build html template:", err)
|
2015-02-10 10:43:45 +02:00
|
|
|
return
|
2015-02-07 14:27:12 +02:00
|
|
|
}
|
|
|
|
|
2015-02-10 10:43:45 +02:00
|
|
|
if textEmailBody, err = c.emailTemplates.ExecuteTemplate(tplConfirmText, url); err != nil {
|
2015-02-16 06:35:32 +02:00
|
|
|
fmt.Fprintln(authboss.Cfg.LogWriter, "confirm: failed to build plaintext template:", err)
|
2015-02-10 10:43:45 +02:00
|
|
|
return
|
2015-02-07 14:27:12 +02:00
|
|
|
}
|
|
|
|
|
2015-02-16 06:35:32 +02:00
|
|
|
if err := authboss.Cfg.Mailer.Send(authboss.Email{
|
2015-02-07 14:27:12 +02:00
|
|
|
To: []string{to},
|
2015-02-16 06:35:32 +02:00
|
|
|
From: authboss.Cfg.EmailFrom,
|
|
|
|
Subject: authboss.Cfg.EmailSubjectPrefix + "Confirm New Account",
|
2015-02-07 14:27:12 +02:00
|
|
|
TextBody: textEmailBody.String(),
|
|
|
|
HTMLBody: htmlEmailBody.String(),
|
|
|
|
}); err != nil {
|
2015-02-16 06:35:32 +02:00
|
|
|
fmt.Fprintln(authboss.Cfg.LogWriter, "confirm: failed to build plaintext template:", err)
|
2015-02-07 14:27:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-10 10:43:45 +02:00
|
|
|
func (c *Confirm) confirmHandler(ctx *authboss.Context, w http.ResponseWriter, r *http.Request) {
|
|
|
|
token, ok := ctx.FirstFormValue(FormValueConfirm)
|
|
|
|
if len(token) == 0 || !ok {
|
|
|
|
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
2015-02-16 23:27:29 +02:00
|
|
|
fmt.Fprintln(authboss.Cfg.LogWriter, "confirm: no confirm token found in request")
|
2015-02-10 10:43:45 +02:00
|
|
|
return
|
|
|
|
}
|
2015-02-07 14:27:12 +02:00
|
|
|
|
2015-02-16 23:27:29 +02:00
|
|
|
toHash, err := base64.URLEncoding.DecodeString(token)
|
2015-02-07 14:27:12 +02:00
|
|
|
if err != nil {
|
2015-02-10 10:43:45 +02:00
|
|
|
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
2015-02-16 06:35:32 +02:00
|
|
|
fmt.Fprintf(authboss.Cfg.LogWriter, "confirm: confirm token failed to decode %q => %v\n", token, err)
|
2015-02-10 10:43:45 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-02-16 23:27:29 +02:00
|
|
|
sum := md5.Sum(toHash)
|
|
|
|
|
|
|
|
dbTok := base64.StdEncoding.EncodeToString(sum[:])
|
2015-02-16 06:35:32 +02:00
|
|
|
user, err := authboss.Cfg.Storer.(authboss.ConfirmStorer).ConfirmUser(dbTok)
|
2015-02-10 10:43:45 +02:00
|
|
|
if err == authboss.ErrUserNotFound {
|
|
|
|
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
2015-02-16 23:27:29 +02:00
|
|
|
fmt.Fprintln(authboss.Cfg.LogWriter, "confirm: token not found:", err)
|
2015-02-10 10:43:45 +02:00
|
|
|
return
|
|
|
|
} else if err != nil {
|
|
|
|
w.WriteHeader(500)
|
2015-02-16 06:35:32 +02:00
|
|
|
fmt.Fprintln(authboss.Cfg.LogWriter, "confirm: error retrieving user token:", err)
|
2015-02-10 10:43:45 +02:00
|
|
|
return
|
2015-02-07 14:27:12 +02:00
|
|
|
}
|
|
|
|
|
2015-02-10 10:43:45 +02:00
|
|
|
ctx.User = authboss.Unbind(user)
|
2015-02-07 14:27:12 +02:00
|
|
|
|
2015-02-16 23:27:29 +02:00
|
|
|
ctx.User[StoreConfirmToken] = ""
|
|
|
|
ctx.User[StoreConfirmed] = true
|
2015-02-07 14:27:12 +02:00
|
|
|
|
2015-02-16 23:27:29 +02:00
|
|
|
key, _ := ctx.User.String(authboss.StoreUsername)
|
2015-02-10 10:43:45 +02:00
|
|
|
ctx.SessionStorer.Put(authboss.SessionKey, key)
|
|
|
|
ctx.SessionStorer.Put(authboss.FlashSuccessKey, "Successfully confirmed your account.")
|
2015-02-07 14:27:12 +02:00
|
|
|
|
2015-02-16 06:35:32 +02:00
|
|
|
if err := ctx.SaveUser(key, authboss.Cfg.Storer); err != nil {
|
|
|
|
fmt.Fprintln(authboss.Cfg.LogWriter, "confirm: failed to clear the user's token:", err)
|
2015-02-10 10:43:45 +02:00
|
|
|
return
|
|
|
|
}
|
2015-02-07 14:27:12 +02:00
|
|
|
|
2015-02-10 10:43:45 +02:00
|
|
|
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
2015-02-07 14:27:12 +02:00
|
|
|
}
|