You've already forked oauth2-proxy
mirror of
https://github.com/oauth2-proxy/oauth2-proxy.git
synced 2025-06-23 00:40:46 +02:00
Move Error page rendering to app package
This commit is contained in:
56
pkg/app/error_page.go
Normal file
56
pkg/app/error_page.go
Normal file
@ -0,0 +1,56 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"net/http"
|
||||
|
||||
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
|
||||
)
|
||||
|
||||
// ErrorPage is used to render error pages.
|
||||
type ErrorPage struct {
|
||||
// Template is the error page HTML template.
|
||||
Template *template.Template
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// Render writes an error page to the given response writer.
|
||||
// It uses the passed redirectURL to give users the option to go back to where
|
||||
// they originally came from or try signing in again.
|
||||
func (e *ErrorPage) Render(rw http.ResponseWriter, status int, redirectURL string, appError string) {
|
||||
rw.WriteHeader(status)
|
||||
|
||||
// We allow unescaped template.HTML since it is user configured options
|
||||
/* #nosec G203 */
|
||||
data := struct {
|
||||
Title string
|
||||
Message string
|
||||
ProxyPrefix string
|
||||
StatusCode int
|
||||
Redirect string
|
||||
Footer template.HTML
|
||||
Version string
|
||||
}{
|
||||
Title: http.StatusText(status),
|
||||
Message: appError,
|
||||
ProxyPrefix: e.ProxyPrefix,
|
||||
StatusCode: status,
|
||||
Redirect: redirectURL,
|
||||
Footer: template.HTML(e.Footer),
|
||||
Version: e.Version,
|
||||
}
|
||||
|
||||
if err := e.Template.Execute(rw, data); err != nil {
|
||||
logger.Printf("Error rendering error template: %v", err)
|
||||
http.Error(rw, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
35
pkg/app/error_page_test.go
Normal file
35
pkg/app/error_page_test.go
Normal file
@ -0,0 +1,35 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"io/ioutil"
|
||||
"net/http/httptest"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("Error Page", func() {
|
||||
|
||||
Context("Render", func() {
|
||||
It("Writes the template to the response writer", func() {
|
||||
tmpl, err := template.New("").Parse("{{.Title}} {{.Message}} {{.ProxyPrefix}} {{.StatusCode}} {{.Redirect}} {{.Footer}} {{.Version}}")
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
errorPage := &ErrorPage{
|
||||
Template: tmpl,
|
||||
ProxyPrefix: "/prefix/",
|
||||
Footer: "Custom Footer Text",
|
||||
Version: "v0.0.0-test",
|
||||
}
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
errorPage.Render(recorder, 403, "/redirect", "Access Denied")
|
||||
|
||||
body, err := ioutil.ReadAll(recorder.Result().Body)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(string(body)).To(Equal("Forbidden Access Denied /prefix/ 403 /redirect Custom Footer Text v0.0.0-test"))
|
||||
})
|
||||
})
|
||||
|
||||
})
|
Reference in New Issue
Block a user