Skip to content

Commit 7cba61b

Browse files
committed
feat(auth): add mechanism to auth for specified user
1 parent 2c6e08b commit 7cba61b

File tree

5 files changed

+65
-13
lines changed

5 files changed

+65
-13
lines changed

src/serverHandler/aliasHandler.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@ type aliasHandler struct {
4040
dirIndexes []string
4141
aliases aliases
4242

43-
globalAuth bool
44-
authUrls []string
45-
authDirs []string
43+
globalAuth bool
44+
authUrls []string
45+
authUrlsUsers pathIntsList
46+
authDirs []string
47+
authDirsUsers pathIntsList
4648

4749
globalRestrictAccess []string
4850
restrictAccessUrls pathStringsList

src/serverHandler/archive.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ func matchSelection(info os.FileInfo, selections []string) (match bool, childSel
4747

4848
func (h *aliasHandler) visitTreeNode(
4949
r *http.Request,
50-
rawReqPath, fsPath, relPath string,
50+
urlPath, fsPath, relPath string,
5151
statNode bool,
5252
childSelections []string,
5353
archiveCallback archiveCallback,
5454
) {
55-
if needAuth, _ := h.needAuth("", rawReqPath, fsPath); needAuth {
56-
if _, _, err := h.verifyAuth(r, needAuth); err != nil {
55+
if needAuth, _ := h.needAuth("", urlPath, fsPath); needAuth {
56+
if _, _, err := h.verifyAuth(r, needAuth, urlPath, fsPath); err != nil {
5757
return
5858
}
5959
}
@@ -105,7 +105,7 @@ func (h *aliasHandler) visitTreeNode(
105105
}
106106

107107
if fInfo.IsDir() {
108-
childInfos, _, _ := h.mergeAlias(rawReqPath, fInfo, childInfos, true)
108+
childInfos, _, _ := h.mergeAlias(urlPath, fInfo, childInfos, true)
109109
childInfos = h.FilterItems(childInfos)
110110

111111
// childInfo can be regular dir/file, or aliased item that shadows regular dir/file
@@ -117,7 +117,7 @@ func (h *aliasHandler) visitTreeNode(
117117

118118
childPath := "/" + childInfo.Name()
119119
childFsPath := fsPath + childPath
120-
childRawReqPath := util.CleanUrlPath(rawReqPath + childPath)
120+
childRawReqPath := util.CleanUrlPath(urlPath + childPath)
121121
childRelPath := relPath + childPath
122122

123123
if childAlias, hasChildAlias := h.aliases.byUrlPath(childRawReqPath); hasChildAlias {

src/serverHandler/auth.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
const authQueryParam = "auth"
1111

12-
func (h *aliasHandler) needAuth(rawQuery, rawReqPath, reqFsPath string) (needAuth, requestAuth bool) {
12+
func (h *aliasHandler) needAuth(rawQuery, vhostReqPath, reqFsPath string) (needAuth, requestAuth bool) {
1313
if strings.HasPrefix(rawQuery, authQueryParam) {
1414
return true, true
1515
}
@@ -18,19 +18,33 @@ func (h *aliasHandler) needAuth(rawQuery, rawReqPath, reqFsPath string) (needAut
1818
return true, false
1919
}
2020

21-
return hasUrlOrDirPrefix(h.authUrls, rawReqPath, h.authDirs, reqFsPath), false
21+
if hasUrlOrDirPrefix(h.authUrls, vhostReqPath, h.authDirs, reqFsPath) {
22+
return true, false
23+
}
24+
25+
if matchPath, _ := hasUrlOrDirPrefixUsers(h.authUrlsUsers, vhostReqPath, h.authDirsUsers, reqFsPath, -1); matchPath {
26+
return true, false
27+
}
28+
29+
return false, false
2230
}
2331

2432
func (h *aliasHandler) notifyAuth(w http.ResponseWriter, r *http.Request) {
2533
w.Header().Set("WWW-Authenticate", "Basic realm=\"files\"")
2634
}
2735

28-
func (h *aliasHandler) verifyAuth(r *http.Request, needAuth bool) (userid int, username string, err error) {
36+
func (h *aliasHandler) verifyAuth(r *http.Request, needAuth bool, vhostReqPath, reqFsPath string) (userid int, username string, err error) {
2937
user, pass, hasAuthReq := r.BasicAuth()
3038

3139
if hasAuthReq {
3240
var success bool
33-
if userid, username, success = h.users.Auth(user, pass); success {
41+
userid, username, success = h.users.Auth(user, pass)
42+
if success && userid >= 0 && (len(h.authUrlsUsers) > 0 || len(h.authDirsUsers) > 0) {
43+
if matchPrefix, match := hasUrlOrDirPrefixUsers(h.authUrlsUsers, vhostReqPath, h.authDirsUsers, reqFsPath, userid); matchPrefix {
44+
success = match
45+
}
46+
}
47+
if success {
3448
return
3549
}
3650
}

src/serverHandler/perm.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,42 @@ func hasUrlOrDirPrefix(urls []string, reqUrl string, dirs []string, reqDir strin
2121
return false
2222
}
2323

24+
func hasUrlOrDirPrefixUsers(urlsUsers pathIntsList, reqUrl string, dirsUsers pathIntsList, reqDir string, userId int) (matchPrefix, match bool) {
25+
for i := range urlsUsers {
26+
if !util.HasUrlPrefixDir(reqUrl, urlsUsers[i].path) {
27+
continue
28+
}
29+
matchPrefix = true
30+
if userId < 0 {
31+
continue
32+
}
33+
for _, uid := range urlsUsers[i].ints {
34+
if uid == userId {
35+
match = true
36+
return
37+
}
38+
}
39+
}
40+
41+
for i := range dirsUsers {
42+
if !util.HasFsPrefixDir(reqDir, dirsUsers[i].path) {
43+
continue
44+
}
45+
matchPrefix = true
46+
if userId < 0 {
47+
continue
48+
}
49+
for _, uid := range dirsUsers[i].ints {
50+
if uid == userId {
51+
match = true
52+
return
53+
}
54+
}
55+
}
56+
57+
return
58+
}
59+
2460
func (h *aliasHandler) getCanUpload(info os.FileInfo, rawReqPath, reqFsPath string) bool {
2561
if info == nil || !info.IsDir() {
2662
return false

src/serverHandler/sessionData.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ func (h *aliasHandler) getSessionData(r *http.Request) (session *sessionContext,
335335
status := http.StatusOK
336336

337337
needAuth, requestAuth := h.needAuth(rawQuery, vhostReqPath, fsPath)
338-
authUserId, authUserName, _authErr := h.verifyAuth(r, needAuth)
338+
authUserId, authUserName, _authErr := h.verifyAuth(r, needAuth, vhostReqPath, fsPath)
339339
authSuccess := _authErr == nil
340340
if needAuth && !authSuccess {
341341
errs = append(errs, _authErr)

0 commit comments

Comments
 (0)