1
0
mirror of https://github.com/volatiletech/authboss.git synced 2025-09-16 09:06:20 +02:00

Added auth module view overriding

This commit is contained in:
Kris Runzer
2015-01-04 14:50:34 -08:00
parent 0f691e7607
commit 3aecf510ad
3 changed files with 368 additions and 29 deletions

View File

@@ -5,6 +5,10 @@ import (
"net/http"
"path"
"io/ioutil"
"bytes"
"github.com/go-authboss/authboss"
)
@@ -19,27 +23,36 @@ func init() {
}
type Auth struct {
Routes authboss.Routes
loginPage io.Reader
routes authboss.Routes
loginPage *bytes.Buffer
logoutRedirect string
}
func (a *Auth) Initialize(c authboss.Config) error {
// create the reader for the default or specified file
func (a *Auth) Initialize(c authboss.Config) (err error) {
var data []byte
if c.AuthLoginPageURI == "" {
if data, err = views_login_tpl_bytes(); err != nil {
return err
}
} else {
if data, err = ioutil.ReadFile(c.AuthLoginPageURI); err != nil {
return err
}
}
a.loginPage = bytes.NewBuffer(data)
var err error
a.loginPage, err = views_login_tpl_bytes()
a.Routes = Routes{
a.routes = authboss.Routes{
path.Join(c.MountPath, "login"): a.loginHandler,
path.Join(c.MountPath, "logout"): a.logoutHandler,
}
a.logoutRedirect = path.Join(c.MountPath, c.AuthLogoutRedirect)
return nil
}
func (a *Auth) Routes() authboss.Routes {
return a.Routes
return a.routes
}
func (a *Auth) Storage() {

View File

@@ -1,44 +1,154 @@
package auth
import (
"bytes"
"io"
"net/http"
"net/http/httptest"
"testing"
"bytes"
"io/ioutil"
"reflect"
"net/http/httptest"
"github.com/go-authboss/authboss"
)
func TestAuth_Initialize_LoadsDefaultLoginPageWhenOverrideNotSpecified(t *testing.T) {
t.Parallel()
a := &Auth{}
if err := a.Initialize(authboss.Config{}); err != nil {
t.Errorf("Unexpected config error: %v", err)
}
bindata, err := views_login_tpl_bytes()
if err != nil {
t.Errorf("Unexpected bindata error: %v", err)
}
if !bytes.Equal(a.loginPage.Bytes(), bindata) {
t.Errorf("Expected '%s', got '%s'", bindata, a.loginPage.Bytes())
}
}
func TestAuth_Initialize_LoadsSpecifiedLoginPageWhenOverrideSpecified(t *testing.T) {
t.Parallel()
a := &Auth{}
if err := a.Initialize(authboss.Config{
AuthLoginPageURI: "auth_test.go",
}); err != nil {
t.Errorf("Unexpected config error: %v", err)
}
file, err := ioutil.ReadFile("auth_test.go")
if err != nil {
t.Errorf("Unexpected bindata error: %v", err)
}
if !bytes.Equal(a.loginPage.Bytes(), file) {
t.Errorf("Expected '%s', got '%s'", file, a.loginPage.Bytes())
}
}
func TestAuth_Initialize_RegistersRoutes(t *testing.T) {
t.Parallel()
a := &Auth{}
if err := a.Initialize(authboss.Config{}); err != nil {
t.Errorf("Unexpected config error: %v", err)
}
if handler, ok := a.routes["login"]; !ok {
t.Error("Expected route 'login' but was not found'")
} else if reflect.ValueOf(handler).Pointer() != reflect.ValueOf(a.loginHandler).Pointer() {
t.Errorf("Expcted func 'loginHandler' but was not found")
}
if handler, ok := a.routes["logout"]; !ok {
t.Error("Expected route 'logout' but was not found'")
} else if reflect.ValueOf(handler).Pointer() != reflect.ValueOf(a.logoutHandler).Pointer() {
t.Errorf("Expcted func 'logoutHandler' but was not found")
}
}
func TestAuth_Routes(t *testing.T) {
t.Parallel()
routes := authboss.Routes{
"a": func(_ http.ResponseWriter, _ *http.Request) {},
"b": func(_ http.ResponseWriter, _ *http.Request) {},
}
a := Auth{routes: routes}
if !reflect.DeepEqual(routes, a.Routes()) {
t.Errorf("Failed to retrieve routes")
}
}
func TestAuth_loginHandler_GET(t *testing.T) {
t.Parallel()
a := &Auth{}
if err := a.Initialize(authboss.Config{}); err != nil {
t.Errorf("Unexpected config error: %$", err)
}
w := httptest.NewRecorder()
r, err := http.NewRequest("GET", "/login", nil)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
bindata, err := views_login_tpl_bytes()
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
a.loginHandler(w, r)
if http.StatusOK != w.Code {
t.Errorf("%Expected response code %d, got %d", http.StatusOK, w.Code)
}
if !bytes.Equal(bindata, w.Body.Bytes()) {
t.Errorf("Expected body '%s', got '%s'", string(bindata), w.Body.String())
}
}
func TestAuth_logoutHandler_GET(t *testing.T) {
t.Parallel()
tests := []struct {
InLoginPage io.ReadWriter
OutBody string
Config authboss.Config
RedirectPath string
}{
{nil, htmlLoginPage},
{bytes.NewBufferString("<form></form>"), "<form></form>"},
{authboss.Config{}, "/"},
{authboss.Config{AuthLogoutRedirect: "/logout"}, "/logout"},
{authboss.Config{MountPath: "/auth", AuthLogoutRedirect: "/logout"}, "/auth/logout"},
}
for i, test := range tests {
var c authConfig
if test.InLoginPage == nil {
c = NewAuthConfig()
} else {
c = authConfig{LoginPage: test.InLoginPage}
a := Auth{}
if err := a.Initialize(test.Config); err != nil {
t.Errorf("%d> Unexpected config error: %v", i, err)
}
a := NewAuth(c)
w := httptest.NewRecorder()
r, err := http.NewRequest("GET", "/login", nil)
r, err := http.NewRequest("GET", "/logout", nil)
if err != nil {
t.Errorf("%d> Unexpected error: %s", i, err)
t.Errorf("%d> Unexpected error: %v", i, err)
}
a.loginHandler(w, r)
a.logoutHandler(w, r)
if http.StatusOK != w.Code {
t.Errorf("%d> Expected response code 200, got %d", i, w.Code)
if http.StatusTemporaryRedirect != w.Code {
t.Errorf("%d> Expected response code %d, got %d", i, http.StatusTemporaryRedirect, w.Code)
}
if test.OutBody != w.Body.String() {
t.Errorf("%d> Expected body '%s', got '%s'", i, test.OutBody, w.Body.String())
if test.RedirectPath != w.HeaderMap["Location"][0] {
t.Errorf("%d> Expected header Location '%s', got '%s'", 1, test.RedirectPath, w.HeaderMap["Location"][0])
}
}
}

216
auth/bindata.go Normal file
View File

@@ -0,0 +1,216 @@
package auth
import (
"bytes"
"compress/gzip"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"time"
)
func bindata_read(data []byte, name string) ([]byte, error) {
gz, err := gzip.NewReader(bytes.NewBuffer(data))
if err != nil {
return nil, fmt.Errorf("Read %q: %v", name, err)
}
var buf bytes.Buffer
_, err = io.Copy(&buf, gz)
gz.Close()
if err != nil {
return nil, fmt.Errorf("Read %q: %v", name, err)
}
return buf.Bytes(), nil
}
type asset struct {
bytes []byte
info os.FileInfo
}
type bindata_file_info struct {
name string
size int64
mode os.FileMode
modTime time.Time
}
func (fi bindata_file_info) Name() string {
return fi.name
}
func (fi bindata_file_info) Size() int64 {
return fi.size
}
func (fi bindata_file_info) Mode() os.FileMode {
return fi.mode
}
func (fi bindata_file_info) ModTime() time.Time {
return fi.modTime
}
func (fi bindata_file_info) IsDir() bool {
return false
}
func (fi bindata_file_info) Sys() interface{} {
return nil
}
var _views_login_tpl = []byte("\x1f\x8b\x08\x00\x00\x09\x6e\x88\x00\xff\x7c\x90\xcd\x8d\xc3\x20\x10\x85\xef\x2b\x6d\x0f\x68\x1a\xa0\x01\xa0\x82\x3d\xec\x65\x0b\x80\x85\xc4\x48\xc0\x20\x33\x28\x71\xf7\x01\x1b\xff\x1c\xa2\xf8\x60\xde\xd3\xbc\xe1\x13\x4f\x4c\x14\x83\xfa\xfe\x12\x06\xed\xd2\xcf\x1b\xce\x91\x45\x47\x13\x5a\x09\x19\x0b\x01\xd3\xff\xe4\x31\x49\xe0\x01\xef\x3e\x41\x4b\xb1\xf6\x89\xa0\x8d\x0b\xac\xe5\x25\xd4\xe2\xe6\xa4\xa3\x03\xf5\x37\x94\xe0\xeb\x78\xcf\xfa\x94\x2b\x31\x5a\xb2\x93\x40\xee\xd9\x2e\xf5\xf6\xb2\xc6\xfa\xff\xea\xf9\x1b\x48\xd6\xa5\x3c\x70\xb6\xa0\x7e\x87\xfa\x00\x39\xc2\x2b\xe8\x74\x1b\xe8\xf4\x07\xc8\x54\x22\x4c\x63\xbb\x54\x13\x3d\x81\xfa\xe9\x0f\x16\x7c\x9b\xf5\x76\x78\xaf\x67\x15\x7b\x5f\x7c\x14\xf8\x0a\x00\x00\xff\xff\x11\x71\x5f\xad\x49\x01\x00\x00")
func views_login_tpl_bytes() ([]byte, error) {
return bindata_read(
_views_login_tpl,
"views/login.tpl",
)
}
func views_login_tpl() (*asset, error) {
bytes, err := views_login_tpl_bytes()
if err != nil {
return nil, err
}
info := bindata_file_info{name: "views/login.tpl", size: 329, mode: os.FileMode(438), modTime: time.Unix(1420331148, 0)}
a := &asset{bytes: bytes, info: info}
return a, nil
}
// Asset loads and returns the asset for the given name.
// It returns an error if the asset could not be found or
// could not be loaded.
func Asset(name string) ([]byte, error) {
cannonicalName := strings.Replace(name, "\\", "/", -1)
if f, ok := _bindata[cannonicalName]; ok {
a, err := f()
if err != nil {
return nil, fmt.Errorf("Asset %s can't read by error: %v", name, err)
}
return a.bytes, nil
}
return nil, fmt.Errorf("Asset %s not found", name)
}
// AssetInfo loads and returns the asset info for the given name.
// It returns an error if the asset could not be found or
// could not be loaded.
func AssetInfo(name string) (os.FileInfo, error) {
cannonicalName := strings.Replace(name, "\\", "/", -1)
if f, ok := _bindata[cannonicalName]; ok {
a, err := f()
if err != nil {
return nil, fmt.Errorf("AssetInfo %s can't read by error: %v", name, err)
}
return a.info, nil
}
return nil, fmt.Errorf("AssetInfo %s not found", name)
}
// AssetNames returns the names of the assets.
func AssetNames() []string {
names := make([]string, 0, len(_bindata))
for name := range _bindata {
names = append(names, name)
}
return names
}
// _bindata is a table, holding each asset generator, mapped to its name.
var _bindata = map[string]func() (*asset, error){
"views/login.tpl": views_login_tpl,
}
// AssetDir returns the file names below a certain
// directory embedded in the file by go-bindata.
// For example if you run go-bindata on data/... and data contains the
// following hierarchy:
// data/
// foo.txt
// img/
// a.png
// b.png
// then AssetDir("data") would return []string{"foo.txt", "img"}
// AssetDir("data/img") would return []string{"a.png", "b.png"}
// AssetDir("foo.txt") and AssetDir("notexist") would return an error
// AssetDir("") will return []string{"data"}.
func AssetDir(name string) ([]string, error) {
node := _bintree
if len(name) != 0 {
cannonicalName := strings.Replace(name, "\\", "/", -1)
pathList := strings.Split(cannonicalName, "/")
for _, p := range pathList {
node = node.Children[p]
if node == nil {
return nil, fmt.Errorf("Asset %s not found", name)
}
}
}
if node.Func != nil {
return nil, fmt.Errorf("Asset %s not found", name)
}
rv := make([]string, 0, len(node.Children))
for name := range node.Children {
rv = append(rv, name)
}
return rv, nil
}
type _bintree_t struct {
Func func() (*asset, error)
Children map[string]*_bintree_t
}
var _bintree = &_bintree_t{nil, map[string]*_bintree_t{
"views/login.tpl": &_bintree_t{views_login_tpl, map[string]*_bintree_t{}},
}}
// Restore an asset under the given directory
func RestoreAsset(dir, name string) error {
data, err := Asset(name)
if err != nil {
return err
}
info, err := AssetInfo(name)
if err != nil {
return err
}
err = os.MkdirAll(_filePath(dir, path.Dir(name)), os.FileMode(0755))
if err != nil {
return err
}
err = ioutil.WriteFile(_filePath(dir, name), data, info.Mode())
if err != nil {
return err
}
err = os.Chtimes(_filePath(dir, name), info.ModTime(), info.ModTime())
if err != nil {
return err
}
return nil
}
// Restore assets under the given directory recursively
func RestoreAssets(dir, name string) error {
children, err := AssetDir(name)
if err != nil { // File
return RestoreAsset(dir, name)
} else { // Dir
for _, child := range children {
err = RestoreAssets(dir, path.Join(name, child))
if err != nil {
return err
}
}
}
return nil
}
func _filePath(dir, name string) string {
cannonicalName := strings.Replace(name, "\\", "/", -1)
return filepath.Join(append([]string{dir}, strings.Split(cannonicalName, "/")...)...)
}