forked from oauth2-proxy/oauth2-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wrap templates and page rendering in PageWriter interface
- Loading branch information
Showing
9 changed files
with
325 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package app | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
// PageWriter 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 { | ||
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 | ||
type pageWriter struct { | ||
*errorPageWriter | ||
*signInPageWriter | ||
} | ||
|
||
// PageWriterOpts contains all options required to configure the template | ||
// rendering within OAuth2 Proxy. | ||
type PageWriterOpts struct { | ||
// TemplatesPath is the path from which to load custom templates for the sign-in and error pages. | ||
TemplatesPath string | ||
|
||
// 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 | ||
|
||
// Debug determines whether errors pages should be rendered with detailed | ||
// errors. | ||
Debug bool | ||
|
||
// DisplayLoginForm determines whether or not the basic auth password form is displayed on the sign-in page. | ||
DisplayLoginForm bool | ||
|
||
// 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 | ||
} | ||
|
||
// NewPageWriter constructs a PageWriter from the options given to allow | ||
// rendering of sign-in and error pages. | ||
func NewPageWriter(opts PageWriterOpts) (PageWriter, error) { | ||
templates, err := loadTemplates(opts.TemplatesPath) | ||
if err != nil { | ||
return nil, fmt.Errorf("error loading templates: %v", err) | ||
} | ||
|
||
errorPage := &errorPageWriter{ | ||
template: templates.Lookup("error.html"), | ||
proxyPrefix: opts.ProxyPrefix, | ||
footer: opts.Footer, | ||
version: opts.Version, | ||
debug: opts.Debug, | ||
} | ||
|
||
signInPage := &signInPageWriter{ | ||
template: templates.Lookup("sign_in.html"), | ||
errorPageWriter: errorPage, | ||
proxyPrefix: opts.ProxyPrefix, | ||
providerName: opts.ProviderName, | ||
signInMessage: opts.SignInMessage, | ||
footer: opts.Footer, | ||
version: opts.Version, | ||
displayLoginForm: opts.DisplayLoginForm, | ||
} | ||
|
||
return &pageWriter{ | ||
errorPageWriter: errorPage, | ||
signInPageWriter: signInPage, | ||
}, nil | ||
} |
Oops, something went wrong.