git.iko.soy/typeable/oauth2-proxy
on master help sign in
Adding Support for multi white listed urls with regex url match.
commitc4d25d271fb80a03bb97d4b668eb2e47132827be authorvishnu chilamakuru <vishnu.chilamakuru@olacabs.com> dateMon 12 Jan 2015, 14:48 (12 years ago) parenta80b931
3 files changed+31
main.go+2 oauthproxy.go+18 options.go+11
main.go+2
@@ -19,6 +19,7 @@ func main() {
1919
2020 googleAppsDomains := StringArray{}
2121 upstreams := StringArray{}
22+ skipAuthRegex := StringArray{}
2223
2324 config := flagSet.String("config", "", "path to config file")
2425 showVersion := flagSet.Bool("version", false, "print version string")
@@ -27,6 +28,7 @@ func main() {
2728 flagSet.String("redirect-url", "", "the OAuth Redirect URL. ie: \"https://internalapp.yourcompany.com/oauth2/callback\"")
2829 flagSet.Var(&upstreams, "upstream", "the http url(s) of the upstream endpoint. If multiple, routing is based on path")
2930 flagSet.Bool("pass-basic-auth", true, "pass HTTP Basic Auth, X-Forwarded-User and X-Forwarded-Email information to upstream")
31+ flagSet.Var(&skipAuthRegex, "skip-auth-regex", "bypass authentication for requests path's that match (may be given multiple times)")
3032
3133 flagSet.Var(&googleAppsDomains, "google-apps-domain", "authenticate against the given Google apps domain (may be given multiple times)")
3234 flagSet.String("client-id", "", "the Google OAuth Client ID: ie: \"123456.apps.googleusercontent.com\"")
oauthproxy.go+18
@@ -12,6 +12,7 @@ import (
1212 "net/url"
1313 "strings"
1414 "time"
15+ "regexp"
1516
1617 "github.com/bitly/go-simplejson"
1718)
@@ -40,6 +41,8 @@ type OauthProxy struct {
4041 DisplayHtpasswdForm bool
4142 serveMux *http.ServeMux
4243 PassBasicAuth bool
44+ skipAuthRegex []string
45+ compiledRegex []*regexp.Regexp
4346}
4447
4548func NewOauthProxy(opts *Options, validator func(string) bool) *OauthProxy {
@@ -52,6 +55,10 @@ func NewOauthProxy(opts *Options, validator func(string) bool) *OauthProxy {
5255 log.Printf("mapping path %q => upstream %q", path, u)
5356 serveMux.Handle(path, httputil.NewSingleHostReverseProxy(u))
5457 }
58+ for _, u := range opts.CompiledRegex {
59+ log.Printf("compiled skip-auth-regex => %q", u)
60+ }
61+
5562 redirectUrl := opts.redirectUrl
5663 redirectUrl.Path = oauthCallbackPath
5764
@@ -76,6 +83,8 @@ func NewOauthProxy(opts *Options, validator func(string) bool) *OauthProxy {
7683 oauthLoginUrl: login,
7784 serveMux: serveMux,
7885 redirectUrl: redirectUrl,
86+ skipAuthRegex: opts.SkipAuthRegex,
87+ compiledRegex: opts.CompiledRegex,
7988 PassBasicAuth: opts.PassBasicAuth,
8089 }
8190}
@@ -299,6 +308,15 @@ func (p *OauthProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
299308 return
300309 }
301310
311+ for _, u := range p.compiledRegex {
312+ match := u.MatchString(req.URL.Path)
313+ if match {
314+ p.serveMux.ServeHTTP(rw, req)
315+ return
316+ }
317+
318+ }
319+
302320 if req.URL.Path == signInPath {
303321 redirect, err := p.GetRedirect(req)
304322 if err != nil {
options.go+11
@@ -5,6 +5,7 @@ import (
55 "fmt"
66 "net/url"
77 "time"
8+ "regexp"
89)
910
1011// Configuration Options that can be set by Command Line Flag, or Config File
@@ -23,10 +24,12 @@ type Options struct {
2324 AuthenticatedEmailsFile string `flag:"authenticated-emails-file" cfg:"authenticated_emails_file"`
2425 GoogleAppsDomains []string `flag:"google-apps-domain" cfg:"google_apps_domains"`
2526 Upstreams []string `flag:"upstream" cfg:"upstreams"`
27+ SkipAuthRegex []string `flag:"skip-auth-regex" cfg:"skip_auth_regex"`
2628
2729 // internal values that are set after config validation
2830 redirectUrl *url.URL
2931 proxyUrls []*url.URL
32+ CompiledRegex []*regexp.Regexp
3033}
3134
3235func NewOptions() *Options {
@@ -70,5 +73,13 @@ func (o *Options) Validate() error {
7073 o.proxyUrls = append(o.proxyUrls, upstreamUrl)
7174 }
7275
76+ for _, u := range o.SkipAuthRegex {
77+ CompiledRegex, err := regexp.Compile(u)
78+ if err != nil {
79+ return fmt.Errorf("error compiling regex=%q %s", u, err)
80+ }
81+ o.CompiledRegex = append(o.CompiledRegex, CompiledRegex)
82+ }
83+
7384 return nil
7485}