2015-01-03 22:03:57 +02:00
|
|
|
package authboss
|
|
|
|
|
2015-01-05 10:18:41 +02:00
|
|
|
import (
|
2018-09-13 04:58:16 +02:00
|
|
|
"net/http"
|
2015-01-24 02:25:12 +02:00
|
|
|
"time"
|
2018-02-05 09:28:31 +02:00
|
|
|
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
2015-02-09 09:08:33 +02:00
|
|
|
)
|
|
|
|
|
2015-01-05 10:18:41 +02:00
|
|
|
// Config holds all the configuration for both authboss and it's modules.
|
2015-01-04 20:33:53 +02:00
|
|
|
type Config struct {
|
2018-02-02 01:42:48 +02:00
|
|
|
Paths struct {
|
|
|
|
// Mount is the path to mount authboss's routes at (eg /auth).
|
|
|
|
Mount string
|
2018-09-13 04:44:34 +02:00
|
|
|
|
2018-07-17 16:09:38 +02:00
|
|
|
// NotAuthorized is the default URL to kick users back to when
|
2018-09-16 00:39:26 +02:00
|
|
|
// they attempt an action that requires them to be logged in and
|
|
|
|
// they're not auth'd
|
2018-07-17 16:09:38 +02:00
|
|
|
NotAuthorized string
|
2018-02-02 01:42:48 +02:00
|
|
|
|
|
|
|
// AuthLoginOK is the redirect path after a successful authentication.
|
|
|
|
AuthLoginOK string
|
|
|
|
|
2018-09-16 00:39:26 +02:00
|
|
|
// ConfirmOK once a user has confirmed their account
|
|
|
|
// this says where they should go
|
2018-02-27 17:14:30 +02:00
|
|
|
ConfirmOK string
|
2018-09-16 00:39:26 +02:00
|
|
|
// ConfirmNotOK is used by the middleware, when a user is still supposed
|
|
|
|
// to confirm their account, this is where they should be redirected to.
|
2018-02-27 17:14:30 +02:00
|
|
|
ConfirmNotOK string
|
|
|
|
|
2018-02-28 07:20:55 +02:00
|
|
|
// LockNotOK is a path to go to when the user fails
|
|
|
|
LockNotOK string
|
|
|
|
|
2018-03-07 21:41:14 +02:00
|
|
|
// LogoutOK is the redirect path after a log out.
|
|
|
|
LogoutOK string
|
|
|
|
|
2018-03-09 04:39:51 +02:00
|
|
|
// OAuth2LoginOK is the redirect path after a successful oauth2 login
|
|
|
|
OAuth2LoginOK string
|
2018-09-16 00:39:26 +02:00
|
|
|
// OAuth2LoginNotOK is the redirect path after
|
|
|
|
// an unsuccessful oauth2 login
|
2018-03-09 04:39:51 +02:00
|
|
|
OAuth2LoginNotOK string
|
|
|
|
|
2018-11-02 07:44:52 +02:00
|
|
|
// RecoverOK is the redirect path after a successful recovery of a
|
|
|
|
// password.
|
2018-02-02 01:42:48 +02:00
|
|
|
RecoverOK string
|
|
|
|
|
|
|
|
// RegisterOK is the redirect path after a successful registration.
|
|
|
|
RegisterOK string
|
|
|
|
|
2018-09-16 00:39:26 +02:00
|
|
|
// RootURL is the scheme+host+port of the web application
|
|
|
|
// (eg https://www.happiness.com:8080) for url generation.
|
|
|
|
// No trailing slash.
|
2018-02-02 01:42:48 +02:00
|
|
|
RootURL string
|
2018-11-02 07:44:52 +02:00
|
|
|
|
|
|
|
// TwoFactorEmailAuthNotOK is where a user is redirected when
|
|
|
|
// the user attempts to add 2fa to their account without verifying
|
|
|
|
// their e-mail OR when they've completed the first step towards
|
|
|
|
// verification and need to check their e-mail to proceed.
|
|
|
|
TwoFactorEmailAuthNotOK string
|
2018-02-02 01:42:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Modules struct {
|
2018-03-06 03:47:11 +02:00
|
|
|
// BCryptCost is the cost of the bcrypt password hashing function.
|
|
|
|
BCryptCost int
|
|
|
|
|
2018-11-02 07:44:52 +02:00
|
|
|
// ConfirmMethod IS DEPRECATED! See MailRouteMethod instead.
|
|
|
|
//
|
2018-09-16 00:39:26 +02:00
|
|
|
// ConfirmMethod controls which http method confirm expects.
|
|
|
|
// This is because typically this is a GET request since it's a link
|
|
|
|
// from an e-mail, but in api-like cases it needs to be able to be a
|
|
|
|
// post since there's data that must be sent to it.
|
2018-09-13 04:58:16 +02:00
|
|
|
ConfirmMethod string
|
|
|
|
|
2018-09-16 00:39:26 +02:00
|
|
|
// ExpireAfter controls the time an account is idle before being
|
|
|
|
// logged out by the ExpireMiddleware.
|
2018-02-02 01:42:48 +02: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
|
2018-02-05 09:28:31 +02:00
|
|
|
|
2018-09-16 00:39:26 +02:00
|
|
|
// LogoutMethod is the method the logout route should use
|
|
|
|
// (default should be DELETE)
|
2018-03-07 21:41:14 +02:00
|
|
|
LogoutMethod string
|
|
|
|
|
2018-11-02 07:44:52 +02:00
|
|
|
// MailRouteMethod is used to set the type of request that's used for
|
|
|
|
// routes that require a token from an e-mail link's query string.
|
|
|
|
// This is things like confirm and two factor e-mail auth.
|
|
|
|
//
|
|
|
|
// You should probably set this to POST if you are building an API
|
|
|
|
// so that the user goes to the frontend with their link & token
|
|
|
|
// and the front-end calls the API with the token in a POST JSON body.
|
|
|
|
//
|
|
|
|
// This configuration setting deprecates ConfirmMethod.
|
|
|
|
// If ConfirmMethod is set to the default value (GET) then
|
2018-12-11 09:00:27 +02:00
|
|
|
// MailRouteMethod is used. If ConfirmMethod is not the default value
|
2018-11-02 07:44:52 +02:00
|
|
|
// then it is used until Authboss v3 when only MailRouteMethod will be
|
|
|
|
// used.
|
|
|
|
MailRouteMethod string
|
|
|
|
|
2020-02-08 00:24:40 +02:00
|
|
|
// MailNoGoroutine is used to prevent the mailer from being launched
|
|
|
|
// in a goroutine by the Authboss modules.
|
|
|
|
//
|
|
|
|
// This behavior will become the default in Authboss v3 and each
|
|
|
|
// Mailer implementation will be required to use goroutines if it sees
|
|
|
|
// fit.
|
|
|
|
//
|
|
|
|
// It's important that this is the case if you are using contexts
|
|
|
|
// as the http request context will be cancelled by the Go http server
|
|
|
|
// and it may interrupt your use of the context that the Authboss module
|
|
|
|
// is passing to you, preventing proper use of it.
|
|
|
|
MailNoGoroutine bool
|
|
|
|
|
2018-09-16 00:39:26 +02:00
|
|
|
// RegisterPreserveFields are fields used with registration that are
|
|
|
|
// to be rendered when post fails in a normal way
|
|
|
|
// (for example validation errors), they will be passed back in the
|
|
|
|
// data of the response under the key DataPreserve which
|
|
|
|
// will be a map[string]string.
|
2018-02-26 01:20:57 +02:00
|
|
|
//
|
2018-09-16 00:39:26 +02:00
|
|
|
// All fields that are to be preserved must be able to be returned by
|
|
|
|
// the ArbitraryValuer.GetValues()
|
2018-02-26 01:20:57 +02:00
|
|
|
//
|
2018-09-16 00:39:26 +02:00
|
|
|
// This means in order to have a field named "address" you would need
|
|
|
|
// to have that returned by the ArbitraryValuer.GetValues() method and
|
|
|
|
// then it would be available to be whitelisted by this
|
2018-02-26 01:20:57 +02:00
|
|
|
// configuration variable.
|
2018-02-05 09:28:31 +02:00
|
|
|
RegisterPreserveFields []string
|
|
|
|
|
2018-09-16 00:39:26 +02:00
|
|
|
// RecoverTokenDuration controls how long a token sent via
|
|
|
|
// email for password recovery is valid for.
|
2018-02-05 09:28:31 +02:00
|
|
|
RecoverTokenDuration time.Duration
|
2018-09-16 00:39:26 +02:00
|
|
|
// RecoverLoginAfterRecovery says for the recovery module after a
|
|
|
|
// user has successfully recovered the password, are they simply
|
|
|
|
// logged in, or are they redirected to the login page with an
|
|
|
|
// "updated password" message.
|
2018-03-06 03:47:11 +02:00
|
|
|
RecoverLoginAfterRecovery bool
|
2018-02-05 09:28:31 +02:00
|
|
|
|
|
|
|
// OAuth2Providers lists all providers that can be used. See
|
|
|
|
// OAuthProvider documentation for more details.
|
|
|
|
OAuth2Providers map[string]OAuth2Provider
|
2018-08-23 06:34:38 +02:00
|
|
|
|
2018-11-02 07:44:52 +02:00
|
|
|
// TwoFactorEmailAuthRequired forces users to first confirm they have
|
|
|
|
// access to their e-mail with the current device by clicking a link
|
|
|
|
// and confirming a token stored in the session.
|
|
|
|
TwoFactorEmailAuthRequired bool
|
|
|
|
|
2018-09-16 00:39:26 +02:00
|
|
|
// TOTP2FAIssuer is the issuer that appears in the url when scanning
|
|
|
|
// a qr code for google authenticator.
|
2018-08-23 06:34:38 +02:00
|
|
|
TOTP2FAIssuer string
|
2018-08-31 23:57:22 +02:00
|
|
|
|
2018-12-11 09:00:27 +02:00
|
|
|
// DEPRECATED: See ResponseOnUnauthed
|
2018-09-16 00:39:26 +02:00
|
|
|
// RoutesRedirectOnUnauthed controls whether or not a user is redirected
|
|
|
|
// or given a 404 when they are unauthenticated and attempting to access
|
|
|
|
// a route that's login-protected inside Authboss itself.
|
|
|
|
// The otp/twofactor modules all use authboss.Middleware to protect
|
|
|
|
// their routes and this is the redirectToLogin parameter in that
|
|
|
|
// middleware that they pass through.
|
2018-09-03 20:57:25 +02:00
|
|
|
RoutesRedirectOnUnauthed bool
|
2018-12-11 09:00:27 +02:00
|
|
|
|
|
|
|
// ResponseOnUnauthed controls how a user is responded to when
|
|
|
|
// attempting to access a route that's login-protected inside Authboss
|
|
|
|
// itself. The otp/twofactor modules all use authboss.Middleware2 to
|
|
|
|
// protect their routes and this is the failResponse parameter in that
|
|
|
|
// middleware that they pass through.
|
|
|
|
//
|
|
|
|
// This deprecates RoutesRedirectOnUnauthed. If RoutesRedirectOnUnauthed
|
|
|
|
// is true, the value of this will be set to RespondRedirect until
|
|
|
|
// authboss v3.
|
|
|
|
ResponseOnUnauthed MWRespondOnFailure
|
2018-02-02 01:42:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Mail struct {
|
2018-09-13 04:44:34 +02:00
|
|
|
// RootURL is a full path to an application that is hosting a front-end
|
|
|
|
// Typically using a combination of Paths.RootURL and Paths.Mount
|
|
|
|
// MailRoot will be assembled if not set.
|
2018-09-16 00:39:26 +02:00
|
|
|
// Typically looks like: https://our-front-end.com/authenication
|
|
|
|
// No trailing slash.
|
2018-09-13 04:44:34 +02:00
|
|
|
RootURL string
|
|
|
|
|
2018-02-02 01:42:48 +02:00
|
|
|
// From is the email address authboss e-mails come from.
|
|
|
|
From string
|
2018-05-14 20:47:34 +02:00
|
|
|
// FromName is the name authboss e-mails come from.
|
|
|
|
FromName string
|
2018-02-02 01:42:48 +02:00
|
|
|
// SubjectPrefix is used to add something to the front of the authboss
|
|
|
|
// email subjects.
|
|
|
|
SubjectPrefix string
|
|
|
|
}
|
|
|
|
|
|
|
|
Storage struct {
|
2018-09-16 00:39:26 +02:00
|
|
|
// Storer is the interface through which Authboss accesses the web apps
|
|
|
|
// database for user operations.
|
2018-02-02 01:42:48 +02:00
|
|
|
Server ServerStorer
|
|
|
|
|
|
|
|
// CookieState must be defined to provide an interface capapable of
|
2018-09-16 00:39:26 +02:00
|
|
|
// storing cookies for the given response, and reading them from the
|
|
|
|
// request.
|
2018-02-02 01:42:48 +02:00
|
|
|
CookieState ClientStateReadWriter
|
|
|
|
// SessionState must be defined to provide an interface capable of
|
|
|
|
// storing session-only values for the given response, and reading them
|
|
|
|
// from the request.
|
|
|
|
SessionState ClientStateReadWriter
|
2018-12-18 09:03:55 +02:00
|
|
|
|
|
|
|
// SessionStateWhitelistKeys are set to preserve keys in the session
|
|
|
|
// when authboss.DelAllSession is called. A correct implementation
|
|
|
|
// of ClientStateReadWriter will delete ALL session key-value pairs
|
|
|
|
// unless that key is whitelisted here.
|
|
|
|
SessionStateWhitelistKeys []string
|
2018-02-02 01:42:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Core struct {
|
|
|
|
// Router is the entity that controls all routing to authboss routes
|
|
|
|
// modules will register their routes with it.
|
|
|
|
Router Router
|
|
|
|
|
2018-02-03 01:41:24 +02:00
|
|
|
// ErrorHandler wraps http requests with centralized error handling.
|
|
|
|
ErrorHandler ErrorHandler
|
|
|
|
|
2018-02-02 01:42:48 +02:00
|
|
|
// Responder takes a generic response from a controller and prepares
|
|
|
|
// the response, uses a renderer to create the body, and replies to the
|
|
|
|
// http request.
|
|
|
|
Responder HTTPResponder
|
|
|
|
|
2018-09-16 00:39:26 +02:00
|
|
|
// Redirector can redirect a response, similar to Responder but
|
|
|
|
// responsible only for redirection.
|
2018-02-02 01:42:48 +02:00
|
|
|
Redirector HTTPRedirector
|
|
|
|
|
2018-09-16 00:39:26 +02:00
|
|
|
// BodyReader reads validatable data from the body of a request to
|
|
|
|
// be able to get data from the user's client.
|
2018-02-05 07:24:55 +02:00
|
|
|
BodyReader BodyReader
|
2018-02-02 01:42:48 +02:00
|
|
|
|
|
|
|
// ViewRenderer loads the templates for the application.
|
|
|
|
ViewRenderer Renderer
|
|
|
|
// MailRenderer loads the templates for mail. If this is nil, it will
|
|
|
|
// fall back to using the Renderer created from the ViewLoader instead.
|
|
|
|
MailRenderer Renderer
|
|
|
|
|
|
|
|
// Mailer is the mailer being used to send e-mails out via smtp
|
|
|
|
Mailer Mailer
|
|
|
|
|
2018-02-02 22:11:47 +02:00
|
|
|
// Logger implies just a few log levels for use, can optionally
|
|
|
|
// also implement the ContextLogger to be able to upgrade to a
|
|
|
|
// request specific logger.
|
|
|
|
Logger Logger
|
2018-02-02 01:42:48 +02:00
|
|
|
}
|
2015-01-05 10:18:41 +02:00
|
|
|
}
|
2015-01-04 20:33:53 +02:00
|
|
|
|
2015-03-31 21:34:03 +02:00
|
|
|
// Defaults sets the configuration's default values.
|
|
|
|
func (c *Config) Defaults() {
|
2018-03-06 03:47:11 +02:00
|
|
|
c.Paths.Mount = "/auth"
|
2018-07-17 16:09:38 +02:00
|
|
|
c.Paths.NotAuthorized = "/"
|
2018-02-05 09:28:31 +02:00
|
|
|
c.Paths.AuthLoginOK = "/"
|
2018-03-06 03:47:11 +02:00
|
|
|
c.Paths.ConfirmOK = "/"
|
|
|
|
c.Paths.ConfirmNotOK = "/"
|
2018-03-10 00:46:33 +02:00
|
|
|
c.Paths.LockNotOK = "/"
|
2018-03-07 21:41:14 +02:00
|
|
|
c.Paths.LogoutOK = "/"
|
2018-03-10 00:46:33 +02:00
|
|
|
c.Paths.OAuth2LoginOK = "/"
|
|
|
|
c.Paths.OAuth2LoginNotOK = "/"
|
2018-02-05 09:28:31 +02:00
|
|
|
c.Paths.RecoverOK = "/"
|
|
|
|
c.Paths.RegisterOK = "/"
|
2018-03-10 00:46:33 +02:00
|
|
|
c.Paths.RootURL = "http://localhost:8080"
|
2018-11-02 07:44:52 +02:00
|
|
|
c.Paths.TwoFactorEmailAuthNotOK = "/"
|
2018-02-05 09:28:31 +02:00
|
|
|
|
2018-03-06 03:47:11 +02:00
|
|
|
c.Modules.BCryptCost = bcrypt.DefaultCost
|
2018-09-13 04:58:16 +02:00
|
|
|
c.Modules.ConfirmMethod = http.MethodGet
|
2018-03-10 00:46:33 +02:00
|
|
|
c.Modules.ExpireAfter = time.Hour
|
2018-02-05 09:28:31 +02:00
|
|
|
c.Modules.LockAfter = 3
|
|
|
|
c.Modules.LockWindow = 5 * time.Minute
|
2018-03-10 00:46:33 +02:00
|
|
|
c.Modules.LockDuration = 12 * time.Hour
|
2018-03-07 21:41:14 +02:00
|
|
|
c.Modules.LogoutMethod = "DELETE"
|
2018-11-02 07:44:52 +02:00
|
|
|
c.Modules.MailRouteMethod = http.MethodGet
|
2018-03-10 00:46:33 +02:00
|
|
|
c.Modules.RecoverLoginAfterRecovery = false
|
|
|
|
c.Modules.RecoverTokenDuration = 24 * time.Hour
|
2015-01-04 20:33:53 +02:00
|
|
|
}
|