2017-02-24 02:13:25 +02:00
|
|
|
package authboss
|
|
|
|
|
|
|
|
import (
|
2018-02-27 17:14:30 +02:00
|
|
|
"context"
|
2017-02-24 02:13:25 +02:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2018-07-17 16:09:38 +02:00
|
|
|
const (
|
|
|
|
// FormValueRedirect should be honored by HTTPRedirector implementations
|
|
|
|
// as the value from the URL that overrides the typical redirect when
|
|
|
|
// FollowRedirParam is set to true.
|
|
|
|
FormValueRedirect = "redir"
|
|
|
|
)
|
|
|
|
|
2018-02-27 17:14:30 +02:00
|
|
|
// HTTPResponder knows how to respond to an HTTP request
|
|
|
|
// Must consider:
|
|
|
|
// - Flash messages
|
|
|
|
// - XSRF handling (template data)
|
|
|
|
// - Assembling template data from various sources
|
|
|
|
//
|
2018-09-16 00:39:26 +02:00
|
|
|
// Authboss controller methods (like the one called in response to
|
|
|
|
// POST /auth/login) will call this method to write a response to the user.
|
2018-02-27 17:14:30 +02:00
|
|
|
type HTTPResponder interface {
|
|
|
|
Respond(w http.ResponseWriter, r *http.Request, code int, templateName string, data HTMLData) error
|
|
|
|
}
|
|
|
|
|
2018-09-16 00:39:26 +02:00
|
|
|
// HTTPRedirector redirects http requests to a different url (must handle
|
|
|
|
// both json and html) When an authboss controller wants to redirect a user to
|
|
|
|
// a different path, it will use this interface.
|
2018-02-27 17:14:30 +02:00
|
|
|
type HTTPRedirector interface {
|
|
|
|
Redirect(w http.ResponseWriter, r *http.Request, ro RedirectOptions) error
|
|
|
|
}
|
|
|
|
|
2017-02-25 02:45:47 +02:00
|
|
|
// RedirectOptions packages up all the pieces a module needs to write out a
|
|
|
|
// response.
|
|
|
|
type RedirectOptions struct {
|
|
|
|
// Success & Failure are used to set Flash messages / JSON messages
|
|
|
|
// if set. They should be mutually exclusive.
|
|
|
|
Success string
|
|
|
|
Failure string
|
|
|
|
|
|
|
|
// Code is used when it's an API request instead of 200.
|
|
|
|
Code int
|
|
|
|
|
|
|
|
// When a request should redirect a user somewhere on completion, these
|
|
|
|
// should be set. RedirectURL tells it where to go. And optionally set
|
2018-09-16 00:39:26 +02:00
|
|
|
// FollowRedirParam to override the RedirectURL if the form parameter
|
|
|
|
// defined by FormValueRedirect is passed in the request.
|
2017-02-25 02:45:47 +02:00
|
|
|
//
|
|
|
|
// Redirecting works differently whether it's an API request or not.
|
|
|
|
// If it's an API request, then it will leave the URL in a "redirect"
|
|
|
|
// parameter.
|
|
|
|
RedirectPath string
|
|
|
|
FollowRedirParam bool
|
|
|
|
}
|
|
|
|
|
2018-02-01 03:07:11 +02:00
|
|
|
// EmailResponseOptions controls how e-mails are rendered and sent
|
|
|
|
type EmailResponseOptions struct {
|
|
|
|
Data HTMLData
|
|
|
|
HTMLTemplate string
|
|
|
|
TextTemplate string
|
2017-02-24 02:13:25 +02:00
|
|
|
}
|
|
|
|
|
2018-09-16 00:39:26 +02:00
|
|
|
// Email renders the e-mail templates for the given email and
|
|
|
|
// sends it using the mailer.
|
2018-02-27 17:14:30 +02:00
|
|
|
func (a *Authboss) Email(ctx context.Context, email Email, ro EmailResponseOptions) error {
|
2018-02-01 03:07:11 +02:00
|
|
|
if len(ro.HTMLTemplate) != 0 {
|
2018-02-02 01:42:48 +02:00
|
|
|
htmlBody, _, err := a.Core.MailRenderer.Render(ctx, ro.HTMLTemplate, ro.Data)
|
2018-02-01 03:07:11 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to render e-mail html body")
|
|
|
|
}
|
|
|
|
email.HTMLBody = string(htmlBody)
|
2017-02-24 02:13:25 +02:00
|
|
|
}
|
2018-02-01 03:07:11 +02:00
|
|
|
|
|
|
|
if len(ro.TextTemplate) != 0 {
|
2018-02-02 01:42:48 +02:00
|
|
|
textBody, _, err := a.Core.MailRenderer.Render(ctx, ro.TextTemplate, ro.Data)
|
2018-02-01 03:07:11 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to render e-mail text body")
|
|
|
|
}
|
|
|
|
email.TextBody = string(textBody)
|
2017-02-24 02:13:25 +02:00
|
|
|
}
|
|
|
|
|
2018-02-02 01:42:48 +02:00
|
|
|
return a.Core.Mailer.Send(ctx, email)
|
2017-02-24 02:13:25 +02:00
|
|
|
}
|