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
99fa4581
Commit
99fa4581
authored
May 10, 2015
by
jordi collell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
basicauth: patch for overlapping rules
parent
4c118549
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
20 additions
and
6 deletions
+20
-6
middleware/basicauth/basicauth.go
middleware/basicauth/basicauth.go
+17
-3
middleware/basicauth/basicauth_test.go
middleware/basicauth/basicauth_test.go
+3
-3
No files found.
middleware/basicauth/basicauth.go
View file @
99fa4581
...
...
@@ -19,6 +19,10 @@ type BasicAuth struct {
// ServeHTTP implements the middleware.Handler interface.
func
(
a
BasicAuth
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
var
hasAuth
bool
var
isAuthenticated
bool
for
_
,
rule
:=
range
a
.
Rules
{
for
_
,
res
:=
range
rule
.
Resources
{
if
!
middleware
.
Path
(
r
.
URL
.
Path
)
.
Matches
(
res
)
{
...
...
@@ -27,16 +31,26 @@ func (a BasicAuth) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
// Path matches; parse auth header
username
,
password
,
ok
:=
r
.
BasicAuth
()
hasAuth
=
true
// Check credentials
if
!
ok
||
username
!=
rule
.
Username
||
password
!=
rule
.
Password
{
w
.
Header
()
.
Set
(
"WWW-Authenticate"
,
"Basic"
)
return
http
.
StatusUnauthorized
,
nil
continue
}
// flag set only on success authentication
isAuthenticated
=
true
}
}
if
hasAuth
{
if
!
isAuthenticated
{
w
.
Header
()
.
Set
(
"WWW-Authenticate"
,
"Basic"
)
return
http
.
StatusUnauthorized
,
nil
}
else
{
// "It's an older code, sir, but it checks out. I was about to clear them."
return
a
.
Next
.
ServeHTTP
(
w
,
r
)
}
}
// Pass-thru when no paths match
...
...
middleware/basicauth/basicauth_test.go
View file @
99fa4581
...
...
@@ -84,12 +84,13 @@ func TestMultipleOverlappingRules(t *testing.T) {
{
"/t"
,
http
.
StatusOK
,
"t:p1"
},
{
"/t/t"
,
http
.
StatusOK
,
"t:p1"
},
{
"/t/t"
,
http
.
StatusOK
,
"t1:p2"
},
{
"/a"
,
http
.
StatusOK
,
"t1:p2"
},
{
"/t/t"
,
http
.
StatusUnauthorized
,
"t1:p3"
},
{
"/t"
,
http
.
StatusUnauthorized
,
"t1:p2"
},
}
for
i
,
test
:=
range
tests
{
req
,
err
:=
http
.
NewRequest
(
"GET"
,
test
.
from
,
nil
)
if
err
!=
nil
{
...
...
@@ -108,7 +109,6 @@ func TestMultipleOverlappingRules(t *testing.T) {
i
,
test
.
result
,
result
)
}
}
}
...
...
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