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

119 lines
2.3 KiB
Go
Raw Normal View History

package auth
import (
2015-01-10 22:49:06 -08:00
"errors"
"net/http"
"path"
2015-01-07 23:45:41 -08:00
"path/filepath"
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
"io/ioutil"
"bytes"
2015-01-09 22:51:02 -08:00
"io"
2015-01-10 22:49:06 -08:00
"log"
)
const (
methodGET = "GET"
methodPOST = "POST"
)
2015-01-10 22:49:06 -08:00
var errAuthFailed = errors.New("invalid username and/or password")
func init() {
a := &Auth{}
2015-01-07 23:45:41 -08:00
authboss.RegisterModule("auth", a)
}
type Auth struct {
2015-01-07 23:45:41 -08:00
routes authboss.RouteTable
2015-01-09 22:51:02 -08:00
storageOptions authboss.StorageOptions
2015-01-10 22:49:06 -08:00
users authboss.Storer
2015-01-04 14:50:34 -08:00
loginPage *bytes.Buffer
logoutRedirect string
2015-01-09 22:51:02 -08:00
logger io.Writer
}
2015-01-07 23:45:41 -08:00
func (a *Auth) Initialize(c *authboss.Config) (err error) {
2015-01-04 14:50:34 -08:00
var data []byte
2015-01-07 23:45:41 -08:00
2015-01-10 22:49:06 -08:00
if data, err = ioutil.ReadFile(filepath.Join(c.ViewsPath, "login.tpl")); err != nil {
2015-01-07 23:45:41 -08:00
if data, err = views_login_tpl_bytes(); err != nil {
2015-01-04 14:50:34 -08:00
return err
}
}
var tpl *template.Template
2015-01-08 21:48:02 -08:00
if tpl, err = template.New("login.tpl").Parse(string(data)); err != nil {
return err
} else {
a.loginPage = &bytes.Buffer{}
if err = tpl.Execute(a.loginPage, nil); err != nil {
return err
}
}
2015-01-10 22:49:06 -08:00
a.storageOptions = authboss.StorageOptions{
"Username": authboss.String,
"Password": authboss.String,
}
2015-01-07 23:45:41 -08:00
a.routes = authboss.RouteTable{
2015-01-09 22:51:02 -08:00
"login": a.loginHandler,
"logout": a.logoutHandler,
}
2015-01-10 22:49:06 -08:00
a.users = c.Storer
2015-01-07 23:45:41 -08:00
a.logoutRedirect = path.Join(c.MountPath, c.AuthLogoutRoute)
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
}
func (a *Auth) loginHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case methodGET:
w.Write(a.loginPage.Bytes())
case methodPOST:
2015-01-10 22:49:06 -08:00
log.Println("in post")
a.authenticate(r.PostFormValue("username"), r.PostFormValue("password"))
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}
2015-01-10 22:49:06 -08:00
func (a *Auth) authenticate(username, password string) error {
userInter, err := a.users.Get(username, nil)
if err != nil {
return errAuthFailed
}
userAttrs := authboss.Unbind(userInter)
if pwd, ok := userAttrs["Password"]; !ok {
return errAuthFailed
} else if pwd.Value.(string) != password {
return errAuthFailed
}
return nil
}
func (a *Auth) logoutHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case methodGET:
http.Redirect(w, r, a.logoutRedirect, http.StatusTemporaryRedirect)
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}