Skip to content

Commit

Permalink
Merge #614: 448 move authorization logic to new layer
Browse files Browse the repository at this point in the history
478e234 feat: [#448] new authorization service implemented in the other services (Mario)
9ebd7b5 feat: [#448] new authorization service (Mario)
b1d5536 feat: [#448] new user repository trait (Mario)
a2ce990  feat: [#448] added mockall dependency (Mario)

Pull request description:

  Resolves #448.

  Replaces #520.

ACKs for top commit:
  josecelano:
    ACK478e2349f159da7d08b60382fd80a3f236067637

Tree-SHA512: ec93e9d95ff8b934397bd1bb13cc76077b33d2abd962bad0b667b134768066ed33aced0b174830eea7947a0e9f53273fe062147ee15342eaf1db8a5dfaaa3cfd
  • Loading branch information
josecelano committed Jun 4, 2024
2 parents 6d4a955 + 478e234 commit 3c4522f
Show file tree
Hide file tree
Showing 13 changed files with 348 additions and 80 deletions.
72 changes: 72 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ hyper = "1"
hyper-util = { version = "0.1.3", features = ["http1", "http2", "tokio"] }
indexmap = "2"
jsonwebtoken = "9"
mockall = "0.12.1"
lazy_static = "1.4.0"
lettre = { version = "0", features = [
"builder",
Expand Down
16 changes: 10 additions & 6 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use crate::services::torrent::{
DbCanonicalInfoHashGroupRepository, DbTorrentAnnounceUrlRepository, DbTorrentFileRepository, DbTorrentInfoRepository,
DbTorrentListingGenerator, DbTorrentRepository, DbTorrentTagRepository,
};
use crate::services::user::{self, DbBannedUserList, DbUserProfileRepository, DbUserRepository};
use crate::services::{proxy, settings, torrent};
use crate::services::user::{self, DbBannedUserList, DbUserProfileRepository, DbUserRepository, Repository};
use crate::services::{authorization, proxy, settings, torrent};
use crate::tracker::statistics_importer::StatisticsImporter;
use crate::web::api::server::signals::Halted;
use crate::web::api::server::v1::auth::Authentication;
Expand Down Expand Up @@ -74,7 +74,7 @@ pub async fn run(configuration: Configuration, api_version: &Version) -> Running
// Repositories
let category_repository = Arc::new(DbCategoryRepository::new(database.clone()));
let tag_repository = Arc::new(DbTagRepository::new(database.clone()));
let user_repository = Arc::new(DbUserRepository::new(database.clone()));
let user_repository: Arc<Box<dyn Repository>> = Arc::new(Box::new(DbUserRepository::new(database.clone())));
let user_authentication_repository = Arc::new(DbUserAuthenticationRepository::new(database.clone()));
let user_profile_repository = Arc::new(DbUserProfileRepository::new(database.clone()));
let torrent_repository = Arc::new(DbTorrentRepository::new(database.clone()));
Expand All @@ -87,15 +87,19 @@ pub async fn run(configuration: Configuration, api_version: &Version) -> Running
let banned_user_list = Arc::new(DbBannedUserList::new(database.clone()));

// Services
let authorization_service = Arc::new(authorization::Service::new(user_repository.clone()));
let tracker_service = Arc::new(tracker::service::Service::new(configuration.clone(), database.clone()).await);
let tracker_statistics_importer =
Arc::new(StatisticsImporter::new(configuration.clone(), tracker_service.clone(), database.clone()).await);
let mailer_service = Arc::new(mailer::Service::new(configuration.clone()).await);
let image_cache_service: Arc<ImageCacheService> = Arc::new(ImageCacheService::new(configuration.clone()).await);
let category_service = Arc::new(category::Service::new(category_repository.clone(), user_repository.clone()));
let tag_service = Arc::new(tag::Service::new(tag_repository.clone(), user_repository.clone()));
let category_service = Arc::new(category::Service::new(
category_repository.clone(),
authorization_service.clone(),
));
let tag_service = Arc::new(tag::Service::new(tag_repository.clone(), authorization_service.clone()));
let proxy_service = Arc::new(proxy::Service::new(image_cache_service.clone(), user_repository.clone()));
let settings_service = Arc::new(settings::Service::new(configuration.clone(), user_repository.clone()));
let settings_service = Arc::new(settings::Service::new(configuration.clone(), authorization_service.clone()));
let torrent_index = Arc::new(torrent::Index::new(
configuration.clone(),
tracker_statistics_importer.clone(),
Expand Down
7 changes: 4 additions & 3 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ use crate::services::torrent::{
DbCanonicalInfoHashGroupRepository, DbTorrentAnnounceUrlRepository, DbTorrentFileRepository, DbTorrentInfoRepository,
DbTorrentListingGenerator, DbTorrentRepository, DbTorrentTagRepository,
};
use crate::services::user::{self, DbBannedUserList, DbUserProfileRepository, DbUserRepository};
use crate::services::user::{self, DbBannedUserList, DbUserProfileRepository, Repository};
use crate::services::{proxy, settings, torrent};
use crate::tracker::statistics_importer::StatisticsImporter;
use crate::web::api::server::v1::auth::Authentication;
use crate::{mailer, tracker};

pub type Username = String;

pub struct AppData {
Expand All @@ -30,7 +31,7 @@ pub struct AppData {
// Repositories
pub category_repository: Arc<DbCategoryRepository>,
pub tag_repository: Arc<DbTagRepository>,
pub user_repository: Arc<DbUserRepository>,
pub user_repository: Arc<Box<dyn Repository>>,
pub user_authentication_repository: Arc<DbUserAuthenticationRepository>,
pub user_profile_repository: Arc<DbUserProfileRepository>,
pub torrent_repository: Arc<DbTorrentRepository>,
Expand Down Expand Up @@ -66,7 +67,7 @@ impl AppData {
// Repositories
category_repository: Arc<DbCategoryRepository>,
tag_repository: Arc<DbTagRepository>,
user_repository: Arc<DbUserRepository>,
user_repository: Arc<Box<dyn Repository>>,
user_authentication_repository: Arc<DbUserAuthenticationRepository>,
user_profile_repository: Arc<DbUserProfileRepository>,
torrent_repository: Arc<DbTorrentRepository>,
Expand Down
7 changes: 4 additions & 3 deletions src/services/authentication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ use argon2::{Argon2, PasswordHash, PasswordVerifier};
use jsonwebtoken::{decode, encode, Algorithm, DecodingKey, EncodingKey, Header, Validation};
use pbkdf2::Pbkdf2;

use super::user::{DbUserProfileRepository, DbUserRepository};
use super::user::DbUserProfileRepository;
use crate::config::Configuration;
use crate::databases::database::{Database, Error};
use crate::errors::ServiceError;
use crate::models::user::{UserAuthentication, UserClaims, UserCompact, UserId};
use crate::services::user::Repository;
use crate::utils::clock;

pub struct Service {
configuration: Arc<Configuration>,
json_web_token: Arc<JsonWebToken>,
user_repository: Arc<DbUserRepository>,
user_repository: Arc<Box<dyn Repository>>,
user_profile_repository: Arc<DbUserProfileRepository>,
user_authentication_repository: Arc<DbUserAuthenticationRepository>,
}
Expand All @@ -24,7 +25,7 @@ impl Service {
pub fn new(
configuration: Arc<Configuration>,
json_web_token: Arc<JsonWebToken>,
user_repository: Arc<DbUserRepository>,
user_repository: Arc<Box<dyn Repository>>,
user_profile_repository: Arc<DbUserProfileRepository>,
user_authentication_repository: Arc<DbUserAuthenticationRepository>,
) -> Self {
Expand Down
Loading

0 comments on commit 3c4522f

Please sign in to comment.