@@ -1,124 +1,20 @@
1−package main
1+package app
22
33import (
4+ "fmt"
45 "html/template"
5− "path"
6+ "os"
7+ "path/filepath"
68 "strings"
79
810 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
911)
1012
11−func loadTemplates(dir string) *template.Template {
12− if dir == "" {
13− return getTemplates()
14− }
15− logger.Printf("using custom template directory %q", dir)
16− funcMap := template.FuncMap{
17− "ToUpper": strings.ToUpper,
18− "ToLower": strings.ToLower,
19− }
20− t, err := template.New("").Funcs(funcMap).ParseFiles(path.Join(dir, "sign_in.html"), path.Join(dir, "error.html"))
21− if err != nil {
22− logger.Fatalf("failed parsing template %s", err)
23− }
24− return t
25−}
13+const (
14+ errorTemplateName = "error.html"
15+ signInTemplateName = "sign_in.html"
2616
27−func getTemplates() *template.Template {
28− t, err := template.New("foo").Parse(`{{define "sign_in.html"}}
29−<!DOCTYPE html>
30−<html lang="en" charset="utf-8">
31− <head>
32− <meta charset="utf-8">
33− <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
34− <title>Sign In</title>
35− <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.1/css/bulma.min.css">
36−
37− <style>
38− body {
39− height: 100vh;
40− }
41− .sign-in-box {
42− max-width: 400px;
43− margin: 1.25rem auto;
44− }
45− footer a {
46− text-decoration: underline;
47− }
48− </style>
49−
50− <script>
51− if (window.location.hash) {
52− (function() {
53− var inputs = document.getElementsByName('rd');
54− for (var i = 0; i < inputs.length; i++) {
55− // Add hash, but make sure it is only added once
56− var idx = inputs[i].value.indexOf('#');
57− if (idx >= 0) {
58− // Remove existing hash from URL
59− inputs[i].value = inputs[i].value.substr(0, idx);
60− }
61− inputs[i].value += window.location.hash;
62− }
63− })();
64− }
65− </script>
66− </head>
67− <body class="has-background-light">
68− <section class="section">
69− <div class="box block sign-in-box has-text-centered">
70− <form method="GET" action="{{.ProxyPrefix}}/start">
71− <input type="hidden" name="rd" value="{{.Redirect}}">
72− {{ if .SignInMessage }}
73− <p class="block">{{.SignInMessage}}</p>
74− {{ end}}
75− <button type="submit" class="button block is-primary">Sign in with {{.ProviderName}}</button>
76− </form>
77−
78− {{ if .CustomLogin }}
79− <hr>
80−
81− <form method="POST" action="{{.ProxyPrefix}}/sign_in" class="block">
82− <input type="hidden" name="rd" value="{{.Redirect}}">
83−
84− <div class="field">
85− <label class="label" for="username">Username</label>
86− <div class="control">
87− <input class="input" type="email" placeholder="e.g. userx@example.com" name="username" id="username">
88− </div>
89− </div>
90−
91− <div class="field">
92− <label class="label" for="password">Password</label>
93− <div class="control">
94− <input class="input" type="password" placeholder="********" name="password" id="password">
95− </div>
96− </div>
97− <button class="button is-primary">Sign in</button>
98− {{ end }}
99− </form>
100− </div>
101− </section>
102−
103− <footer class="footer has-text-grey has-background-light is-size-7">
104− <div class="content has-text-centered">
105− {{ if eq .Footer "-" }}
106− {{ else if eq .Footer ""}}
107− <p>Secured with <a href="https://github.com/oauth2-proxy/oauth2-proxy#oauth2_proxy" class="has-text-grey">OAuth2 Proxy</a> version {{.Version}}</p>
108− {{ else }}
109− <p>{{.Footer}}</p>
110− {{ end }}
111− </div>
112− </footer>
113−
114− </body>
115−</html>
116−{{end}}`)
117− if err != nil {
118− logger.Fatalf("failed parsing template %s", err)
119− }
120−
121− t, err = t.Parse(`{{define "error.html"}}
17+ defaultErrorTemplate = `{{define "error.html"}}
12218<!DOCTYPE html>
12319<html lang="en" charset="utf-8">
12420<head>
@@ -215,9 +111,147 @@ func getTemplates() *template.Template {
215111
216112 </body>
217113</html>
218−{{end}}`)
114+{{end}}`
115+
116+ defaultSignInTemplate = `{{define "sign_in.html"}}
117+<!DOCTYPE html>
118+<html lang="en" charset="utf-8">
119+ <head>
120+ <meta charset="utf-8">
121+ <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
122+ <title>Sign In</title>
123+ <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.1/css/bulma.min.css">
124+
125+ <style>
126+ body {
127+ height: 100vh;
128+ }
129+ .sign-in-box {
130+ max-width: 400px;
131+ margin: 1.25rem auto;
132+ }
133+ footer a {
134+ text-decoration: underline;
135+ }
136+ </style>
137+
138+ <script>
139+ if (window.location.hash) {
140+ (function() {
141+ var inputs = document.getElementsByName('rd');
142+ for (var i = 0; i < inputs.length; i++) {
143+ // Add hash, but make sure it is only added once
144+ var idx = inputs[i].value.indexOf('#');
145+ if (idx >= 0) {
146+ // Remove existing hash from URL
147+ inputs[i].value = inputs[i].value.substr(0, idx);
148+ }
149+ inputs[i].value += window.location.hash;
150+ }
151+ })();
152+ }
153+ </script>
154+ </head>
155+ <body class="has-background-light">
156+ <section class="section">
157+ <div class="box block sign-in-box has-text-centered">
158+ <form method="GET" action="{{.ProxyPrefix}}/start">
159+ <input type="hidden" name="rd" value="{{.Redirect}}">
160+ {{ if .SignInMessage }}
161+ <p class="block">{{.SignInMessage}}</p>
162+ {{ end}}
163+ <button type="submit" class="button block is-primary">Sign in with {{.ProviderName}}</button>
164+ </form>
165+
166+ {{ if .CustomLogin }}
167+ <hr>
168+
169+ <form method="POST" action="{{.ProxyPrefix}}/sign_in" class="block">
170+ <input type="hidden" name="rd" value="{{.Redirect}}">
171+
172+ <div class="field">
173+ <label class="label" for="username">Username</label>
174+ <div class="control">
175+ <input class="input" type="email" placeholder="e.g. userx@example.com" name="username" id="username">
176+ </div>
177+ </div>
178+
179+ <div class="field">
180+ <label class="label" for="password">Password</label>
181+ <div class="control">
182+ <input class="input" type="password" placeholder="********" name="password" id="password">
183+ </div>
184+ </div>
185+ <button class="button is-primary">Sign in</button>
186+ {{ end }}
187+ </form>
188+ </div>
189+ </section>
190+
191+ <footer class="footer has-text-grey has-background-light is-size-7">
192+ <div class="content has-text-centered">
193+ {{ if eq .Footer "-" }}
194+ {{ else if eq .Footer ""}}
195+ <p>Secured with <a href="https://github.com/oauth2-proxy/oauth2-proxy#oauth2_proxy" class="has-text-grey">OAuth2 Proxy</a> version {{.Version}}</p>
196+ {{ else }}
197+ <p>{{.Footer}}</p>
198+ {{ end }}
199+ </div>
200+ </footer>
201+
202+ </body>
203+</html>
204+{{end}}`
205+)
206+
207+// LoadTemplates adds the Sign In and Error templates from the custom template
208+// directory, or uses the defaults if they do not exist or the custom directory
209+// is not provided.
210+func LoadTemplates(customDir string) (*template.Template, error) {
211+ t := template.New("").Funcs(template.FuncMap{
212+ "ToUpper": strings.ToUpper,
213+ "ToLower": strings.ToLower,
214+ })
215+ var err error
216+ t, err = addTemplate(t, customDir, signInTemplateName, defaultSignInTemplate)
217+ if err != nil {
218+ return nil, fmt.Errorf("could not add Sign In template: %v", err)
219+ }
220+ t, err = addTemplate(t, customDir, errorTemplateName, defaultErrorTemplate)
221+ if err != nil {
222+ return nil, fmt.Errorf("could not add Error template: %v", err)
223+ }
224+
225+ return t, nil
226+}
227+
228+// addTemplate will add the template from the custom directory if provided,
229+// else it will add the default template.
230+func addTemplate(t *template.Template, customDir, fileName, defaultTemplate string) (*template.Template, error) {
231+ filePath := filepath.Join(customDir, fileName)
232+ if customDir != "" && isFile(filePath) {
233+ t, err := t.ParseFiles(filePath)
234+ if err != nil {
235+ return nil, fmt.Errorf("failed to parse template %s: %v", filePath, err)
236+ }
237+ return t, nil
238+ }
239+ t, err := t.Parse(defaultTemplate)
240+ if err != nil {
241+ // This should not happen.
242+ // Default templates should be tested and so should never fail to parse.
243+ logger.Panic("Could not parse defaultTemplate: ", err)
244+ }
245+ return t, nil
246+}
247+
248+// isFile checks if the file exists and checks whether it is a regular file.
249+// If either of these fail then it cannot be used as a template file.
250+func isFile(fileName string) bool {
251+ info, err := os.Stat(fileName)
219252 if err != nil {
220− logger.Fatalf("failed parsing template %s", err)
253+ logger.Errorf("Could not load file %s: %v, will use default template", fileName, err)
254+ return false
221255 }
222− return t
256+ return info.Mode().IsRegular()
223257}