MCPcopy Create free account
hub / github.com/SpectoLabs/hoverfly / proxyBasicAndBearer

Function proxyBasicAndBearer

core/proxy.go:195–216  ·  view source on GitHub ↗
(proxy *goproxy.ProxyHttpServer, realm string, basicFunc func(user, passwd string) bool, bearerFunc func(token string) bool)

Source from the content-addressed store, hash-verified

193}
194
195func proxyBasicAndBearer(proxy *goproxy.ProxyHttpServer, realm string, basicFunc func(user, passwd string) bool, bearerFunc func(token string) bool) {
196
197 proxy.OnRequest().Do(goproxy.FuncReqHandler(func(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
198 if strings.HasSuffix(req.URL.Host, ":443") {
199 return req, nil
200 }
201 err := authFromHeader(req, basicFunc, bearerFunc)
202 if err != nil {
203 return nil, unauthorizedError(req, realm, err.Error())
204 }
205 return req, nil
206 }))
207
208 proxy.OnRequest().HandleConnect(goproxy.FuncHttpsHandler(func(host string, ctx *goproxy.ProxyCtx) (*goproxy.ConnectAction, string) {
209 err := authFromHeader(ctx.Req, basicFunc, bearerFunc)
210 if err != nil {
211 ctx.Resp = unauthorizedError(ctx.Req, realm, err.Error())
212 return goproxy.RejectConnect, host
213 }
214 return goproxy.MitmConnect, host
215 }))
216}
217
218func authFromHeader(req *http.Request, basicFunc func(user, passwd string) bool, bearerFunc func(token string) bool) error {
219 headerValue := req.Header.Get(ProxyAuthorizationHeader)

Callers 1

NewProxyFunction · 0.85

Calls 3

authFromHeaderFunction · 0.85
unauthorizedErrorFunction · 0.85
ErrorMethod · 0.45

Tested by

no test coverage detected