Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic auth fixed #428

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type ProxyCtx struct {
Session int64
certStore CertStorage
Proxy *ProxyHttpServer
// Will prevent second authentication on the already authenticated requests
Authenticated bool
}

type RoundTripper interface {
Expand Down
8 changes: 5 additions & 3 deletions ext/auth/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ func auth(req *http.Request, f func(user, passwd string) bool) bool {
// You probably want to use auth.ProxyBasic(proxy) to enable authentication for all proxy activities
func Basic(realm string, f func(user, passwd string) bool) goproxy.ReqHandler {
return goproxy.FuncReqHandler(func(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
if !auth(req, f) {
if !ctx.Authenticated && !auth(req, f) {
return nil, BasicUnauthorized(req, realm)
}
ctx.Authenticated = true
return req, nil
})
}
Expand All @@ -64,11 +65,12 @@ func Basic(realm string, f func(user, passwd string) bool) goproxy.ReqHandler {
// You probably want to use auth.ProxyBasic(proxy) to enable authentication for all proxy activities
func BasicConnect(realm string, f func(user, passwd string) bool) goproxy.HttpsHandler {
return goproxy.FuncHttpsHandler(func(host string, ctx *goproxy.ProxyCtx) (*goproxy.ConnectAction, string) {
if !auth(ctx.Req, f) {
if !ctx.Authenticated && !auth(ctx.Req, f) {
ctx.Resp = BasicUnauthorized(ctx.Req, realm)
return goproxy.RejectConnect, host
}
return goproxy.OkConnect, host
ctx.Authenticated = true
return nil, host
})
}

Expand Down
2 changes: 1 addition & 1 deletion https.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func (proxy *ProxyHttpServer) handleHttps(w http.ResponseWriter, r *http.Request
clientTlsReader := bufio.NewReader(rawClientTls)
for !isEof(clientTlsReader) {
req, err := http.ReadRequest(clientTlsReader)
var ctx = &ProxyCtx{Req: req, Session: atomic.AddInt64(&proxy.sess, 1), Proxy: proxy, UserData: ctx.UserData}
var ctx = &ProxyCtx{Req: req, Session: atomic.AddInt64(&proxy.sess, 1), Proxy: proxy, UserData: ctx.UserData, Authenticated: ctx.Authenticated}
if err != nil && err != io.EOF {
return
}
Expand Down