Skip to content

Commit 0b66578

Browse files
committed
Add full-circle remember me test.
- Add more helpers to context.
1 parent 670c6f3 commit 0b66578

5 files changed

Lines changed: 54 additions & 13 deletions

File tree

context.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,16 @@ func (c *Context) PostFormValue(key string) ([]string, bool) {
5757
}
5858

5959
// LoadUser loads the user Attributes if they haven't already been loaded.
60-
func (c *Context) LoadUser(storer Storer) error {
60+
func (c *Context) LoadUser(key string, storer Storer) error {
6161
if c.User != nil {
6262
return nil
6363
}
6464

65+
intf, err := storer.Get(key, moduleAttrMeta)
66+
if err != nil {
67+
return err
68+
}
69+
70+
c.User = Unbind(intf)
6571
return nil
6672
}

module.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package authboss
22

33
var modules = make(map[string]Modularizer)
44

5+
var moduleAttrMeta = make(AttributeMeta)
6+
57
// Modularizer should be implemented by all the authboss modules.
68
type Modularizer interface {
79
Initialize(*Config) error
@@ -13,6 +15,10 @@ type Modularizer interface {
1315
// integrate into authboss.
1416
func RegisterModule(name string, m Modularizer) {
1517
modules[name] = m
18+
19+
for k, v := range m.Storage() {
20+
moduleAttrMeta[k] = v
21+
}
1622
}
1723

1824
// LoadedModules returns a list of modules that are currently loaded.

remember/remember.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,17 @@ func (r *Remember) Storage() authboss.StorageOptions {
6868

6969
// AfterAuth is called after authentication is successful.
7070
func (r *Remember) AfterAuth(ctx *authboss.Context) {
71-
if val, ok := ctx.Get(ValueKey); ok && val != "true" {
71+
if val, ok := ctx.FormValue(ValueKey); !ok || val[0] != "true" {
7272
return
7373
}
7474

75-
if err := ctx.LoadUser(r.storer); err != nil {
76-
fmt.Fprintln(r.logger, "remember: Failed to load user:", err)
77-
return
75+
if ctx.User == nil {
76+
fmt.Fprintf(r.logger, "remember: AfterAuth no user loaded")
7877
}
7978

80-
key := ctx.User["Username"].(string)
79+
key := ctx.User["username"].(string)
8180
if _, err := r.New(ctx.CookieStorer, key); err != nil {
82-
fmt.Fprintf(r.logger, "Failed to create remember token: %v", err)
81+
fmt.Fprintf(r.logger, "remember: Failed to create remember token: %v", err)
8382
}
8483
}
8584

remember/remember_test.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package remember
22

33
import (
4+
"bytes"
5+
"net/http"
46
"testing"
57

68
"gopkg.in/authboss.v0"
@@ -75,11 +77,31 @@ func TestInitialize(t *testing.T) {
7577
}
7678

7779
func TestAfterAuth(t *testing.T) {
78-
// TODO(aarondl): This
80+
storer := &testTokenStorer{}
81+
R.storer = storer
82+
cookies := make(testClientStorer)
83+
session := make(testClientStorer)
84+
85+
req, err := http.NewRequest("POST", "http://localhost", bytes.NewBufferString("rm=true"))
86+
if err != nil {
87+
t.Error("Unexpected Error:", err)
88+
}
89+
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
90+
91+
ctx, err := authboss.ContextFromRequest(req)
92+
if err != nil {
93+
t.Error("Unexpected error:", err)
94+
}
7995

80-
/*ctx := authboss.NewContext()
8196
ctx.SessionStorer = session
82-
ctx.CookieStorer = cookies*/
97+
ctx.CookieStorer = cookies
98+
ctx.User = authboss.Attributes{"username": "testuser"}
99+
100+
R.AfterAuth(ctx)
101+
102+
if _, ok := cookies[ValueKey]; !ok {
103+
t.Error("Expected a cookie to have been set.")
104+
}
83105
}
84106

85107
func TestNew(t *testing.T) {

router.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,27 @@ func NewRouter(config *Config) http.Handler {
1919
for name, mod := range modules {
2020
for route, handler := range mod.Routes() {
2121
fmt.Fprintf(logger, "[%-10s] Register Route: %s\n", name, route)
22-
mux.Handle(path.Join(config.MountPath, route), contextRoute{handler})
22+
mux.Handle(path.Join(config.MountPath, route), contextRoute{handler, config})
2323
}
2424
}
2525

2626
return mux
2727
}
2828

2929
type contextRoute struct {
30-
fn HandlerFunc
30+
fn HandlerFunc
31+
config *Config
3132
}
3233

3334
func (c contextRoute) ServeHTTP(w http.ResponseWriter, r *http.Request) {
34-
ctx := NewContext()
35+
ctx, err := ContextFromRequest(r)
36+
if err != nil {
37+
fmt.Fprintf(c.config.LogWriter, "route: Malformed request, could not create context: %v", err)
38+
return
39+
}
40+
41+
ctx.CookieStorer = c.config.CookieStoreMaker(r)
42+
ctx.SessionStorer = c.config.SessionStoreMaker(r)
3543

3644
c.fn(ctx, w, r)
3745
}

0 commit comments

Comments
 (0)