Skip to content

Commit ea71919

Browse files
committed
feat(serverHandler): add force auth handler
1 parent e92f4ef commit ea71919

File tree

8 files changed

+60
-17
lines changed

8 files changed

+60
-17
lines changed

doc/en-US/api.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,9 @@ Example:
154154
```sh
155155
curl -X POST -d 'name=dir1&name=dir2&name=dir3' 'http://localhost/tmp/?delete'
156156
```
157+
158+
# Login
159+
Perform a login authentication even not required by current path:
160+
```
161+
GET <path>?auth
162+
```

doc/zh-CN/api.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,9 @@ name=<dir1>&name=<dir2>&...name=<dirN>
151151
```sh
152152
curl -X POST -d 'name=dir1&name=dir2&name=dir3' 'http://localhost/tmp/?delete'
153153
```
154+
155+
# 登录
156+
发起登录认证,即使当前路径无需验证:
157+
```
158+
GET <path>?auth
159+
```

src/serverHandler/aliasHandler.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,19 @@ func (h *aliasHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
110110
}
111111

112112
if data.NeedAuth {
113-
h.needAuth(w, r)
114-
}
115-
if !data.AuthSuccess {
116-
if !h.postMiddleware(w, r, data, fsPath) {
117-
h.authFailed(w, data.Status)
113+
h.notifyAuth(w, r)
114+
115+
if !data.AuthSuccess {
116+
if !h.postMiddleware(w, r, data, fsPath) {
117+
h.authFailed(w, data.Status)
118+
}
119+
return
120+
}
121+
122+
if data.forceAuth {
123+
h.redirectWithoutForceAuth(w, r, data)
124+
return
118125
}
119-
return
120126
}
121127

122128
if !data.AllowAccess {

src/serverHandler/auth.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,24 @@ package serverHandler
33
import (
44
"errors"
55
"net/http"
6+
"strings"
67
)
78

8-
func (h *aliasHandler) needAuth(w http.ResponseWriter, r *http.Request) {
9+
const authQueryParam = "auth"
10+
11+
func (h *aliasHandler) needAuth(rawQuery, rawReqPath, reqFsPath string) (need, force bool) {
12+
if strings.HasPrefix(rawQuery, authQueryParam) {
13+
return true, true
14+
}
15+
16+
if h.globalAuth {
17+
return true, false
18+
}
19+
20+
return hasUrlOrDirPrefix(h.authUrls, rawReqPath, h.authDirs, reqFsPath), false
21+
}
22+
23+
func (h *aliasHandler) notifyAuth(w http.ResponseWriter, r *http.Request) {
924
w.Header().Set("WWW-Authenticate", "Basic realm=\"files\"")
1025
}
1126

src/serverHandler/perm.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,3 @@ func (h *aliasHandler) getCanCors(rawReqPath, reqFsPath string) bool {
7676

7777
return hasUrlOrDirPrefix(h.corsUrls, rawReqPath, h.corsDirs, reqFsPath)
7878
}
79-
80-
func (h *aliasHandler) getNeedAuth(rawReqPath, reqFsPath string) bool {
81-
if h.globalAuth {
82-
return true
83-
}
84-
85-
return hasUrlOrDirPrefix(h.authUrls, rawReqPath, h.authDirs, reqFsPath)
86-
}

src/serverHandler/redirect.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,12 @@ func (h *aliasHandler) redirectWithSlashSuffix(w http.ResponseWriter, r *http.Re
1010

1111
http.Redirect(w, r, target, h.forceDirSlash)
1212
}
13+
14+
func (h *aliasHandler) redirectWithoutForceAuth(w http.ResponseWriter, r *http.Request, data *responseData) {
15+
returnUrl := r.Header.Get("Referer")
16+
if len(returnUrl) == 0 {
17+
returnUrl = data.prefixReqPath + data.Context.QueryString()
18+
}
19+
20+
http.Redirect(w, r, returnUrl, http.StatusFound)
21+
}

src/serverHandler/responseData.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type responseData struct {
3131
handlerReqPath string
3232

3333
NeedAuth bool
34+
forceAuth bool
3435
AuthUserName string
3536
AuthSuccess bool
3637

@@ -305,9 +306,11 @@ func (h *aliasHandler) getResponseData(r *http.Request) (data *responseData, fsP
305306
reqPath := util.CleanUrlPath(rawReqPath[len(h.aliasPrefix):])
306307
reqFsPath := filepath.Clean(h.root + reqPath)
307308

309+
rawQuery := r.URL.RawQuery
310+
308311
status := http.StatusOK
309312

310-
needAuth := h.getNeedAuth(rawReqPath, reqFsPath)
313+
needAuth, forceAuth := h.needAuth(rawQuery, rawReqPath, reqFsPath)
311314
authUserName, authSuccess, _authErr := h.verifyAuth(r, needAuth)
312315
if needAuth {
313316
if _authErr != nil {
@@ -320,7 +323,6 @@ func (h *aliasHandler) getResponseData(r *http.Request) (data *responseData, fsP
320323

321324
headers := h.getHeaders(rawReqPath, reqFsPath, authSuccess)
322325

323-
rawQuery := r.URL.RawQuery
324326
isDownload := false
325327
isDownloadFile := false
326328
isUpload := false
@@ -432,6 +434,7 @@ func (h *aliasHandler) getResponseData(r *http.Request) (data *responseData, fsP
432434
handlerReqPath: reqPath,
433435

434436
NeedAuth: needAuth,
437+
forceAuth: forceAuth,
435438
AuthUserName: authUserName,
436439
AuthSuccess: authSuccess,
437440

test/case/007.auth.bash

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,10 @@ assert "$userhellostatus" '200'
1717
userhelloheadstatus=$(curl_head_status http://alice:AliceSecret@127.0.0.1:3003/hello/)
1818
assert "$userhelloheadstatus" '200'
1919

20+
hellostatus=$(curl_get_status http://127.0.0.1:3003/yes/?auth)
21+
assert "$hellostatus" '401'
22+
23+
hellostatus=$(curl_get_status http://alice:AliceSecret@127.0.0.1:3003/yes/?auth)
24+
assert "$hellostatus" '302'
25+
2026
jobs -p | xargs kill &> /dev/null

0 commit comments

Comments
 (0)