git.iko.soy/typeable/oauth2-proxy
on master help sign in
Enable specific oauth2proxy path; change cookie name to _oauth2proxy
commitc5ccd43767c99eac5d0ec73913bdcbcc03a27789 authortonymeng <tony@tonymeng.com> dateFri 29 May 2015, 15:47 (11 years ago) parent71b79ba
6 files changed+40−18
README.md+5−1 contrib/oauth2_proxy.cfg.example+2 main.go+2 oauthproxy.go+24−14 options.go+4 templates.go+3−3
README.md+5−1
@@ -90,17 +90,20 @@ An example [oauth2_proxy.cfg](contrib/oauth2_proxy.cfg.example) config file is i
9090Usage of oauth2_proxy:
9191 -authenticated-emails-file="": authenticate against emails via file (one per line)
9292 -client-id="": the OAuth Client ID: ie: "123456.apps.googleusercontent.com"
93− -client-secret="": the Client Secret
93+ -client-secret="": the OAuth Client Secret
9494 -config="": path to config file
9595 -cookie-domain="": an optional cookie domain to force cookies to (ie: .yourcompany.com)*
9696 -cookie-expire=168h0m0s: expire timeframe for cookie
9797 -cookie-httponly=true: set HttpOnly cookie flag
9898 -cookie-https-only=true: set secure (HTTPS) cookies (deprecated. use --cookie-secure setting)
99+ -cookie-key="_oauth2proxy": the name of the cookie that the oauth_proxy creates
99100 -cookie-refresh=0: refresh the cookie when less than this much time remains before expiration; 0 to disable
100101 -cookie-secret="": the seed string for secure cookies
101102 -cookie-secure=true: set secure (HTTPS) cookie flag
102103 -custom-templates-dir="": path to custom html templates
103104 -display-htpasswd-form=true: display username / password login form if an htpasswd file is provided
105+ -github-org="": restrict logins to members of this organisation
106+ -github-team="": restrict logins to members of this team
104107 -google-apps-domain=: authenticate against the given Google apps domain (may be given multiple times)
105108 -htpasswd-file="": additionally authenticate against a htpasswd file. Entries must be created with "htpasswd -s" for SHA encryption
106109 -http-address="127.0.0.1:4180": [http://]<addr>:<port> or unix://<path> to listen on for HTTP clients
@@ -110,6 +113,7 @@ Usage of oauth2_proxy:
110113 -pass-host-header=true: pass the request Host Header to upstream
111114 -profile-url="": Profile access endpoint
112115 -provider="": Oauth provider (defaults to Google)
116+ -proxy-prefix="/oauth2": the url root path that this proxy should be nested under (e.g. /<oauth2>/sign_in)
113117 -redeem-url="": Token redemption endpoint
114118 -redirect-url="": the OAuth Redirect URL. ie: "https://internalapp.yourcompany.com/oauth2/callback"
115119 -request-logging=true: Log requests to stdout
contrib/oauth2_proxy.cfg.example+2
@@ -47,6 +47,7 @@
4747# custom_templates_dir = ""
4848
4949## Cookie Settings
50+## Key - the cookie name
5051## Secret - the seed string for secure cookies; should be 16, 24, or 32 bytes
5152## for use with an AES cipher when cookie_refresh or pass_access_token
5253## is set
@@ -57,6 +58,7 @@
5758## Refresh revalidated the OAuth token to ensure it is still valid. ie: 24h
5859## Secure - secure cookies are only sent by the browser of a HTTPS connection (recommended)
5960## HttpOnly - httponly cookies are not readable by javascript (recommended)
61+# cookie_key = "_oauth2proxy"
6062# cookie_secret = ""
6163# cookie_domain = ""
6264# cookie_expire = "168h"
main.go+2
@@ -44,7 +44,9 @@ func main() {
4444 flagSet.String("htpasswd-file", "", "additionally authenticate against a htpasswd file. Entries must be created with \"htpasswd -s\" for SHA encryption")
4545 flagSet.Bool("display-htpasswd-form", true, "display username / password login form if an htpasswd file is provided")
4646 flagSet.String("custom-templates-dir", "", "path to custom html templates")
47+ flagSet.String("proxy-prefix", "/oauth2", "the url root path that this proxy should be nested under (e.g. /<oauth2>/sign_in)")
4748
49+ flagSet.String("cookie-key", "_oauth2proxy", "the name of the cookie that the oauth_proxy creates")
4850 flagSet.String("cookie-secret", "", "the seed string for secure cookies")
4951 flagSet.String("cookie-domain", "", "an optional cookie domain to force cookies to (ie: .yourcompany.com)*")
5052 flagSet.Duration("cookie-expire", time.Duration(168)*time.Hour, "expire timeframe for cookie")
oauthproxy.go+24−14
@@ -19,12 +19,6 @@ import (
1919 "github.com/bitly/oauth2_proxy/providers"
2020)
2121
22−const robotsPath = "/robots.txt"
23−const pingPath = "/ping"
24−const signInPath = "/oauth2/sign_in"
25−const oauthStartPath = "/oauth2/start"
26−const oauthCallbackPath = "/oauth2/callback"
27−
2822type OauthProxy struct {
2923 CookieSeed string
3024 CookieKey string
@@ -35,6 +29,12 @@ type OauthProxy struct {
3529 CookieRefresh time.Duration
3630 Validator func(string) bool
3731
32+ RobotsPath string
33+ PingPath string
34+ SignInPath string
35+ OauthStartPath string
36+ OauthCallbackPath string
37+
3838 redirectUrl *url.URL // the url to receive requests at
3939 provider providers.Provider
4040 oauthLoginUrl *url.URL // to redirect the user to
@@ -42,6 +42,7 @@ type OauthProxy struct {
4242 oauthScope string
4343 clientID string
4444 clientSecret string
45+ ProxyPrefix string
4546 SignInMessage string
4647 HtpasswdFile *HtpasswdFile
4748 DisplayHtpasswdForm bool
@@ -106,7 +107,7 @@ func NewOauthProxy(opts *Options, validator func(string) bool) *OauthProxy {
106107 }
107108
108109 redirectUrl := opts.redirectUrl
109− redirectUrl.Path = oauthCallbackPath
110+ redirectUrl.Path = fmt.Sprintf("%s/callback", opts.ProxyPrefix)
110111
111112 log.Printf("OauthProxy configured for %s", opts.ClientID)
112113 domain := opts.CookieDomain
@@ -131,7 +132,7 @@ func NewOauthProxy(opts *Options, validator func(string) bool) *OauthProxy {
131132 }
132133
133134 return &OauthProxy{
134− CookieKey: "_oauthproxy",
135+ CookieKey: opts.CookieKey,
135136 CookieSeed: opts.CookieSecret,
136137 CookieDomain: opts.CookieDomain,
137138 CookieSecure: opts.CookieSecure,
@@ -140,8 +141,15 @@ func NewOauthProxy(opts *Options, validator func(string) bool) *OauthProxy {
140141 CookieRefresh: opts.CookieRefresh,
141142 Validator: validator,
142143
144+ RobotsPath: "/robots.txt",
145+ PingPath: "/ping",
146+ SignInPath: fmt.Sprintf("%s/sign_in", opts.ProxyPrefix),
147+ OauthStartPath: fmt.Sprintf("%s/start", opts.ProxyPrefix),
148+ OauthCallbackPath: fmt.Sprintf("%s/callback", opts.ProxyPrefix),
149+
143150 clientID: opts.ClientID,
144151 clientSecret: opts.ClientSecret,
152+ ProxyPrefix: opts.ProxyPrefix,
145153 oauthScope: opts.provider.Data().Scope,
146154 provider: opts.provider,
147155 oauthLoginUrl: opts.provider.Data().LoginUrl,
@@ -300,7 +308,7 @@ func (p *OauthProxy) SignInPage(rw http.ResponseWriter, req *http.Request, code
300308 rw.WriteHeader(code)
301309
302310 redirect_url := req.URL.RequestURI()
303− if redirect_url == signInPath {
311+ if redirect_url == p.SignInPath {
304312 redirect_url = "/"
305313 }
306314
@@ -310,12 +318,14 @@ func (p *OauthProxy) SignInPage(rw http.ResponseWriter, req *http.Request, code
310318 CustomLogin bool
311319 Redirect string
312320 Version string
321+ ProxyPrefix string
313322 }{
314323 ProviderName: p.provider.Data().ProviderName,
315324 SignInMessage: p.SignInMessage,
316325 CustomLogin: p.displayCustomLoginForm(),
317326 Redirect: redirect_url,
318327 Version: VERSION,
328+ ProxyPrefix: p.ProxyPrefix,
319329 }
320330 p.templates.ExecuteTemplate(rw, "sign_in.html", t)
321331}
@@ -365,12 +375,12 @@ func (p *OauthProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
365375 var email string
366376 var access_token string
367377
368− if req.URL.Path == robotsPath {
378+ if req.URL.Path == p.RobotsPath {
369379 p.RobotsTxt(rw)
370380 return
371381 }
372382
373− if req.URL.Path == pingPath {
383+ if req.URL.Path == p.PingPath {
374384 p.PingPage(rw)
375385 return
376386 }
@@ -384,7 +394,7 @@ func (p *OauthProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
384394
385395 }
386396
387− if req.URL.Path == signInPath {
397+ if req.URL.Path == p.SignInPath {
388398 redirect, err := p.GetRedirect(req)
389399 if err != nil {
390400 p.ErrorPage(rw, 500, "Internal Error", err.Error())
@@ -400,7 +410,7 @@ func (p *OauthProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
400410 }
401411 return
402412 }
403− if req.URL.Path == oauthStartPath {
413+ if req.URL.Path == p.OauthStartPath {
404414 redirect, err := p.GetRedirect(req)
405415 if err != nil {
406416 p.ErrorPage(rw, 500, "Internal Error", err.Error())
@@ -409,7 +419,7 @@ func (p *OauthProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
409419 http.Redirect(rw, req, p.GetLoginURL(req.Host, redirect), 302)
410420 return
411421 }
412− if req.URL.Path == oauthCallbackPath {
422+ if req.URL.Path == p.OauthCallbackPath {
413423 // finish the oauth cycle
414424 err := req.ParseForm()
415425 if err != nil {
options.go+4
@@ -12,6 +12,7 @@ import (
1212
1313// Configuration Options that can be set by Command Line Flag, or Config File
1414type Options struct {
15+ ProxyPrefix string `flag:"proxy-prefix" cfg:"proxy-prefix"`
1516 HttpAddress string `flag:"http-address" cfg:"http_address"`
1617 RedirectUrl string `flag:"redirect-url" cfg:"redirect_url"`
1718 ClientID string `flag:"client-id" cfg:"client_id" env:"OAUTH2_PROXY_CLIENT_ID"`
@@ -25,6 +26,7 @@ type Options struct {
2526 DisplayHtpasswdForm bool `flag:"display-htpasswd-form" cfg:"display_htpasswd_form"`
2627 CustomTemplatesDir string `flag:"custom-templates-dir" cfg:"custom_templates_dir"`
2728
29+ CookieKey string `flag:"cookie-key" cfg:"cookie_key" env:"OAUTH2_PROXY_COOKIE_KEY"`
2830 CookieSecret string `flag:"cookie-secret" cfg:"cookie_secret" env:"OAUTH2_PROXY_COOKIE_SECRET"`
2931 CookieDomain string `flag:"cookie-domain" cfg:"cookie_domain" env:"OAUTH2_PROXY_COOKIE_DOMAIN"`
3032 CookieExpire time.Duration `flag:"cookie-expire" cfg:"cookie_expire" env:"OAUTH2_PROXY_COOKIE_EXPIRE"`
@@ -59,8 +61,10 @@ type Options struct {
5961
6062func NewOptions() *Options {
6163 return &Options{
64+ ProxyPrefix: "/oauth2",
6265 HttpAddress: "127.0.0.1:4180",
6366 DisplayHtpasswdForm: true,
67+ CookieKey: "_oauthproxy",
6468 CookieHttpsOnly: true,
6569 CookieSecure: true,
6670 CookieHttpOnly: true,
templates.go+3−3
@@ -110,7 +110,7 @@ func getTemplates() *template.Template {
110110</head>
111111<body>
112112 <div class="signin center">
113− <form method="GET" action="/oauth2/start">
113+ <form method="GET" action="{{.ProxyPrefix}}/start">
114114 <input type="hidden" name="rd" value="{{.Redirect}}">
115115 {{ if .SignInMessage }}
116116 <p>{{.SignInMessage}}</p>
@@ -121,7 +121,7 @@ func getTemplates() *template.Template {
121121
122122 {{ if .CustomLogin }}
123123 <div class="signin">
124− <form method="POST" action="/oauth2/sign_in">
124+ <form method="POST" action="{{.ProxyPrefix}}/sign_in">
125125 <input type="hidden" name="rd" value="{{.Redirect}}">
126126 <label for="username">Username:</label><input type="text" name="username" id="username" size="10"><br/>
127127 <label for="password">Password:</label><input type="password" name="password" id="password" size="10"><br/>
@@ -150,7 +150,7 @@ func getTemplates() *template.Template {
150150 <h2>{{.Title}}</h2>
151151 <p>{{.Message}}</p>
152152 <hr>
153− <p><a href="/oauth2/sign_in">Sign In</a></p>
153+ <p><a href="{{.ProxyPrefix}}/sign_in">Sign In</a></p>
154154</body>
155155</html>{{end}}`)
156156 if err != nil {