Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
caddy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
caddy
Commits
62e8c4b7
Commit
62e8c4b7
authored
Jul 22, 2016
by
Viacheslav Chimishuk
Committed by
Matt Holt
Jul 22, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use authentification credentials from proxy's configuration as a default. (#951)
parent
6490ff62
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
73 additions
and
0 deletions
+73
-0
caddyhttp/proxy/proxy.go
caddyhttp/proxy/proxy.go
+6
-0
caddyhttp/proxy/proxy_test.go
caddyhttp/proxy/proxy_test.go
+67
-0
No files found.
caddyhttp/proxy/proxy.go
View file @
62e8c4b7
...
...
@@ -110,6 +110,12 @@ func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
if
proxy
==
nil
{
proxy
=
NewSingleHostReverseProxy
(
nameURL
,
host
.
WithoutPathPrefix
)
}
// use upstream credentials by default
if
outreq
.
Header
.
Get
(
"Authorization"
)
==
""
&&
nameURL
.
User
!=
nil
{
pwd
,
_
:=
nameURL
.
User
.
Password
()
outreq
.
SetBasicAuth
(
nameURL
.
User
.
Username
(),
pwd
)
}
}
else
{
outreq
.
Host
=
host
.
Name
}
...
...
caddyhttp/proxy/proxy_test.go
View file @
62e8c4b7
...
...
@@ -642,6 +642,73 @@ func TestHostHeaderReplacedUsingForward(t *testing.T) {
}
}
func
TestBasicAuth
(
t
*
testing
.
T
)
{
basicAuthTestcase
(
t
,
nil
,
nil
)
basicAuthTestcase
(
t
,
nil
,
url
.
UserPassword
(
"username"
,
"password"
))
basicAuthTestcase
(
t
,
url
.
UserPassword
(
"usename"
,
"password"
),
nil
)
basicAuthTestcase
(
t
,
url
.
UserPassword
(
"unused"
,
"unused"
),
url
.
UserPassword
(
"username"
,
"password"
))
}
func
basicAuthTestcase
(
t
*
testing
.
T
,
upstreamUser
,
clientUser
*
url
.
Userinfo
)
{
backend
:=
httptest
.
NewServer
(
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
u
,
p
,
ok
:=
r
.
BasicAuth
()
if
ok
{
w
.
Write
([]
byte
(
u
))
}
if
ok
&&
p
!=
""
{
w
.
Write
([]
byte
(
":"
))
w
.
Write
([]
byte
(
p
))
}
}))
defer
backend
.
Close
()
backUrl
,
err
:=
url
.
Parse
(
backend
.
URL
)
if
err
!=
nil
{
t
.
Fatalf
(
"Failed to parse URL: %v"
,
err
)
}
backUrl
.
User
=
upstreamUser
p
:=
&
Proxy
{
Next
:
httpserver
.
EmptyNext
,
Upstreams
:
[]
Upstream
{
newFakeUpstream
(
backUrl
.
String
(),
false
)},
}
r
,
err
:=
http
.
NewRequest
(
"GET"
,
"/foo"
,
nil
)
if
err
!=
nil
{
t
.
Fatalf
(
"Failed to create request: %v"
,
err
)
}
if
clientUser
!=
nil
{
u
:=
clientUser
.
Username
()
p
,
_
:=
clientUser
.
Password
()
r
.
SetBasicAuth
(
u
,
p
)
}
w
:=
httptest
.
NewRecorder
()
p
.
ServeHTTP
(
w
,
r
)
if
w
.
Code
!=
200
{
t
.
Fatalf
(
"Invalid response code: %d"
,
w
.
Code
)
}
body
,
_
:=
ioutil
.
ReadAll
(
w
.
Body
)
if
clientUser
!=
nil
{
if
string
(
body
)
!=
clientUser
.
String
()
{
t
.
Fatalf
(
"Invalid auth info: %s"
,
string
(
body
))
}
}
else
{
if
upstreamUser
!=
nil
{
if
string
(
body
)
!=
upstreamUser
.
String
()
{
t
.
Fatalf
(
"Invalid auth info: %s"
,
string
(
body
))
}
}
else
{
if
string
(
body
)
!=
""
{
t
.
Fatalf
(
"Invalid auth info: %s"
,
string
(
body
))
}
}
}
}
func
newFakeUpstream
(
name
string
,
insecure
bool
)
*
fakeUpstream
{
uri
,
_
:=
url
.
Parse
(
name
)
u
:=
&
fakeUpstream
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment