Skip to content
This repository was archived by the owner on Jan 24, 2019. It is now read-only.

Commit 85e025d

Browse files
committed
Merge pull request #70 from jehiah/templates_dir_70
Custom Sign In Template Support
2 parents b2dfbd8 + 2b2324e commit 85e025d

6 files changed

Lines changed: 27 additions & 6 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ Usage of google_auth_proxy:
7575
-pass-host-header=true: pass the request Host Header to upstream
7676
-redirect-url="": the OAuth Redirect URL. ie: "https://internalapp.yourcompany.com/oauth2/callback"
7777
-skip-auth-regex=: bypass authentication for requests path's that match (may be given multiple times)
78+
-custom templates-dir="": path to custom html templates
7879
-upstream=: the http url(s) of the upstream endpoint. If multiple, routing is based on path
7980
-version=false: print version string
8081
```

contrib/google_auth_proxy.cfg.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
## enabling exposes a username/login signin form
3737
# htpasswd_file = ""
3838

39+
## Templates
40+
## optional directory with custom sign_in.html and error.html
41+
# custom_templates_dir = ""
42+
3943

4044
## Cookie Settings
4145
## Secret - the seed string for secure cookies

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func main() {
3838
flagSet.String("authenticated-emails-file", "", "authenticate against emails via file (one per line)")
3939
flagSet.String("htpasswd-file", "", "additionally authenticate against a htpasswd file. Entries must be created with \"htpasswd -s\" for SHA encryption")
4040
flagSet.Bool("display-htpasswd-form", true, "display username / password login form if an htpasswd file is provided")
41+
flagSet.String("custom-templates-dir", "", "path to custom html templates")
4142

4243
flagSet.String("cookie-secret", "", "the seed string for secure cookies")
4344
flagSet.String("cookie-domain", "", "an optional cookie domain to force cookies to (ie: .yourcompany.com)*")

oauthproxy.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/base64"
66
"errors"
77
"fmt"
8+
"html/template"
89
"io/ioutil"
910
"log"
1011
"net/http"
@@ -44,6 +45,7 @@ type OauthProxy struct {
4445
PassBasicAuth bool
4546
skipAuthRegex []string
4647
compiledRegex []*regexp.Regexp
48+
templates *template.Template
4749
}
4850

4951
func NewReverseProxy(target *url.URL) (proxy *httputil.ReverseProxy) {
@@ -103,6 +105,7 @@ func NewOauthProxy(opts *Options, validator func(string) bool) *OauthProxy {
103105
skipAuthRegex: opts.SkipAuthRegex,
104106
compiledRegex: opts.CompiledRegex,
105107
PassBasicAuth: opts.PassBasicAuth,
108+
templates: loadTemplates(opts.CustomTemplatesDir),
106109
}
107110
}
108111

@@ -245,21 +248,19 @@ func (p *OauthProxy) PingPage(rw http.ResponseWriter) {
245248
func (p *OauthProxy) ErrorPage(rw http.ResponseWriter, code int, title string, message string) {
246249
log.Printf("ErrorPage %d %s %s", code, title, message)
247250
rw.WriteHeader(code)
248-
templates := getTemplates()
249251
t := struct {
250252
Title string
251253
Message string
252254
}{
253255
Title: fmt.Sprintf("%d %s", code, title),
254256
Message: message,
255257
}
256-
templates.ExecuteTemplate(rw, "error.html", t)
258+
p.templates.ExecuteTemplate(rw, "error.html", t)
257259
}
258260

259261
func (p *OauthProxy) SignInPage(rw http.ResponseWriter, req *http.Request, code int) {
260262
p.ClearCookie(rw, req)
261263
rw.WriteHeader(code)
262-
templates := getTemplates()
263264

264265
t := struct {
265266
SignInMessage string
@@ -272,7 +273,7 @@ func (p *OauthProxy) SignInPage(rw http.ResponseWriter, req *http.Request, code
272273
Redirect: req.URL.RequestURI(),
273274
Version: VERSION,
274275
}
275-
templates.ExecuteTemplate(rw, "sign_in.html", t)
276+
p.templates.ExecuteTemplate(rw, "sign_in.html", t)
276277
}
277278

278279
func (p *OauthProxy) ManualSignIn(rw http.ResponseWriter, req *http.Request) (string, bool) {

options.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type Options struct {
1919
GoogleAppsDomains []string `flag:"google-apps-domain" cfg:"google_apps_domains"`
2020
HtpasswdFile string `flag:"htpasswd-file" cfg:"htpasswd_file"`
2121
DisplayHtpasswdForm bool `flag:"display-htpasswd-form" cfg:"display_htpasswd_form"`
22+
CustomTemplatesDir string `flag:"custom-templates-dir" cfg:"custom_templates_dir"`
2223

2324
CookieSecret string `flag:"cookie-secret" cfg:"cookie_secret" env:"GOOGLE_AUTH_PROXY_COOKIE_SECRET"`
2425
CookieDomain string `flag:"cookie-domain" cfg:"cookie_domain" env:"GOOGLE_AUTH_PROXY_COOKIE_DOMAIN"`

templates.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,21 @@ package main
33
import (
44
"html/template"
55
"log"
6+
"path"
67
)
78

9+
func loadTemplates(dir string) *template.Template {
10+
if dir == "" {
11+
return getTemplates()
12+
}
13+
log.Printf("using custom template directory %q", dir)
14+
t, err := template.New("").ParseFiles(path.Join(dir, "sign_in.html"), path.Join(dir, "error.html"))
15+
if err != nil {
16+
log.Fatalf("failed parsing template %s", err)
17+
}
18+
return t
19+
}
20+
821
func getTemplates() *template.Template {
922
t, err := template.New("foo").Parse(`{{define "sign_in.html"}}
1023
<!DOCTYPE html>
@@ -123,7 +136,7 @@ func getTemplates() *template.Template {
123136
</html>
124137
{{end}}`)
125138
if err != nil {
126-
log.Fatalf("failed parsing template %s", err.Error())
139+
log.Fatalf("failed parsing template %s", err)
127140
}
128141

129142
t, err = t.Parse(`{{define "error.html"}}
@@ -141,7 +154,7 @@ func getTemplates() *template.Template {
141154
</body>
142155
</html>{{end}}`)
143156
if err != nil {
144-
log.Fatalf("failed parsing template %s", err.Error())
157+
log.Fatalf("failed parsing template %s", err)
145158
}
146159
return t
147160
}

0 commit comments

Comments
 (0)