1
0
mirror of https://github.com/oauth2-proxy/oauth2-proxy.git synced 2025-07-17 01:52:30 +02:00

Move all pagewriter related code to dedicated pagewriter package

This commit is contained in:
Joel Speed
2021-02-13 11:38:33 +00:00
parent e8e2af73df
commit 225ff478a1
10 changed files with 27 additions and 27 deletions

View File

@ -17,7 +17,7 @@ import (
middlewareapi "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/middleware" middlewareapi "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/middleware"
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options" "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options"
sessionsapi "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/sessions" 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/authentication/basic"
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/cookies" "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/cookies"
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/encryption" "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/encryption"
@ -101,7 +101,7 @@ type OAuthProxy struct {
sessionChain alice.Chain sessionChain alice.Chain
headersChain alice.Chain headersChain alice.Chain
preAuthChain alice.Chain preAuthChain alice.Chain
pageWriter app.PageWriter pageWriter pagewriter.Writer
} }
// NewOAuthProxy creates a new instance of OAuthProxy from the options provided // 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, TemplatesPath: opts.Templates.Path,
ProxyPrefix: opts.ProxyPrefix, ProxyPrefix: opts.ProxyPrefix,
Footer: opts.Templates.Footer, Footer: opts.Templates.Footer,

View File

@ -1,4 +1,4 @@
package app package pagewriter
import ( import (
"fmt" "fmt"

View File

@ -1,4 +1,4 @@
package app package pagewriter
import ( import (
"errors" "errors"

View File

@ -1,29 +1,29 @@
package app package pagewriter
import ( import (
"fmt" "fmt"
"net/http" "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. // error pages.
// It can also be used to write errors for the http.ReverseProxy used in the // It can also be used to write errors for the http.ReverseProxy used in the
// upstream package. // upstream package.
type PageWriter interface { type Writer interface {
WriteSignInPage(rw http.ResponseWriter, redirectURL string) WriteSignInPage(rw http.ResponseWriter, redirectURL string)
WriteErrorPage(rw http.ResponseWriter, status int, redirectURL string, appError string, messages ...interface{}) WriteErrorPage(rw http.ResponseWriter, status int, redirectURL string, appError string, messages ...interface{})
ProxyErrorHandler(rw http.ResponseWriter, req *http.Request, proxyErr error) ProxyErrorHandler(rw http.ResponseWriter, req *http.Request, proxyErr error)
} }
// pageWriter implements the PageWriter interface // pageWriter implements the Writer interface
type pageWriter struct { type pageWriter struct {
*errorPageWriter *errorPageWriter
*signInPageWriter *signInPageWriter
} }
// PageWriterOpts contains all options required to configure the template // Opts contains all options required to configure the template
// rendering within OAuth2 Proxy. // 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 is the path from which to load custom templates for the sign-in and error pages.
TemplatesPath string TemplatesPath string
@ -51,9 +51,9 @@ type PageWriterOpts struct {
SignInMessage string 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. // rendering of sign-in and error pages.
func NewPageWriter(opts PageWriterOpts) (PageWriter, error) { func NewWriter(opts Opts) (Writer, error) {
templates, err := loadTemplates(opts.TemplatesPath) templates, err := loadTemplates(opts.TemplatesPath)
if err != nil { if err != nil {
return nil, fmt.Errorf("error loading templates: %v", err) return nil, fmt.Errorf("error loading templates: %v", err)

View File

@ -1,4 +1,4 @@
package app package pagewriter
import ( import (
"testing" "testing"

View File

@ -1,4 +1,4 @@
package app package pagewriter
import ( import (
"io/ioutil" "io/ioutil"
@ -10,13 +10,13 @@ import (
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
) )
var _ = Describe("PageWriter", func() { var _ = Describe("Writer", func() {
Context("NewPageWriter", func() { Context("NewWriter", func() {
var writer PageWriter var writer Writer
var opts PageWriterOpts var opts Opts
BeforeEach(func() { BeforeEach(func() {
opts = PageWriterOpts{ opts = Opts{
TemplatesPath: "", TemplatesPath: "",
ProxyPrefix: "/prefix", ProxyPrefix: "/prefix",
Footer: "<Footer>", Footer: "<Footer>",
@ -31,7 +31,7 @@ var _ = Describe("PageWriter", func() {
Context("With no custom templates", func() { Context("With no custom templates", func() {
BeforeEach(func() { BeforeEach(func() {
var err error var err error
writer, err = NewPageWriter(opts) writer, err = NewWriter(opts)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
}) })
@ -70,7 +70,7 @@ var _ = Describe("PageWriter", func() {
opts.TemplatesPath = customDir opts.TemplatesPath = customDir
writer, err = NewPageWriter(opts) writer, err = NewWriter(opts)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
}) })
@ -117,7 +117,7 @@ var _ = Describe("PageWriter", func() {
}) })
It("Should return an error", 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(err).To(MatchError(ContainSubstring("template: sign_in.html:1: function \"Custom\" not defined")))
Expect(writer).To(BeNil()) Expect(writer).To(BeNil())
}) })

View File

@ -1,4 +1,4 @@
package app package pagewriter
import ( import (
"html/template" "html/template"

View File

@ -1,4 +1,4 @@
package app package pagewriter
import ( import (
"html/template" "html/template"

View File

@ -1,4 +1,4 @@
package app package pagewriter
import ( import (
"fmt" "fmt"

View File

@ -1,4 +1,4 @@
package app package pagewriter
import ( import (
"bytes" "bytes"