1
0
mirror of https://github.com/volatiletech/authboss.git synced 2025-01-26 05:27:33 +02:00
authboss/auth/auth.go

90 lines
1.6 KiB
Go
Raw Normal View History

package auth
import (
"net/http"
"path"
2015-01-07 23:45:41 -08:00
"path/filepath"
2015-01-07 23:45:41 -08:00
"html/template"
2015-01-04 14:50:34 -08:00
"io/ioutil"
"bytes"
2015-01-07 23:45:41 -08:00
"gopkg.in/authboss.v0"
)
const (
methodGET = "GET"
methodPOST = "POST"
)
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-04 14:50:34 -08:00
loginPage *bytes.Buffer
logoutRedirect string
}
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
if data, err = ioutil.ReadFile(filepath.Join(c.ViewsPath, "login.html")); err != nil {
return err
2015-01-04 14:50:34 -08:00
} else {
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
if tpl, err = template.New("login.html").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-07 23:45:41 -08:00
a.routes = authboss.RouteTable{
path.Join(c.MountPath, "login"): a.loginHandler,
path.Join(c.MountPath, "logout"): a.logoutHandler,
}
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 {
return nil
}
func (a *Auth) loginHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case methodGET:
w.Write(a.loginPage.Bytes())
case methodPOST:
// TODO
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}
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)
}
}