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
253c069b
Commit
253c069b
authored
May 08, 2015
by
jordi collell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
if basic auth fails should write unauthorized to response
parent
64d20349
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
13 additions
and
3 deletions
+13
-3
middleware/basicauth/basicauth.go
middleware/basicauth/basicauth.go
+1
-0
middleware/basicauth/basicauth_test.go
middleware/basicauth/basicauth_test.go
+12
-3
No files found.
middleware/basicauth/basicauth.go
View file @
253c069b
...
@@ -31,6 +31,7 @@ func (a BasicAuth) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
...
@@ -31,6 +31,7 @@ func (a BasicAuth) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
// Check credentials
// Check credentials
if
!
ok
||
username
!=
rule
.
Username
||
password
!=
rule
.
Password
{
if
!
ok
||
username
!=
rule
.
Username
||
password
!=
rule
.
Password
{
w
.
Header
()
.
Set
(
"WWW-Authenticate"
,
"Basic"
)
w
.
Header
()
.
Set
(
"WWW-Authenticate"
,
"Basic"
)
w
.
WriteHeader
(
http
.
StatusUnauthorized
)
return
http
.
StatusUnauthorized
,
nil
return
http
.
StatusUnauthorized
,
nil
}
}
...
...
middleware/basicauth/basicauth_test.go
View file @
253c069b
...
@@ -24,12 +24,14 @@ func TestBasicAuth(t *testing.T) {
...
@@ -24,12 +24,14 @@ func TestBasicAuth(t *testing.T) {
result
int
result
int
cred
string
cred
string
}{
}{
{
"/testing"
,
http
.
StatusUnauthorized
,
"ttest:test"
},
{
"/testing"
,
http
.
StatusOK
,
"test:ttest"
},
{
"/testing"
,
http
.
StatusOK
,
"test:ttest"
},
{
"/testing"
,
http
.
StatusUnauthorized
,
""
},
{
"/testing"
,
http
.
StatusUnauthorized
,
""
},
}
}
//auth := "Basic " + base64.StdEncoding.EncodeToString([]byte("foo:bar"))
for
i
,
test
:=
range
tests
{
for
i
,
test
:=
range
tests
{
...
@@ -41,7 +43,14 @@ func TestBasicAuth(t *testing.T) {
...
@@ -41,7 +43,14 @@ func TestBasicAuth(t *testing.T) {
req
.
Header
.
Set
(
"Authorization"
,
auth
)
req
.
Header
.
Set
(
"Authorization"
,
auth
)
rec
:=
httptest
.
NewRecorder
()
rec
:=
httptest
.
NewRecorder
()
rw
.
ServeHTTP
(
rec
,
req
)
result
,
err
:=
rw
.
ServeHTTP
(
rec
,
req
)
if
err
!=
nil
{
t
.
Fatalf
(
"Test %d: Could not ServeHTTP %v"
,
i
,
err
)
}
if
result
!=
test
.
result
{
t
.
Errorf
(
"Test %d: Expected Header '%d' but was '%d'"
,
i
,
test
.
result
,
result
)
}
if
rec
.
Code
!=
test
.
result
{
if
rec
.
Code
!=
test
.
result
{
t
.
Errorf
(
"Test %d: Expected Header '%d' but was '%d'"
,
t
.
Errorf
(
"Test %d: Expected Header '%d' but was '%d'"
,
...
@@ -54,5 +63,5 @@ func TestBasicAuth(t *testing.T) {
...
@@ -54,5 +63,5 @@ func TestBasicAuth(t *testing.T) {
func
contentHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
func
contentHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
fmt
.
Fprintf
(
w
,
r
.
URL
.
String
())
fmt
.
Fprintf
(
w
,
r
.
URL
.
String
())
return
0
,
nil
return
http
.
StatusOK
,
nil
}
}
\ No newline at end of file
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