Skip to content

Commit 5dca9cb

Browse files
committed
feat: accept return url from auth param
1 parent 8d7c90c commit 5dca9cb

File tree

4 files changed

+26
-11
lines changed

4 files changed

+26
-11
lines changed

doc/en-US/api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,5 +158,5 @@ curl -X POST -d 'name=dir1&name=dir2&name=dir3' 'http://localhost/tmp/?delete'
158158
# Login
159159
Perform a login authentication even not required by current path:
160160
```
161-
GET <path>?auth
161+
GET <path>?auth[=return_url]
162162
```

doc/zh-CN/api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,5 +155,5 @@ curl -X POST -d 'name=dir1&name=dir2&name=dir3' 'http://localhost/tmp/?delete'
155155
# 登录
156156
发起登录认证,即使当前路径无需验证:
157157
```
158-
GET <path>?auth
158+
GET <path>?auth[=return_url]
159159
```

src/serverHandler/auth.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package serverHandler
33
import (
44
"errors"
55
"net/http"
6+
"net/url"
67
"strings"
78
)
89

@@ -46,3 +47,26 @@ func (h *aliasHandler) verifyAuth(r *http.Request, needAuth bool) (userid int, u
4647

4748
return
4849
}
50+
51+
func (h *aliasHandler) redirectWithoutRequestAuth(w http.ResponseWriter, r *http.Request, session *sessionContext, data *responseData) {
52+
var returnUrl string
53+
index := strings.Index(r.URL.RawQuery, authQueryParam+"=")
54+
if index >= 0 {
55+
returnUrl = r.URL.RawQuery[index+len(authQueryParam)+1:]
56+
index = strings.LastIndexByte(returnUrl, '&')
57+
if index >= 0 {
58+
returnUrl = returnUrl[:index]
59+
}
60+
url, err := url.QueryUnescape(returnUrl)
61+
if err == nil {
62+
returnUrl = url
63+
}
64+
} else {
65+
returnUrl = r.Header.Get("Referer")
66+
}
67+
if len(returnUrl) == 0 {
68+
returnUrl = session.prefixReqPath + data.Context.QueryString()
69+
}
70+
71+
http.Redirect(w, r, returnUrl, http.StatusFound)
72+
}

src/serverHandler/redirect.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,3 @@ func redirect(w http.ResponseWriter, r *http.Request, path string, code int) {
99
}
1010
http.Redirect(w, r, target, code)
1111
}
12-
13-
func (h *aliasHandler) redirectWithoutRequestAuth(w http.ResponseWriter, r *http.Request, session *sessionContext, data *responseData) {
14-
returnUrl := r.Header.Get("Referer")
15-
if len(returnUrl) == 0 {
16-
returnUrl = session.prefixReqPath + data.Context.QueryString()
17-
}
18-
19-
http.Redirect(w, r, returnUrl, http.StatusFound)
20-
}

0 commit comments

Comments
 (0)