2015-01-04 10:33:53 -08:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"path"
|
2015-01-07 23:45:41 -08:00
|
|
|
"path/filepath"
|
2015-01-04 10:33:53 -08:00
|
|
|
|
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"
|
2015-01-04 10:33:53 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
methodGET = "GET"
|
|
|
|
methodPOST = "POST"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
a := &Auth{}
|
2015-01-07 23:45:41 -08:00
|
|
|
authboss.RegisterModule("auth", a)
|
2015-01-04 10:33:53 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2015-01-04 10:33:53 -08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2015-01-04 20:41:20 -08:00
|
|
|
|
|
|
|
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-04 10:33:53 -08:00
|
|
|
|
2015-01-07 23:45:41 -08:00
|
|
|
a.routes = authboss.RouteTable{
|
2015-01-04 10:33:53 -08:00
|
|
|
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
|
|
|
|
2015-01-04 10:33:53 -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-04 10:33:53 -08:00
|
|
|
}
|
|
|
|
|
2015-01-07 23:45:41 -08:00
|
|
|
func (a *Auth) Storage() authboss.StorageOptions {
|
|
|
|
return nil
|
2015-01-04 10:33:53 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Auth) loginHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
switch r.Method {
|
|
|
|
case methodGET:
|
2015-01-04 20:41:20 -08:00
|
|
|
w.Write(a.loginPage.Bytes())
|
2015-01-04 10:33:53 -08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|