Skip to content

Commit bab1475

Browse files
committed
Change config to be global. Updated most modules and tests.
1 parent 1aa0da8 commit bab1475

25 files changed

Lines changed: 284 additions & 421 deletions

auth/auth.go

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,13 @@ package auth
33
import (
44
"errors"
55
"fmt"
6+
"html/template"
67
"net/http"
78

89
"golang.org/x/crypto/bcrypt"
910

1011
"gopkg.in/authboss.v0"
1112
"gopkg.in/authboss.v0/internal/views"
12-
13-
"html/template"
14-
15-
"io"
1613
)
1714

1815
const (
@@ -44,19 +41,14 @@ type AuthPage struct {
4441
type Auth struct {
4542
routes authboss.RouteTable
4643
storageOptions authboss.StorageOptions
47-
storer authboss.Storer
48-
logoutRedirect string
49-
loginRedirect string
50-
logger io.Writer
5144
templates map[string]*template.Template
52-
callbacks *authboss.Callbacks
5345

5446
isRememberLoaded bool
5547
isRecoverLoaded bool
5648
}
5749

58-
func (a *Auth) Initialize(config *authboss.Config) (err error) {
59-
if a.templates, err = views.Get(config.Layout, config.ViewsPath, pageLogin); err != nil {
50+
func (a *Auth) Initialize() (err error) {
51+
if a.templates, err = views.Get(authboss.Cfg.Layout, authboss.Cfg.ViewsPath, pageLogin); err != nil {
6052
return err
6153
}
6254

@@ -68,11 +60,6 @@ func (a *Auth) Initialize(config *authboss.Config) (err error) {
6860
attrUsername: authboss.String,
6961
attrPassword: authboss.String,
7062
}
71-
a.storer = config.Storer
72-
a.logoutRedirect = config.AuthLogoutRoute
73-
a.loginRedirect = config.AuthLoginSuccessRoute
74-
a.logger = config.LogWriter
75-
a.callbacks = config.Callbacks
7663

7764
a.isRememberLoaded = authboss.IsLoaded("remember")
7865
a.isRecoverLoaded = authboss.IsLoaded("recover")
@@ -93,7 +80,7 @@ func (a *Auth) loginHandlerFunc(ctx *authboss.Context, w http.ResponseWriter, r
9380
case methodGET:
9481
if _, ok := ctx.SessionStorer.Get(authboss.SessionKey); ok {
9582
if halfAuthed, ok := ctx.SessionStorer.Get(authboss.HalfAuthKey); !ok || halfAuthed == "false" {
96-
http.Redirect(w, r, a.loginRedirect, http.StatusFound)
83+
http.Redirect(w, r, authboss.Cfg.AuthLoginSuccessRoute, http.StatusFound)
9784
}
9885
}
9986

@@ -106,14 +93,13 @@ func (a *Auth) loginHandlerFunc(ctx *authboss.Context, w http.ResponseWriter, r
10693

10794
tpl := a.templates[pageLogin]
10895
tpl.Execute(w, page)
109-
// tpl.ExecuteTemplate(w, tpl.Name(), page)
11096
case methodPOST:
11197
u, ok := ctx.FirstPostFormValue("username")
11298
if !ok {
113-
fmt.Fprintln(a.logger, errors.New("auth: Expected postFormValue 'username' to be in the context"))
99+
fmt.Fprintln(authboss.Cfg.LogWriter, errors.New("auth: Expected postFormValue 'username' to be in the context"))
114100
}
115101

116-
if err := a.callbacks.FireBefore(authboss.EventAuth, ctx); err != nil {
102+
if err := authboss.Cfg.Callbacks.FireBefore(authboss.EventAuth, ctx); err != nil {
117103
w.WriteHeader(http.StatusForbidden)
118104

119105
tpl := a.templates[pageLogin]
@@ -122,21 +108,21 @@ func (a *Auth) loginHandlerFunc(ctx *authboss.Context, w http.ResponseWriter, r
122108

123109
p, ok := ctx.FirstPostFormValue("password")
124110
if !ok {
125-
fmt.Fprintln(a.logger, errors.New("auth: Expected postFormValue 'password' to be in the context"))
111+
fmt.Fprintln(authboss.Cfg.LogWriter, errors.New("auth: Expected postFormValue 'password' to be in the context"))
126112
}
127113

128114
if err := a.authenticate(ctx, u, p); err != nil {
129-
fmt.Fprintln(a.logger, err)
115+
fmt.Fprintln(authboss.Cfg.LogWriter, err)
130116
w.WriteHeader(http.StatusForbidden)
131117
tpl := a.templates[pageLogin]
132118
tpl.ExecuteTemplate(w, tpl.Name(), AuthPage{"invalid username and/or password", u, a.isRememberLoaded, a.isRecoverLoaded, "", ""})
133119
return
134120
}
135121

136122
ctx.SessionStorer.Put(authboss.SessionKey, u)
137-
a.callbacks.FireAfter(authboss.EventAuth, ctx)
123+
authboss.Cfg.Callbacks.FireAfter(authboss.EventAuth, ctx)
138124

139-
http.Redirect(w, r, a.loginRedirect, http.StatusFound)
125+
http.Redirect(w, r, authboss.Cfg.AuthLoginSuccessRoute, http.StatusFound)
140126
default:
141127
w.WriteHeader(http.StatusMethodNotAllowed)
142128
}
@@ -145,7 +131,7 @@ func (a *Auth) loginHandlerFunc(ctx *authboss.Context, w http.ResponseWriter, r
145131
func (a *Auth) authenticate(ctx *authboss.Context, username, password string) error {
146132
var userInter interface{}
147133
var err error
148-
if userInter, err = a.storer.Get(username, nil); err != nil {
134+
if userInter, err = authboss.Cfg.Storer.Get(username, nil); err != nil {
149135
return err
150136
}
151137

@@ -172,7 +158,7 @@ func (a *Auth) logoutHandlerFunc(ctx *authboss.Context, w http.ResponseWriter, r
172158
switch r.Method {
173159
case methodGET:
174160
ctx.SessionStorer.Del(authboss.SessionKey)
175-
http.Redirect(w, r, a.logoutRedirect, http.StatusFound)
161+
http.Redirect(w, r, authboss.Cfg.AuthLogoutRoute, http.StatusFound)
176162
default:
177163
w.WriteHeader(http.StatusMethodNotAllowed)
178164
}

auth/auth_test.go

Lines changed: 30 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package auth
22

3-
import (
3+
/*import (
44
"bytes"
55
"html/template"
66
"io/ioutil"
@@ -33,10 +33,8 @@ func getCompiledTemplate(path string, data interface{}) (b *bytes.Buffer, err er
3333
}
3434
3535
func TestAuth_Storage(t *testing.T) {
36-
t.Parallel()
37-
3836
a := &Auth{}
39-
if err := a.Initialize(authboss.NewConfig()); err != nil {
37+
if err := a.Initialize(); err != nil {
4038
t.Errorf("Unexpected config error: %v", err)
4139
}
4240
options := a.Storage()
@@ -61,10 +59,8 @@ func TestAuth_Storage(t *testing.T) {
6159
}
6260
6361
func TestAuth_Routes(t *testing.T) {
64-
t.Parallel()
65-
6662
a := &Auth{}
67-
if err := a.Initialize(authboss.NewConfig()); err != nil {
63+
if err := a.Initialize(); err != nil {
6864
t.Errorf("Unexpected config error: %v", err)
6965
}
7066
routes := a.Routes()
@@ -86,53 +82,35 @@ func TestAuth_Routes(t *testing.T) {
8682
}
8783
8884
func TestAuth_loginHandlerFunc_GET(t *testing.T) {
89-
t.Parallel()
90-
91-
tests := []struct {
92-
Config *authboss.Config
93-
}{
94-
{authboss.NewConfig()},
95-
{&authboss.Config{}},
96-
{&authboss.Config{ViewsPath: "views"}},
85+
a := &Auth{}
86+
if err := a.Initialize(); err != nil {
87+
t.Errorf("Unexpected config error: %v", err)
9788
}
9889
99-
for i, test := range tests {
100-
a := &Auth{}
101-
if err := a.Initialize(test.Config); err != nil {
102-
t.Errorf("%d> Unexpected config error: %v", i, err)
103-
continue
104-
}
105-
106-
r, err := http.NewRequest("GET", "/login", nil)
107-
if err != nil {
108-
t.Errorf("Unexpected error '%s'", err)
109-
}
110-
w := httptest.NewRecorder()
90+
r, err := http.NewRequest("GET", "/login", nil)
91+
if err != nil {
92+
t.Errorf("Unexpected error '%s'", err)
93+
}
94+
w := httptest.NewRecorder()
11195
112-
ctx, err := authboss.ContextFromRequest(r)
113-
if err != nil {
114-
t.Errorf("%d> Unexpected error '%s'", i, err)
115-
continue
116-
}
117-
ctx.SessionStorer = testClientStorer{}
96+
ctx, err := authboss.ContextFromRequest(r)
97+
if err != nil {
98+
t.Errorf("Unexpected error '%s'", err)
99+
}
100+
ctx.SessionStorer = testClientStorer{}
118101
119-
a.loginHandlerFunc(ctx, w, r)
102+
a.loginHandlerFunc(ctx, w, r)
120103
121-
if tpl, err := getCompiledTemplate("views/login.tpl", nil); err != nil {
122-
t.Errorf("%d> Unexpected error '%s'", i, err)
123-
continue
124-
} else {
125-
if !bytes.Equal(tpl.Bytes(), w.Body.Bytes()) {
126-
t.Errorf("%d> Expected '%s', got '%s'", i, tpl.Bytes(), w.Body.Bytes())
127-
continue
128-
}
104+
if tpl, err := getCompiledTemplate("views/login.tpl", nil); err != nil {
105+
t.Errorf("Unexpected error '%s'", err)
106+
} else {
107+
if !bytes.Equal(tpl.Bytes(), w.Body.Bytes()) {
108+
t.Errorf("Expected '%s', got '%s'", tpl.Bytes(), w.Body.Bytes())
129109
}
130110
}
131111
}
132112
133113
func TestAuth_loginHandlerFunc_POST(t *testing.T) {
134-
t.Parallel()
135-
136114
tests := []struct {
137115
Username, Password string
138116
StatusCode int
@@ -141,17 +119,16 @@ func TestAuth_loginHandlerFunc_POST(t *testing.T) {
141119
BodyData *AuthPage
142120
}{
143121
{"john", "1234", http.StatusFound, true, "/dashboard", nil},
144-
{"jane", "1234", http.StatusForbidden, false, "", &AuthPage{"invalid username and/or password", "jane", false, false}},
145-
{"mike", "", http.StatusForbidden, false, "", &AuthPage{"invalid username and/or password", "jane", false, false}},
122+
{"jane", "1234", http.StatusForbidden, false, "", &AuthPage{"invalid username and/or password", "jane", false, false, "", ""}},
123+
{"mike", "", http.StatusForbidden, false, "", &AuthPage{"invalid username and/or password", "jane", false, false, "", ""}},
146124
}
147125
148-
c := authboss.NewConfig()
149-
c.Storer = NewMockUserStorer()
150-
c.AuthLoginSuccessRoute = "/dashboard"
126+
authboss.Cfg.Storer = NewMockUserStorer()
127+
authboss.Cfg.AuthLoginSuccessRoute = "/dashboard"
151128
152129
for i, test := range tests {
153130
a := &Auth{}
154-
if err := a.Initialize(c); err != nil {
131+
if err := a.Initialize(); err != nil {
155132
t.Errorf("%d> Unexpected config error: %v", i, err)
156133
continue
157134
}
@@ -211,8 +188,6 @@ func TestAuth_loginHandlerFunc_POST(t *testing.T) {
211188
}
212189
213190
func TestAuth_loginHandlerFunc_OtherMethods(t *testing.T) {
214-
t.Parallel()
215-
216191
a := Auth{}
217192
methods := []string{"HEAD", "PUT", "DELETE", "TRACE", "CONNECT"}
218193
@@ -233,10 +208,9 @@ func TestAuth_loginHandlerFunc_OtherMethods(t *testing.T) {
233208
}
234209
235210
func TestAuth_logoutHandlerFunc_GET(t *testing.T) {
236-
t.Parallel()
237-
211+
authboss.Cfg.AuthLogoutRoute = "/dashboard"
238212
a := Auth{}
239-
if err := a.Initialize(&authboss.Config{AuthLogoutRoute: "/dashboard"}); err != nil {
213+
if err := a.Initialize(); err != nil {
240214
t.Errorf("Unexpeced config error '%s'", err)
241215
}
242216
r, err := http.NewRequest("GET", "/logout", nil)
@@ -267,8 +241,6 @@ func TestAuth_logoutHandlerFunc_GET(t *testing.T) {
267241
}
268242
269243
func TestAuth_logoutHandlerFunc_OtherMethods(t *testing.T) {
270-
t.Parallel()
271-
272244
a := Auth{}
273245
methods := []string{"HEAD", "POST", "PUT", "DELETE", "TRACE", "CONNECT"}
274246
@@ -287,3 +259,4 @@ func TestAuth_logoutHandlerFunc_OtherMethods(t *testing.T) {
287259
}
288260
}
289261
}
262+
*/

authboss.go

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,15 @@ Remember Me tokens, or passwords.
88
package authboss // import "gopkg.in/authboss.v0"
99

1010
import (
11-
"errors"
1211
"fmt"
1312
"net/http"
1413
)
1514

16-
var (
17-
cfg *Config
18-
)
19-
20-
// Init authboss and it's loaded modules with a configuration.
21-
func Init(config *Config) error {
22-
if config.Storer == nil {
23-
return errors.New("configuration must provide a storer.")
24-
}
25-
26-
cfg = config
27-
15+
// Init authboss and it's loaded modules.
16+
func Init() error {
2817
for name, mod := range modules {
29-
fmt.Fprintf(cfg.LogWriter, "%-10s Initializing\n", "["+name+"]")
30-
if err := mod.Initialize(config); err != nil {
18+
fmt.Fprintf(Cfg.LogWriter, "%-10s Initializing\n", "["+name+"]")
19+
if err := mod.Initialize(); err != nil {
3120
return fmt.Errorf("[%s] Error Initializing: %v", name, err)
3221
}
3322
}
@@ -37,7 +26,7 @@ func Init(config *Config) error {
3726

3827
// CurrentUser retrieves the current user from the session and the database.
3928
func CurrentUser(w http.ResponseWriter, r *http.Request) (interface{}, error) {
40-
sessions := cfg.SessionStoreMaker(w, r)
29+
sessions := Cfg.SessionStoreMaker(w, r)
4130
key, ok := sessions.Get(SessionKey)
4231
if !ok {
4332
return nil, nil
@@ -48,17 +37,17 @@ func CurrentUser(w http.ResponseWriter, r *http.Request) (interface{}, error) {
4837
return nil, err
4938
}
5039

51-
err = ctx.LoadUser(key, cfg.Storer)
40+
err = ctx.LoadUser(key, Cfg.Storer)
5241
if err != nil {
5342
return nil, err
5443
}
5544

56-
err = cfg.Callbacks.FireBefore(EventGet, ctx)
45+
err = Cfg.Callbacks.FireBefore(EventGet, ctx)
5746
if err != nil {
5847
return nil, err
5948
}
6049

61-
return cfg.Storer.Get(key, ModuleAttrMeta)
50+
return Cfg.Storer.Get(key, ModuleAttrMeta)
6251
}
6352

6453
// CurrentUserP retrieves the current user but panics if it's not available for

0 commit comments

Comments
 (0)