1
0
mirror of https://github.com/volatiletech/authboss.git synced 2025-02-03 13:21:22 +02:00

Refactor storers into modules.

This commit is contained in:
Aaron 2015-02-24 11:04:27 -08:00
parent 468113a398
commit c723f46a3e
5 changed files with 45 additions and 46 deletions

View File

@ -28,6 +28,16 @@ var (
errUserMissing = errors.New("confirm: After registration user must be loaded")
)
// ConfirmStorer must be implemented in order to satisfy the confirm module's
// storage requirements.
type ConfirmStorer interface {
authboss.Storer
// ConfirmUser looks up a user by a confirm token. See confirm module for
// attribute names. If the token is not found in the data store,
// simply return nil, ErrUserNotFound.
ConfirmUser(confirmToken string) (interface{}, error)
}
// C is the singleton instance of the confirm module which will have been
// configured and ready to use after authboss.Init()
var C *Confirm
@ -43,7 +53,7 @@ type Confirm struct {
func (c *Confirm) Initialize() (err error) {
var ok bool
storer, ok := authboss.Cfg.Storer.(authboss.ConfirmStorer)
storer, ok := authboss.Cfg.Storer.(ConfirmStorer)
if storer == nil || !ok {
return errors.New("confirm: Need a ConfirmStorer.")
}
@ -146,7 +156,7 @@ func (c *Confirm) confirmHandler(ctx *authboss.Context, w http.ResponseWriter, r
sum := md5.Sum(toHash)
dbTok := base64.StdEncoding.EncodeToString(sum[:])
user, err := authboss.Cfg.Storer.(authboss.ConfirmStorer).ConfirmUser(dbTok)
user, err := authboss.Cfg.Storer.(ConfirmStorer).ConfirmUser(dbTok)
if err == authboss.ErrUserNotFound {
return authboss.ErrAndRedirect{Endpoint: "/", Err: errors.New("confirm: token not found")}
} else if err != nil {

View File

@ -220,7 +220,7 @@ func TestConfirm_Confirm(t *testing.T) {
if key, ok := ctx.SessionStorer.Get(authboss.SessionKey); !ok || len(key) == 0 {
t.Error("Should have logged the user in.")
}
if success, ok := ctx.CookieStorer.Get(authboss.FlashSuccessKey); !ok || len(success) == 0 {
if success, ok := ctx.SessionStorer.Get(authboss.FlashSuccessKey); !ok || len(success) == 0 {
t.Error("Should have left a nice message.")
}
}

View File

@ -30,6 +30,16 @@ const (
var errRecoveryTokenExpired = errors.New("recovery token expired")
// RecoverStorer must be implemented in order to satisfy the recover module's
// storage requirements.
type RecoverStorer interface {
authboss.Storer
// RecoverUser looks a user up by a recover token. See recover module for
// attribute names. If the key is not found in the data store,
// simply return nil, ErrUserNotFound.
RecoverUser(recoverToken string) (interface{}, error)
}
func init() {
m := &Recover{}
authboss.RegisterModule("recover", m)
@ -45,7 +55,7 @@ func (r *Recover) Initialize() (err error) {
return errors.New("recover: Need a RecoverStorer.")
}
if _, ok := authboss.Cfg.Storer.(authboss.RecoverStorer); !ok {
if _, ok := authboss.Cfg.Storer.(RecoverStorer); !ok {
return errors.New("recover: RecoverStorer required for recover functionality.")
}
@ -247,7 +257,7 @@ func verifyToken(ctx *authboss.Context) (attrs authboss.Attributes, err error) {
}
sum := md5.Sum(decoded)
storer := authboss.Cfg.Storer.(authboss.RecoverStorer)
storer := authboss.Cfg.Storer.(RecoverStorer)
userInter, err := storer.RecoverUser(base64.StdEncoding.EncodeToString(sum[:]))
if err != nil {

View File

@ -17,13 +17,28 @@ import (
const (
// RememberKey is used for cookies and form input names.
RememberKey = "rm"
nRandBytes = 32
)
var (
errUserMissing = errors.New("remember: User not loaded in callback")
)
const nRandBytes = 32
// TokenStorer must be implemented in order to satisfy the remember module's
// storage requirements. If the implementer is a typical database then
// the tokens should be stored in a separate table since they require a 1-n
// with the user for each device the user wishes to remain logged in on.
type TokenStorer interface {
authboss.Storer
// AddToken saves a new token for the key.
AddToken(key, token string) error
// DelTokens removes all tokens for a given key.
DelTokens(key string) error
// UseToken finds the key-token pair, removes the entry in the store
// and returns the key that was found. If the token could not be found
// return "", ErrTokenNotFound
UseToken(givenKey, token string) (key string, err error)
}
// R is the singleton instance of the remember module which will have been
// configured and ready to use after authboss.Init()
@ -41,7 +56,7 @@ func (r *Remember) Initialize() error {
return errors.New("remember: Need a TokenStorer")
}
if _, ok := authboss.Cfg.Storer.(authboss.TokenStorer); !ok {
if _, ok := authboss.Cfg.Storer.(TokenStorer); !ok {
return errors.New("remember: TokenStorer required for remember me functionality")
}
@ -97,7 +112,7 @@ func (r *Remember) New(cstorer authboss.ClientStorer, storageKey string) (string
storageToken := base64.StdEncoding.EncodeToString(sum[:])
// Save the token in the DB
if err := authboss.Cfg.Storer.(authboss.TokenStorer).AddToken(storageKey, storageToken); err != nil {
if err := authboss.Cfg.Storer.(TokenStorer).AddToken(storageKey, storageToken); err != nil {
return "", err
}
@ -132,7 +147,7 @@ func (r *Remember) Auth(
// Verify the tokens match.
sum := md5.Sum(token)
key, err := authboss.Cfg.Storer.(authboss.TokenStorer).UseToken(string(givenKey), base64.StdEncoding.EncodeToString(sum[:]))
key, err := authboss.Cfg.Storer.(TokenStorer).UseToken(string(givenKey), base64.StdEncoding.EncodeToString(sum[:]))
if err == authboss.ErrTokenNotFound {
return "", nil
} else if err != nil {

View File

@ -36,48 +36,12 @@ type Storer interface {
// help serialization without using type assertions.
Put(key string, attr Attributes) error
// Get is for retrieving attributes for a given key. The return value
// must be a struct that contains a field with the correct type as shown
// must be a struct that contains all fields with the correct types as shown
// by attrMeta. If the key is not found in the data store simply
// return nil, ErrUserNotFound.
Get(key string, attrMeta AttributeMeta) (interface{}, error)
}
// TokenStorer must be implemented in order to satisfy the remember module's
// storage requirements. If the implementer is a typical database then
// the tokens should be stored in a separate table since they require a 1-n
// with the user for each device the user wishes to remain logged in on.
type TokenStorer interface {
Storer
// AddToken saves a new token for the key.
AddToken(key, token string) error
// DelTokens removes all tokens for a given key.
DelTokens(key string) error
// UseToken finds the key-token pair, removes the entry in the store
// and returns the key that was found. If the token could not be found
// return "", ErrTokenNotFound
UseToken(givenKey, token string) (key string, err error)
}
// RecoverStorer must be implemented in order to satisfy the recover module's
// storage requirements.
type RecoverStorer interface {
Storer
// RecoverUser looks a user up by a recover token. See recover module for
// attribute names. If the key is not found in the data store,
// simply return nil, ErrUserNotFound.
RecoverUser(recoverToken string) (interface{}, error)
}
// ConfirmStorer must be implemented in order to satisfy the confirm module's
// storage requirements.
type ConfirmStorer interface {
Storer
// ConfirmUser looks up a user by a confirm token. See confirm module for
// attribute names. If the token is not found in the data store,
// simply return nil, ErrUserNotFound.
ConfirmUser(confirmToken string) (interface{}, error)
}
// DataType represents the various types that clients must be able to store.
type DataType int