From 59454bf909b45e7a2b7147c4959fda1ccff1d470 Mon Sep 17 00:00:00 2001 From: Kris Runzer Date: Mon, 12 Jan 2015 22:28:42 -0800 Subject: [PATCH] Began work on recover module --- auth/auth.go | 8 +- recover/bindata.go | 217 ++++++++++++++++++++++++++++++++++++++ recover/recover.go | 93 ++++++++++++++++ recover/recover_test.go | 1 + recover/views/recover.tpl | 1 + 5 files changed, 316 insertions(+), 4 deletions(-) create mode 100644 recover/bindata.go create mode 100644 recover/recover.go create mode 100644 recover/recover_test.go create mode 100644 recover/views/recover.tpl diff --git a/auth/auth.go b/auth/auth.go index b4d0078..df2d5fa 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -56,14 +56,14 @@ func (a *Auth) Initialize(c *authboss.Config) (err error) { } } - a.storageOptions = authboss.StorageOptions{ - attrUsername: authboss.String, - attrPassword: authboss.String, - } a.routes = authboss.RouteTable{ "login": a.loginHandlerFunc, "logout": a.logoutHandlerFunc, } + a.storageOptions = authboss.StorageOptions{ + attrUsername: authboss.String, + attrPassword: authboss.String, + } a.users = c.Storer a.logoutRedirect = c.AuthLogoutRoute a.loginRedirect = c.AuthLoginSuccessRoute diff --git a/recover/bindata.go b/recover/bindata.go new file mode 100644 index 0000000..29c828e --- /dev/null +++ b/recover/bindata.go @@ -0,0 +1,217 @@ +package recover + +import ( + "bytes" + "compress/gzip" + "fmt" + "io" + "strings" + "os" + "time" + "io/ioutil" + "path" + "path/filepath" +) + +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_recover_tpl = []byte("\x1f\x8b\x08\x00\x00\x09\x6e\x88\x00\xff\xca\xcf\x4d\xaf\x52\xc8\x54\x48\xcc\x55\x28\x4a\x4d\xce\x2f\x4b\x2d\xaa\x02\x04\x00\x00\xff\xff\x36\xaf\xf6\xd6\x12\x00\x00\x00") + +func views_recover_tpl_bytes() ([]byte, error) { + return bindata_read( + _views_recover_tpl, + "views/recover.tpl", + ) +} + +func views_recover_tpl() (*asset, error) { + bytes, err := views_recover_tpl_bytes() + if err != nil { + return nil, err + } + + info := bindata_file_info{name: "views/recover.tpl", size: 18, mode: os.FileMode(438), modTime: time.Unix(1421128431, 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/recover.tpl": views_recover_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/recover.tpl": &_bintree_t{views_recover_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, "/")...)...) +} + diff --git a/recover/recover.go b/recover/recover.go new file mode 100644 index 0000000..eefdc23 --- /dev/null +++ b/recover/recover.go @@ -0,0 +1,93 @@ +package recover + +import ( + "errors" + "html/template" + "net/http" + "path/filepath" + "strings" + + "gopkg.in/authboss.v0" +) + +const ( + methodGET = "GET" + methodPOST = "POST" + + pageRecover = "recover.tpl" + + attrUsername = "Username" +) + +func init() { + m := &RecoverModule{} + authboss.RegisterModule("recover", m) +} + +type RecoverPage struct { + Username, Error string +} + +type RecoverModule struct { + templates *template.Template + routes authboss.RouteTable + storageOptions authboss.StorageOptions + users authboss.Storer +} + +func (m *RecoverModule) Initialize(c *authboss.Config) (err error) { + if m.templates, err = template.ParseFiles(filepath.Join(c.ViewsPath, pageRecover)); err != nil { + var recoverTplBytes []byte + if recoverTplBytes, err = views_recover_tpl_bytes(); err != nil { + return err + } + + if m.templates, err = template.New(pageRecover).Parse(string(recoverTplBytes)); err != nil { + return err + } + } + + m.routes = authboss.RouteTable{ + "recover": m.recoverHandlerFunc, + } + m.storageOptions = authboss.StorageOptions{ + attrUsername: authboss.String, + } + m.users = c.Storer + + return nil +} + +func (m *RecoverModule) Routes() authboss.RouteTable { + return m.routes +} + +func (m *RecoverModule) Storage() authboss.StorageOptions { + return m.storageOptions +} + +func (m *RecoverModule) recoverHandlerFunc(c *authboss.Context, w http.ResponseWriter, r *http.Request) { + switch r.Method { + case methodGET: + m.templates.ExecuteTemplate(w, pageRecover, nil) + case methodPOST: + u := r.PostFormValue("username") + cu := r.PostFormValue("confirmUsername") + + if err := recoverAccount(u, cu); err != nil { + w.WriteHeader(http.StatusBadRequest) + m.templates.ExecuteTemplate(w, pageRecover, RecoverPage{u, err.Error()}) + return + } + default: + w.WriteHeader(http.StatusMethodNotAllowed) + } +} + +func recoverAccount(username, confirmUsername string) error { + if !strings.EqualFold(username, confirmUsername) { + return errors.New("Confirm username does not match") + } + + return nil +} diff --git a/recover/recover_test.go b/recover/recover_test.go new file mode 100644 index 0000000..916ca08 --- /dev/null +++ b/recover/recover_test.go @@ -0,0 +1 @@ +package recover diff --git a/recover/views/recover.tpl b/recover/views/recover.tpl new file mode 100644 index 0000000..098ca81 --- /dev/null +++ b/recover/views/recover.tpl @@ -0,0 +1 @@ +omgz i am recoverz \ No newline at end of file