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

Move all html to internal packge views

This commit is contained in:
Kris Runzer
2015-01-18 14:24:20 -08:00
parent e4dfe30d02
commit 934ddf5531
11 changed files with 93 additions and 497 deletions

View File

@@ -4,11 +4,11 @@ import (
"errors"
"fmt"
"net/http"
"path/filepath"
"golang.org/x/crypto/bcrypt"
"gopkg.in/authboss.v0"
"gopkg.in/authboss.v0/internal/views"
"html/template"
@@ -52,16 +52,9 @@ type Auth struct {
isRecoverLoaded bool
}
func (a *Auth) Initialize(c *authboss.Config) (err error) {
if a.templates, err = template.ParseFiles(filepath.Join(c.ViewsPath, pageLogin)); err != nil {
var loginTplBytes []byte
if loginTplBytes, err = views_login_tpl_bytes(); err != nil {
return err
}
if a.templates, err = template.New(pageLogin).Parse(string(loginTplBytes)); err != nil {
return err
}
func (a *Auth) Initialize(config *authboss.Config) (err error) {
if a.templates, err = views.Get(config.ViewsPath, pageLogin); err != nil {
return err
}
a.routes = authboss.RouteTable{
@@ -72,11 +65,11 @@ func (a *Auth) Initialize(c *authboss.Config) (err error) {
attrUsername: authboss.String,
attrPassword: authboss.String,
}
a.storer = c.Storer
a.logoutRedirect = c.AuthLogoutRoute
a.loginRedirect = c.AuthLoginSuccessRoute
a.logger = c.LogWriter
a.callbacks = c.Callbacks
a.storer = config.Storer
a.logoutRedirect = config.AuthLogoutRoute
a.loginRedirect = config.AuthLoginSuccessRoute
a.logger = config.LogWriter
a.callbacks = config.Callbacks
a.isRememberLoaded = authboss.IsLoaded("remember")
a.isRecoverLoaded = authboss.IsLoaded("recover")

View File

@@ -1,217 +0,0 @@
package auth
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_login_tpl = []byte("\x1f\x8b\x08\x00\x00\x09\x6e\x88\x00\xff\x8c\x51\x4d\x4f\xc4\x20\x10\xbd\x9b\xf8\x1f\xc8\x78\xde\x34\xde\x69\x8f\x9e\xf6\x60\x4c\xfc\x01\xb4\xcc\x16\x12\x5a\x70\x80\xd5\xfd\xf7\x4e\x2b\xa0\x8d\x31\x91\xcb\xbc\xf9\xe0\xf1\x78\x23\x4d\x5a\xdc\x70\x7f\x27\x47\xaf\x6f\x5b\xd4\xf6\x2a\x26\xa7\x62\xec\xc1\xf9\xd9\xae\xa7\x49\x91\x06\xee\x08\x3e\xd2\x3c\x0e\x67\x3f\x9f\xec\x2a\x3b\x86\x72\xa4\xda\xb8\x78\x5a\x0a\xde\x73\xbb\x86\x9c\x44\xba\x05\xec\x21\xe1\x47\x02\xb1\xaa\x85\x71\x8e\x48\x1b\x02\x11\x9c\x9a\xd0\x78\xa7\x91\x7a\x78\x6d\x65\xc2\xb7\x6c\x09\x35\x5f\xa3\x8c\xf0\x17\x67\x60\x85\xef\x9e\x95\x15\xde\xef\xfc\xc0\xfb\xdc\xca\xff\xe4\x8d\x79\x5c\x6c\x53\xbb\x3b\x00\x07\x3f\xc4\x97\x2b\x75\xee\xaa\x5c\xe6\xc1\xf3\x3e\x58\xcd\xe8\x8a\x1b\x25\xfd\x65\xa9\x41\x17\x0e\x0a\x94\x30\x84\x97\x1e\x1e\x60\x78\xc1\xd9\xc6\x84\x24\x3b\x35\xfc\xac\x3f\x79\x9a\x7d\x12\xf5\x43\x5b\xbb\xbe\xc6\xfc\xdb\xe6\x5a\xac\xab\xec\xca\x6e\x3f\x03\x00\x00\xff\xff\x05\x40\xcd\x07\xe4\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: 484, mode: os.FileMode(438), modTime: time.Unix(1421030024, 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, "/")...)...)
}

View File

@@ -1,16 +0,0 @@
<html>
<body>
<div class="login-card">
<h1>Log-in</h1><br>
<form>
<input type="text" name="username" placeholder="Username" required="true">
<input type="password" name="password" placeholder="Password" required="true">
<input type="submit" name="login" class="login login-submit" value="Login">
</form>
<div class="login-help">
<a href="#">Register</a><a href="#">Forgot Password</a>
</div>
</div>
</body>
</html>

View File

@@ -14,8 +14,7 @@ import (
)
var (
cfg *Config
emailer mailer
cfg *Config
)
// Init authboss and it's loaded modules with a configuration.

View File

@@ -13,9 +13,11 @@ type Config struct {
AuthLogoutRoute string `json:"authLogoutRoute" xml:"authLogoutRoute"`
AuthLoginSuccessRoute string `json:"authLoginSuccessRoute" xml:"authLoginSuccessRoute"`
ValidateEmail Validator
ValidateUsername Validator
ValidatePassword Validator
RecoverFromEmail string `json:"recoverFromEmail" xml:"recoverFromEmail"`
ValidateEmail Validator `json:"-" xml:"-"`
ValidateUsername Validator `json:"-" xml:"-"`
ValidatePassword Validator `json:"-" xml:"-"`
Storer Storer `json:"-" xml:"-"`
CookieStoreMaker CookieStoreMaker `json:"-" xml:"-"`
@@ -32,7 +34,9 @@ func NewConfig() *Config {
ViewsPath: "/",
AuthLogoutRoute: "/",
AuthLoginSuccessRoute: "http://www.google.com",
AuthLoginSuccessRoute: "/",
RecoverFromEmail: "no-reply@authboss.com",
LogWriter: ioutil.Discard,
Callbacks: NewCallbacks(),

View File

@@ -5,6 +5,10 @@ import (
"io"
)
var (
emailer mailer
)
type Mailer int
const (
@@ -12,8 +16,8 @@ const (
MailerSMTP
)
func SendEmail(to, from string, msg []byte) {
func SendEmail(to, from string, msg []byte) (err error) {
return emailer.Send(to, from, msg)
}
type mailer interface {
@@ -29,6 +33,6 @@ func newLogMailer(w io.Writer) logMailer {
}
func (e logMailer) Send(to, from string, msg []byte) error {
fmt.Fprintf(e.writer, "email sent\n\nto:\t %s\nfrom:\t %s\nmsg: %s", to, from, msg)
fmt.Fprintf(e.writer, "[emailer] Sent Email => to [%s], from [%s], msg [%s]", to, from, msg)
return nil
}

View File

@@ -1,217 +0,0 @@
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, "/")...)...)
}

View File

@@ -6,22 +6,30 @@ import (
"fmt"
"html/template"
"net/http"
"path/filepath"
"strings"
"io"
"bytes"
"crypto/md5"
"encoding/base64"
"log"
"gopkg.in/authboss.v0"
"gopkg.in/authboss.v0/internal/views"
)
const (
methodGET = "GET"
methodPOST = "POST"
pageRecover = "recover.tpl"
tplRecover = "recover.tpl"
tplRecoverComplete = "recover-complete.tpl"
tplInitEmail = "recover-init.email"
attrUsername = "username"
attrResetToken = "resettoken"
attrEmail = "email"
)
func init() {
@@ -39,29 +47,27 @@ type RecoverModule struct {
storageOptions authboss.StorageOptions
storer authboss.Storer
logger io.Writer
fromEmail string
}
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
}
func (m *RecoverModule) Initialize(config *authboss.Config) (err error) {
if m.templates, err = views.Get(config.ViewsPath, tplRecover, tplRecoverComplete, tplInitEmail); err != nil {
return err
}
m.routes = authboss.RouteTable{
"recover": m.recoverHandlerFunc,
"recover": m.recoverHandlerFunc,
"recover/complete": m.recoverCompleteHandlerFunc,
}
m.storageOptions = authboss.StorageOptions{
attrUsername: authboss.String,
attrResetToken: authboss.String,
attrEmail: authboss.String,
}
m.storer = c.Storer
m.logger = c.LogWriter
m.storer = config.Storer
m.logger = config.LogWriter
m.fromEmail = config.RecoverFromEmail
return nil
}
@@ -77,7 +83,7 @@ func (m *RecoverModule) Storage() authboss.StorageOptions {
func (m *RecoverModule) recoverHandlerFunc(ctx *authboss.Context, w http.ResponseWriter, r *http.Request) {
switch r.Method {
case methodGET:
m.templates.ExecuteTemplate(w, pageRecover, nil)
m.templates.ExecuteTemplate(w, tplRecover, nil)
case methodPOST:
username, ok := ctx.FirstPostFormValue("username")
if !ok {
@@ -89,9 +95,10 @@ func (m *RecoverModule) recoverHandlerFunc(ctx *authboss.Context, w http.Respons
fmt.Fprintln(m.logger, errors.New("recover: Expected postFormValue 'confirmUsername' to be in the context"))
}
if err := m.initiateRecover(ctx, username, confirmUsername); err != nil {
if err := m.initiateRecover(ctx, username, confirmUsername, r.Host); err != nil {
fmt.Fprintln(m.logger, fmt.Sprintf("recover: %s"), err.Error())
w.WriteHeader(http.StatusBadRequest)
m.templates.ExecuteTemplate(w, pageRecover, RecoverPage{username, confirmUsername, err.Error()})
m.templates.ExecuteTemplate(w, tplRecover, RecoverPage{username, confirmUsername, err.Error()})
return
}
default:
@@ -99,7 +106,7 @@ func (m *RecoverModule) recoverHandlerFunc(ctx *authboss.Context, w http.Respons
}
}
func (m *RecoverModule) initiateRecover(ctx *authboss.Context, username, confirmUsername string) error {
func (m *RecoverModule) initiateRecover(ctx *authboss.Context, username, confirmUsername, host string) error {
if !strings.EqualFold(username, confirmUsername) {
return errors.New("Confirm username does not match")
}
@@ -113,7 +120,47 @@ func (m *RecoverModule) initiateRecover(ctx *authboss.Context, username, confirm
return err
}
authboss.SendEmail("", "", []byte)
emailInter, ok := ctx.User[attrEmail]
if !ok {
return errors.New("user does not have mapped email")
}
email, ok := emailInter.(string)
if !ok {
return errors.New("user does not have a valid email")
}
// TODO : email regex check on to and from
sum := md5.Sum(token)
ctx.User[attrResetToken] = base64.StdEncoding.EncodeToString(sum[:])
log.Printf("%#v", ctx.User)
if err := ctx.SaveUser(username, m.storer); err != nil {
return err
}
emailBody := &bytes.Buffer{}
if err := m.templates.ExecuteTemplate(emailBody, tplInitEmail, struct{ Link string }{
fmt.Sprintf("%s/recover/complete?token=%s", host, base64.URLEncoding.EncodeToString(token)),
}); err != nil {
return err
}
if err := authboss.SendEmail(email, m.fromEmail, emailBody.Bytes()); err != nil {
fmt.Fprintln(m.logger, err)
}
return nil
}
func (m *RecoverModule) recoverCompleteHandlerFunc(ctx *authboss.Context, w http.ResponseWriter, r *http.Request) {
switch r.Method {
case methodGET:
m.templates.ExecuteTemplate(w, tplRecoverComplete, nil)
case methodPOST:
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}

View File

@@ -1 +0,0 @@
omgz i am recoverz

View File

@@ -49,7 +49,7 @@ func (r *Remember) Initialize(config *authboss.Config) error {
r.storer = storer
}
r.logger = c.LogWriter
r.logger = config.LogWriter
config.Callbacks.After(authboss.EventAuth, r.AfterAuth)
return nil