2015-01-13 08:28:42 +02:00
|
|
|
package recover
|
|
|
|
|
|
|
|
import (
|
2015-01-17 07:49:23 +02:00
|
|
|
"crypto/rand"
|
2015-01-13 08:28:42 +02:00
|
|
|
"errors"
|
2015-01-17 07:49:23 +02:00
|
|
|
"fmt"
|
2015-01-13 08:28:42 +02:00
|
|
|
"html/template"
|
|
|
|
"net/http"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2015-01-17 07:49:23 +02:00
|
|
|
"io"
|
|
|
|
|
2015-01-13 08:28:42 +02:00
|
|
|
"gopkg.in/authboss.v0"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
methodGET = "GET"
|
|
|
|
methodPOST = "POST"
|
|
|
|
|
|
|
|
pageRecover = "recover.tpl"
|
|
|
|
|
2015-01-17 07:49:23 +02:00
|
|
|
attrUsername = "username"
|
|
|
|
attrResetToken = "resettoken"
|
2015-01-13 08:28:42 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
m := &RecoverModule{}
|
|
|
|
authboss.RegisterModule("recover", m)
|
|
|
|
}
|
|
|
|
|
|
|
|
type RecoverPage struct {
|
2015-01-17 07:49:23 +02:00
|
|
|
Username, ConfirmUsername, Error string
|
2015-01-13 08:28:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type RecoverModule struct {
|
|
|
|
templates *template.Template
|
|
|
|
routes authboss.RouteTable
|
|
|
|
storageOptions authboss.StorageOptions
|
2015-01-17 07:49:23 +02:00
|
|
|
storer authboss.Storer
|
|
|
|
logger io.Writer
|
2015-01-13 08:28:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *RecoverModule) Initialize(c *authboss.Config) (err error) {
|
|
|
|
if m.templates, err = template.ParseFiles(filepath.Join(c.ViewsPath, pageRecover)); err != nil {
|
|
|
|
var recoverTplBytes []byte
|
|
|
|
if recoverTplBytes, err = views_recover_tpl_bytes(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.templates, err = template.New(pageRecover).Parse(string(recoverTplBytes)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m.routes = authboss.RouteTable{
|
|
|
|
"recover": m.recoverHandlerFunc,
|
|
|
|
}
|
|
|
|
m.storageOptions = authboss.StorageOptions{
|
2015-01-17 07:49:23 +02:00
|
|
|
attrUsername: authboss.String,
|
|
|
|
attrResetToken: authboss.String,
|
2015-01-13 08:28:42 +02:00
|
|
|
}
|
2015-01-17 07:49:23 +02:00
|
|
|
m.storer = c.Storer
|
|
|
|
m.logger = c.LogWriter
|
2015-01-13 08:28:42 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *RecoverModule) Routes() authboss.RouteTable {
|
|
|
|
return m.routes
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *RecoverModule) Storage() authboss.StorageOptions {
|
|
|
|
return m.storageOptions
|
|
|
|
}
|
|
|
|
|
2015-01-17 07:49:23 +02:00
|
|
|
func (m *RecoverModule) recoverHandlerFunc(ctx *authboss.Context, w http.ResponseWriter, r *http.Request) {
|
2015-01-13 08:28:42 +02:00
|
|
|
switch r.Method {
|
|
|
|
case methodGET:
|
|
|
|
m.templates.ExecuteTemplate(w, pageRecover, nil)
|
|
|
|
case methodPOST:
|
2015-01-17 07:49:23 +02:00
|
|
|
username, ok := ctx.FirstPostFormValue("username")
|
|
|
|
if !ok {
|
|
|
|
fmt.Fprintln(m.logger, errors.New("recover: Expected postFormValue 'username' to be in the context"))
|
|
|
|
}
|
2015-01-13 08:28:42 +02:00
|
|
|
|
2015-01-17 07:49:23 +02:00
|
|
|
confirmUsername, ok := ctx.FirstPostFormValue("confirmUsername")
|
|
|
|
if !ok {
|
|
|
|
fmt.Fprintln(m.logger, errors.New("recover: Expected postFormValue 'confirmUsername' to be in the context"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := m.initiateRecover(ctx, username, confirmUsername); err != nil {
|
2015-01-13 08:28:42 +02:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
2015-01-17 07:49:23 +02:00
|
|
|
m.templates.ExecuteTemplate(w, pageRecover, RecoverPage{username, confirmUsername, err.Error()})
|
2015-01-13 08:28:42 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-17 07:49:23 +02:00
|
|
|
func (m *RecoverModule) initiateRecover(ctx *authboss.Context, username, confirmUsername string) error {
|
2015-01-13 08:28:42 +02:00
|
|
|
if !strings.EqualFold(username, confirmUsername) {
|
|
|
|
return errors.New("Confirm username does not match")
|
|
|
|
}
|
|
|
|
|
2015-01-17 07:49:23 +02:00
|
|
|
token := make([]byte, 32)
|
|
|
|
if _, err := rand.Read(token); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ctx.LoadUser(username, m.storer); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
authboss.SendEmail("", "", []byte)
|
|
|
|
|
2015-01-13 08:28:42 +02:00
|
|
|
return nil
|
|
|
|
}
|