1
0
mirror of https://github.com/volatiletech/authboss.git synced 2025-02-03 13:21:22 +02:00
authboss/recover/recover.go

100 lines
2.5 KiB
Go
Raw Normal View History

2015-01-12 22:28:42 -08:00
package recover
import (
"errors"
2015-01-16 21:49:23 -08:00
"fmt"
2015-02-08 23:08:33 -08:00
"io"
2015-01-12 22:28:42 -08:00
"net/http"
"gopkg.in/authboss.v0"
2015-01-18 14:24:20 -08:00
"gopkg.in/authboss.v0/internal/views"
2015-01-12 22:28:42 -08:00
)
const (
methodGET = "GET"
methodPOST = "POST"
tplLogin = "login.tpl"
2015-01-18 14:24:20 -08:00
tplRecover = "recover.tpl"
tplRecoverComplete = "recover-complete.tpl"
2015-01-30 15:38:28 -08:00
tplInitHTMLEmail = "recover-html.email"
tplInitTextEmail = "recover-text.email"
2015-01-12 22:28:42 -08:00
2015-01-30 15:38:28 -08:00
attrUsername = "username"
attrRecoverToken = "recover_token"
attrRecoverTokenExpiry = "recover_token_expiry"
attrEmail = "email"
attrPassword = "password"
errFormat = "recover [%s]: %s\n"
2015-01-12 22:28:42 -08:00
)
func init() {
m := &RecoverModule{}
authboss.RegisterModule("recover", m)
}
type RecoverModule struct {
templates views.Templates
emailTemplates views.Templates
config *authboss.Config
2015-01-18 14:24:20 -08:00
}
2015-01-12 22:28:42 -08:00
2015-01-18 14:24:20 -08:00
func (m *RecoverModule) Initialize(config *authboss.Config) (err error) {
if config.Storer == nil {
return errors.New("recover: Need a RecoverStorer.")
}
2015-01-30 15:38:28 -08:00
if _, ok := config.Storer.(authboss.RecoverStorer); !ok {
return errors.New("recover: RecoverStorer required for recover functionality.")
}
2015-02-05 10:30:57 -08:00
if config.Layout == nil {
return errors.New("recover: Layout required for Recover functionallity.")
}
if m.templates, err = views.Get(config.Layout, config.ViewsPath, tplRecover, tplRecoverComplete); err != nil {
return err
}
2015-02-05 10:30:57 -08:00
if config.LayoutEmail == nil {
return errors.New("recover: LayoutEmail required for Recover functionallity.")
}
if m.emailTemplates, err = views.Get(config.LayoutEmail, config.ViewsPath, tplInitHTMLEmail, tplInitTextEmail); err != nil {
2015-01-18 14:24:20 -08:00
return err
2015-01-12 22:28:42 -08:00
}
2015-01-30 15:38:28 -08:00
m.config = config
2015-01-12 22:28:42 -08:00
return nil
}
2015-01-30 15:38:28 -08:00
func (m *RecoverModule) Routes() authboss.RouteTable {
return authboss.RouteTable{
"recover": m.recoverHandlerFunc,
2015-02-10 23:03:02 -08:00
"recover/complete": m.recoverCompleteHandlerFunc,
2015-01-30 15:38:28 -08:00
}
}
func (m *RecoverModule) Storage() authboss.StorageOptions {
return authboss.StorageOptions{
attrUsername: authboss.String,
attrRecoverToken: authboss.String,
attrEmail: authboss.String,
attrRecoverTokenExpiry: authboss.String,
attrPassword: authboss.String,
}
}
2015-02-10 23:03:02 -08:00
func (m *RecoverModule) execTpl(tpl string, w http.ResponseWriter, data interface{}) {
buffer, err := m.templates.ExecuteTemplate(tpl, data)
2015-02-08 23:08:33 -08:00
if err != nil {
fmt.Fprintf(m.config.LogWriter, errFormat, "unable to execute template", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if _, err := io.Copy(w, buffer); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
}