@@ -8,10 +8,18 @@ import (
88 "strings"
99 "time"
1010
11+ "regexp"
12+
1113 "github.com/anduintransaction/oauth-proxy/utils"
1214 "gottb.io/goru/config"
15+ "gottb.io/goru/log"
1316)
1417
18+ type whilelist struct {
19+ method string
20+ path * regexp.Regexp
21+ }
22+
1523type Proxy struct {
1624 Provider string `config:"provider"`
1725 Scheme string `config:"scheme"`
@@ -24,9 +32,11 @@ type Proxy struct {
2432 CallbackURI string `config:"callback_uri"`
2533 Organizations []string `config:"organizations"`
2634 Teams []string `config:"teams"`
35+ Whitelists []string `config:"whitelists"`
2736 organizations utils.StringSet
2837 teams utils.StringSet
2938 target * url.URL
39+ whitelists []* whilelist
3040 reverseProxy * httputil.ReverseProxy
3141}
3242
@@ -42,6 +52,23 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
4252 p .reverseProxy .ServeHTTP (w , r )
4353}
4454
55+ func (p * Proxy ) IsWhiteList (method , path string ) bool {
56+ for _ , w := range p .whitelists {
57+ if w .method != "ANY" && w .method != method {
58+ continue
59+ }
60+ path = strings .TrimRight (path , "/" )
61+ if path == "" {
62+ path = "/"
63+ }
64+ matched := w .path .MatchString (path )
65+ if matched {
66+ return true
67+ }
68+ }
69+ return false
70+ }
71+
4572func (p * Proxy ) createReverseProxy () {
4673 p .reverseProxy = & httputil.ReverseProxy {
4774 Director : p .transformRequest ,
@@ -133,8 +160,25 @@ func Start(config *config.Config) error {
133160 if err != nil {
134161 return err
135162 }
163+ proxy .whitelists = []* whilelist {}
164+ for _ , wl := range proxy .Whitelists {
165+ w := & whilelist {}
166+ pieces := strings .SplitN (wl , ":" , 2 )
167+ if len (pieces ) == 1 {
168+ w .method = "ANY"
169+ w .path , err = regexp .Compile ("^" + pieces [0 ] + "$" )
170+ } else {
171+ w .method = strings .ToUpper (pieces [0 ])
172+ w .path , err = regexp .Compile ("^" + pieces [1 ] + "$" )
173+ }
174+ if err != nil {
175+ return err
176+ }
177+ proxy .whitelists = append (proxy .whitelists , w )
178+ }
136179 proxy .createReverseProxy ()
137180 proxyMap [proxy .RequestHost ] = proxy
181+ log .Debug (proxy )
138182 }
139183
140184 rand .Seed (time .Now ().UnixNano ())
0 commit comments