1
0
mirror of https://github.com/volatiletech/authboss.git synced 2024-11-24 08:42:17 +02:00

Fix up expire module.

This commit is contained in:
Aaron 2015-02-22 00:24:57 -08:00
parent 94f441f3d7
commit 681bfdeac0
4 changed files with 30 additions and 24 deletions

View File

@ -44,11 +44,14 @@ const (
// InterruptAccountNotConfirmed occurs if a user's account is not confirmed
// and therefore cannot be used yet.
InterruptAccountNotConfirmed
// InterruptSessionExpired occurs when the user's account has had no activity for the
// configured duration.
InterruptSessionExpired
)
const interruptNames = "InterruptNoneInterruptAccountLockedInterruptAccountNotConfirmed"
const interruptNames = "InterruptNoneInterruptAccountLockedInterruptAccountNotConfirmedInterruptSessionExpired"
var interruptIndexes = [...]uint8{0, 13, 35, 63}
var interruptIndexes = [...]uint8{0, 13, 35, 63, 86}
func (i Interrupt) String() string {
if i < 0 || i+1 >= Interrupt(len(interruptIndexes)) {

View File

@ -181,6 +181,7 @@ func TestInterruptString(t *testing.T) {
{InterruptNone, "InterruptNone"},
{InterruptAccountLocked, "InterruptAccountLocked"},
{InterruptAccountNotConfirmed, "InterruptAccountNotConfirmed"},
{InterruptSessionExpired, "InterruptSessionExpired"},
}
for i, test := range tests {

View File

@ -12,8 +12,8 @@ import (
)
const (
// UserLastAction is the session key to retrieve the last action of a user.
UserLastAction = "last_action"
// StoreLastAction is the session key to retrieve the last action of a user.
StoreLastAction = "last_action"
)
var (
@ -32,7 +32,7 @@ func init() {
type Expire struct{}
func (e *Expire) Initialize() error {
authboss.Cfg.Callbacks.Before(authboss.EventGet, e.BeforeAuth)
authboss.Cfg.Callbacks.Before(authboss.EventGet, e.BeforeGet)
return nil
}
@ -40,28 +40,28 @@ func (e *Expire) Initialize() error {
func (_ *Expire) Routes() authboss.RouteTable { return nil }
func (_ *Expire) Storage() authboss.StorageOptions { return nil }
// BeforeAuth ensures the account is not locked.
func (e *Expire) BeforeAuth(ctx *authboss.Context) error {
// BeforeGet ensures the account is not expired.
func (e *Expire) BeforeGet(ctx *authboss.Context) (authboss.Interrupt, error) {
if _, ok := ctx.SessionStorer.Get(authboss.SessionKey); !ok {
return nil
return authboss.InterruptNone, nil
}
dateStr, ok := ctx.SessionStorer.Get(UserLastAction)
dateStr, ok := ctx.SessionStorer.Get(StoreLastAction)
if ok {
if date, err := time.Parse(time.RFC3339, dateStr); err != nil {
Touch(ctx.SessionStorer)
} else if time.Now().UTC().After(date.Add(authboss.Cfg.ExpireAfter)) {
ctx.SessionStorer.Del(authboss.SessionKey)
return ErrExpired
return authboss.InterruptSessionExpired, nil
}
}
return nil
return authboss.InterruptNone, nil
}
// Touch updates the last action for the user, so he doesn't become expired.
func Touch(session authboss.ClientStorer) {
session.Put(UserLastAction, time.Now().UTC().Format(time.RFC3339))
session.Put(StoreLastAction, time.Now().UTC().Format(time.RFC3339))
}
type middleware struct {

View File

@ -13,11 +13,11 @@ func TestExpire_Touch(t *testing.T) {
authboss.NewConfig()
session := mocks.NewMockClientStorer()
if _, ok := session.Get(UserLastAction); ok {
if _, ok := session.Get(StoreLastAction); ok {
t.Error("It should not have been set")
}
Touch(session)
if dateStr, ok := session.Get(UserLastAction); !ok || len(dateStr) == 0 {
if dateStr, ok := session.Get(StoreLastAction); !ok || len(dateStr) == 0 {
t.Error("It should have been set")
} else if date, err := time.Parse(time.RFC3339, dateStr); err != nil {
t.Error("Date is malformed:", dateStr)
@ -26,7 +26,7 @@ func TestExpire_Touch(t *testing.T) {
}
}
func TestExpire_BeforeAuth(t *testing.T) {
func TestExpire_BeforeGet(t *testing.T) {
authboss.NewConfig()
authboss.Cfg.ExpireAfter = time.Hour
expire := &Expire{}
@ -35,16 +35,16 @@ func TestExpire_BeforeAuth(t *testing.T) {
ctx := mocks.MockRequestContext()
ctx.SessionStorer = session
if err := expire.BeforeAuth(ctx); err != nil {
if interrupted, err := expire.BeforeGet(ctx); err != nil || interrupted != authboss.InterruptNone {
t.Error("There's no user in session, should be no-op.")
}
session.Values[authboss.SessionKey] = "moo"
session.Values[UserLastAction] = "cow"
if err := expire.BeforeAuth(ctx); err != nil {
t.Error("There's a malformed date, this should not error, just fix it:", err)
session.Values[StoreLastAction] = "cow"
if interrupted, err := expire.BeforeGet(ctx); err != nil || interrupted != authboss.InterruptNone {
t.Error("There's a malformed date, this should not error, just fix it:", err, interrupted)
}
if dateStr, ok := session.Get(UserLastAction); !ok || len(dateStr) == 0 {
if dateStr, ok := session.Get(StoreLastAction); !ok || len(dateStr) == 0 {
t.Error("It should have been set")
} else if date, err := time.Parse(time.RFC3339, dateStr); err != nil {
t.Error("Date is malformed:", dateStr)
@ -52,9 +52,11 @@ func TestExpire_BeforeAuth(t *testing.T) {
t.Error("The time is set in the future.")
}
session.Values[UserLastAction] = time.Now().UTC().Add(-2 * time.Hour).Format(time.RFC3339)
if err := expire.BeforeAuth(ctx); err != ErrExpired {
t.Error("The user should have been expired, got:", err)
session.Values[StoreLastAction] = time.Now().UTC().Add(-2 * time.Hour).Format(time.RFC3339)
if interrupted, err := expire.BeforeGet(ctx); err != nil {
t.Error(err)
} else if interrupted != authboss.InterruptSessionExpired {
t.Error("Expected a session expired interrupt:", interrupted)
}
if _, ok := session.Values[authboss.SessionKey]; ok {
@ -84,7 +86,7 @@ func TestExpire_Middleware(t *testing.T) {
t.Error("Expected middleware's chain to be called.")
}
if dateStr, ok := session.Get(UserLastAction); !ok || len(dateStr) == 0 {
if dateStr, ok := session.Get(StoreLastAction); !ok || len(dateStr) == 0 {
t.Error("It should have been set")
} else if date, err := time.Parse(time.RFC3339, dateStr); err != nil {
t.Error("Date is malformed:", dateStr)