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

169 lines
4.0 KiB
Go
Raw Normal View History

package auth
import (
2015-01-11 08:49:06 +02:00
"errors"
"fmt"
"net/http"
2015-01-08 09:45:41 +02:00
"path/filepath"
2015-01-13 09:51:25 +02:00
"golang.org/x/crypto/bcrypt"
2015-01-13 07:08:52 +02:00
2015-01-10 08:51:02 +02:00
"gopkg.in/authboss.v0"
2015-01-08 09:45:41 +02:00
"html/template"
2015-01-05 00:50:34 +02:00
2015-01-10 08:51:02 +02:00
"io"
)
const (
methodGET = "GET"
methodPOST = "POST"
2015-01-13 07:08:52 +02:00
pageLogin = "login.tpl"
attrUsername = "username"
attrPassword = "password"
2015-01-13 07:08:52 +02:00
)
2015-01-11 08:49:06 +02:00
func init() {
a := &Auth{}
2015-01-08 09:45:41 +02:00
authboss.RegisterModule("auth", a)
}
2015-01-13 07:08:52 +02:00
type AuthPage struct {
Error string
Username string
ShowRemember bool
2015-01-13 07:08:52 +02:00
}
type Auth struct {
2015-01-13 07:08:52 +02:00
routes authboss.RouteTable
storageOptions authboss.StorageOptions
storer authboss.Storer
2015-01-13 07:08:52 +02:00
logoutRedirect string
loginRedirect string
logger io.Writer
templates *template.Template
2015-01-16 00:01:01 +02:00
callbacks *authboss.Callbacks
isRememberLoaded bool
}
2015-01-08 09:45:41 +02:00
func (a *Auth) Initialize(c *authboss.Config) (err error) {
2015-01-13 07:08:52 +02:00
if a.templates, err = template.ParseFiles(filepath.Join(c.ViewsPath, pageLogin)); err != nil {
var loginTplBytes []byte
if loginTplBytes, err = views_login_tpl_bytes(); err != nil {
2015-01-05 00:50:34 +02:00
return err
}
2015-01-13 07:08:52 +02:00
if a.templates, err = template.New(pageLogin).Parse(string(loginTplBytes)); err != nil {
return err
}
}
2015-01-08 09:45:41 +02:00
a.routes = authboss.RouteTable{
2015-01-13 07:08:52 +02:00
"login": a.loginHandlerFunc,
"logout": a.logoutHandlerFunc,
}
2015-01-13 08:28:42 +02:00
a.storageOptions = authboss.StorageOptions{
attrUsername: authboss.String,
attrPassword: authboss.String,
}
a.storer = c.Storer
2015-01-11 09:12:40 +02:00
a.logoutRedirect = c.AuthLogoutRoute
2015-01-13 07:08:52 +02:00
a.loginRedirect = c.AuthLoginSuccessRoute
a.logger = c.LogWriter
2015-01-16 00:01:01 +02:00
a.callbacks = c.Callbacks
2015-01-05 00:50:34 +02:00
a.isRememberLoaded = authboss.IsLoaded("remember")
return nil
}
2015-01-08 09:45:41 +02:00
func (a *Auth) Routes() authboss.RouteTable {
2015-01-05 00:50:34 +02:00
return a.routes
}
2015-01-08 09:45:41 +02:00
func (a *Auth) Storage() authboss.StorageOptions {
2015-01-10 08:51:02 +02:00
return a.storageOptions
}
func (a *Auth) loginHandlerFunc(ctx *authboss.Context, w http.ResponseWriter, r *http.Request) {
switch r.Method {
case methodGET:
if _, ok := ctx.SessionStorer.Get(authboss.SessionKey); ok {
if halfAuthed, ok := ctx.SessionStorer.Get(authboss.HalfAuthKey); !ok || halfAuthed == "false" {
http.Redirect(w, r, a.loginRedirect, http.StatusFound)
}
}
a.templates.ExecuteTemplate(w, pageLogin, AuthPage{ShowRemember: a.isRememberLoaded})
case methodPOST:
u, ok := ctx.FirstPostFormValue("username")
2015-01-16 00:01:01 +02:00
if !ok {
fmt.Fprintln(a.logger, errors.New("auth: Expected postFormValue 'username' to be in the context"))
}
if err := a.callbacks.FireBefore(authboss.EventAuth, ctx); err != nil {
2015-01-16 00:01:01 +02:00
w.WriteHeader(http.StatusForbidden)
a.templates.ExecuteTemplate(w, pageLogin, AuthPage{err.Error(), u, a.isRememberLoaded})
2015-01-16 00:01:01 +02:00
}
p, ok := ctx.FirstPostFormValue("password")
2015-01-16 00:01:01 +02:00
if !ok {
fmt.Fprintln(a.logger, errors.New("auth: Expected postFormValue 'password' to be in the context"))
}
2015-01-13 07:08:52 +02:00
if err := a.authenticate(ctx, u, p); err != nil {
fmt.Fprintln(a.logger, err)
2015-01-11 09:12:40 +02:00
w.WriteHeader(http.StatusForbidden)
a.templates.ExecuteTemplate(w, pageLogin, AuthPage{"invalid username and/or password", u, a.isRememberLoaded})
2015-01-11 09:12:40 +02:00
return
}
ctx.SessionStorer.Put(authboss.SessionKey, u)
a.callbacks.FireAfter(authboss.EventAuth, ctx)
2015-01-13 07:08:52 +02:00
http.Redirect(w, r, a.loginRedirect, http.StatusFound)
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}
func (a *Auth) authenticate(ctx *authboss.Context, username, password string) error {
var userInter interface{}
var err error
if userInter, err = a.storer.Get(username, nil); err != nil {
2015-01-13 07:08:52 +02:00
return err
}
ctx.User = authboss.Unbind(userInter)
pwdIntf, ok := ctx.User[attrPassword]
if !ok {
return errors.New("auth: User attributes did not include a password.")
}
pwd, ok := pwdIntf.(string)
if !ok {
return errors.New("auth: User password was not a string somehow.")
}
if err := bcrypt.CompareHashAndPassword([]byte(pwd), []byte(password)); err != nil {
return errors.New("invalid password")
2015-01-11 08:49:06 +02:00
}
return nil
}
func (a *Auth) logoutHandlerFunc(ctx *authboss.Context, w http.ResponseWriter, r *http.Request) {
switch r.Method {
case methodGET:
ctx.SessionStorer.Del(authboss.SessionKey)
2015-01-11 09:12:40 +02:00
http.Redirect(w, r, a.logoutRedirect, http.StatusFound)
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}