Documentation ¶
Overview ¶
Package maildoor provides a passwordless authentication system which uses the email as the main authentication method. It allows applications to configure its specific behavior and takes care of the email authentication behavior. It provides default implementations for token generation and validation as well as email sending through SMTP.
Index ¶
- Constants
- Variables
- func GenerateJWT(d time.Duration, secret []byte) (string, error)
- func NewSMTPSender(opts SMTPOptions) func(*Message) error
- func NewWithOptions(csrfToken string, options ...Option) (*handler, error)
- func ValidateJWT(tt string, secret []byte) (bool, error)
- type CookieValuer
- type DefaultTokenManager
- type Emailable
- type Logger
- type Message
- type MessageBody
- type Option
- func UseAfterLogin(fn func(w http.ResponseWriter, r *http.Request, user Emailable) error) Option
- func UseBaseURL(baseURL string) Option
- func UseFavicon(faviconURL string) Option
- func UseFinder(fn func(token string) (Emailable, error)) Option
- func UseLogger(logger Logger) Option
- func UseLogo(logoURL string) Option
- func UseLogout(fn func(w http.ResponseWriter, r *http.Request) error) Option
- func UsePrefix(prefix string) Option
- func UseProductName(name string) Option
- func UseSender(fn func(message *Message) error) Option
- func UseTokenManager(tokenManager TokenManager) Option
- type SMTPOptions
- type TokenManager
Constants ¶
const ( // DefaultCookieName is the of the cookie that // Maildoor will use to store the user's email address. // when the default login/logout functions are not overridden. DefaultCookieName = "maildoor-cookie" )
Variables ¶
var ( // BasicLogger is a simple logger that prints to stdout // using the `log` package. BasicLogger = stdOutLogger(2) )
Functions ¶
func GenerateJWT ¶
GenerateJWT token with the specified duration and secret.
func NewSMTPSender ¶
func NewSMTPSender(opts SMTPOptions) func(*Message) error
NewSMTPSender creates a new sender that will use the options to send the message HTML through SMTP.
func NewWithOptions ¶ added in v0.2.0
Types ¶
type CookieValuer ¶ added in v0.2.2
type DefaultTokenManager ¶
type DefaultTokenManager []byte
DefaultTokenManager is the default token manager which is an alias for a byte slice that will implement the TokenManager interface by using JWT.
type Emailable ¶
type Emailable interface { // EmailAddress returns the email address of the user // that will be authenticated. This address is used to // send the authentication email to the user. EmailAddress() string }
Emailable is the type that will be returned from the finder, for maildoor to work it needs to be able to use a type that can provide an email address.
type Logger ¶
type Logger interface { // Log a message at the Info level. Info(args ...interface{}) // Log a formatted message at the Info level. Infof(format string, args ...interface{}) // Log a message at the Error level. Error(args ...interface{}) // Log a formatted message at the Error level. Errorf(format string, args ...interface{}) }
Logger interface defines the minimum set of methods that a logger should satisfy to be used by the library.
type Message ¶
type Message struct { From string To string Subject string Bodies []*MessageBody }
Message allows to pass the message content to the sender, that way the sender function can take care of sending the message by using the most appropriate email sending provider for the app.
type MessageBody ¶
MessageBody is a part of the email message, maildoor adds text/plain and text/html to the message, for each of these bodies it adds respective content.
type Option ¶ added in v0.2.0
type Option func(*handler)
Option is a function that can be passed to NewWithOptions to customize the behavior of the handler.
func UseAfterLogin ¶ added in v0.2.0
UseAfterLogin sets the function to be called after a successful login.
func UseFavicon ¶ added in v0.2.0
UseFavicon for the login form and email.
func UseLogout ¶ added in v0.2.0
UseLogout sets the function to be called after a successful logout.
func UsePrefix ¶ added in v0.2.0
UsePrefix sets the prefix for the handler, this is useful for links and mounting the handler.
func UseProductName ¶ added in v0.2.0
UseProductName to be used in emails and login form.
func UseTokenManager ¶ added in v0.2.0
func UseTokenManager(tokenManager TokenManager) Option
UseTokenManager sets the token manager to be used.
type SMTPOptions ¶
type SMTPOptions struct { // The email address of the sender, this is the one used in // the message. From string // Host to authenticate/send the email. Host string // Port to authenticate/send the email. Port string // Username to authenticate/send the email, if not specified, the From field // is used. Username string // Password to authenticate/send the email. its recommended to use an environment // variable to pull this. Password string }
SMTPOptions are the options for the SMTP sender.
type TokenManager ¶
type TokenManager interface { // Generate is expected to return a token string // that will be used as part of the email sent to then // be validated by the validate handler. Generate(user Emailable) (string, error) // Validate is the method in charge of validating a // received token, if the token is valid it should return true // if there is any error while validating (p.e. database connection) // it should return the error. Validate(token string) (bool, error) }
TokenManager interface allows to provide a custom token manager for maildoor to use. The default implementation of the tokenmanager is the JWT token manager which generates and validates tokens signed with JWT. This interface allows applications to replace JWT with other token validations mechanics such as Database or anything else.