2022-05-05 20:12:06 +00:00
|
|
|
// Copyright 2022 the Pinniped contributors. All Rights Reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
// Package loginhtml defines HTML templates used by the Supervisor.
|
|
|
|
//nolint: gochecknoglobals // This package uses globals to ensure that all parsing and minifying happens at init.
|
|
|
|
package loginhtml
|
|
|
|
|
|
|
|
import (
|
|
|
|
_ "embed" // Needed to trigger //go:embed directives below.
|
|
|
|
"html/template"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/tdewolff/minify/v2/minify"
|
|
|
|
|
|
|
|
"go.pinniped.dev/internal/oidc/provider/csp"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
//go:embed login_form.css
|
|
|
|
rawCSS string
|
2022-05-19 23:02:08 +00:00
|
|
|
minifiedCSS = panicOnError(minify.CSS(rawCSS))
|
2022-05-05 20:12:06 +00:00
|
|
|
|
|
|
|
//go:embed login_form.gohtml
|
|
|
|
rawHTMLTemplate string
|
|
|
|
)
|
|
|
|
|
|
|
|
// Parse the Go templated HTML and inject functions providing the minified inline CSS and JS.
|
|
|
|
var parsedHTMLTemplate = template.Must(template.New("login_form.gohtml").Funcs(template.FuncMap{
|
2022-05-19 23:02:08 +00:00
|
|
|
"minifiedCSS": func() template.CSS { return template.CSS(CSS()) },
|
2022-05-05 20:12:06 +00:00
|
|
|
}).Parse(rawHTMLTemplate))
|
|
|
|
|
|
|
|
// Generate the CSP header value once since it's effectively constant.
|
|
|
|
var cspValue = strings.Join([]string{
|
|
|
|
`default-src 'none'`,
|
|
|
|
`style-src '` + csp.Hash(minifiedCSS) + `'`,
|
|
|
|
`frame-ancestors 'none'`,
|
|
|
|
}, "; ")
|
|
|
|
|
2022-05-19 23:02:08 +00:00
|
|
|
func panicOnError(s string, err error) string {
|
2022-05-05 20:12:06 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// ContentSecurityPolicy returns the Content-Security-Policy header value to make the Template() operate correctly.
|
|
|
|
//
|
|
|
|
// See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy.
|
|
|
|
func ContentSecurityPolicy() string { return cspValue }
|
|
|
|
|
|
|
|
// Template returns the html/template.Template for rendering the login page.
|
|
|
|
func Template() *template.Template { return parsedHTMLTemplate }
|
|
|
|
|
|
|
|
// CSS returns the minified CSS that will be embedded into the page template.
|
|
|
|
func CSS() string { return minifiedCSS }
|
|
|
|
|
|
|
|
// PageData represents the inputs to the template.
|
|
|
|
type PageData struct {
|
|
|
|
State string
|
|
|
|
IDPName string
|
|
|
|
HasAlertError bool
|
|
|
|
AlertMessage string
|
|
|
|
MinifiedCSS template.CSS
|
|
|
|
PostPath string
|
|
|
|
}
|