Skip to content

Commit 24c69c4

Browse files
authored
Merge pull request #164 from Hirevo/feat/private-registry
Disallow anonymous crate browsing in the frontend
2 parents 3829053 + f2b0b61 commit 24c69c4

File tree

11 files changed

+34
-0
lines changed

11 files changed

+34
-0
lines changed

alexandrie.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ links = [
1111
{ name = "Github repository", href = "https://github.com/Hirevo/alexandrie" },
1212
{ name = "User documentation", href = "https://hirevo.github.io/alexandrie" },
1313
]
14+
login_required = false
1415

1516
[frontend.sessions]
1617
cookie_name = "alexandrie.sid"

crates/alexandrie/src/config/frontend/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ pub struct FrontendConfig {
6060
pub favicon: Option<String>,
6161
/// Some related links.
6262
pub links: Option<Vec<Link>>,
63+
/// Whether to disallow anonymous browsing of the registry.
64+
pub login_required: bool,
6365
/// Assets configuration options.
6466
pub assets: AssetsConfig,
6567
/// Templates configuration options.

crates/alexandrie/src/config/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,11 @@ impl From<Config> for State {
9494
}
9595
}
9696
}
97+
98+
impl State {
99+
/// Returns whether we require users to log in to browse crates.
100+
#[cfg(feature = "frontend")]
101+
pub fn is_login_required(&self) -> bool {
102+
self.frontend.config.login_required
103+
}
104+
}

crates/alexandrie/src/frontend/index.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ use crate::State;
1313

1414
pub(crate) async fn get(req: Request<State>) -> tide::Result {
1515
let user = req.get_author();
16+
if req.state().is_login_required() && user.is_none() {
17+
return Ok(utils::response::redirect("/account/login"));
18+
}
19+
1620
let state = req.state().clone();
1721
let db = &state.db;
1822

crates/alexandrie/src/frontend/krate.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ pub(crate) async fn get(req: Request<State>) -> tide::Result {
2727
let canon_name = utils::canonical_name(name);
2828

2929
let user = req.get_author();
30+
if req.state().is_login_required() && user.is_none() {
31+
return Ok(utils::response::redirect("/account/login"));
32+
}
33+
3034
let state = req.state().clone();
3135
let db = &state.db;
3236

crates/alexandrie/src/frontend/last_updated.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ pub(crate) async fn get(req: Request<State>) -> tide::Result {
2727
let page_number = params.page.map_or_else(|| 1, |page| page.get());
2828

2929
let user = req.get_author();
30+
if req.state().is_login_required() && user.is_none() {
31+
return Ok(utils::response::redirect("/account/login"));
32+
}
33+
3034
let state = req.state().clone();
3135
let db = &state.db;
3236

crates/alexandrie/src/frontend/most_downloaded.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ pub(crate) async fn get(req: Request<State>) -> tide::Result {
2727
let page_number = params.page.map_or_else(|| 1, |page| page.get());
2828

2929
let user = req.get_author();
30+
if req.state().is_login_required() && user.is_none() {
31+
return Ok(utils::response::redirect("/account/login"));
32+
}
33+
3034
let state = req.state().clone();
3135
let db = &state.db;
3236

crates/alexandrie/src/frontend/search.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ pub(crate) async fn get(req: Request<State>) -> tide::Result {
3434
let page_number = params.page.map_or_else(|| 1, |page| page.get());
3535

3636
let user = req.get_author();
37+
if req.state().is_login_required() && user.is_none() {
38+
return Ok(utils::response::redirect("/account/login"));
39+
}
40+
3741
let state = req.state().clone();
3842
let db = &state.db;
3943

docker/mysql/alexandrie.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ max_crate_size = "50 MB"
1313
enabled = true
1414
title = "Alexandrie"
1515
description = "An alternative crate registry for Cargo, the Rust package manager."
16+
login_required = false
1617

1718
[frontend.sessions]
1819
cookie_name = "alexandrie.sid"

docker/postgres/alexandrie.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ max_crate_size = "50 MB"
1313
enabled = true
1414
title = "Alexandrie"
1515
description = "An alternative crate registry for Cargo, the Rust package manager."
16+
login_required = false
1617

1718
[frontend.sessions]
1819
cookie_name = "alexandrie.sid"

0 commit comments

Comments
 (0)