2021-02-13 13:38:33 +02:00
|
|
|
package pagewriter
|
2021-02-12 19:53:01 +02:00
|
|
|
|
|
|
|
import (
|
2021-02-18 21:13:27 +02:00
|
|
|
// Import embed to allow importing default logo
|
|
|
|
_ "embed"
|
|
|
|
|
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2021-02-12 19:53:01 +02:00
|
|
|
"html/template"
|
|
|
|
"net/http"
|
|
|
|
|
2021-03-21 20:20:57 +02:00
|
|
|
middlewareapi "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/middleware"
|
2021-02-12 19:53:01 +02:00
|
|
|
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
|
|
|
|
)
|
|
|
|
|
2021-02-18 21:13:27 +02:00
|
|
|
//go:embed default_logo.svg
|
|
|
|
var defaultLogoData string
|
|
|
|
|
2021-02-12 20:25:46 +02:00
|
|
|
// signInPageWriter is used to render sign-in pages.
|
|
|
|
type signInPageWriter struct {
|
2021-02-12 19:53:01 +02:00
|
|
|
// Template is the sign-in page HTML template.
|
2021-02-12 20:25:46 +02:00
|
|
|
template *template.Template
|
2021-02-12 19:53:01 +02:00
|
|
|
|
2021-02-12 20:25:46 +02:00
|
|
|
// errorPageWriter is used to render an error if there are problems with rendering the sign-in page.
|
|
|
|
errorPageWriter *errorPageWriter
|
2021-02-12 19:53:01 +02:00
|
|
|
|
|
|
|
// ProxyPrefix is the prefix under which OAuth2 Proxy pages are served.
|
2021-02-12 20:25:46 +02:00
|
|
|
proxyPrefix string
|
2021-02-12 19:53:01 +02:00
|
|
|
|
|
|
|
// ProviderName is the name of the provider that should be displayed on the login button.
|
2021-02-12 20:25:46 +02:00
|
|
|
providerName string
|
2021-02-12 19:53:01 +02:00
|
|
|
|
|
|
|
// SignInMessage is the messge displayed above the login button.
|
2021-02-12 20:25:46 +02:00
|
|
|
signInMessage string
|
2021-02-12 19:53:01 +02:00
|
|
|
|
|
|
|
// Footer is the footer to be displayed at the bottom of the page.
|
|
|
|
// If not set, a default footer will be used.
|
2021-02-12 20:25:46 +02:00
|
|
|
footer string
|
2021-02-12 19:53:01 +02:00
|
|
|
|
|
|
|
// Version is the OAuth2 Proxy version to be used in the default footer.
|
2021-02-12 20:25:46 +02:00
|
|
|
version string
|
2021-02-12 19:53:01 +02:00
|
|
|
|
|
|
|
// DisplayLoginForm determines whether or not the basic auth password form is displayed on the sign-in page.
|
2021-02-12 20:25:46 +02:00
|
|
|
displayLoginForm bool
|
2021-02-18 21:13:27 +02:00
|
|
|
|
|
|
|
// LogoData is the logo to render in the template.
|
|
|
|
// This should contain valid html.
|
|
|
|
logoData string
|
2021-02-12 19:53:01 +02:00
|
|
|
}
|
|
|
|
|
2021-02-12 20:25:46 +02:00
|
|
|
// WriteSignInPage writes the sign-in page to the given response writer.
|
2021-02-12 19:53:01 +02:00
|
|
|
// It uses the redirectURL to be able to set the final destination for the user post login.
|
2021-03-21 20:20:57 +02:00
|
|
|
func (s *signInPageWriter) WriteSignInPage(rw http.ResponseWriter, req *http.Request, redirectURL string) {
|
2021-02-12 19:53:01 +02:00
|
|
|
// We allow unescaped template.HTML since it is user configured options
|
|
|
|
/* #nosec G203 */
|
|
|
|
t := struct {
|
|
|
|
ProviderName string
|
|
|
|
SignInMessage template.HTML
|
|
|
|
CustomLogin bool
|
|
|
|
Redirect string
|
|
|
|
Version string
|
|
|
|
ProxyPrefix string
|
|
|
|
Footer template.HTML
|
2021-02-18 21:13:27 +02:00
|
|
|
LogoData template.HTML
|
2021-02-12 19:53:01 +02:00
|
|
|
}{
|
2021-02-12 20:25:46 +02:00
|
|
|
ProviderName: s.providerName,
|
|
|
|
SignInMessage: template.HTML(s.signInMessage),
|
|
|
|
CustomLogin: s.displayLoginForm,
|
2021-02-12 19:53:01 +02:00
|
|
|
Redirect: redirectURL,
|
2021-02-12 20:25:46 +02:00
|
|
|
Version: s.version,
|
|
|
|
ProxyPrefix: s.proxyPrefix,
|
|
|
|
Footer: template.HTML(s.footer),
|
2021-02-18 21:13:27 +02:00
|
|
|
LogoData: template.HTML(s.logoData),
|
2021-02-12 19:53:01 +02:00
|
|
|
}
|
|
|
|
|
2021-02-12 20:25:46 +02:00
|
|
|
err := s.template.Execute(rw, t)
|
2021-02-12 19:53:01 +02:00
|
|
|
if err != nil {
|
|
|
|
logger.Printf("Error rendering sign-in template: %v", err)
|
2021-03-21 20:20:57 +02:00
|
|
|
scope := middlewareapi.GetRequestScope(req)
|
|
|
|
s.errorPageWriter.WriteErrorPage(rw, ErrorPageOpts{
|
|
|
|
Status: http.StatusInternalServerError,
|
|
|
|
RedirectURL: redirectURL,
|
|
|
|
RequestID: scope.RequestID,
|
|
|
|
AppError: err.Error(),
|
|
|
|
})
|
2021-02-12 19:53:01 +02:00
|
|
|
}
|
|
|
|
}
|
2021-02-18 21:13:27 +02:00
|
|
|
|
|
|
|
// loadCustomLogo loads the logo file from the path and encodes it to an HTML
|
|
|
|
// entity. If no custom logo is provided, the OAuth2 Proxy Icon is used instead.
|
|
|
|
func loadCustomLogo(logoPath string) (string, error) {
|
|
|
|
if logoPath == "" {
|
|
|
|
// The default logo is an SVG so this will be valid to just return.
|
|
|
|
return defaultLogoData, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if logoPath == "-" {
|
|
|
|
// Return no logo when the custom logo is set to `-`.
|
|
|
|
// This disables the logo rendering.
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
logoData, err := os.ReadFile(logoPath)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("could not read logo file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
extension := strings.ToLower(filepath.Ext(logoPath))
|
|
|
|
switch extension {
|
|
|
|
case ".svg":
|
|
|
|
return string(logoData), nil
|
|
|
|
case ".jpg", ".jpeg":
|
|
|
|
return encodeImg(logoData, "jpeg"), nil
|
|
|
|
case ".png":
|
|
|
|
return encodeImg(logoData, "png"), nil
|
|
|
|
default:
|
|
|
|
return "", fmt.Errorf("unknown extension: %q, supported extensions are .svg, .jpg, .jpeg and .png", extension)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// encodeImg takes the raw image data and converts it to an HTML Img tag with
|
|
|
|
// a base64 data source.
|
|
|
|
func encodeImg(data []byte, format string) string {
|
|
|
|
b64Data := base64.StdEncoding.EncodeToString(data)
|
|
|
|
return fmt.Sprintf("<img src=\"data:image/%s;base64,%s\" alt=\"Logo\" />", format, b64Data)
|
|
|
|
}
|