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

Add full-circle remember me test.

- Add more helpers to context.
This commit is contained in:
Aaron
2015-01-15 02:56:13 -08:00
parent 670c6f3b9f
commit 0b66578b88
5 changed files with 54 additions and 13 deletions

View File

@@ -57,10 +57,16 @@ func (c *Context) PostFormValue(key string) ([]string, bool) {
}
// LoadUser loads the user Attributes if they haven't already been loaded.
func (c *Context) LoadUser(storer Storer) error {
func (c *Context) LoadUser(key string, storer Storer) error {
if c.User != nil {
return nil
}
intf, err := storer.Get(key, moduleAttrMeta)
if err != nil {
return err
}
c.User = Unbind(intf)
return nil
}

View File

@@ -2,6 +2,8 @@ package authboss
var modules = make(map[string]Modularizer)
var moduleAttrMeta = make(AttributeMeta)
// Modularizer should be implemented by all the authboss modules.
type Modularizer interface {
Initialize(*Config) error
@@ -13,6 +15,10 @@ type Modularizer interface {
// integrate into authboss.
func RegisterModule(name string, m Modularizer) {
modules[name] = m
for k, v := range m.Storage() {
moduleAttrMeta[k] = v
}
}
// LoadedModules returns a list of modules that are currently loaded.

View File

@@ -68,18 +68,17 @@ func (r *Remember) Storage() authboss.StorageOptions {
// AfterAuth is called after authentication is successful.
func (r *Remember) AfterAuth(ctx *authboss.Context) {
if val, ok := ctx.Get(ValueKey); ok && val != "true" {
if val, ok := ctx.FormValue(ValueKey); !ok || val[0] != "true" {
return
}
if err := ctx.LoadUser(r.storer); err != nil {
fmt.Fprintln(r.logger, "remember: Failed to load user:", err)
return
if ctx.User == nil {
fmt.Fprintf(r.logger, "remember: AfterAuth no user loaded")
}
key := ctx.User["Username"].(string)
key := ctx.User["username"].(string)
if _, err := r.New(ctx.CookieStorer, key); err != nil {
fmt.Fprintf(r.logger, "Failed to create remember token: %v", err)
fmt.Fprintf(r.logger, "remember: Failed to create remember token: %v", err)
}
}

View File

@@ -1,6 +1,8 @@
package remember
import (
"bytes"
"net/http"
"testing"
"gopkg.in/authboss.v0"
@@ -75,11 +77,31 @@ func TestInitialize(t *testing.T) {
}
func TestAfterAuth(t *testing.T) {
// TODO(aarondl): This
storer := &testTokenStorer{}
R.storer = storer
cookies := make(testClientStorer)
session := make(testClientStorer)
req, err := http.NewRequest("POST", "http://localhost", bytes.NewBufferString("rm=true"))
if err != nil {
t.Error("Unexpected Error:", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
ctx, err := authboss.ContextFromRequest(req)
if err != nil {
t.Error("Unexpected error:", err)
}
/*ctx := authboss.NewContext()
ctx.SessionStorer = session
ctx.CookieStorer = cookies*/
ctx.CookieStorer = cookies
ctx.User = authboss.Attributes{"username": "testuser"}
R.AfterAuth(ctx)
if _, ok := cookies[ValueKey]; !ok {
t.Error("Expected a cookie to have been set.")
}
}
func TestNew(t *testing.T) {

View File

@@ -19,7 +19,7 @@ func NewRouter(config *Config) http.Handler {
for name, mod := range modules {
for route, handler := range mod.Routes() {
fmt.Fprintf(logger, "[%-10s] Register Route: %s\n", name, route)
mux.Handle(path.Join(config.MountPath, route), contextRoute{handler})
mux.Handle(path.Join(config.MountPath, route), contextRoute{handler, config})
}
}
@@ -27,11 +27,19 @@ func NewRouter(config *Config) http.Handler {
}
type contextRoute struct {
fn HandlerFunc
fn HandlerFunc
config *Config
}
func (c contextRoute) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := NewContext()
ctx, err := ContextFromRequest(r)
if err != nil {
fmt.Fprintf(c.config.LogWriter, "route: Malformed request, could not create context: %v", err)
return
}
ctx.CookieStorer = c.config.CookieStoreMaker(r)
ctx.SessionStorer = c.config.SessionStoreMaker(r)
c.fn(ctx, w, r)
}