1
0
mirror of https://github.com/volatiletech/authboss.git synced 2025-07-01 00:44:57 +02:00
Files
authboss/auth/auth.go

122 lines
2.7 KiB
Go
Raw Normal View History

package auth
import (
2015-01-10 22:49:06 -08:00
"errors"
"net/http"
2015-01-07 23:45:41 -08:00
"path/filepath"
2015-01-12 21:08:52 -08:00
"code.google.com/p/go.crypto/bcrypt"
2015-01-09 22:51:02 -08:00
"gopkg.in/authboss.v0"
2015-01-07 23:45:41 -08:00
"html/template"
2015-01-04 14:50:34 -08:00
2015-01-09 22:51:02 -08:00
"io"
)
const (
methodGET = "GET"
methodPOST = "POST"
2015-01-12 21:08:52 -08:00
pageLogin = "login.tpl"
attrUsername = "Username"
attrPassword = "Password"
)
2015-01-10 22:49:06 -08:00
func init() {
a := &Auth{}
2015-01-07 23:45:41 -08:00
authboss.RegisterModule("auth", a)
}
2015-01-12 21:08:52 -08:00
type AuthPage struct {
Error string
Username string
}
type Auth struct {
2015-01-12 21:08:52 -08:00
routes authboss.RouteTable
storageOptions authboss.StorageOptions
users authboss.Storer
logoutRedirect string
loginRedirect string
logger io.Writer
templates *template.Template
}
2015-01-07 23:45:41 -08:00
func (a *Auth) Initialize(c *authboss.Config) (err error) {
2015-01-12 21:08:52 -08: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-04 14:50:34 -08:00
return err
}
2015-01-12 21:08:52 -08:00
if a.templates, err = template.New(pageLogin).Parse(string(loginTplBytes)); err != nil {
return err
}
}
2015-01-10 22:49:06 -08:00
a.storageOptions = authboss.StorageOptions{
2015-01-12 21:08:52 -08:00
attrUsername: authboss.String,
attrPassword: authboss.String,
2015-01-10 22:49:06 -08:00
}
2015-01-07 23:45:41 -08:00
a.routes = authboss.RouteTable{
2015-01-12 21:08:52 -08:00
"login": a.loginHandlerFunc,
"logout": a.logoutHandlerFunc,
}
2015-01-10 22:49:06 -08:00
a.users = c.Storer
2015-01-10 23:12:40 -08:00
a.logoutRedirect = c.AuthLogoutRoute
2015-01-12 21:08:52 -08:00
a.loginRedirect = c.AuthLoginSuccessRoute
2015-01-04 14:50:34 -08:00
return nil
}
2015-01-07 23:45:41 -08:00
func (a *Auth) Routes() authboss.RouteTable {
2015-01-04 14:50:34 -08:00
return a.routes
}
2015-01-07 23:45:41 -08:00
func (a *Auth) Storage() authboss.StorageOptions {
2015-01-09 22:51:02 -08:00
return a.storageOptions
}
2015-01-12 21:08:52 -08:00
func (a *Auth) loginHandlerFunc(c *authboss.Context, w http.ResponseWriter, r *http.Request) {
switch r.Method {
case methodGET:
2015-01-12 21:08:52 -08:00
a.templates.ExecuteTemplate(w, pageLogin, nil)
case methodPOST:
2015-01-12 21:08:52 -08:00
u := r.PostFormValue("username")
p := r.PostFormValue("password")
if err := a.authenticate(u, p); err != nil {
2015-01-10 23:12:40 -08:00
w.WriteHeader(http.StatusForbidden)
2015-01-12 21:08:52 -08:00
a.templates.ExecuteTemplate(w, pageLogin, AuthPage{"invalid username and/or password", u})
2015-01-10 23:12:40 -08:00
return
}
2015-01-12 21:08:52 -08:00
http.Redirect(w, r, a.loginRedirect, http.StatusFound)
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}
2015-01-10 22:49:06 -08:00
func (a *Auth) authenticate(username, password string) error {
2015-01-12 21:08:52 -08: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-10 22:49:06 -08:00
}
return nil
}
2015-01-12 21:08:52 -08:00
func (a *Auth) logoutHandlerFunc(c *authboss.Context, w http.ResponseWriter, r *http.Request) {
switch r.Method {
case methodGET:
2015-01-10 23:12:40 -08:00
http.Redirect(w, r, a.logoutRedirect, http.StatusFound)
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}