1
0
mirror of https://github.com/volatiletech/authboss.git synced 2025-01-24 05:17:10 +02:00
authboss/config.go

167 lines
5.7 KiB
Go
Raw Normal View History

package authboss
import (
"html/template"
"io"
"io/ioutil"
"net/http"
"time"
2015-01-30 15:38:28 -08:00
"golang.org/x/crypto/bcrypt"
2015-02-08 23:08:33 -08:00
)
// Config holds all the configuration for both authboss and it's modules.
type Config struct {
2015-02-22 13:16:11 -08:00
// MountPath is the path to mount authboss's routes at (eg /auth).
MountPath string
2015-02-22 13:16:11 -08:00
// ViewsPath is the path to search for overridden templates.
ViewsPath string
2015-03-12 19:20:15 -07:00
// RootURL is the scheme+host+port of the web application (eg https://www.happiness.com:8080) for url generation. No trailing slash.
RootURL string
2015-02-22 13:16:11 -08:00
// BCryptCost is the cost of the bcrypt password hashing function.
2015-01-30 15:38:28 -08:00
BCryptCost int
2015-02-22 13:16:11 -08:00
// PrimaryID is the primary identifier of the user. Set to one of:
// authboss.StoreEmail, authboss.StoreUsername (StoreEmail is default)
PrimaryID string
2015-03-15 11:25:01 -07:00
// Layout that all authboss views will be inserted into.
Layout *template.Template
// LayoutHTMLEmail is for emails going out in HTML form, authbosses e-mail templates
// will be inserted into this layout.
LayoutHTMLEmail *template.Template
2015-03-15 11:25:01 -07:00
// LayoutTextEmail is for emails going out in text form, authbosses e-mail templates
// will be inserted into this layout.
LayoutTextEmail *template.Template
2015-03-15 11:25:01 -07:00
// LayoutDataMaker is a function that can provide authboss with the layout's
// template data. It will be merged with the data being provided for the current
// view in order to render the templates.
LayoutDataMaker ViewDataMaker
2015-03-15 11:25:01 -07:00
// OAuth2Providers lists all providers that can be used. See
// OAuthProvider documentation for more details.
OAuth2Providers map[string]OAuth2Provider
// ErrorHandler handles would be 500 errors.
ErrorHandler http.Handler
// BadRequestHandler handles would be 400 errors.
BadRequestHandler http.Handler
// NotFoundHandler handles would be 404 errors.
NotFoundHandler http.Handler
2015-03-15 11:25:01 -07:00
// AuthLoginOKPath is the redirect path after a successful authentication.
AuthLoginOKPath string
// AuthLoginFailPath is the redirect path after a failed authentication.
AuthLoginFailPath string
2015-03-15 11:25:01 -07:00
// AuthLogoutOKPath is the redirect path after a log out.
AuthLogoutOKPath string
2015-03-15 11:25:01 -07:00
// RecoverOKPath is the redirect path after a successful recovery of a password.
RecoverOKPath string
// RecoverTokenDuration controls how long a token sent via email for password
// recovery is valid for.
RecoverTokenDuration time.Duration
2015-01-17 23:37:05 -08:00
2015-03-15 11:25:01 -07:00
// RegisterOKPath is the redirect path after a successful registration.
RegisterOKPath string
// Policies control validation of form fields and are automatically run
// against form posts that include the fields.
Policies []Validator
// ConfirmFields are fields that are supposed to be submitted with confirmation
// fields alongside them, passwords, emails etc.
ConfirmFields []string
// PreserveFields are fields used with registration that are to be rendered when
// post fails.
PreserveFields []string
2015-01-25 18:13:32 -08:00
// ExpireAfter controls the time an account is idle before being logged out
// by the ExpireMiddleware.
2015-02-22 13:16:11 -08:00
ExpireAfter time.Duration
// LockAfter this many tries.
LockAfter int
// LockWindow is the waiting time before the number of attemps are reset.
LockWindow time.Duration
// LockDuration is how long an account is locked for.
LockDuration time.Duration
// EmailFrom is the email address authboss e-mails come from.
EmailFrom string
// EmailSubjectPrefix is used to add something to the front of the authboss
// email subjects.
EmailSubjectPrefix string
// XSRFName is the name of the xsrf token to put in the hidden form fields.
XSRFName string
// XSRFMaker is a function that returns an xsrf token for the current non-POST request.
2015-01-27 17:34:55 -08:00
XSRFMaker XSRF
2015-01-25 15:40:57 -08:00
// Storer is the interface through which Authboss accesses the web apps database.
Storer Storer
// OAuth2Storer is a different kind of storer only meant for OAuth2.
OAuth2Storer OAuth2Storer
// CookieStoreMaker must be defined to provide an interface capapable of storing cookies
// for the given response, and reading them from the request.
CookieStoreMaker CookieStoreMaker
// SessionStoreMaker must be defined to provide an interface capable of storing session-only
// values for the given response, and reading them from the request.
SessionStoreMaker SessionStoreMaker
// LogWriter is written to when errors occur, as well as on startup to show which modules are loaded
// and which routes they registered. By default writes to io.Discard.
LogWriter io.Writer
// Mailer is the mailer being used to send e-mails out. Authboss defines two loggers for use
// LogMailer and SMTPMailer, the default is a LogMailer to io.Discard.
Mailer Mailer
}
// Defaults sets the configuration's default values.
func (c *Config) Defaults() {
c.MountPath = "/"
c.ViewsPath = "./"
c.RootURL = "http://localhost:8080"
c.BCryptCost = bcrypt.DefaultCost
c.PrimaryID = StoreEmail
c.Layout = template.Must(template.New("").Parse(`<!DOCTYPE html><html><body>{{template "authboss" .}}</body></html>`))
c.LayoutHTMLEmail = template.Must(template.New("").Parse(`<!DOCTYPE html><html><body>{{template "authboss" .}}</body></html>`))
c.LayoutTextEmail = template.Must(template.New("").Parse(`{{template "authboss" .}}`))
c.AuthLoginOKPath = "/"
c.AuthLoginFailPath = "/"
c.AuthLogoutOKPath = "/"
c.RecoverOKPath = "/"
c.RecoverTokenDuration = time.Duration(24) * time.Hour
c.RegisterOKPath = "/"
c.Policies = []Validator{
Rules{
FieldName: "email",
Required: true,
AllowWhitespace: false,
2015-01-30 15:38:28 -08:00
},
Rules{
FieldName: "password",
Required: true,
MinLength: 4,
MaxLength: 8,
AllowWhitespace: false,
2015-02-22 13:16:11 -08:00
},
}
c.ConfirmFields = []string{
StorePassword, ConfirmPrefix + StorePassword,
}
2015-01-30 15:38:28 -08:00
c.ExpireAfter = 60 * time.Minute
c.LockAfter = 3
c.LockWindow = 5 * time.Minute
c.LockDuration = 5 * time.Hour
c.LogWriter = NewDefaultLogger()
c.Mailer = LogMailer(ioutil.Discard)
}