Skip to content

Commit 8b85ccd

Browse files
committed
feat(serverHandler/responseData): add auth error to log
1 parent ff0811b commit 8b85ccd

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

src/serverHandler/auth.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
package serverHandler
22

3-
import "net/http"
3+
import (
4+
"errors"
5+
"net/http"
6+
)
47

58
func (h *handler) needAuth(w http.ResponseWriter, r *http.Request) {
69
w.Header().Set("WWW-Authenticate", "Basic realm=\""+r.URL.Path+"\"")
710
}
811

9-
func (h *handler) verifyAuth(r *http.Request) (username string, success bool) {
12+
func (h *handler) verifyAuth(r *http.Request) (username string, success bool, err error) {
1013
var password string
1114
var hasAuthReq bool
1215
username, password, hasAuthReq = r.BasicAuth()
1316
if hasAuthReq {
1417
success = h.users.Auth(username, password)
18+
if !success {
19+
err = errors.New(r.RemoteAddr + " auth failed")
20+
}
21+
} else {
22+
err = errors.New(r.RemoteAddr + " missing auth info")
1523
}
1624

1725
return

src/serverHandler/responseData.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,8 @@ func (h *handler) stateIndexFile(rawReqPath, baseDir string, baseItem os.FileInf
263263
}
264264

265265
func (h *handler) getResponseData(r *http.Request) *responseData {
266+
var errs []error
267+
266268
requestUri := r.URL.Path
267269
if len(requestUri) == 0 {
268270
requestUri = "/"
@@ -277,7 +279,11 @@ func (h *handler) getResponseData(r *http.Request) *responseData {
277279
authUserName := ""
278280
authSuccess := true
279281
if needAuth {
280-
authUserName, authSuccess = h.verifyAuth(r)
282+
var _authErr error
283+
authUserName, authSuccess, _authErr = h.verifyAuth(r)
284+
if _authErr != nil {
285+
errs = append(errs, _authErr)
286+
}
281287
}
282288

283289
rawQuery := r.URL.RawQuery
@@ -301,7 +307,6 @@ func (h *handler) getResponseData(r *http.Request) *responseData {
301307
}
302308
wantJson := strings.HasPrefix(rawQuery, "json") || strings.Contains(rawQuery, "&json")
303309

304-
errs := []error{}
305310
status := http.StatusOK
306311
isRoot := rawReqPath == "/"
307312

test/case/007.auth.bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
source "$root"/lib.bash
44

5-
"$ghfs" -l 3003 -r "$fs"/vhost1 --auth /hello --user alice:AliceSecret &
5+
"$ghfs" -l 3003 -r "$fs"/vhost1 --auth /hello --user alice:AliceSecret -E '' &
66
sleep 0.05 # wait server ready
77

88
yesstatus=$(curl_get_status http://127.0.0.1:3003/yes/)

0 commit comments

Comments
 (0)