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

122 lines
2.7 KiB
Go
Raw Normal View History

package auth
import (
2015-01-11 08:49:06 +02:00
"errors"
"net/http"
2015-01-08 09:45:41 +02:00
"path/filepath"
2015-01-13 07:08:52 +02:00
"code.google.com/p/go.crypto/bcrypt"
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-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
}
type Auth struct {
2015-01-13 07:08:52 +02:00
routes authboss.RouteTable
storageOptions authboss.StorageOptions
users authboss.Storer
logoutRedirect string
loginRedirect string
logger io.Writer
templates *template.Template
}
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-11 08:49:06 +02:00
a.storageOptions = authboss.StorageOptions{
2015-01-13 07:08:52 +02:00
attrUsername: authboss.String,
attrPassword: authboss.String,
2015-01-11 08:49:06 +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-11 08:49:06 +02:00
a.users = 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-05 00:50:34 +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-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-13 07:08:52 +02:00
func (a *Auth) loginHandlerFunc(c *authboss.Context, w http.ResponseWriter, r *http.Request) {
switch r.Method {
case methodGET:
2015-01-13 07:08:52 +02:00
a.templates.ExecuteTemplate(w, pageLogin, nil)
case methodPOST:
2015-01-13 07:08:52 +02:00
u := r.PostFormValue("username")
p := r.PostFormValue("password")
if err := a.authenticate(u, p); err != nil {
2015-01-11 09:12:40 +02:00
w.WriteHeader(http.StatusForbidden)
2015-01-13 07:08:52 +02:00
a.templates.ExecuteTemplate(w, pageLogin, AuthPage{"invalid username and/or password", u})
2015-01-11 09:12:40 +02:00
return
}
2015-01-13 07:08:52 +02:00
http.Redirect(w, r, a.loginRedirect, http.StatusFound)
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}
2015-01-11 08:49:06 +02:00
func (a *Auth) authenticate(username, password string) error {
2015-01-13 07:08:52 +02:00
if userInter, err := a.users.Get(username, nil); err != nil {
return err
} else {
userAttrs := authboss.Unbind(userInter)
if err := bcrypt.CompareHashAndPassword([]byte(userAttrs[attrPassword].Value.(string)), []byte(password)); err != nil {
return errors.New("invalid password")
}
2015-01-11 08:49:06 +02:00
}
return nil
}
2015-01-13 07:08:52 +02:00
func (a *Auth) logoutHandlerFunc(c *authboss.Context, w http.ResponseWriter, r *http.Request) {
switch r.Method {
case methodGET:
2015-01-11 09:12:40 +02:00
http.Redirect(w, r, a.logoutRedirect, http.StatusFound)
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}