Skip to content

Commit a51df1c

Browse files
committed
Remove the useless mut requirement in userstore
1 parent 41d096b commit a51df1c

8 files changed

Lines changed: 19 additions & 50 deletions

File tree

src/auth_url/handle_status.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use crate::service::StatusAuth;
3030
///
3131
/// In order to use the one-time-code functionality, some setup is required,
3232
/// documented in [the example](https://magicentry.rs/#/installation?id=example-valuesyaml)
33+
// TODO: Cache the response for about a second or so
3334
#[axum::debug_handler]
3435
pub async fn handle_status(
3536
config: LiveConfig,
@@ -45,10 +46,7 @@ pub async fn handle_status(
4546
.unwrap_or(false);
4647

4748
if log_authurl_lines {
48-
let cookies: Vec<&str> = jar
49-
.iter()
50-
.map(|cookie| cookie.name())
51-
.collect();
49+
let cookies: Vec<&str> = jar.iter().map(|cookie| cookie.name()).collect();
5250
let headers: Vec<&str> = request_headers
5351
.iter()
5452
.map(|(name, _value)| name.as_str())

src/config.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ use crate::CONFIG;
2121
use crate::database::{ConfigKVRow, Database};
2222
use crate::service::Services;
2323
use crate::user::User;
24-
use crate::user_store::{FileUserStore, SQLUserStore, StaticUserStore, UserStoreKind};
25-
// use crate::user_store::{SQLUserStore, StaticUserStore, UserStoreKind};
24+
use crate::user_store::{FileUserStore, SQLUserStore, StaticUserStore, UserStore};
2625

2726
/// The actual, deserialized config data
2827
///
@@ -253,15 +252,14 @@ impl Config {
253252
.replace("\n", ""))
254253
}
255254

256-
pub fn get_user_store(&self) -> anyhow::Result<UserStoreKind> {
255+
pub fn get_user_store(&self) -> anyhow::Result<Arc<dyn UserStore>> {
257256
if self.users.len() > 0 {
258-
return Ok(UserStoreKind::Static(StaticUserStore::new(
259-
self.users.clone(),
260-
)));
257+
// TODO: This does not hot-reload
258+
return Ok(Arc::new(StaticUserStore::new(self.users.clone())));
261259
}
262260

263261
if let Some(users_file) = self.users_file.clone() {
264-
return Ok(UserStoreKind::File(FileUserStore::new(users_file)));
262+
return Ok(Arc::new(FileUserStore::new(users_file)));
265263
}
266264

267265
if let Some(url) = self.users_sql_url.clone() {
@@ -276,15 +274,12 @@ impl Config {
276274
));
277275
};
278276

279-
return Ok(UserStoreKind::SQL(SQLUserStore::new(
280-
&url,
281-
query_all,
282-
query_email,
283-
)?));
277+
return Ok(Arc::new(SQLUserStore::new(&url, query_all, query_email)?));
284278
}
285279

286280
error!("No users configured, using an empty user store");
287-
Ok(UserStoreKind::Static(StaticUserStore::new(vec![])))
281+
// TODO: This does not hot-reload
282+
Ok(Arc::new(StaticUserStore::new(vec![])))
288283
}
289284
}
290285

src/handle_admin.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use crate::error::{AppError, AuthError};
1111
use crate::secret::LoginLinkSecret;
1212
use crate::secret::admin_token::{AdminApiTokenMetadata, AdminApiTokenSecret};
1313
use crate::secret::login_link::LoginLinkRedirect;
14-
use crate::user_store::UserStore;
1514
use crate::{AppState, secret::BrowserSessionSecret};
1615

