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

144 lines
3.9 KiB
Go
Raw Normal View History

package auth
import (
"fmt"
"net/http"
2015-01-13 09:51:25 +02:00
"golang.org/x/crypto/bcrypt"
2015-01-10 08:51:02 +02:00
"gopkg.in/authboss.v0"
2015-02-21 09:33:35 +02:00
"gopkg.in/authboss.v0/internal/render"
)
const (
methodGET = "GET"
methodPOST = "POST"
2015-02-21 09:33:35 +02:00
tplLogin = "login.tpl"
2015-01-13 07:08:52 +02:00
)
2015-01-11 08:49:06 +02:00
func init() {
2015-02-21 09:33:35 +02:00
a := &AuthModule{}
2015-01-08 09:45:41 +02:00
authboss.RegisterModule("auth", a)
}
2015-02-21 09:33:35 +02:00
type AuthModule struct {
templates render.Templates
policies []authboss.Validator
isRememberLoaded bool
2015-01-17 07:49:23 +02:00
isRecoverLoaded bool
}
2015-02-21 09:33:35 +02:00
func (a *AuthModule) Initialize() (err error) {
a.templates, err = render.LoadTemplates(authboss.Cfg.Layout, authboss.Cfg.ViewsPath, tplLogin)
if err != nil {
2015-01-19 00:24:20 +02:00
return err
}
2015-02-22 23:16:11 +02:00
a.policies = authboss.FilterValidators(authboss.Cfg.Policies, authboss.Cfg.PrimaryID, authboss.StorePassword)
2015-01-05 00:50:34 +02:00
a.isRememberLoaded = authboss.IsLoaded("remember")
2015-01-17 07:49:23 +02:00
a.isRecoverLoaded = authboss.IsLoaded("recover")
return nil
}
2015-02-21 09:33:35 +02:00
func (a *AuthModule) Routes() authboss.RouteTable {
return authboss.RouteTable{
"login": a.loginHandlerFunc,
"logout": a.logoutHandlerFunc,
}
}
2015-02-21 09:33:35 +02:00
func (a *AuthModule) Storage() authboss.StorageOptions {
return authboss.StorageOptions{
2015-02-22 23:16:11 +02:00
authboss.Cfg.PrimaryID: authboss.String,
authboss.StorePassword: authboss.String,
2015-02-21 09:33:35 +02:00
}
}
2015-02-21 09:33:35 +02:00
func (a *AuthModule) 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 23:16:11 +02:00
if halfAuthed, ok := ctx.SessionStorer.Get(authboss.SessionHalfAuthKey); !ok || halfAuthed == "false" {
http.Redirect(w, r, authboss.Cfg.AuthLoginSuccessRoute, http.StatusFound)
}
}
2015-02-21 09:33:35 +02:00
data := authboss.NewHTMLData("showRemember", a.isRememberLoaded, "showRecover", a.isRecoverLoaded)
return a.templates.Render(ctx, w, r, tplLogin, data)
case methodPOST:
2015-02-21 09:33:35 +02:00
interrupted, err := authboss.Cfg.Callbacks.FireBefore(authboss.EventAuth, ctx)
if err != nil {
return err
2015-02-22 22:47:02 +02: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."
}
render.Redirect(ctx, w, r, "/", "", reason)
return nil
2015-01-16 00:01:01 +02:00
}
2015-02-22 23:16:11 +02:00
key, _ := ctx.FirstPostFormValue(authboss.Cfg.PrimaryID)
2015-02-21 09:33:35 +02:00
password, _ := ctx.FirstPostFormValue("password")
2015-01-16 00:01:01 +02:00
2015-02-21 09:33:35 +02:00
errData := authboss.NewHTMLData(
"error", "invalid username and/or password",
2015-02-22 23:16:11 +02:00
authboss.Cfg.PrimaryID, key,
2015-02-21 09:33:35 +02:00
"showRemember", a.isRememberLoaded,
"showRecover", a.isRecoverLoaded,
)
if validationErrs := ctx.Validate(a.policies); len(validationErrs) > 0 {
fmt.Fprintln(authboss.Cfg.LogWriter, "auth: form validation failed:", validationErrs.Map())
return a.templates.Render(ctx, w, r, tplLogin, errData)
2015-01-16 00:01:01 +02:00
}
2015-01-13 07:08:52 +02:00
2015-02-22 23:16:11 +02:00
if err := validateCredentials(ctx, key, password); err != nil {
2015-02-21 09:33:35 +02:00
fmt.Fprintln(authboss.Cfg.LogWriter, "auth: failed to validate credentials:", err)
return a.templates.Render(ctx, w, r, tplLogin, errData)
2015-01-11 09:12:40 +02:00
}
2015-02-22 23:16:11 +02:00
ctx.SessionStorer.Put(authboss.SessionKey, key)
authboss.Cfg.Callbacks.FireAfter(authboss.EventAuth, ctx)
http.Redirect(w, r, authboss.Cfg.AuthLoginSuccessRoute, http.StatusFound)
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
2015-02-21 09:33:35 +02:00
return nil
}
2015-02-22 23:16:11 +02:00
func validateCredentials(ctx *authboss.Context, key, password string) error {
if err := ctx.LoadUser(key); err != nil {
2015-01-13 07:08:52 +02:00
return err
}
2015-02-22 23:16:11 +02:00
actualPassword, err := ctx.User.StringErr(authboss.StorePassword)
2015-02-21 09:33:35 +02:00
if err != nil {
return err
}
2015-02-21 09:33:35 +02:00
if err := bcrypt.CompareHashAndPassword([]byte(actualPassword), []byte(password)); err != nil {
return err
2015-01-11 08:49:06 +02:00
}
return nil
}
2015-02-21 09:33:35 +02:00
func (a *AuthModule) 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-21 09:33:35 +02:00
return nil
}