| 1 |
package auth |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/base64" |
| 6 |
"io/ioutil" |
| 7 |
"net/http" |
| 8 |
"strings" |
| 9 |
|
| 10 |
"github.com/elazarl/goproxy" |
| 11 |
) |
| 12 |
|
| 13 |
var unauthorizedMsg = []byte("407 Proxy Authentication Required") |
| 14 |
|
| 15 |
func BasicUnauthorized(req *http.Request, realm string) *http.Response { |
| 16 |
// TODO(elazar): verify realm is well formed |
| 17 |
return &http.Response{ |
| 18 |
StatusCode: 407, |
| 19 |
ProtoMajor: 1, |
| 20 |
ProtoMinor: 1, |
| 21 |
Request: req, |
| 22 |
Header: http.Header{ |
| 23 |
"Proxy-Authenticate": []string{"Basic realm=" + realm}, |
| 24 |
"Proxy-Connection": []string{"close"}, |
| 25 |
}, |
| 26 |
Body: ioutil.NopCloser(bytes.NewBuffer(unauthorizedMsg)), |
| 27 |
ContentLength: int64(len(unauthorizedMsg)), |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
var proxyAuthorizationHeader = "Proxy-Authorization" |
| 32 |
|
| 33 |
func auth(req *http.Request, f func(user, passwd string) bool) bool { |
| 34 |
authheader := strings.SplitN(req.Header.Get(proxyAuthorizationHeader), " ", 2) |
| 35 |
req.Header.Del(proxyAuthorizationHeader) |
| 36 |
if len(authheader) != 2 || authheader[0] != "Basic" { |
| 37 |
return false |
| 38 |
} |
| 39 |
userpassraw, err := base64.StdEncoding.DecodeString(authheader[1]) |
| 40 |
if err != nil { |
| 41 |
return false |
| 42 |
} |
| 43 |
userpass := strings.SplitN(string(userpassraw), ":", 2) |
| 44 |
if len(userpass) != 2 { |
| 45 |
return false |
| 46 |
} |
| 47 |
return f(userpass[0], userpass[1]) |
| 48 |
} |
| 49 |
|
| 50 |
// Basic returns a basic HTTP authentication handler for requests |
| 51 |
// |
| 52 |
// You probably want to use auth.ProxyBasic(proxy) to enable authentication for all proxy activities |
| 53 |
func Basic(realm string, f func(user, passwd string) bool) goproxy.ReqHandler { |
| 54 |
return goproxy.FuncReqHandler(func(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) { |
| 55 |
if !auth(req, f) { |
| 56 |
return nil, BasicUnauthorized(req, realm) |
| 57 |
} |
| 58 |
return req, nil |
| 59 |
}) |
| 60 |
} |
| 61 |
|
| 62 |
// BasicConnect returns a basic HTTP authentication handler for CONNECT requests |
| 63 |
// |
| 64 |
// You probably want to use auth.ProxyBasic(proxy) to enable authentication for all proxy activities |
| 65 |
func BasicConnect(realm string, f func(user, passwd string) bool) goproxy.HttpsHandler { |
| 66 |
return goproxy.FuncHttpsHandler(func(host string, ctx *goproxy.ProxyCtx) (*goproxy.ConnectAction, string) { |
| 67 |
if !auth(ctx.Req, f) { |
| 68 |
ctx.Resp = BasicUnauthorized(ctx.Req, realm) |
| 69 |
return goproxy.RejectConnect, host |
| 70 |
} |
| 71 |
return nil, host |
| 72 |
}) |
| 73 |
} |
| 74 |
|
| 75 |
// ProxyBasic will force HTTP authentication before any request to the proxy is processed |
| 76 |
func ProxyBasic(proxy *goproxy.ProxyHttpServer, realm string, f func(user, passwd string) bool) { |
| 77 |
proxy.OnRequest().Do(Basic(realm, f)) |
| 78 |
proxy.OnRequest().HandleConnect(BasicConnect(realm, f)) |
| 79 |
} |