Skip to content

Commit bb55e6a

Browse files
committed
new auth provider: reject by HTTP reverse proxy
1 parent fe9e9d9 commit bb55e6a

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

auth/auth.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ func NewAuth(paramstr string, logger *clog.CondLogger) (Auth, error) {
3636
return NewRedisAuth(url, true, logger)
3737
case "none":
3838
return NoAuth{}, nil
39+
case "reject-http", "reject-https":
40+
return NewRejectHTTPAuth(url, logger)
3941
default:
4042
return nil, errors.New("Unknown auth scheme")
4143
}

auth/rejecthttp.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package auth
2+
3+
import (
4+
"context"
5+
"net/http"
6+
"net/http/httputil"
7+
"net/url"
8+
"strings"
9+
10+
clog "github.com/SenseUnit/dumbproxy/log"
11+
)
12+
13+
type RejectHTTPAuth struct {
14+
proxy *httputil.ReverseProxy
15+
}
16+
17+
func NewRejectHTTPAuth(u *url.URL, logger *clog.CondLogger) (*RejectHTTPAuth, error) {
18+
values, err := url.ParseQuery(u.RawQuery)
19+
if err != nil {
20+
return nil, err
21+
}
22+
scheme, _ := strings.CutPrefix(strings.ToLower(u.Scheme), "reject-")
23+
target := &url.URL{
24+
Scheme: scheme,
25+
User: u.User,
26+
Host: u.Host,
27+
Path: u.Path,
28+
RawPath: u.RawPath,
29+
RawQuery: values.Get("qs"),
30+
}
31+
method := values.Get("method")
32+
return &RejectHTTPAuth{
33+
proxy: &httputil.ReverseProxy{
34+
Rewrite: func(r *httputil.ProxyRequest) {
35+
r.Out.URL = target
36+
r.Out.Host = ""
37+
if method != "" {
38+
r.Out.Method = method
39+
}
40+
},
41+
},
42+
}, nil
43+
}
44+
45+
func (a *RejectHTTPAuth) Validate(ctx context.Context, w http.ResponseWriter, r *http.Request) (string, bool) {
46+
r = r.Clone(ctx)
47+
a.proxy.ServeHTTP(w, r)
48+
return "", false
49+
}
50+
51+
func (_ *RejectHTTPAuth) Valid(_, _, _ string) bool {
52+
return false
53+
}
54+
55+
func (_ *RejectHTTPAuth) Stop() {}

0 commit comments

Comments
 (0)