2015-03-15 17:06:08 +02:00
|
|
|
// Package confirm implements confirmation of user registration via e-mail
|
2015-02-07 14:27:12 +02:00
|
|
|
package confirm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/md5"
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/base64"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2015-02-10 10:43:45 +02:00
|
|
|
"net/url"
|
2015-02-27 08:01:53 +02:00
|
|
|
"path"
|
2015-02-07 14:27:12 +02:00
|
|
|
|
|
|
|
"gopkg.in/authboss.v0"
|
2015-02-22 10:09:52 +02:00
|
|
|
"gopkg.in/authboss.v0/internal/render"
|
2015-02-07 14:27:12 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
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 (
|
2015-02-22 10:09:52 +02:00
|
|
|
errUserMissing = errors.New("confirm: After registration user must be loaded")
|
2015-02-07 14:27:12 +02:00
|
|
|
)
|
|
|
|
|
2015-02-24 21:04:27 +02:00
|
|
|
// ConfirmStorer must be implemented in order to satisfy the confirm module's
|
|
|
|
// storage requirements.
|
|
|
|
type ConfirmStorer interface {
|
|
|
|
authboss.Storer
|
|
|
|
// ConfirmUser looks up a user by a confirm token. See confirm module for
|
|
|
|
// attribute names. If the token is not found in the data store,
|
|
|
|
// simply return nil, ErrUserNotFound.
|
|
|
|
ConfirmUser(confirmToken string) (interface{}, error)
|
|
|
|
}
|
|
|
|
|
2015-02-07 14:27:12 +02:00
|
|
|
func init() {
|
2015-02-27 09:09:37 +02:00
|
|
|
authboss.RegisterModule("confirm", &Confirm{})
|
2015-02-07 14:27:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type Confirm struct {
|
2015-03-03 21:23:49 +02:00
|
|
|
emailHTMLTemplates render.Templates
|
|
|
|
emailTextTemplates render.Templates
|
2015-02-07 14:27:12 +02:00
|
|
|
}
|
|
|
|
|
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-24 21:04:27 +02:00
|
|
|
storer, ok := authboss.Cfg.Storer.(ConfirmStorer)
|
2015-02-16 06:35:32 +02:00
|
|
|
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-03-03 21:23:49 +02:00
|
|
|
c.emailHTMLTemplates, err = render.LoadTemplates(authboss.Cfg.LayoutHTMLEmail, authboss.Cfg.ViewsPath, tplConfirmHTML)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.emailTextTemplates, err = render.LoadTemplates(authboss.Cfg.LayoutTextEmail, authboss.Cfg.ViewsPath, 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)
|
2015-02-26 22:15:33 +02:00
|
|
|
authboss.Cfg.Callbacks.Before(authboss.EventAuth, c.BeforeGet)
|
2015-02-16 06:35:32 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-22 10:09:52 +02:00
|
|
|
func (c *Confirm) BeforeGet(ctx *authboss.Context) (authboss.Interrupt, error) {
|
|
|
|
if confirmed, err := ctx.User.BoolErr(StoreConfirmed); err != nil {
|
|
|
|
return authboss.InterruptNone, err
|
|
|
|
} else if !confirmed {
|
|
|
|
return authboss.InterruptAccountNotConfirmed, nil
|
2015-02-07 14:27:12 +02:00
|
|
|
}
|
2015-02-10 10:43:45 +02:00
|
|
|
|
2015-02-22 10:09:52 +02:00
|
|
|
return authboss.InterruptNone, nil
|
2015-02-07 14:27:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// AfterRegister ensures the account is not activated.
|
2015-02-22 10:09:52 +02:00
|
|
|
func (c *Confirm) AfterRegister(ctx *authboss.Context) error {
|
2015-02-07 14:27:12 +02:00
|
|
|
if ctx.User == nil {
|
2015-02-22 10:09:52 +02:00
|
|
|
return errUserMissing
|
2015-02-07 14:27:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
token := make([]byte, 32)
|
|
|
|
if _, err := rand.Read(token); err != nil {
|
2015-02-22 10:09:52 +02:00
|
|
|
return 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-18 18:57:50 +02:00
|
|
|
if err := ctx.SaveUser(); err != nil {
|
2015-02-22 10:09:52 +02:00
|
|
|
return err
|
2015-02-07 14:27:12 +02:00
|
|
|
}
|
|
|
|
|
2015-02-22 10:09:52 +02:00
|
|
|
email, err := ctx.User.StringErr(authboss.StoreEmail)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-02-07 14:27:12 +02:00
|
|
|
}
|
2015-02-22 10:09:52 +02:00
|
|
|
|
2015-02-27 08:01:53 +02:00
|
|
|
goConfirmEmail(c, email, base64.URLEncoding.EncodeToString(token))
|
2015-02-22 10:09:52 +02:00
|
|
|
|
|
|
|
return nil
|
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-27 08:01:53 +02:00
|
|
|
p := path.Join(authboss.Cfg.MountPath, "confirm")
|
2015-03-13 04:20:15 +02:00
|
|
|
url := fmt.Sprintf("%s%s?%s=%s", authboss.Cfg.RootURL, p, url.QueryEscape(FormValueConfirm), url.QueryEscape(token))
|
2015-02-07 14:27:12 +02:00
|
|
|
|
2015-02-22 10:09:52 +02:00
|
|
|
email := authboss.Email{
|
|
|
|
To: []string{to},
|
|
|
|
From: authboss.Cfg.EmailFrom,
|
|
|
|
Subject: authboss.Cfg.EmailSubjectPrefix + "Confirm New Account",
|
2015-02-07 14:27:12 +02:00
|
|
|
}
|
|
|
|
|
2015-03-03 21:23:49 +02:00
|
|
|
err := render.RenderEmail(email, c.emailHTMLTemplates, tplConfirmHTML, c.emailTextTemplates, tplConfirmText, url)
|
2015-02-22 10:09:52 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(authboss.Cfg.LogWriter, "confirm: Failed to send e-mail: %v", err)
|
2015-02-07 14:27:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-22 10:09:52 +02:00
|
|
|
func (c *Confirm) confirmHandler(ctx *authboss.Context, w http.ResponseWriter, r *http.Request) error {
|
|
|
|
token, err := ctx.FirstFormValueErr(FormValueConfirm)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-02-10 10:43:45 +02:00
|
|
|
}
|
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-22 10:09:52 +02:00
|
|
|
return authboss.ErrAndRedirect{
|
2015-02-25 00:45:37 +02:00
|
|
|
Location: "/", Err: fmt.Errorf("confirm: token failed to decode %q => %v\n", token, err),
|
2015-02-22 10:09:52 +02:00
|
|
|
}
|
2015-02-10 10:43:45 +02:00
|
|
|
}
|
|
|
|
|
2015-02-16 23:27:29 +02:00
|
|
|
sum := md5.Sum(toHash)
|
|
|
|
|
|
|
|
dbTok := base64.StdEncoding.EncodeToString(sum[:])
|
2015-02-24 21:04:27 +02:00
|
|
|
user, err := authboss.Cfg.Storer.(ConfirmStorer).ConfirmUser(dbTok)
|
2015-02-10 10:43:45 +02:00
|
|
|
if err == authboss.ErrUserNotFound {
|
2015-02-25 00:45:37 +02:00
|
|
|
return authboss.ErrAndRedirect{Location: "/", Err: errors.New("confirm: token not found")}
|
2015-02-10 10:43:45 +02:00
|
|
|
} else if err != nil {
|
2015-02-22 10:09:52 +02:00
|
|
|
return err
|
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-27 08:01:53 +02:00
|
|
|
key, err := ctx.User.StringErr(authboss.Cfg.PrimaryID)
|
2015-02-22 10:09:52 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-02-07 14:27:12 +02:00
|
|
|
|
2015-02-18 18:57:50 +02:00
|
|
|
if err := ctx.SaveUser(); err != nil {
|
2015-02-22 10:09:52 +02:00
|
|
|
return err
|
2015-02-10 10:43:45 +02:00
|
|
|
}
|
2015-02-07 14:27:12 +02:00
|
|
|
|
2015-02-22 10:09:52 +02:00
|
|
|
ctx.SessionStorer.Put(authboss.SessionKey, key)
|
2015-03-15 20:56:15 +02:00
|
|
|
render.Redirect(ctx, w, r, authboss.Cfg.RegisterOKPath, "You have successfully confirmed your account.", "")
|
2015-02-22 10:09:52 +02:00
|
|
|
|
|
|
|
return nil
|
2015-02-07 14:27:12 +02:00
|
|
|
}
|