1716
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
@@ -35,7 +34,7 @@ pub struct GenerateMagicLinkResponse {
3534
#[axum::debug_handler]
3635
pub async fn handle_admin_generate_magic_link(
3736
_: AdminApiTokenSecret,
38-
State(mut state): State<AppState>,
37+
State(state): State<AppState>,
3938
config: LiveConfig,
4039
Json(generate_magic_link_request): Json<GenerateMagicLinkRequest>,
4140
) -> Response {

src/handle_login_post.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ use crate::pages::{LoginActionPage, Page};
1919
use crate::secret::LoginLinkSecret;
2020
use crate::secret::login_link::LoginLinkRedirect;
2121

22-
use crate::user_store::UserStore as _;
23-
2422
/// Used to get the login form data for from the login page
2523
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
2624
pub struct LoginInfo {
@@ -30,7 +28,7 @@ pub struct LoginInfo {
3028
#[axum::debug_handler]
3129
pub async fn handle_login_post(
3230
config: LiveConfig,
33-
State(mut state): State<AppState>,
31+
State(state): State<AppState>,
3432
Query(login_redirect): Query<LoginLinkRedirect>,
3533
Form(form): Form<LoginInfo>,
3634
) -> Result<Response, AppError> {

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ use tracing_subscriber::util::SubscriberInitExt;
5858
use tracing_subscriber::{EnvFilter, fmt};
5959

6060
use crate::error::AppError;
61-
use crate::user_store::UserStoreKind;
61+
use crate::user_store::UserStore;
6262
use crate::{config::Config, user::User};
6363

6464
pub mod app_build;
@@ -143,7 +143,7 @@ pub struct InFlightConfig(Arc<Config>);
143143
pub struct AppState {
144144
pub db: crate::Database,
145145
config: Arc<ArcSwap<Config>>,
146-
pub user_store: UserStoreKind,
146+
pub user_store: Arc<dyn UserStore>,
147147
pub link_senders: Vec<Arc<dyn LinkSender>>,
148148

149149
pub key: jsonwebtoken::EncodingKey,

src/user_store.rs

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,7 @@ use crate::user::User;
66

77
#[async_trait::async_trait]
88
pub trait UserStore: Send + Sync {
9-
async fn from_email(&mut self, email: &str) -> Option<User>;
10-
}
11-
12-
#[derive(Debug, Clone)]
13-
pub enum UserStoreKind {
14-
Static(StaticUserStore),
15-
File(FileUserStore),
16-
SQL(SQLUserStore),
17-
}
18-
19-
#[async_trait::async_trait]
20-
impl UserStore for UserStoreKind {
21-
async fn from_email(&mut self, email: &str) -> Option<User> {
22-
match self {
23-
UserStoreKind::Static(store) => store.from_email(email).await,
24-
UserStoreKind::File(store) => store.from_email(email).await,
25-
UserStoreKind::SQL(store) => store.from_email(email).await,
26-
}
27-
}
9+
async fn from_email(&self, email: &str) -> Option<User>;
2810
}
2911

3012
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -38,7 +20,7 @@ impl StaticUserStore {
3820

3921
#[async_trait::async_trait]
4022
impl UserStore for StaticUserStore {
41-
async fn from_email(&mut self, email: &str) -> Option<User> {
23+
async fn from_email(&self, email: &str) -> Option<User> {
4224
self.0.iter().find(|user| user.email == email).cloned()
4325
}
4426
}
@@ -54,7 +36,7 @@ impl FileUserStore {
5436

5537
#[async_trait::async_trait]
5638
impl UserStore for FileUserStore {
57-
async fn from_email(&mut self, email: &str) -> Option<User> {
39+
async fn from_email(&self, email: &str) -> Option<User> {
5840
let users_contents = match std::fs::read_to_string(self.0.clone()) {
5941
Ok(contents) => contents,
6042
Err(e) => {
@@ -95,7 +77,7 @@ impl SQLUserStore {
9577

9678
#[async_trait::async_trait]
9779
impl UserStore for SQLUserStore {
98-
async fn from_email(&mut self, email: &str) -> Option<User> {
80+
async fn from_email(&self, email: &str) -> Option<User> {
9981
// let Ok(conn) = self.conn.acquire().await else {
10082
// error!("Failed to acquire connection from the SQL user pool");
10183
// return None;

src/utils.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ pub mod tests {
1818
use crate::config::Config;
1919
use crate::database::init_database;
2020
use crate::user::User;
21-
use crate::user_store::UserStore;
2221
use crate::{CONFIG_FILE, Database};
2322

2423
use super::*;

src/webauthn/handle_auth_start.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@ use crate::error::{AppError, WebAuthnError};
1111
use crate::handle_login_post::LoginInfo;
1212
use crate::secret::WebAuthnAuthSecret;
1313

14-
use crate::user_store::UserStore as _;
15-
1614
#[axum::debug_handler]
1715
pub async fn handle_auth_start(
1816
config: LiveConfig,
19-
State(mut state): State<AppState>,
17+
State(state): State<AppState>,
2018
jar: CookieJar,
2119
form: Json<LoginInfo>,
2220
) -> Result<(CookieJar, impl IntoResponse), AppError> {

0 commit comments

Comments
 (0)