2015-01-04 20:33:53 +02:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
2015-01-11 08:49:06 +02:00
|
|
|
"errors"
|
2015-01-15 23:24:12 +02:00
|
|
|
"fmt"
|
2015-01-04 20:33:53 +02:00
|
|
|
"net/http"
|
2015-01-08 09:45:41 +02:00
|
|
|
"path/filepath"
|
2015-01-04 20:33:53 +02:00
|
|
|
|
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"
|
2015-01-04 20:33:53 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
methodGET = "GET"
|
|
|
|
methodPOST = "POST"
|
|
|
|
|
2015-01-13 07:08:52 +02:00
|
|
|
pageLogin = "login.tpl"
|
|
|
|
|
2015-01-15 23:24:12 +02:00
|
|
|
attrUsername = "username"
|
|
|
|
attrPassword = "password"
|
2015-01-13 07:08:52 +02:00
|
|
|
)
|
2015-01-11 08:49:06 +02:00
|
|
|
|
2015-01-04 20:33:53 +02:00
|
|
|
func init() {
|
|
|
|
a := &Auth{}
|
2015-01-08 09:45:41 +02:00
|
|
|
authboss.RegisterModule("auth", a)
|
2015-01-04 20:33:53 +02:00
|
|
|
}
|
|
|
|
|
2015-01-13 07:08:52 +02:00
|
|
|
type AuthPage struct {
|
|
|
|
Error string
|
|
|
|
Username string
|
2015-01-16 01:10:47 +02:00
|
|
|
|
|
|
|
ShowRemember bool
|
2015-01-17 07:49:23 +02:00
|
|
|
ShowRecover bool
|
2015-01-13 07:08:52 +02:00
|
|
|
}
|
|
|
|
|
2015-01-04 20:33:53 +02:00
|
|
|
type Auth struct {
|
2015-01-13 07:08:52 +02:00
|
|
|
routes authboss.RouteTable
|
|
|
|
storageOptions authboss.StorageOptions
|
2015-01-15 23:24:12 +02:00
|
|
|
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
|
2015-01-16 01:10:47 +02:00
|
|
|
|
|
|
|
isRememberLoaded bool
|
2015-01-17 07:49:23 +02:00
|
|
|
isRecoverLoaded bool
|
2015-01-04 20:33:53 +02:00
|
|
|
}
|
|
|
|
|
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-05 06:41:20 +02:00
|
|
|
|
2015-01-13 07:08:52 +02:00
|
|
|
if a.templates, err = template.New(pageLogin).Parse(string(loginTplBytes)); err != nil {
|
2015-01-05 06:41:20 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-01-04 20:33:53 +02:00
|
|
|
|
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-04 20:33:53 +02:00
|
|
|
}
|
2015-01-13 08:28:42 +02:00
|
|
|
a.storageOptions = authboss.StorageOptions{
|
|
|
|
attrUsername: authboss.String,
|
|
|
|
attrPassword: authboss.String,
|
|
|
|
}
|
2015-01-15 23:24:12 +02:00
|
|
|
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
|
2015-01-15 23:24:12 +02:00
|
|
|
a.logger = c.LogWriter
|
2015-01-16 00:01:01 +02:00
|
|
|
a.callbacks = c.Callbacks
|
2015-01-05 00:50:34 +02:00
|
|
|
|
2015-01-16 01:10:47 +02:00
|
|
|
a.isRememberLoaded = authboss.IsLoaded("remember")
|
2015-01-17 07:49:23 +02:00
|
|
|
a.isRecoverLoaded = authboss.IsLoaded("recover")
|
2015-01-16 01:10:47 +02:00
|
|
|
|
2015-01-04 20:33:53 +02:00
|
|
|
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-04 20:33:53 +02:00
|
|
|
}
|
|
|
|
|
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
|
2015-01-04 20:33:53 +02:00
|
|
|
}
|
|
|
|
|
2015-01-16 02:04:33 +02:00
|
|
|
func (a *Auth) loginHandlerFunc(ctx *authboss.Context, w http.ResponseWriter, r *http.Request) {
|
2015-01-04 20:33:53 +02:00
|
|
|
switch r.Method {
|
|
|
|
case methodGET:
|
2015-01-16 02:04:33 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-17 07:49:23 +02:00
|
|
|
a.templates.ExecuteTemplate(w, pageLogin, AuthPage{ShowRemember: a.isRememberLoaded, ShowRecover: a.isRecoverLoaded})
|
2015-01-04 20:33:53 +02:00
|
|
|
case methodPOST:
|
2015-01-16 02:04:33 +02:00
|
|
|
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"))
|
|
|
|
}
|
|
|
|
|
2015-01-16 02:04:33 +02:00
|
|
|
if err := a.callbacks.FireBefore(authboss.EventAuth, ctx); err != nil {
|
2015-01-16 00:01:01 +02:00
|
|
|
w.WriteHeader(http.StatusForbidden)
|
2015-01-17 07:49:23 +02:00
|
|
|
a.templates.ExecuteTemplate(w, pageLogin, AuthPage{err.Error(), u, a.isRememberLoaded, a.isRecoverLoaded})
|
2015-01-16 00:01:01 +02:00
|
|
|
}
|
|
|
|
|
2015-01-16 02:04:33 +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
|
|
|
|
2015-01-16 02:04:33 +02:00
|
|
|
if err := a.authenticate(ctx, u, p); err != nil {
|
2015-01-15 23:24:12 +02:00
|
|
|
fmt.Fprintln(a.logger, err)
|
2015-01-11 09:12:40 +02:00
|
|
|
w.WriteHeader(http.StatusForbidden)
|
2015-01-17 07:49:23 +02:00
|
|
|
a.templates.ExecuteTemplate(w, pageLogin, AuthPage{"invalid username and/or password", u, a.isRememberLoaded, a.isRecoverLoaded})
|
2015-01-11 09:12:40 +02:00
|
|
|
return
|
|
|
|
}
|
2015-01-16 01:10:47 +02:00
|
|
|
|
2015-01-16 02:04:33 +02:00
|
|
|
ctx.SessionStorer.Put(authboss.SessionKey, u)
|
|
|
|
a.callbacks.FireAfter(authboss.EventAuth, ctx)
|
2015-01-16 01:10:47 +02:00
|
|
|
|
2015-01-13 07:08:52 +02:00
|
|
|
http.Redirect(w, r, a.loginRedirect, http.StatusFound)
|
2015-01-04 20:33:53 +02:00
|
|
|
default:
|
|
|
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-16 02:04:33 +02:00
|
|
|
func (a *Auth) authenticate(ctx *authboss.Context, username, password string) error {
|
2015-01-15 23:24:12 +02:00
|
|
|
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
|
2015-01-15 23:24:12 +02:00
|
|
|
}
|
|
|
|
|
2015-01-16 02:04:33 +02:00
|
|
|
ctx.User = authboss.Unbind(userInter)
|
2015-01-15 23:24:12 +02:00
|
|
|
|
2015-01-16 02:04:33 +02:00
|
|
|
pwdIntf, ok := ctx.User[attrPassword]
|
2015-01-15 23:24:12 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-01-16 02:04:33 +02:00
|
|
|
func (a *Auth) logoutHandlerFunc(ctx *authboss.Context, w http.ResponseWriter, r *http.Request) {
|
2015-01-04 20:33:53 +02:00
|
|
|
switch r.Method {
|
|
|
|
case methodGET:
|
2015-01-16 02:04:33 +02:00
|
|
|
ctx.SessionStorer.Del(authboss.SessionKey)
|
2015-01-11 09:12:40 +02:00
|
|
|
http.Redirect(w, r, a.logoutRedirect, http.StatusFound)
|
2015-01-04 20:33:53 +02:00
|
|
|
default:
|
|
|
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
|
|
|
}
|
|
|
|
}
|