Skip to content

Commit f4c7b97

Browse files
committed
refactor: [torrust#1195] extract DatabaseKeyRepository
1 parent 44255e2 commit f4c7b97

File tree

4 files changed

+64
-9
lines changed

4 files changed

+64
-9
lines changed

src/core/authentication/key/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
//!
3838
//! assert!(authentication::key::verify_key_expiration(&expiring_key).is_ok());
3939
//! ```
40+
pub mod repository;
4041

4142
use std::panic::Location;
4243
use std::str::FromStr;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod persisted;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use std::sync::Arc;
2+
3+
use crate::core::authentication::key::{Key, PeerKey};
4+
use crate::core::databases::{self, Database};
5+
6+
/// The database repository for the authentication keys.
7+
pub struct DatabaseKeyRepository {
8+
database: Arc<Box<dyn Database>>,
9+
}
10+
11+
impl DatabaseKeyRepository {
12+
#[must_use]
13+
pub fn new(database: &Arc<Box<dyn Database>>) -> Self {
14+
Self {
15+
database: database.clone(),
16+
}
17+
}
18+
19+
/// It adds a new key to the database.
20+
///
21+
/// # Errors
22+
///
23+
/// Will return a `databases::error::Error` if unable to add the `auth_key` to the database.
24+
pub fn add(&self, peer_key: &PeerKey) -> Result<(), databases::error::Error> {
25+
self.database.add_key_to_keys(peer_key)?;
26+
Ok(())
27+
}
28+
29+
/// It removes an key from the database.
30+
///
31+
/// # Errors
32+
///
33+
/// Will return a `database::Error` if unable to remove the `key` from the database.
34+
pub fn remove(&self, key: &Key) -> Result<(), databases::error::Error> {
35+
self.database.remove_key_from_keys(key)?;
36+
Ok(())
37+
}
38+
39+
/// It loads all keys from the database.
40+
///
41+
/// # Errors
42+
///
43+
/// Will return a `database::Error` if unable to load the keys from the database.
44+
pub fn load_keys(&self) -> Result<Vec<PeerKey>, databases::error::Error> {
45+
let keys = self.database.load_keys()?;
46+
Ok(keys)
47+
}
48+
}

src/core/authentication/mod.rs

+14-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::panic::Location;
22
use std::sync::Arc;
33
use std::time::Duration;
44

5+
use key::repository::persisted::DatabaseKeyRepository;
56
use torrust_tracker_clock::clock::Time;
67
use torrust_tracker_configuration::Core;
78
use torrust_tracker_located_error::Located;
@@ -35,21 +36,20 @@ pub struct Facade {
3536
/// The tracker configuration.
3637
config: Core,
3738

38-
/// A database driver implementation: [`Sqlite3`](crate::core::databases::sqlite)
39-
/// or [`MySQL`](crate::core::databases::mysql)
40-
database: Arc<Box<dyn Database>>,
41-
4239
/// Tracker users' keys. Only for private trackers.
4340
keys: tokio::sync::RwLock<std::collections::HashMap<Key, PeerKey>>,
41+
42+
/// The database repository for the authentication keys.
43+
db_key_repository: DatabaseKeyRepository,
4444
}
4545

4646
impl Facade {
4747
#[must_use]
4848
pub fn new(config: &Core, database: &Arc<Box<dyn Database>>) -> Self {
4949
Self {
5050
config: config.clone(),
51-
database: database.clone(),
5251
keys: tokio::sync::RwLock::new(std::collections::HashMap::new()),
52+
db_key_repository: DatabaseKeyRepository::new(database),
5353
}
5454
}
5555

@@ -205,7 +205,8 @@ impl Facade {
205205
pub async fn generate_auth_key(&self, lifetime: Option<Duration>) -> Result<PeerKey, databases::error::Error> {
206206
let auth_key = key::generate_key(lifetime);
207207

208-
self.database.add_key_to_keys(&auth_key)?;
208+
self.db_key_repository.add(&auth_key)?;
209+
209210
self.keys.write().await.insert(auth_key.key.clone(), auth_key.clone());
210211
Ok(auth_key)
211212
}
@@ -254,7 +255,8 @@ impl Facade {
254255
// code-review: should we return a friendly error instead of the DB
255256
// constrain error when the key already exist? For now, it's returning
256257
// the specif error for each DB driver when a UNIQUE constrain fails.
257-
self.database.add_key_to_keys(&auth_key)?;
258+
self.db_key_repository.add(&auth_key)?;
259+
258260
self.keys.write().await.insert(auth_key.key.clone(), auth_key.clone());
259261
Ok(auth_key)
260262
}
@@ -267,8 +269,10 @@ impl Facade {
267269
///
268270
/// Will return a `database::Error` if unable to remove the `key` to the database.
269271
pub async fn remove_auth_key(&self, key: &Key) -> Result<(), databases::error::Error> {
270-
self.database.remove_key_from_keys(key)?;
272+
self.db_key_repository.remove(key)?;
273+
271274
self.remove_in_memory_auth_key(key).await;
275+
272276
Ok(())
273277
}
274278

@@ -290,7 +294,8 @@ impl Facade {
290294
///
291295
/// Will return a `database::Error` if unable to `load_keys` from the database.
292296
pub async fn load_keys_from_database(&self) -> Result<(), databases::error::Error> {
293-
let keys_from_database = self.database.load_keys()?;
297+
let keys_from_database = self.db_key_repository.load_keys()?;
298+
294299
let mut keys = self.keys.write().await;
295300

296301
keys.clear();

0 commit comments

Comments
 (0)