2021-02-13 11:38:33 +00:00
|
|
|
package pagewriter
|
2021-02-12 18:25:46 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2021-02-13 11:38:33 +00:00
|
|
|
// Writer is an interface for rendering html templates for both sign-in and
|
2021-02-12 18:25:46 +00:00
|
|
|
// error pages.
|
|
|
|
// It can also be used to write errors for the http.ReverseProxy used in the
|
|
|
|
// upstream package.
|
2021-02-13 11:38:33 +00:00
|
|
|
type Writer interface {
|
2021-03-21 11:20:57 -07:00
|
|
|
WriteSignInPage(rw http.ResponseWriter, req *http.Request, redirectURL string)
|
|
|
|
WriteErrorPage(rw http.ResponseWriter, opts ErrorPageOpts)
|
2021-02-12 18:25:46 +00:00
|
|
|
ProxyErrorHandler(rw http.ResponseWriter, req *http.Request, proxyErr error)
|
2021-03-13 09:52:07 +00:00
|
|
|
WriteRobotsTxt(rw http.ResponseWriter, req *http.Request)
|
2021-02-12 18:25:46 +00:00
|
|
|
}
|
|
|
|
|
2021-02-13 11:38:33 +00:00
|
|
|
// pageWriter implements the Writer interface
|
2021-02-12 18:25:46 +00:00
|
|
|
type pageWriter struct {
|
|
|
|
*errorPageWriter
|
|
|
|
*signInPageWriter
|
2021-03-13 09:52:07 +00:00
|
|
|
*staticPageWriter
|
2021-02-12 18:25:46 +00:00
|
|
|
}
|
|
|
|
|
2021-02-13 11:38:33 +00:00
|
|
|
// Opts contains all options required to configure the template
|
2021-02-12 18:25:46 +00:00
|
|
|
// rendering within OAuth2 Proxy.
|
2021-02-13 11:38:33 +00:00
|
|
|
type Opts struct {
|
2021-02-12 18:25:46 +00:00
|
|
|
// TemplatesPath is the path from which to load custom templates for the sign-in and error pages.
|
|
|
|
TemplatesPath string
|
|
|
|
|
|
|
|
// ProxyPrefix is the prefix under which OAuth2 Proxy pages are served.
|
|
|
|
ProxyPrefix string
|
|
|
|
|
|
|
|
// Footer is the footer to be displayed at the bottom of the page.
|
|
|
|
// If not set, a default footer will be used.
|
|
|
|
Footer string
|
|
|
|
|
|
|
|
// Version is the OAuth2 Proxy version to be used in the default footer.
|
|
|
|
Version string
|
|
|
|
|
|
|
|
// Debug determines whether errors pages should be rendered with detailed
|
|
|
|
// errors.
|
|
|
|
Debug bool
|
|
|
|
|
|
|
|
// DisplayLoginForm determines whether or not the basic auth password form is displayed on the sign-in page.
|
|
|
|
DisplayLoginForm bool
|
|
|
|
|
|
|
|
// ProviderName is the name of the provider that should be displayed on the login button.
|
|
|
|
ProviderName string
|
|
|
|
|
|
|
|
// SignInMessage is the messge displayed above the login button.
|
|
|
|
SignInMessage string
|
2021-02-18 19:13:27 +00:00
|
|
|
|
|
|
|
// CustomLogo is the path to a logo to be displayed on the sign in page.
|
|
|
|
// The logo can be either PNG, JPG/JPEG or SVG.
|
|
|
|
CustomLogo string
|
2021-02-12 18:25:46 +00:00
|
|
|
}
|
|
|
|
|
2021-02-13 11:38:33 +00:00
|
|
|
// NewWriter constructs a Writer from the options given to allow
|
2021-02-12 18:25:46 +00:00
|
|
|
// rendering of sign-in and error pages.
|
2021-02-13 11:38:33 +00:00
|
|
|
func NewWriter(opts Opts) (Writer, error) {
|
2021-02-12 18:25:46 +00:00
|
|
|
templates, err := loadTemplates(opts.TemplatesPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error loading templates: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-02-18 19:13:27 +00:00
|
|
|
logoData, err := loadCustomLogo(opts.CustomLogo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error loading logo: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-02-12 18:25:46 +00:00
|
|
|
errorPage := &errorPageWriter{
|
|
|
|
template: templates.Lookup("error.html"),
|
|
|
|
proxyPrefix: opts.ProxyPrefix,
|
|
|
|
footer: opts.Footer,
|
|
|
|
version: opts.Version,
|
|
|
|
debug: opts.Debug,
|
|
|
|
}
|
|
|
|
|
|
|
|
signInPage := &signInPageWriter{
|
|
|
|
template: templates.Lookup("sign_in.html"),
|
|
|
|
errorPageWriter: errorPage,
|
|
|
|
proxyPrefix: opts.ProxyPrefix,
|
|
|
|
providerName: opts.ProviderName,
|
|
|
|
signInMessage: opts.SignInMessage,
|
|
|
|
footer: opts.Footer,
|
|
|
|
version: opts.Version,
|
|
|
|
displayLoginForm: opts.DisplayLoginForm,
|
2021-02-18 19:13:27 +00:00
|
|
|
logoData: logoData,
|
2021-02-12 18:25:46 +00:00
|
|
|
}
|
|
|
|
|
2021-03-13 09:52:07 +00:00
|
|
|
staticPages, err := newStaticPageWriter(opts.TemplatesPath, errorPage)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error loading static page writer: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-02-12 18:25:46 +00:00
|
|
|
return &pageWriter{
|
|
|
|
errorPageWriter: errorPage,
|
|
|
|
signInPageWriter: signInPage,
|
2021-03-13 09:52:07 +00:00
|
|
|
staticPageWriter: staticPages,
|
2021-02-12 18:25:46 +00:00
|
|
|
}, nil
|
|
|
|
}
|