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.
Move SignIn page rendering to app pkg
- Loading branch information
Showing
3 changed files
with
188 additions
and
80 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()) | ||
} | ||
} |
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,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")) | ||
}) | ||
}) | ||
}) |