mirror of
https://github.com/oauth2-proxy/oauth2-proxy.git
synced 2024-11-28 09:08:44 +02:00
Move all pagewriter related code to dedicated pagewriter package
This commit is contained in:
parent
e8e2af73df
commit
225ff478a1
@ -17,7 +17,7 @@ import (
|
||||
middlewareapi "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/middleware"
|
||||
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options"
|
||||
sessionsapi "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/sessions"
|
||||
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/app"
|
||||
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/app/pagewriter"
|
||||
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/authentication/basic"
|
||||
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/cookies"
|
||||
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/encryption"
|
||||
@ -101,7 +101,7 @@ type OAuthProxy struct {
|
||||
sessionChain alice.Chain
|
||||
headersChain alice.Chain
|
||||
preAuthChain alice.Chain
|
||||
pageWriter app.PageWriter
|
||||
pageWriter pagewriter.Writer
|
||||
}
|
||||
|
||||
// NewOAuthProxy creates a new instance of OAuthProxy from the options provided
|
||||
@ -121,7 +121,7 @@ func NewOAuthProxy(opts *options.Options, validator func(string) bool) (*OAuthPr
|
||||
}
|
||||
}
|
||||
|
||||
pageWriter, err := app.NewPageWriter(app.PageWriterOpts{
|
||||
pageWriter, err := pagewriter.NewWriter(pagewriter.Opts{
|
||||
TemplatesPath: opts.Templates.Path,
|
||||
ProxyPrefix: opts.ProxyPrefix,
|
||||
Footer: opts.Templates.Footer,
|
||||
|
@ -1,4 +1,4 @@
|
||||
package app
|
||||
package pagewriter
|
||||
|
||||
import (
|
||||
"fmt"
|
@ -1,4 +1,4 @@
|
||||
package app
|
||||
package pagewriter
|
||||
|
||||
import (
|
||||
"errors"
|
@ -1,29 +1,29 @@
|
||||
package app
|
||||
package pagewriter
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// PageWriter is an interface for rendering html templates for both sign-in and
|
||||
// Writer is an interface for rendering html templates for both sign-in and
|
||||
// error pages.
|
||||
// It can also be used to write errors for the http.ReverseProxy used in the
|
||||
// upstream package.
|
||||
type PageWriter interface {
|
||||
type Writer interface {
|
||||
WriteSignInPage(rw http.ResponseWriter, redirectURL string)
|
||||
WriteErrorPage(rw http.ResponseWriter, status int, redirectURL string, appError string, messages ...interface{})
|
||||
ProxyErrorHandler(rw http.ResponseWriter, req *http.Request, proxyErr error)
|
||||
}
|
||||
|
||||
// pageWriter implements the PageWriter interface
|
||||
// pageWriter implements the Writer interface
|
||||
type pageWriter struct {
|
||||
*errorPageWriter
|
||||
*signInPageWriter
|
||||
}
|
||||
|
||||
// PageWriterOpts contains all options required to configure the template
|
||||
// Opts contains all options required to configure the template
|
||||
// rendering within OAuth2 Proxy.
|
||||
type PageWriterOpts struct {
|
||||
type Opts struct {
|
||||
// TemplatesPath is the path from which to load custom templates for the sign-in and error pages.
|
||||
TemplatesPath string
|
||||
|
||||
@ -51,9 +51,9 @@ type PageWriterOpts struct {
|
||||
SignInMessage string
|
||||
}
|
||||
|
||||
// NewPageWriter constructs a PageWriter from the options given to allow
|
||||
// NewWriter constructs a Writer from the options given to allow
|
||||
// rendering of sign-in and error pages.
|
||||
func NewPageWriter(opts PageWriterOpts) (PageWriter, error) {
|
||||
func NewWriter(opts Opts) (Writer, error) {
|
||||
templates, err := loadTemplates(opts.TemplatesPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error loading templates: %v", err)
|
@ -1,4 +1,4 @@
|
||||
package app
|
||||
package pagewriter
|
||||
|
||||
import (
|
||||
"testing"
|
@ -1,4 +1,4 @@
|
||||
package app
|
||||
package pagewriter
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
@ -10,13 +10,13 @@ import (
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("PageWriter", func() {
|
||||
Context("NewPageWriter", func() {
|
||||
var writer PageWriter
|
||||
var opts PageWriterOpts
|
||||
var _ = Describe("Writer", func() {
|
||||
Context("NewWriter", func() {
|
||||
var writer Writer
|
||||
var opts Opts
|
||||
|
||||
BeforeEach(func() {
|
||||
opts = PageWriterOpts{
|
||||
opts = Opts{
|
||||
TemplatesPath: "",
|
||||
ProxyPrefix: "/prefix",
|
||||
Footer: "<Footer>",
|
||||
@ -31,7 +31,7 @@ var _ = Describe("PageWriter", func() {
|
||||
Context("With no custom templates", func() {
|
||||
BeforeEach(func() {
|
||||
var err error
|
||||
writer, err = NewPageWriter(opts)
|
||||
writer, err = NewWriter(opts)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
})
|
||||
|
||||
@ -70,7 +70,7 @@ var _ = Describe("PageWriter", func() {
|
||||
|
||||
opts.TemplatesPath = customDir
|
||||
|
||||
writer, err = NewPageWriter(opts)
|
||||
writer, err = NewWriter(opts)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
})
|
||||
|
||||
@ -117,7 +117,7 @@ var _ = Describe("PageWriter", func() {
|
||||
})
|
||||
|
||||
It("Should return an error", func() {
|
||||
writer, err := NewPageWriter(opts)
|
||||
writer, err := NewWriter(opts)
|
||||
Expect(err).To(MatchError(ContainSubstring("template: sign_in.html:1: function \"Custom\" not defined")))
|
||||
Expect(writer).To(BeNil())
|
||||
})
|
@ -1,4 +1,4 @@
|
||||
package app
|
||||
package pagewriter
|
||||
|
||||
import (
|
||||
"html/template"
|
@ -1,4 +1,4 @@
|
||||
package app
|
||||
package pagewriter
|
||||
|
||||
import (
|
||||
"html/template"
|
@ -1,4 +1,4 @@
|
||||
package app
|
||||
package pagewriter
|
||||
|
||||
import (
|
||||
"fmt"
|
@ -1,4 +1,4 @@
|
||||
package app
|
||||
package pagewriter
|
||||
|
||||
import (
|
||||
"bytes"
|
Loading…
Reference in New Issue
Block a user