Skip to content

Commit 4016715

Browse files
authored
fix: login btn does not work for readonly annoymous (#620)
1 parent f8a7873 commit 4016715

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

assets/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ async function setupAuth() {
534534
$loginBtn.classList.remove("hidden");
535535
$loginBtn.addEventListener("click", async () => {
536536
try {
537-
await checkAuth();
537+
await checkAuth("login");
538538
} catch { }
539539
location.reload();
540540
});
@@ -782,9 +782,10 @@ async function saveChange() {
782782
}
783783
}
784784

785-
async function checkAuth() {
785+
async function checkAuth(variant) {
786786
if (!DATA.auth) return;
787-
const res = await fetch(baseUrl(), {
787+
const qs = variant ? `?${variant}` : "";
788+
const res = await fetch(baseUrl() + qs, {
788789
method: "CHECKAUTH",
789790
});
790791
await assertResOK(res);

src/server.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,18 @@ impl Server {
211211
}
212212

213213
if method.as_str() == "CHECKAUTH" {
214-
*res.body_mut() = body_full(user.clone().unwrap_or_default());
214+
match user.clone() {
215+
Some(user) => {
216+
*res.body_mut() = body_full(user);
217+
}
218+
None => {
219+
if has_query_flag(&query_params, "login") || !access_paths.perm().readwrite() {
220+
self.auth_reject(&mut res)?
221+
} else {
222+
*res.body_mut() = body_full("");
223+
}
224+
}
225+
}
215226
return Ok(res);
216227
} else if method.as_str() == "LOGOUT" {
217228
self.auth_reject(&mut res)?;

tests/auth.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ fn auth_no_skip_if_anonymous(
147147
fn auth_check(
148148
#[with(&["--auth", "user:pass@/:rw", "--auth", "user2:pass2@/", "-A"])] server: TestServer,
149149
) -> Result<(), Error> {
150-
let url = format!("{}index.html", server.url());
150+
let url = format!("{}", server.url());
151151
let resp = fetch!(b"CHECKAUTH", &url).send()?;
152152
assert_eq!(resp.status(), 401);
153153
let resp = send_with_digest_auth(fetch!(b"CHECKAUTH", &url), "user", "pass")?;
@@ -161,7 +161,7 @@ fn auth_check(
161161
fn auth_check2(
162162
#[with(&["--auth", "user:pass@/:rw|user2:pass2@/", "-A"])] server: TestServer,
163163
) -> Result<(), Error> {
164-
let url = format!("{}index.html", server.url());
164+
let url = format!("{}", server.url());
165165
let resp = fetch!(b"CHECKAUTH", &url).send()?;
166166
assert_eq!(resp.status(), 401);
167167
let resp = send_with_digest_auth(fetch!(b"CHECKAUTH", &url), "user", "pass")?;
@@ -171,6 +171,18 @@ fn auth_check2(
171171
Ok(())
172172
}
173173

174+
#[rstest]
175+
fn auth_check3(
176+
#[with(&["--auth", "user:pass@/:rw", "--auth", "@/dir1:rw", "-A"])] server: TestServer,
177+
) -> Result<(), Error> {
178+
let url = format!("{}dir1/", server.url());
179+
let resp = fetch!(b"CHECKAUTH", &url).send()?;
180+
assert_eq!(resp.status(), 200);
181+
let resp = fetch!(b"CHECKAUTH", format!("{url}?login")).send()?;
182+
assert_eq!(resp.status(), 401);
183+
Ok(())
184+
}
185+
174186
#[rstest]
175187
fn auth_logout(
176188
#[with(&["--auth", "user:pass@/:rw", "-A"])] server: TestServer,

0 commit comments

Comments
 (0)