Skip to content

Commit 7ac304f

Browse files
committed
feat(serverhandler): skip further action on auth failed
1 parent d14dbb9 commit 7ac304f

File tree

4 files changed

+41
-22
lines changed

4 files changed

+41
-22
lines changed

src/serverHandler/auth.go

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

33
import "net/http"
44

5-
func (h *handler) auth(w http.ResponseWriter, r *http.Request, data *responseData) (success bool) {
6-
header := w.Header()
7-
header.Set("WWW-Authenticate", "Basic realm=\""+r.URL.Path+"\"")
5+
func (h *handler) needAuth(w http.ResponseWriter, r *http.Request) {
6+
w.Header().Set("WWW-Authenticate", "Basic realm=\""+r.URL.Path+"\"")
7+
}
88

9-
username, password, hasAuthReq := r.BasicAuth()
9+
func (h *handler) verifyAuth(r *http.Request) (username string, success bool) {
10+
var password string
11+
var hasAuthReq bool
12+
username, password, hasAuthReq = r.BasicAuth()
1013
if hasAuthReq {
1114
success = h.users.Auth(username, password)
1215
}
1316

14-
if success {
15-
data.AuthUserName = username
16-
} else {
17-
w.WriteHeader(http.StatusUnauthorized)
18-
}
19-
2017
return
2118
}
19+
20+
func (h *handler) authFailed(w http.ResponseWriter) {
21+
w.WriteHeader(http.StatusUnauthorized)
22+
}

src/serverHandler/handler.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,11 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
9393
defer file.Close()
9494
}
9595

96-
if data.NeedAuth && !h.auth(w, r, data) {
96+
if data.NeedAuth {
97+
h.needAuth(w, r)
98+
}
99+
if !data.AuthSuccess {
100+
h.authFailed(w)
97101
return
98102
}
99103

src/serverHandler/json.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ type jsonItem struct {
1717
}
1818

1919
type jsonResponseData struct {
20+
NeedAuth bool `json:"needAuth"`
21+
AuthUserName string `json:"authUserName"`
22+
AuthSuccess bool `json:"authSuccess"`
2023
IsRoot bool `json:"isRoot"`
2124
Path string `json:"path"`
2225
Paths []*pathEntry `json:"paths"`
@@ -27,7 +30,6 @@ type jsonResponseData struct {
2730
CanDelete bool `json:"canDelete"`
2831
CanArchive bool `json:"canArchive"`
2932
CanCors bool `json:"canCors"`
30-
NeedAuth bool `json:"needAuth"`
3133

3234
Item *jsonItem `json:"item"`
3335
SubItems []*jsonItem `json:"subItems"`
@@ -57,6 +59,9 @@ func getJsonData(data *responseData) *jsonResponseData {
5759
}
5860

5961
return &jsonResponseData{
62+
NeedAuth: data.NeedAuth,
63+
AuthUserName: data.AuthUserName,
64+
AuthSuccess: data.AuthSuccess,
6065
IsRoot: data.IsRoot,
6166
Path: data.Path,
6267
Paths: data.Paths,
@@ -67,7 +72,6 @@ func getJsonData(data *responseData) *jsonResponseData {
6772
CanDelete: data.CanDelete,
6873
CanArchive: data.CanArchive,
6974
CanCors: data.CanCors,
70-
NeedAuth: data.NeedAuth,
7175

7276
Item: item,
7377
SubItems: subItems,

src/serverHandler/responseData.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ type responseData struct {
2828
rawReqPath string
2929
handlerReqPath string
3030

31+
NeedAuth bool
32+
AuthUserName string
33+
AuthSuccess bool
34+
3135
errors []error
3236
Status int
3337

@@ -51,8 +55,6 @@ type responseData struct {
5155
HasDeletable bool
5256
CanArchive bool
5357
CanCors bool
54-
NeedAuth bool
55-
AuthUserName string
5658

5759
IsDownload bool
5860
IsUpload bool
@@ -269,10 +271,18 @@ func (h *handler) getResponseData(r *http.Request) *responseData {
269271

270272
rawReqPath := util.CleanUrlPath(requestUri)
271273
reqPath := util.CleanUrlPath(rawReqPath[len(h.urlPrefix):]) // strip url prefix path
274+
reqFsPath, _ := util.NormalizeFsPath(h.root + reqPath)
275+
276+
needAuth := h.getNeedAuth(rawReqPath, reqFsPath)
277+
authUserName := ""
278+
authSuccess := true
279+
if needAuth {
280+
authUserName, authSuccess = h.verifyAuth(r)
281+
}
282+
272283
errs := []error{}
273284
status := http.StatusOK
274285
isRoot := rawReqPath == "/"
275-
276286
rawQuery := r.URL.RawQuery
277287

278288
pathEntries := getPathEntries(rawReqPath, tailSlash)
@@ -283,9 +293,7 @@ func (h *handler) getResponseData(r *http.Request) *responseData {
283293
rootRelPath = "./"
284294
}
285295

286-
reqFsPath, _ := util.NormalizeFsPath(h.root + reqPath)
287-
288-
file, item, _statErr := stat(reqFsPath, !h.emptyRoot)
296+
file, item, _statErr := stat(reqFsPath, authSuccess && !h.emptyRoot)
289297
if _statErr != nil {
290298
errs = append(errs, _statErr)
291299
status = getStatusByErr(_statErr)
@@ -307,7 +315,7 @@ func (h *handler) getResponseData(r *http.Request) *responseData {
307315

308316
itemName := getItemName(item, r)
309317

310-
subItems, _readdirErr := readdir(file, item, needResponseBody(r.Method))
318+
subItems, _readdirErr := readdir(file, item, authSuccess && needResponseBody(r.Method))
311319
if _readdirErr != nil {
312320
errs = append(errs, _readdirErr)
313321
status = http.StatusInternalServerError
@@ -334,7 +342,6 @@ func (h *handler) getResponseData(r *http.Request) *responseData {
334342
hasDeletable := canDelete && len(subItems) > len(aliasSubItems)
335343
canArchive := h.getCanArchive(subItems, rawReqPath, reqFsPath)
336344
canCors := h.getCanCors(rawReqPath, reqFsPath)
337-
needAuth := h.getNeedAuth(rawReqPath, reqFsPath)
338345

339346
isDownload := false
340347
isUpload := false
@@ -366,6 +373,10 @@ func (h *handler) getResponseData(r *http.Request) *responseData {
366373
rawReqPath: rawReqPath,
367374
handlerReqPath: reqPath,
368375

376+
NeedAuth: needAuth,
377+
AuthUserName: authUserName,
378+
AuthSuccess: authSuccess,
379+
369380
errors: errs,
370381
Status: status,
371382

@@ -389,7 +400,6 @@ func (h *handler) getResponseData(r *http.Request) *responseData {
389400
HasDeletable: hasDeletable,
390401
CanArchive: canArchive,
391402
CanCors: canCors,
392-
NeedAuth: needAuth,
393403

394404
IsDownload: isDownload,
395405
IsUpload: isUpload,

0 commit comments

Comments
 (0)