1
0
mirror of https://github.com/volatiletech/authboss.git synced 2025-01-24 05:17:10 +02:00
authboss/auth/auth.go

155 lines
3.9 KiB
Go
Raw Normal View History

package auth
import (
2015-02-23 15:51:42 -08:00
"errors"
"fmt"
"net/http"
2015-01-12 23:51:25 -08:00
"golang.org/x/crypto/bcrypt"
2015-01-09 22:51:02 -08:00
"gopkg.in/authboss.v0"
2015-02-20 23:33:35 -08:00
"gopkg.in/authboss.v0/internal/render"
)
const (
methodGET = "GET"
methodPOST = "POST"
2015-02-20 23:33:35 -08:00
tplLogin = "login.tpl"
2015-01-12 21:08:52 -08:00
)
2015-01-10 22:49:06 -08:00
func init() {
2015-02-23 15:51:42 -08:00
a := &Auth{}
2015-01-07 23:45:41 -08:00
authboss.RegisterModule("auth", a)
}
2015-02-23 15:51:42 -08:00
type Auth struct {
2015-02-24 10:12:23 -08:00
templates render.Templates
}
2015-02-23 15:51:42 -08:00
func (a *Auth) Initialize() (err error) {
if authboss.Cfg.Storer == nil {
return errors.New("auth: Need a Storer.")
}
2015-02-24 15:01:56 -08:00
if len(authboss.Cfg.XSRFName) == 0 {
return errors.New("auth: XSRFName must be set")
}
if authboss.Cfg.XSRFMaker == nil {
return errors.New("auth: XSRFMaker must be defined")
}
2015-02-20 23:33:35 -08:00
a.templates, err = render.LoadTemplates(authboss.Cfg.Layout, authboss.Cfg.ViewsPath, tplLogin)
if err != nil {
2015-01-18 14:24:20 -08:00
return err
}
return nil
}
2015-02-23 15:51:42 -08:00
func (a *Auth) Routes() authboss.RouteTable {
2015-02-20 23:33:35 -08:00
return authboss.RouteTable{
"/login": a.loginHandlerFunc,
"/logout": a.logoutHandlerFunc,
2015-02-20 23:33:35 -08:00
}
}
2015-02-23 15:51:42 -08:00
func (a *Auth) Storage() authboss.StorageOptions {
2015-02-20 23:33:35 -08:00
return authboss.StorageOptions{
2015-02-22 13:16:11 -08:00
authboss.Cfg.PrimaryID: authboss.String,
authboss.StorePassword: authboss.String,
2015-02-20 23:33:35 -08:00
}
}
2015-02-23 15:51:42 -08:00
func (a *Auth) loginHandlerFunc(ctx *authboss.Context, w http.ResponseWriter, r *http.Request) error {
switch r.Method {
case methodGET:
if _, ok := ctx.SessionStorer.Get(authboss.SessionKey); ok {
2015-02-22 13:16:11 -08:00
if halfAuthed, ok := ctx.SessionStorer.Get(authboss.SessionHalfAuthKey); !ok || halfAuthed == "false" {
http.Redirect(w, r, authboss.Cfg.AuthLoginSuccessRoute, http.StatusFound)
2015-02-24 15:01:56 -08:00
return nil
}
}
2015-02-23 15:51:42 -08:00
data := authboss.NewHTMLData(
2015-02-24 10:12:23 -08:00
"showRemember", authboss.IsLoaded("remember"),
"showRecover", authboss.IsLoaded("recover"),
2015-02-23 15:51:42 -08:00
"primaryID", authboss.Cfg.PrimaryID,
"primaryIDValue", "",
)
2015-02-20 23:33:35 -08:00
return a.templates.Render(ctx, w, r, tplLogin, data)
case methodPOST:
2015-02-20 23:33:35 -08:00
interrupted, err := authboss.Cfg.Callbacks.FireBefore(authboss.EventAuth, ctx)
if err != nil {
return err
2015-02-22 12:47:02 -08:00
} else if interrupted != authboss.InterruptNone {
var reason string
switch interrupted {
case authboss.InterruptAccountLocked:
reason = "Your account has been locked."
case authboss.InterruptAccountNotConfirmed:
reason = "Your account has not been confirmed."
}
2015-02-24 15:01:56 -08:00
render.Redirect(ctx, w, r, "/login", "", reason)
2015-02-22 12:47:02 -08:00
return nil
2015-01-15 14:01:01 -08:00
}
2015-02-22 13:16:11 -08:00
key, _ := ctx.FirstPostFormValue(authboss.Cfg.PrimaryID)
2015-02-20 23:33:35 -08:00
password, _ := ctx.FirstPostFormValue("password")
2015-01-15 14:01:01 -08:00
2015-02-20 23:33:35 -08:00
errData := authboss.NewHTMLData(
2015-02-23 15:51:42 -08:00
"error", fmt.Sprintf("invalid %s and/or password", authboss.Cfg.PrimaryID),
"primaryID", authboss.Cfg.PrimaryID,
"primaryIDValue", key,
2015-02-24 10:12:23 -08:00
"showRemember", authboss.IsLoaded("remember"),
"showRecover", authboss.IsLoaded("recover"),
2015-02-20 23:33:35 -08:00
)
2015-02-24 10:12:23 -08:00
policies := authboss.FilterValidators(authboss.Cfg.Policies, authboss.Cfg.PrimaryID, authboss.StorePassword)
if validationErrs := ctx.Validate(policies); len(validationErrs) > 0 {
2015-02-20 23:33:35 -08:00
return a.templates.Render(ctx, w, r, tplLogin, errData)
2015-01-15 14:01:01 -08:00
}
2015-01-12 21:08:52 -08:00
2015-02-22 13:16:11 -08:00
if err := validateCredentials(ctx, key, password); err != nil {
2015-02-20 23:33:35 -08:00
return a.templates.Render(ctx, w, r, tplLogin, errData)
2015-01-10 23:12:40 -08:00
}
authboss.Cfg.Callbacks.FireAfter(authboss.EventAuth, ctx)
http.Redirect(w, r, authboss.Cfg.AuthLoginSuccessRoute, http.StatusFound)
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
2015-02-20 23:33:35 -08:00
return nil
}
2015-02-22 13:16:11 -08:00
func validateCredentials(ctx *authboss.Context, key, password string) error {
if err := ctx.LoadUser(key); err != nil {
2015-01-12 21:08:52 -08:00
return err
}
2015-02-22 13:16:11 -08:00
actualPassword, err := ctx.User.StringErr(authboss.StorePassword)
2015-02-20 23:33:35 -08:00
if err != nil {
return err
}
2015-02-20 23:33:35 -08:00
if err := bcrypt.CompareHashAndPassword([]byte(actualPassword), []byte(password)); err != nil {
return err
2015-01-10 22:49:06 -08:00
}
2015-02-24 10:12:23 -08:00
ctx.SessionStorer.Put(authboss.SessionKey, key)
2015-01-10 22:49:06 -08:00
return nil
}
2015-02-23 15:51:42 -08:00
func (a *Auth) logoutHandlerFunc(ctx *authboss.Context, w http.ResponseWriter, r *http.Request) error {
switch r.Method {
case methodGET:
ctx.SessionStorer.Del(authboss.SessionKey)
http.Redirect(w, r, authboss.Cfg.AuthLogoutRoute, http.StatusFound)
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
2015-02-20 23:33:35 -08:00
return nil
}