1
0
mirror of https://github.com/oauth2-proxy/oauth2-proxy.git synced 2025-11-06 08:59:21 +02:00

Move SignIn page rendering to app pkg

This commit is contained in:
Joel Speed
2021-02-12 17:53:01 +00:00
committed by Joel Speed
parent 1e3d8547d7
commit dba6989054
3 changed files with 187 additions and 79 deletions

66
pkg/app/sign_in_page.go Normal file
View File

@@ -0,0 +1,66 @@
package app
import (
"html/template"
"net/http"
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
)
// SignInPage is used to render sign-in pages.
type SignInPage struct {
// Template is the sign-in page HTML template.
Template *template.Template
// ErrorPage is used to render an error if there are problems with rendering the sign-in page.
ErrorPage *ErrorPage
// ProxyPrefix is the prefix under which OAuth2 Proxy pages are served.
ProxyPrefix string
// 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
// 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
// DisplayLoginForm determines whether or not the basic auth password form is displayed on the sign-in page.
DisplayLoginForm bool
}
// Render writes the sign-in page to the given response writer.
// It uses the redirectURL to be able to set the final destination for the user post login.
func (s *SignInPage) Render(rw http.ResponseWriter, redirectURL string) {
// 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
}{
ProviderName: s.ProviderName,
SignInMessage: template.HTML(s.SignInMessage),
CustomLogin: s.DisplayLoginForm,
Redirect: redirectURL,
Version: s.Version,
ProxyPrefix: s.ProxyPrefix,
Footer: template.HTML(s.Footer),
}
err := s.Template.Execute(rw, t)
if err != nil {
logger.Printf("Error rendering sign-in template: %v", err)
s.ErrorPage.Render(rw, http.StatusInternalServerError, redirectURL, err.Error())
}
}

View File

@@ -0,0 +1,61 @@
package app
import (
"html/template"
"io/ioutil"
"net/http/httptest"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("SignIn Page", func() {
var signInPage *SignInPage
BeforeEach(func() {
errorTmpl, err := template.New("").Parse("{{.Title}}")
Expect(err).ToNot(HaveOccurred())
errorPage := &ErrorPage{
Template: errorTmpl,
}
tmpl, err := template.New("").Parse("{{.ProxyPrefix}} {{.ProviderName}} {{.SignInMessage}} {{.Footer}} {{.Version}} {{.Redirect}} {{.CustomLogin}}")
Expect(err).ToNot(HaveOccurred())
signInPage = &SignInPage{
Template: tmpl,
ErrorPage: errorPage,
ProxyPrefix: "/prefix/",
ProviderName: "My Provider",
SignInMessage: "Sign In Here",
Footer: "Custom Footer Text",
Version: "v0.0.0-test",
DisplayLoginForm: true,
}
})
Context("Render", func() {
It("Writes the template to the response writer", func() {
recorder := httptest.NewRecorder()
signInPage.Render(recorder, "/redirect")
body, err := ioutil.ReadAll(recorder.Result().Body)
Expect(err).ToNot(HaveOccurred())
Expect(string(body)).To(Equal("/prefix/ My Provider Sign In Here Custom Footer Text v0.0.0-test /redirect true"))
})
It("Writes an error if the template can't be rendered", func() {
// Overwrite the template with something bad
tmpl, err := template.New("").Parse("{{.Unknown}}")
Expect(err).ToNot(HaveOccurred())
signInPage.Template = tmpl
recorder := httptest.NewRecorder()
signInPage.Render(recorder, "/redirect")
body, err := ioutil.ReadAll(recorder.Result().Body)
Expect(err).ToNot(HaveOccurred())
Expect(string(body)).To(Equal("Internal Server Error"))
})
})
})