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

97 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
2015-01-18 14:24:20 -08:00
}
2015-01-12 22:28:42 -08:00
func (m *RecoverModule) Initialize() (err error) {
if authboss.Cfg.Storer == nil {
return errors.New("recover: Need a RecoverStorer.")
}
if _, ok := authboss.Cfg.Storer.(authboss.RecoverStorer); !ok {
return errors.New("recover: RecoverStorer required for recover functionality.")
}
if authboss.Cfg.Layout == nil {
2015-02-05 10:30:57 -08:00
return errors.New("recover: Layout required for Recover functionallity.")
}
if m.templates, err = views.Get(authboss.Cfg.Layout, authboss.Cfg.ViewsPath, tplRecover, tplRecoverComplete); err != nil {
return err
}
if authboss.Cfg.LayoutEmail == nil {
2015-02-05 10:30:57 -08:00
return errors.New("recover: LayoutEmail required for Recover functionallity.")
}
if m.emailTemplates, err = views.Get(authboss.Cfg.LayoutEmail, authboss.Cfg.ViewsPath, tplInitHTMLEmail, tplInitTextEmail); err != nil {
2015-01-18 14:24:20 -08:00
return err
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(authboss.Cfg.LogWriter, errFormat, "unable to execute template", err)
2015-02-08 23:08:33 -08:00
w.WriteHeader(http.StatusInternalServerError)
return
}
if _, err := io.Copy(w, buffer); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
}