git.iko.soy/typeable/oauth2-proxy
on master help sign in
Merge pull request #70 from jehiah/templates_dir_70
85e025dJehiah Czebotar
README.md+1
@@ -75,6 +75,7 @@ Usage of google_auth_proxy:
75 -pass-host-header=true: pass the request Host Header to upstream
75 -pass-host-header=true: pass the request Host Header to upstream
76 -redirect-url="": the OAuth Redirect URL. ie: "https://internalapp.yourcompany.com/oauth2/callback"
76 -redirect-url="": the OAuth Redirect URL. ie: "https://internalapp.yourcompany.com/oauth2/callback"
77 -skip-auth-regex=: bypass authentication for requests path's that match (may be given multiple times)
77 -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
78 -upstream=: the http url(s) of the upstream endpoint. If multiple, routing is based on path
79 -upstream=: the http url(s) of the upstream endpoint. If multiple, routing is based on path
79 -version=false: print version string
80 -version=false: print version string
80```
81```
contrib/google_auth_proxy.cfg.example+4
@@ -36,6 +36,10 @@
36## enabling exposes a username/login signin form
36## enabling exposes a username/login signin form
37# htpasswd_file = ""
37# htpasswd_file = ""
38
38
39+## Templates
40+## optional directory with custom sign_in.html and error.html
41+# custom_templates_dir = ""
42+
39
43
40## Cookie Settings
44## Cookie Settings
41## Secret - the seed string for secure cookies
45## Secret - the seed string for secure cookies
main.go+1
@@ -38,6 +38,7 @@ func main() {
38 flagSet.String("authenticated-emails-file", "", "authenticate against emails via file (one per line)")
38 flagSet.String("authenticated-emails-file", "", "authenticate against emails via file (one per line)")
39 flagSet.String("htpasswd-file", "", "additionally authenticate against a htpasswd file. Entries must be created with \"htpasswd -s\" for SHA encryption")
39 flagSet.String("htpasswd-file", "", "additionally authenticate against a htpasswd file. Entries must be created with \"htpasswd -s\" for SHA encryption")
40 flagSet.Bool("display-htpasswd-form", true, "display username / password login form if an htpasswd file is provided")
40 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")
41
42
42 flagSet.String("cookie-secret", "", "the seed string for secure cookies")
43 flagSet.String("cookie-secret", "", "the seed string for secure cookies")
43 flagSet.String("cookie-domain", "", "an optional cookie domain to force cookies to (ie: .yourcompany.com)*")
44 flagSet.String("cookie-domain", "", "an optional cookie domain to force cookies to (ie: .yourcompany.com)*")
oauthproxy.go+5−4
@@ -5,6 +5,7 @@ import (
5 "encoding/base64"
5 "encoding/base64"
6 "errors"
6 "errors"
7 "fmt"
7 "fmt"
8+ "html/template"
8 "io/ioutil"
9 "io/ioutil"
9 "log"
10 "log"
10 "net/http"
11 "net/http"
@@ -44,6 +45,7 @@ type OauthProxy struct {
44 PassBasicAuth bool
45 PassBasicAuth bool
45 skipAuthRegex []string
46 skipAuthRegex []string
46 compiledRegex []*regexp.Regexp
47 compiledRegex []*regexp.Regexp
48+ templates *template.Template
47}
49}
48
50
49func NewReverseProxy(target *url.URL) (proxy *httputil.ReverseProxy) {
51func NewReverseProxy(target *url.URL) (proxy *httputil.ReverseProxy) {
@@ -103,6 +105,7 @@ func NewOauthProxy(opts *Options, validator func(string) bool) *OauthProxy {
103 skipAuthRegex: opts.SkipAuthRegex,
105 skipAuthRegex: opts.SkipAuthRegex,
104 compiledRegex: opts.CompiledRegex,
106 compiledRegex: opts.CompiledRegex,
105 PassBasicAuth: opts.PassBasicAuth,
107 PassBasicAuth: opts.PassBasicAuth,
108+ templates: loadTemplates(opts.CustomTemplatesDir),
106 }
109 }
107}
110}
108
111
@@ -245,7 +248,6 @@ func (p *OauthProxy) PingPage(rw http.ResponseWriter) {
245func (p *OauthProxy) ErrorPage(rw http.ResponseWriter, code int, title string, message string) {
248func (p *OauthProxy) ErrorPage(rw http.ResponseWriter, code int, title string, message string) {
246 log.Printf("ErrorPage %d %s %s", code, title, message)
249 log.Printf("ErrorPage %d %s %s", code, title, message)
247 rw.WriteHeader(code)
250 rw.WriteHeader(code)
248− templates := getTemplates()
249 t := struct {
251 t := struct {
250 Title string
252 Title string
251 Message string
253 Message string
@@ -253,13 +255,12 @@ func (p *OauthProxy) ErrorPage(rw http.ResponseWriter, code int, title string, m
253 Title: fmt.Sprintf("%d %s", code, title),
255 Title: fmt.Sprintf("%d %s", code, title),
254 Message: message,
256 Message: message,
255 }
257 }
256− templates.ExecuteTemplate(rw, "error.html", t)
258+ p.templates.ExecuteTemplate(rw, "error.html", t)
257}
259}
258
260
259func (p *OauthProxy) SignInPage(rw http.ResponseWriter, req *http.Request, code int) {
261func (p *OauthProxy) SignInPage(rw http.ResponseWriter, req *http.Request, code int) {
260 p.ClearCookie(rw, req)
262 p.ClearCookie(rw, req)
261 rw.WriteHeader(code)
263 rw.WriteHeader(code)
262− templates := getTemplates()
263
264
264 t := struct {
265 t := struct {
265 SignInMessage string
266 SignInMessage string
@@ -272,7 +273,7 @@ func (p *OauthProxy) SignInPage(rw http.ResponseWriter, req *http.Request, code
272 Redirect: req.URL.RequestURI(),
273 Redirect: req.URL.RequestURI(),
273 Version: VERSION,
274 Version: VERSION,
274 }
275 }
275− templates.ExecuteTemplate(rw, "sign_in.html", t)
276+ p.templates.ExecuteTemplate(rw, "sign_in.html", t)
276}
277}
277
278
278func (p *OauthProxy) ManualSignIn(rw http.ResponseWriter, req *http.Request) (string, bool) {
279func (p *OauthProxy) ManualSignIn(rw http.ResponseWriter, req *http.Request) (string, bool) {
options.go+1
@@ -19,6 +19,7 @@ type Options struct {
19 GoogleAppsDomains []string `flag:"google-apps-domain" cfg:"google_apps_domains"`
19 GoogleAppsDomains []string `flag:"google-apps-domain" cfg:"google_apps_domains"`
20 HtpasswdFile string `flag:"htpasswd-file" cfg:"htpasswd_file"`
20 HtpasswdFile string `flag:"htpasswd-file" cfg:"htpasswd_file"`
21 DisplayHtpasswdForm bool `flag:"display-htpasswd-form" cfg:"display_htpasswd_form"`
21 DisplayHtpasswdForm bool `flag:"display-htpasswd-form" cfg:"display_htpasswd_form"`
22+ CustomTemplatesDir string `flag:"custom-templates-dir" cfg:"custom_templates_dir"`
22
23
23 CookieSecret string `flag:"cookie-secret" cfg:"cookie_secret" env:"GOOGLE_AUTH_PROXY_COOKIE_SECRET"`
24 CookieSecret string `flag:"cookie-secret" cfg:"cookie_secret" env:"GOOGLE_AUTH_PROXY_COOKIE_SECRET"`
24 CookieDomain string `flag:"cookie-domain" cfg:"cookie_domain" env:"GOOGLE_AUTH_PROXY_COOKIE_DOMAIN"`
25 CookieDomain string `flag:"cookie-domain" cfg:"cookie_domain" env:"GOOGLE_AUTH_PROXY_COOKIE_DOMAIN"`
templates.go+15−2
@@ -3,8 +3,21 @@ package main
3import (
3import (
4 "html/template"
4 "html/template"
5 "log"
5 "log"
6+ "path"
6)
7)
7
8
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+
8func getTemplates() *template.Template {
21func getTemplates() *template.Template {
9 t, err := template.New("foo").Parse(`{{define "sign_in.html"}}
22 t, err := template.New("foo").Parse(`{{define "sign_in.html"}}
10<!DOCTYPE html>
23<!DOCTYPE html>
@@ -123,7 +136,7 @@ func getTemplates() *template.Template {
123</html>
136</html>
124{{end}}`)
137{{end}}`)
125 if err != nil {
138 if err != nil {
126− log.Fatalf("failed parsing template %s", err.Error())
139+ log.Fatalf("failed parsing template %s", err)
127 }
140 }
128
141
129 t, err = t.Parse(`{{define "error.html"}}
142 t, err = t.Parse(`{{define "error.html"}}
@@ -141,7 +154,7 @@ func getTemplates() *template.Template {
141</body>
154</body>
142</html>{{end}}`)
155</html>{{end}}`)
143 if err != nil {
156 if err != nil {
144− log.Fatalf("failed parsing template %s", err.Error())
157+ log.Fatalf("failed parsing template %s", err)
145 }
158 }
146 return t
159 return t
147}
160}