Skip to content

Commit d01c10e

Browse files
committed
Add whitelist
1 parent 28f61b7 commit d01c10e

File tree

4 files changed

+57
-3
lines changed

4 files changed

+57
-3
lines changed

api/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ func Main(ctx *goru.Context) {
1717
gorux.ResponseJSON(ctx, http.StatusOK, Error("Anduin OAUTH proxy version "+service.Version()))
1818
return
1919
}
20+
if service.CheckWhitelist(ctx, p) {
21+
service.ReverseProxy(ctx, p, nil)
22+
return
23+
}
2024
user := service.CheckSession(ctx)
2125
if user != nil {
2226
service.ReverseProxy(ctx, p, user)

proxy/proxy.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
1523
type 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+
4572
func (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())

service/misc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
)
77

88
func Version() string {
9-
return "0.3.0"
9+
return "0.4.0"
1010
}
1111

1212
func generateRandomState() (string, error) {

service/proxy.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ func DoRedirect(ctx *goru.Context, prox *proxy.Proxy) {
3030
goru.Redirect(ctx, redirectURI)
3131
}
3232

33+
func CheckWhitelist(ctx *goru.Context, prox *proxy.Proxy) bool {
34+
return prox.IsWhiteList(ctx.Request.Method, ctx.Request.URL.Path)
35+
}
36+
3337
func CheckSession(ctx *goru.Context) *proxy.UserInfo {
3438
authCookie, err := ctx.Request.Cookie(proxy.Config.CookieName)
3539
if err != nil {
@@ -61,8 +65,10 @@ func CheckSession(ctx *goru.Context) *proxy.UserInfo {
6165
}
6266

6367
func ReverseProxy(ctx *goru.Context, prox *proxy.Proxy, user *proxy.UserInfo) {
64-
ctx.Request.Header.Add("X-Forwarded-User", user.Name)
65-
ctx.Request.Header.Add("X-Forwarded-Email", user.Email)
68+
if user != nil {
69+
ctx.Request.Header.Add("X-Forwarded-User", user.Name)
70+
ctx.Request.Header.Add("X-Forwarded-Email", user.Email)
71+
}
6672
log.Debugf("Reverse proxy for %s to %s", prox.RequestHost, ctx.Request.URL.String())
6773
prox.ServeHTTP(ctx.ResponseWriter, ctx.Request)
6874
}

0 commit comments

Comments
 (0)