Skip to content

Commit 0754b96

Browse files
committed
Update config documentation.
- Fix aarondl#47: Remove ModuleAttrMeta from Storers. Rename to ModuleAttributes. - Add some additional deafult values to config.
1 parent f93fb38 commit 0754b96

10 files changed

Lines changed: 73 additions & 35 deletions

File tree

authboss.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ func CurrentUser(w http.ResponseWriter, r *http.Request) (interface{}, error) {
5656
}
5757

5858
if index := strings.IndexByte(key, ';'); index > 0 {
59-
return Cfg.OAuth2Storer.GetOAuth(key[:index], key[index+1:], ModuleAttrMeta)
60-
} else {
61-
return Cfg.Storer.Get(key, ModuleAttrMeta)
59+
return Cfg.OAuth2Storer.GetOAuth(key[:index], key[index+1:])
6260
}
61+
62+
return Cfg.Storer.Get(key)
6363
}
6464

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

callbacks.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ type Callbacks struct {
8181
after map[Event][]After
8282
}
8383

84+
// NewCallbacks creates a new set of before and after callbacks.
8485
func NewCallbacks() *Callbacks {
8586
return &Callbacks{
8687
make(map[Event][]Before),

config.go

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
)
1313

1414
// Cfg is the singleton instance of Config
15-
var Cfg *Config = NewConfig()
15+
var Cfg = NewConfig()
1616

1717
// Config holds all the configuration for both authboss and it's modules.
1818
type Config struct {
@@ -50,36 +50,66 @@ type Config struct {
5050
RecoverOKPath string
5151
RecoverTokenDuration time.Duration
5252

53-
Policies []Validator
53+
// Policies control validation of form fields and are automatically run
54+
// against form posts that include the fields.
55+
Policies []Validator
56+
// ConfirmFields are fields that are supposed to be submitted with confirmation
57+
// fields alongside them, passwords, emails etc.
5458
ConfirmFields []string
5559

60+
// ExpireAfter controls the time an account is idle before being logged out
61+
// by the ExpireMiddleware.
5662
ExpireAfter time.Duration
5763

58-
LockAfter int
59-
LockWindow time.Duration
64+
// LockAfter this many tries.
65+
LockAfter int
66+
// LockWindow is the waiting time before the number of attemps are reset.
67+
LockWindow time.Duration
68+
// LockDuration is how long an account is locked for.
6069
LockDuration time.Duration
6170

62-
EmailFrom string
71+
// EmailFrom is the email address authboss e-mails come from.
72+
EmailFrom string
73+
// EmailSubjectPrefix is used to add something to the front of the authboss
74+
// email subjects.
6375
EmailSubjectPrefix string
64-
SMTPAddress string
65-
SMTPAuth smtp.Auth
66-
67-
XSRFName string
76+
// SMTPAddress is the address of the SMTP server.
77+
SMTPAddress string
78+
// SMTPAuth is authentication details for the SMTP server, can be nil and if not
79+
// will repeat the SMTPAddress, this is intentional.
80+
SMTPAuth smtp.Auth
81+
82+
// XSRFName is the name of the xsrf token to put in the hidden form fields.
83+
XSRFName string
84+
// XSRFMaker is a function that returns an xsrf token for the current non-POST request.
6885
XSRFMaker XSRF
6986

70-
Storer Storer
71-
OAuth2Storer OAuth2Storer
72-
CookieStoreMaker CookieStoreMaker
87+
// Storer is the interface through which Authboss accesses the web apps database.
88+
Storer Storer
89+
// OAuth2Storer is a different kind of storer only meant for OAuth2.
90+
OAuth2Storer OAuth2Storer
91+
// CookieStoreMaker must be defined to provide an interface capapable of storing cookies
92+
// for the given response, and reading them from the request.
93+
CookieStoreMaker CookieStoreMaker
94+
// SessionStoreMaker must be defined to provide an interface capable of storing session-only
95+
// values for the given response, and reading them from the request.
7396
SessionStoreMaker SessionStoreMaker
74-
LogWriter io.Writer
75-
Callbacks *Callbacks
76-
Mailer Mailer
97+
// LogWriter is written to when errors occur, as well as on startup to show which modules are loaded
98+
// and which routes they registered. By default writes to io.Discard.
99+
LogWriter io.Writer
100+
// Callbacks is an internal mechanism that can be used by implementers and will be set automatically.
101+
Callbacks *Callbacks
102+
// Mailer is the mailer being used to send e-mails out. Authboss defines two loggers for use
103+
// LogMailer and SMTPMailer, the default is a LogMailer to io.Discard.
104+
Mailer Mailer
77105
}
78106

107+
// NewConfig creates a config full of healthy default values.
108+
// Notable exceptions to default values are the Storers.
79109
func NewConfig() *Config {
80110
return &Config{
81111
MountPath: "/",
82-
ViewsPath: "/",
112+
ViewsPath: "./",
83113
RootURL: "http://localhost:8080",
84114
BCryptCost: bcrypt.DefaultCost,
85115

@@ -116,6 +146,10 @@ func NewConfig() *Config {
116146

117147
ExpireAfter: 60 * time.Minute,
118148

149+
LockAfter: 3,
150+
LockWindow: 5 * time.Minute,
151+
LockDuration: 5 * time.Hour,
152+
119153
RecoverOKPath: "/",
120154
RecoverTokenDuration: time.Duration(24) * time.Hour,
121155

context.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ func (c *Context) LoadUser(key string) error {
109109
var err error
110110

111111
if index := strings.IndexByte(key, ';'); index > 0 {
112-
user, err = Cfg.OAuth2Storer.GetOAuth(key[:index], key[index+1:], ModuleAttrMeta)
112+
user, err = Cfg.OAuth2Storer.GetOAuth(key[:index], key[index+1:])
113113
} else {
114-
user, err = Cfg.Storer.Get(key, ModuleAttrMeta)
114+
user, err = Cfg.Storer.Get(key)
115115
}
116116
if err != nil {
117117
return err

internal/mocks/mocks.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func (m *MockStorer) Put(key string, attr authboss.Attributes) error {
7373
return nil
7474
}
7575

76-
func (m *MockStorer) Get(key string, attrMeta authboss.AttributeMeta) (result interface{}, err error) {
76+
func (m *MockStorer) Get(key string) (result interface{}, err error) {
7777
if len(m.GetErr) > 0 {
7878
return nil, errors.New(m.GetErr)
7979
}
@@ -106,7 +106,7 @@ func (m *MockStorer) PutOAuth(uid, provider string, attr authboss.Attributes) er
106106
return nil
107107
}
108108

109-
func (m *MockStorer) GetOAuth(uid, provider string, attrMeta authboss.AttributeMeta) (result interface{}, err error) {
109+
func (m *MockStorer) GetOAuth(uid, provider string) (result interface{}, err error) {
110110
if len(m.GetErr) > 0 {
111111
return nil, errors.New(m.GetErr)
112112
}
@@ -217,7 +217,7 @@ func (_ MockFailStorer) Create(_ string, _ authboss.Attributes) error {
217217
func (_ MockFailStorer) Put(_ string, _ authboss.Attributes) error {
218218
return errors.New("fail storer: put")
219219
}
220-
func (_ MockFailStorer) Get(_ string, _ authboss.AttributeMeta) (interface{}, error) {
220+
func (_ MockFailStorer) Get(_ string) (interface{}, error) {
221221
return nil, errors.New("fail storer: get")
222222
}
223223

lock/lock.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func (l *Lock) AfterAuthFail(ctx *authboss.Context) error {
118118

119119
// Lock a user manually.
120120
func (l *Lock) Lock(key string) error {
121-
user, err := authboss.Cfg.Storer.Get(key, authboss.ModuleAttrMeta)
121+
user, err := authboss.Cfg.Storer.Get(key)
122122
if err != nil {
123123
return err
124124
}
@@ -128,14 +128,14 @@ func (l *Lock) Lock(key string) error {
128128
return err
129129
}
130130

131-
attr[StoreLocked] = true
131+
attr[StoreLocked] = time.Now().UTC().Add(authboss.Cfg.LockDuration)
132132

133133
return authboss.Cfg.Storer.Put(key, attr)
134134
}
135135

136136
// Unlock a user that was locked by this module.
137137
func (l *Lock) Unlock(key string) error {
138-
user, err := authboss.Cfg.Storer.Get(key, authboss.ModuleAttrMeta)
138+
user, err := authboss.Cfg.Storer.Get(key)
139139
if err != nil {
140140
return err
141141
}

mocks_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func (m mockStorer) Put(key string, attr Attributes) error {
2323
return nil
2424
}
2525

26-
func (m mockStorer) Get(key string, attrMeta AttributeMeta) (result interface{}, err error) {
26+
func (m mockStorer) Get(key string) (result interface{}, err error) {
2727
return &mockUser{
2828
m[key]["email"].(string), m[key]["password"].(string),
2929
}, nil
@@ -34,7 +34,7 @@ func (m mockStorer) PutOAuth(uid, provider string, attr Attributes) error {
3434
return nil
3535
}
3636

37-
func (m mockStorer) GetOAuth(uid, provider string, attrMeta AttributeMeta) (result interface{}, err error) {
37+
func (m mockStorer) GetOAuth(uid, provider string) (result interface{}, err error) {
3838
return &mockUser{
3939
m[uid+provider]["email"].(string), m[uid+provider]["password"].(string),
4040
}, nil

module.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ package authboss
22

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

5-
var ModuleAttrMeta = make(AttributeMeta)
5+
// ModuleAttributes is the list of attributes required by all the loaded modules.
6+
// Authboss implementers can use this at runtime to determine what data is necessary
7+
// to store.
8+
var ModuleAttributes = make(AttributeMeta)
69

710
// Modularizer should be implemented by all the authboss modules.
811
type Modularizer interface {
@@ -17,7 +20,7 @@ func RegisterModule(name string, m Modularizer) {
1720
modules[name] = m
1821

1922
for k, v := range m.Storage() {
20-
ModuleAttrMeta[k] = v
23+
ModuleAttributes[k] = v
2124
}
2225
}
2326

register/register_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func TestRegisterPostValidationErrs(t *testing.T) {
112112
t.Error("Confirm password should have an error:", str)
113113
}
114114

115-
if _, err := authboss.Cfg.Storer.Get(email, authboss.AttributeMeta(reg.Storage())); err != authboss.ErrUserNotFound {
115+
if _, err := authboss.Cfg.Storer.Get(email); err != authboss.ErrUserNotFound {
116116
t.Error("The user should not have been saved.")
117117
}
118118
}
@@ -145,7 +145,7 @@ func TestRegisterPostSuccess(t *testing.T) {
145145
t.Error("Redirected to the wrong location", loc)
146146
}
147147

148-
user, err := authboss.Cfg.Storer.Get(email, authboss.AttributeMeta(reg.Storage()))
148+
user, err := authboss.Cfg.Storer.Get(email)
149149
if err == authboss.ErrUserNotFound {
150150
t.Error("The user have been saved.")
151151
}

storer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ type Storer interface {
4848
// must be a struct that contains all fields with the correct types as shown
4949
// by attrMeta. If the key is not found in the data store simply
5050
// return nil, ErrUserNotFound.
51-
Get(key string, attrMeta AttributeMeta) (interface{}, error)
51+
Get(key string) (interface{}, error)
5252
}
5353

5454
// OAuth2Storer is a replacement (or addition) to the Storer interface.
@@ -57,7 +57,7 @@ type OAuth2Storer interface {
5757
// PutOAuth creates or updates an existing record (unlike Storer.Put)
5858
// because in the OAuth flow there is no separate create/update.
5959
PutOAuth(uid, provider string, attr Attributes) error
60-
GetOAuth(uid, provider string, attrMeta AttributeMeta) (interface{}, error)
60+
GetOAuth(uid, provider string) (interface{}, error)
6161
}
6262

6363
// DataType represents the various types that clients must be able to store.

0 commit comments

Comments
 (0)