From a44e33931cfe950bd724527b7253789c1f5f20bc Mon Sep 17 00:00:00 2001 From: Quentin Gliech Date: Thu, 16 Sep 2021 14:43:56 +0200 Subject: [PATCH] Split the service in multiple crates --- Cargo.lock | 74 ++++++++++++---- Cargo.toml | 5 +- crates/cli/Cargo.toml | 27 ++++++ .../src/cli => crates/cli/src}/config.rs | 2 +- .../src/cli => crates/cli/src}/database.rs | 3 +- .../src/cli/mod.rs => crates/cli/src/main.rs | 38 ++++++++- .../src/cli => crates/cli/src}/manage.rs | 3 +- .../src/cli => crates/cli/src}/server.rs | 12 +-- crates/config/Cargo.toml | 38 +++++++++ .../config => crates/config/src}/cookies.rs | 4 + .../src/config => crates/config/src}/csrf.rs | 4 + .../config => crates/config/src}/database.rs | 22 +++-- .../src/config => crates/config/src}/http.rs | 4 + .../config/mod.rs => crates/config/src/lib.rs | 10 +++ .../config => crates/config/src}/oauth2.rs | 85 +++++++++---------- .../src/config => crates/config/src}/util.rs | 3 + .../core}/Cargo.toml | 8 +- .../20210709203828_timestamp_trigger.down.sql | 0 .../20210709203828_timestamp_trigger.up.sql | 0 .../migrations/20210716213724_users.down.sql | 0 .../migrations/20210716213724_users.up.sql | 0 .../20210722072901_user_sessions.down.sql | 0 .../20210722072901_user_sessions.up.sql | 0 .../20210731130515_oauth2_sessions.down.sql | 0 .../20210731130515_oauth2_sessions.up.sql | 0 ...210813080413_oauth2_access_tokens.down.sql | 0 ...20210813080413_oauth2_access_tokens.up.sql | 0 ...10827112032_oauth2_refresh_tokens.down.sql | 0 ...0210827112032_oauth2_refresh_tokens.up.sql | 0 .../core}/sqlx-data.json | 0 .../core}/src/errors.rs | 2 +- .../core}/src/filters/authenticate.rs | 0 .../core}/src/filters/client.rs | 4 + .../core}/src/filters/cookies.rs | 6 +- .../core}/src/filters/csrf.rs | 8 +- .../core}/src/filters/database.rs | 0 .../core}/src/filters/errors.rs | 0 .../core}/src/filters/headers.rs | 0 .../core}/src/filters/mod.rs | 4 +- .../core}/src/filters/session.rs | 6 +- .../core}/src/handlers/health.rs | 0 .../core}/src/handlers/mod.rs | 2 +- .../src/handlers/oauth2/authorization.rs | 0 .../core}/src/handlers/oauth2/discovery.rs | 0 .../src/handlers/oauth2/introspection.rs | 0 .../core}/src/handlers/oauth2/keys.rs | 0 .../core}/src/handlers/oauth2/mod.rs | 0 .../core}/src/handlers/oauth2/token.rs | 0 .../core}/src/handlers/oauth2/userinfo.rs | 0 .../core}/src/handlers/views/index.rs | 0 .../core}/src/handlers/views/login.rs | 0 .../core}/src/handlers/views/logout.rs | 0 .../core}/src/handlers/views/mod.rs | 0 .../core}/src/handlers/views/reauth.rs | 0 .../core}/src/handlers/views/register.rs | 0 crates/core/src/lib.rs | 31 +++++++ .../core}/src/storage/mod.rs | 0 .../core}/src/storage/oauth2/access_token.rs | 2 +- .../src/storage/oauth2/authorization_code.rs | 0 .../core}/src/storage/oauth2/mod.rs | 0 .../core}/src/storage/oauth2/refresh_token.rs | 0 .../core}/src/storage/oauth2/session.rs | 2 +- .../core}/src/storage/user.rs | 2 +- .../core}/src/tasks/database.rs | 2 +- .../core}/src/tasks/mod.rs | 0 .../core}/src/templates.rs | 12 +-- .../core}/src/tokens.rs | 0 .../core}/templates/base.html | 0 .../core}/templates/error.html | 0 .../core}/templates/error.txt | 0 .../core}/templates/form_post.html | 0 .../core}/templates/index.html | 0 .../core}/templates/login.html | 0 .../core}/templates/reauth.html | 0 .../core}/templates/register.html | 0 .../oauth2-types}/Cargo.toml | 0 .../oauth2-types}/src/errors.rs | 0 .../oauth2-types}/src/lib.rs | 0 .../oauth2-types}/src/oidc.rs | 0 .../oauth2-types}/src/pkce.rs | 0 .../oauth2-types}/src/requests.rs | 0 .../oauth2-types}/src/test_utils.rs | 0 matrix-authentication-service/src/main.rs | 60 ------------- 83 files changed, 311 insertions(+), 174 deletions(-) create mode 100644 crates/cli/Cargo.toml rename {matrix-authentication-service/src/cli => crates/cli/src}/config.rs (97%) rename {matrix-authentication-service/src/cli => crates/cli/src}/database.rs (95%) rename matrix-authentication-service/src/cli/mod.rs => crates/cli/src/main.rs (62%) rename {matrix-authentication-service/src/cli => crates/cli/src}/manage.rs (95%) rename {matrix-authentication-service/src/cli => crates/cli/src}/server.rs (96%) create mode 100644 crates/config/Cargo.toml rename {matrix-authentication-service/src/config => crates/config/src}/cookies.rs (95%) rename {matrix-authentication-service/src/config => crates/config/src}/csrf.rs (97%) rename {matrix-authentication-service/src/config => crates/config/src}/database.rs (91%) rename {matrix-authentication-service/src/config => crates/config/src}/http.rs (95%) rename matrix-authentication-service/src/config/mod.rs => crates/config/src/lib.rs (86%) rename {matrix-authentication-service/src/config => crates/config/src}/oauth2.rs (98%) rename {matrix-authentication-service/src/config => crates/config/src}/util.rs (97%) rename {matrix-authentication-service => crates/core}/Cargo.toml (90%) rename {matrix-authentication-service => crates/core}/migrations/20210709203828_timestamp_trigger.down.sql (100%) rename {matrix-authentication-service => crates/core}/migrations/20210709203828_timestamp_trigger.up.sql (100%) rename {matrix-authentication-service => crates/core}/migrations/20210716213724_users.down.sql (100%) rename {matrix-authentication-service => crates/core}/migrations/20210716213724_users.up.sql (100%) rename {matrix-authentication-service => crates/core}/migrations/20210722072901_user_sessions.down.sql (100%) rename {matrix-authentication-service => crates/core}/migrations/20210722072901_user_sessions.up.sql (100%) rename {matrix-authentication-service => crates/core}/migrations/20210731130515_oauth2_sessions.down.sql (100%) rename {matrix-authentication-service => crates/core}/migrations/20210731130515_oauth2_sessions.up.sql (100%) rename {matrix-authentication-service => crates/core}/migrations/20210813080413_oauth2_access_tokens.down.sql (100%) rename {matrix-authentication-service => crates/core}/migrations/20210813080413_oauth2_access_tokens.up.sql (100%) rename {matrix-authentication-service => crates/core}/migrations/20210827112032_oauth2_refresh_tokens.down.sql (100%) rename {matrix-authentication-service => crates/core}/migrations/20210827112032_oauth2_refresh_tokens.up.sql (100%) rename {matrix-authentication-service => crates/core}/sqlx-data.json (100%) rename {matrix-authentication-service => crates/core}/src/errors.rs (98%) rename {matrix-authentication-service => crates/core}/src/filters/authenticate.rs (100%) rename {matrix-authentication-service => crates/core}/src/filters/client.rs (98%) rename {matrix-authentication-service => crates/core}/src/filters/cookies.rs (97%) rename {matrix-authentication-service => crates/core}/src/filters/csrf.rs (96%) rename {matrix-authentication-service => crates/core}/src/filters/database.rs (100%) rename {matrix-authentication-service => crates/core}/src/filters/errors.rs (100%) rename {matrix-authentication-service => crates/core}/src/filters/headers.rs (100%) rename {matrix-authentication-service => crates/core}/src/filters/mod.rs (95%) rename {matrix-authentication-service => crates/core}/src/filters/session.rs (94%) rename {matrix-authentication-service => crates/core}/src/handlers/health.rs (100%) rename {matrix-authentication-service => crates/core}/src/handlers/mod.rs (98%) rename {matrix-authentication-service => crates/core}/src/handlers/oauth2/authorization.rs (100%) rename {matrix-authentication-service => crates/core}/src/handlers/oauth2/discovery.rs (100%) rename {matrix-authentication-service => crates/core}/src/handlers/oauth2/introspection.rs (100%) rename {matrix-authentication-service => crates/core}/src/handlers/oauth2/keys.rs (100%) rename {matrix-authentication-service => crates/core}/src/handlers/oauth2/mod.rs (100%) rename {matrix-authentication-service => crates/core}/src/handlers/oauth2/token.rs (100%) rename {matrix-authentication-service => crates/core}/src/handlers/oauth2/userinfo.rs (100%) rename {matrix-authentication-service => crates/core}/src/handlers/views/index.rs (100%) rename {matrix-authentication-service => crates/core}/src/handlers/views/login.rs (100%) rename {matrix-authentication-service => crates/core}/src/handlers/views/logout.rs (100%) rename {matrix-authentication-service => crates/core}/src/handlers/views/mod.rs (100%) rename {matrix-authentication-service => crates/core}/src/handlers/views/reauth.rs (100%) rename {matrix-authentication-service => crates/core}/src/handlers/views/register.rs (100%) create mode 100644 crates/core/src/lib.rs rename {matrix-authentication-service => crates/core}/src/storage/mod.rs (100%) rename {matrix-authentication-service => crates/core}/src/storage/oauth2/access_token.rs (98%) rename {matrix-authentication-service => crates/core}/src/storage/oauth2/authorization_code.rs (100%) rename {matrix-authentication-service => crates/core}/src/storage/oauth2/mod.rs (100%) rename {matrix-authentication-service => crates/core}/src/storage/oauth2/refresh_token.rs (100%) rename {matrix-authentication-service => crates/core}/src/storage/oauth2/session.rs (98%) rename {matrix-authentication-service => crates/core}/src/storage/user.rs (99%) rename {matrix-authentication-service => crates/core}/src/tasks/database.rs (94%) rename {matrix-authentication-service => crates/core}/src/tasks/mod.rs (100%) rename {matrix-authentication-service => crates/core}/src/templates.rs (95%) rename {matrix-authentication-service => crates/core}/src/tokens.rs (100%) rename {matrix-authentication-service => crates/core}/templates/base.html (100%) rename {matrix-authentication-service => crates/core}/templates/error.html (100%) rename {matrix-authentication-service => crates/core}/templates/error.txt (100%) rename {matrix-authentication-service => crates/core}/templates/form_post.html (100%) rename {matrix-authentication-service => crates/core}/templates/index.html (100%) rename {matrix-authentication-service => crates/core}/templates/login.html (100%) rename {matrix-authentication-service => crates/core}/templates/reauth.html (100%) rename {matrix-authentication-service => crates/core}/templates/register.html (100%) rename {oauth2-types => crates/oauth2-types}/Cargo.toml (100%) rename {oauth2-types => crates/oauth2-types}/src/errors.rs (100%) rename {oauth2-types => crates/oauth2-types}/src/lib.rs (100%) rename {oauth2-types => crates/oauth2-types}/src/oidc.rs (100%) rename {oauth2-types => crates/oauth2-types}/src/pkce.rs (100%) rename {oauth2-types => crates/oauth2-types}/src/requests.rs (100%) rename {oauth2-types => crates/oauth2-types}/src/test_utils.rs (100%) delete mode 100644 matrix-authentication-service/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 96c9e227..53962c25 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1355,22 +1355,55 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3e2e65a1a2e43cfcb47a895c4c8b10d1f4a61097f9f254f183aee60cad9c651d" [[package]] -name = "matchers" -version = "0.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f099785f7595cc4b4553a174ce30dd7589ef93391ff414dbb67f62392b9e0ce1" +name = "mas-cli" +version = "0.1.0" dependencies = [ - "regex-automata", + "anyhow", + "argon2", + "clap", + "dotenv", + "hyper", + "indoc", + "mas-config", + "mas-core", + "schemars", + "serde_yaml", + "tokio", + "tower", + "tower-http", + "tracing", + "tracing-subscriber", + "warp", ] [[package]] -name = "matches" -version = "0.1.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a3e378b66a060d48947b590737b30a1be76706c8dd7b8ba0f2fe3989c68a853f" +name = "mas-config" +version = "0.1.0" +dependencies = [ + "anyhow", + "async-trait", + "chrono", + "elliptic-curve", + "figment", + "indoc", + "jwt-compact", + "k256", + "pkcs8", + "rand 0.8.4", + "rsa", + "schemars", + "serde", + "serde_json", + "serde_with", + "sqlx", + "thiserror", + "tokio", + "tracing", + "url", +] [[package]] -name = "matrix-authentication-service" +name = "mas-core" version = "0.1.0" dependencies = [ "anyhow", @@ -1379,11 +1412,9 @@ dependencies = [ "bincode", "chacha20poly1305", "chrono", - "clap", "cookie", "crc", "data-encoding", - "dotenv", "elliptic-curve", "figment", "futures-util", @@ -1393,6 +1424,7 @@ dependencies = [ "itertools", "jwt-compact", "k256", + "mas-config", "mime", "oauth2-types", "password-hash", @@ -1411,14 +1443,26 @@ dependencies = [ "thiserror", "tokio", "tokio-stream", - "tower", - "tower-http", "tracing", - "tracing-subscriber", "url", "warp", ] +[[package]] +name = "matchers" +version = "0.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f099785f7595cc4b4553a174ce30dd7589ef93391ff414dbb67f62392b9e0ce1" +dependencies = [ + "regex-automata", +] + +[[package]] +name = "matches" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3e378b66a060d48947b590737b30a1be76706c8dd7b8ba0f2fe3989c68a853f" + [[package]] name = "md-5" version = "0.9.1" diff --git a/Cargo.toml b/Cargo.toml index 4a4909c7..bcc77e62 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,8 +1,5 @@ [workspace] -members = [ - "oauth2-types", - "matrix-authentication-service", -] +members = ["crates/*"] resolver = "2" diff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.toml new file mode 100644 index 00000000..3ae34ba2 --- /dev/null +++ b/crates/cli/Cargo.toml @@ -0,0 +1,27 @@ +[package] +name = "mas-cli" +version = "0.1.0" +authors = ["Quentin Gliech "] +edition = "2018" +license = "Apache-2.0" + +[dependencies] +tokio = { version = "1.11.0", features = ["full"] } +anyhow = "1.0.44" +clap = "3.0.0-beta.4" +tracing = "0.1.27" +tracing-subscriber = "0.2.22" +dotenv = "0.15.0" +schemars = { version = "0.8.3", features = ["url", "chrono"] } +tower = { version = "0.4.8", features = ["full"] } +tower-http = { version = "0.1.1", features = ["full"] } +hyper = { version = "0.14.12", features = ["full"] } +serde_yaml = "0.8.21" +warp = "0.3.1" +argon2 = { version = "0.3.1", features = ["password-hash"] } + +mas-config = { path = "../config" } +mas-core = { path = "../core" } + +[dev-dependencies] +indoc = "1.0.3" diff --git a/matrix-authentication-service/src/cli/config.rs b/crates/cli/src/config.rs similarity index 97% rename from matrix-authentication-service/src/cli/config.rs rename to crates/cli/src/config.rs index 35451b30..052e683f 100644 --- a/matrix-authentication-service/src/cli/config.rs +++ b/crates/cli/src/config.rs @@ -13,11 +13,11 @@ // limitations under the License. use clap::Clap; +use mas_config::{ConfigurationSection, RootConfig}; use schemars::schema_for; use tracing::info; use super::RootCommand; -use crate::config::{ConfigurationSection, RootConfig}; #[derive(Clap, Debug)] pub(super) struct ConfigCommand { diff --git a/matrix-authentication-service/src/cli/database.rs b/crates/cli/src/database.rs similarity index 95% rename from matrix-authentication-service/src/cli/database.rs rename to crates/cli/src/database.rs index c95469f0..5108bd9e 100644 --- a/matrix-authentication-service/src/cli/database.rs +++ b/crates/cli/src/database.rs @@ -14,9 +14,10 @@ use anyhow::Context; use clap::Clap; +use mas_config::DatabaseConfig; +use mas_core::storage::MIGRATOR; use super::RootCommand; -use crate::{config::DatabaseConfig, storage::MIGRATOR}; #[derive(Clap, Debug)] pub(super) struct DatabaseCommand { diff --git a/matrix-authentication-service/src/cli/mod.rs b/crates/cli/src/main.rs similarity index 62% rename from matrix-authentication-service/src/cli/mod.rs rename to crates/cli/src/main.rs index ba6ac857..7da9154c 100644 --- a/matrix-authentication-service/src/cli/mod.rs +++ b/crates/cli/src/main.rs @@ -12,18 +12,22 @@ // See the License for the specific language governing permissions and // limitations under the License. -// Clippy seems confused by clap.rs derive macros +#![forbid(unsafe_code)] +#![deny(clippy::all)] +#![warn(clippy::pedantic)] +#![allow(clippy::module_name_repetitions)] #![allow(clippy::suspicious_else_formatting)] use std::path::PathBuf; use anyhow::Context; use clap::Clap; +use mas_config::ConfigurationSection; +use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Registry}; use self::{ config::ConfigCommand, database::DatabaseCommand, manage::ManageCommand, server::ServerCommand, }; -use crate::config::ConfigurationSection; mod config; mod database; @@ -46,7 +50,7 @@ enum Subcommand { } #[derive(Clap, Debug)] -pub struct RootCommand { +struct RootCommand { /// Path to the configuration file #[clap(short, long, global = true, default_value = "config.yaml")] config: PathBuf, @@ -56,7 +60,7 @@ pub struct RootCommand { } impl RootCommand { - pub async fn run(&self) -> anyhow::Result<()> { + async fn run(&self) -> anyhow::Result<()> { use Subcommand as S; match &self.subcommand { Some(S::Config(c)) => c.run(self).await, @@ -71,3 +75,29 @@ impl RootCommand { T::load_from_file(&self.config).context("could not load configuration") } } + +#[tokio::main] +async fn main() -> anyhow::Result<()> { + // Load environment variables from .env files + if let Err(e) = dotenv::dotenv() { + // Display the error if it is something other than the .env file not existing + if !e.not_found() { + return Err(e).context("could not load .env file"); + } + } + + // Setup logging & tracing + let fmt_layer = tracing_subscriber::fmt::layer().with_writer(std::io::stderr); + let filter_layer = EnvFilter::try_from_default_env().or_else(|_| EnvFilter::try_new("info"))?; + + let subscriber = Registry::default().with(filter_layer).with(fmt_layer); + subscriber + .try_init() + .context("could not initialize logging")?; + + // Parse the CLI arguments + let opts = RootCommand::parse(); + + // And run the command + opts.run().await +} diff --git a/matrix-authentication-service/src/cli/manage.rs b/crates/cli/src/manage.rs similarity index 95% rename from matrix-authentication-service/src/cli/manage.rs rename to crates/cli/src/manage.rs index f5a3c750..6b283453 100644 --- a/matrix-authentication-service/src/cli/manage.rs +++ b/crates/cli/src/manage.rs @@ -14,10 +14,11 @@ use argon2::Argon2; use clap::Clap; +use mas_config::DatabaseConfig; +use mas_core::storage::register_user; use tracing::{info, warn}; use super::RootCommand; -use crate::{config::DatabaseConfig, storage::register_user}; #[derive(Clap, Debug)] pub(super) struct ManageCommand { diff --git a/matrix-authentication-service/src/cli/server.rs b/crates/cli/src/server.rs similarity index 96% rename from matrix-authentication-service/src/cli/server.rs rename to crates/cli/src/server.rs index 11e349f4..f23fdad9 100644 --- a/matrix-authentication-service/src/cli/server.rs +++ b/crates/cli/src/server.rs @@ -20,6 +20,11 @@ use std::{ use anyhow::Context; use clap::Clap; use hyper::{header, Server}; +use mas_config::RootConfig; +use mas_core::{ + tasks::{self, TaskQueue}, + templates::Templates, +}; use tower::{make::Shared, ServiceBuilder}; use tower_http::{ compression::CompressionLayer, @@ -29,11 +34,6 @@ use tower_http::{ }; use super::RootCommand; -use crate::{ - config::RootConfig, - tasks::{self, TaskQueue}, - templates::Templates, -}; #[derive(Clap, Debug, Default)] pub(super) struct ServerCommand; @@ -52,7 +52,7 @@ impl ServerCommand { let templates = Templates::load().context("could not load templates")?; // Start the server - let root = crate::handlers::root(&pool, &templates, &config); + let root = mas_core::handlers::root(&pool, &templates, &config); let queue = TaskQueue::default(); queue.recuring(Duration::from_secs(15), tasks::cleanup_expired(&pool)); diff --git a/crates/config/Cargo.toml b/crates/config/Cargo.toml new file mode 100644 index 00000000..3991e972 --- /dev/null +++ b/crates/config/Cargo.toml @@ -0,0 +1,38 @@ +[package] +name = "mas-config" +version = "0.1.0" +authors = ["Quentin Gliech "] +edition = "2018" +license = "Apache-2.0" + +[dependencies] +tokio = { version = "1.11.0", features = [] } +tracing = "0.1.27" +async-trait = "0.1.51" + +thiserror = "1.0.29" +anyhow = "1.0.44" + +schemars = { version = "0.8.3", features = ["url", "chrono"] } +figment = { version = "0.10.6", features = ["env", "yaml", "test"] } +chrono = { version = "0.4.19", features = ["serde"] } +url = { version = "2.2.2", features = ["serde"] } + +serde = { version = "1.0.130", features = ["derive"] } +serde_with = { version = "1.10.0", features = ["hex", "chrono"] } +serde_json = "1.0.68" +sqlx = { version = "0.5.7", features = ["runtime-tokio-rustls", "postgres"] } + +rand = "0.8.4" +rsa = "0.5.0" +k256 = "0.9.6" +pkcs8 = { version = "0.7.6", features = ["pem"] } +elliptic-curve = { version = "0.10.6", features = ["pem"] } + +indoc = "1.0.3" + +[dependencies.jwt-compact] +# Waiting on the next release because of the bump of the `rsa` dependency +git = "https://github.com/slowli/jwt-compact.git" +rev = "7a6dee6824c1d4e7c7f81019c9a968e5c9e44923" +features = ["rsa", "k256"] diff --git a/matrix-authentication-service/src/config/cookies.rs b/crates/config/src/cookies.rs similarity index 95% rename from matrix-authentication-service/src/config/cookies.rs rename to crates/config/src/cookies.rs index a91d3895..4273b3e4 100644 --- a/matrix-authentication-service/src/config/cookies.rs +++ b/crates/config/src/cookies.rs @@ -42,4 +42,8 @@ impl ConfigurationSection<'_> for CookiesConfig { secret: rand::random(), }) } + + fn test() -> Self { + Self { secret: [0xEA; 32] } + } } diff --git a/matrix-authentication-service/src/config/csrf.rs b/crates/config/src/csrf.rs similarity index 97% rename from matrix-authentication-service/src/config/csrf.rs rename to crates/config/src/csrf.rs index f1ba6fa6..d38ce234 100644 --- a/matrix-authentication-service/src/config/csrf.rs +++ b/crates/config/src/csrf.rs @@ -52,6 +52,10 @@ impl ConfigurationSection<'_> for CsrfConfig { async fn generate() -> anyhow::Result { Ok(Self::default()) } + + fn test() -> Self { + Self::default() + } } #[cfg(test)] diff --git a/matrix-authentication-service/src/config/database.rs b/crates/config/src/database.rs similarity index 91% rename from matrix-authentication-service/src/config/database.rs rename to crates/config/src/database.rs index 1c51f535..f517a658 100644 --- a/matrix-authentication-service/src/config/database.rs +++ b/crates/config/src/database.rs @@ -19,12 +19,11 @@ use async_trait::async_trait; use schemars::{gen::SchemaGenerator, schema::Schema, JsonSchema}; use serde::{Deserialize, Serialize}; use serde_with::{serde_as, skip_serializing_none}; -use sqlx::{ - postgres::{PgConnectOptions, PgPool, PgPoolOptions}, - ConnectOptions, -}; -use tracing::log::LevelFilter; +use sqlx::postgres::{PgConnectOptions, PgPool, PgPoolOptions}; +// FIXME +// use sqlx::ConnectOptions +// use tracing::log::LevelFilter; use super::ConfigurationSection; fn default_uri() -> String { @@ -102,15 +101,16 @@ pub struct DatabaseConfig { impl DatabaseConfig { #[tracing::instrument(err)] pub async fn connect(&self) -> anyhow::Result { - let mut options = self + let options = self .uri .parse::() .context("invalid database URL")? .application_name("matrix-authentication-service"); - options - .log_statements(LevelFilter::Debug) - .log_slow_statements(LevelFilter::Warn, Duration::from_millis(100)); + // FIXME + // options + // .log_statements(LevelFilter::Debug) + // .log_slow_statements(LevelFilter::Warn, Duration::from_millis(100)); PgPoolOptions::new() .max_connections(self.max_connections) @@ -133,6 +133,10 @@ impl ConfigurationSection<'_> for DatabaseConfig { async fn generate() -> anyhow::Result { Ok(Self::default()) } + + fn test() -> Self { + Self::default() + } } #[cfg(test)] diff --git a/matrix-authentication-service/src/config/http.rs b/crates/config/src/http.rs similarity index 95% rename from matrix-authentication-service/src/config/http.rs rename to crates/config/src/http.rs index 90fd3d56..fe8a4ec9 100644 --- a/matrix-authentication-service/src/config/http.rs +++ b/crates/config/src/http.rs @@ -45,4 +45,8 @@ impl ConfigurationSection<'_> for HttpConfig { async fn generate() -> anyhow::Result { Ok(Self::default()) } + + fn test() -> Self { + Self::default() + } } diff --git a/matrix-authentication-service/src/config/mod.rs b/crates/config/src/lib.rs similarity index 86% rename from matrix-authentication-service/src/config/mod.rs rename to crates/config/src/lib.rs index d04ee3ac..c06de274 100644 --- a/matrix-authentication-service/src/config/mod.rs +++ b/crates/config/src/lib.rs @@ -63,4 +63,14 @@ impl ConfigurationSection<'_> for RootConfig { csrf: CsrfConfig::generate().await?, }) } + + fn test() -> Self { + Self { + oauth2: OAuth2Config::test(), + http: HttpConfig::test(), + database: DatabaseConfig::test(), + cookies: CookiesConfig::test(), + csrf: CsrfConfig::test(), + } + } } diff --git a/matrix-authentication-service/src/config/oauth2.rs b/crates/config/src/oauth2.rs similarity index 98% rename from matrix-authentication-service/src/config/oauth2.rs rename to crates/config/src/oauth2.rs index ea49fa66..bc08b39e 100644 --- a/matrix-authentication-service/src/config/oauth2.rs +++ b/crates/config/src/oauth2.rs @@ -341,9 +341,49 @@ impl OAuth2Config { .join(".well-known/openid-configuration") .expect("could not build discovery url") } +} - #[cfg(test)] - pub fn test() -> Self { +#[async_trait] +impl ConfigurationSection<'_> for OAuth2Config { + fn path() -> &'static str { + "oauth2" + } + + #[tracing::instrument] + async fn generate() -> anyhow::Result { + info!("Generating keys..."); + + let span = tracing::info_span!("rsa"); + let rsa_key = task::spawn_blocking(move || { + let _entered = span.enter(); + let mut rng = rand::thread_rng(); + let ret = + RsaPrivateKey::new(&mut rng, 2048).context("could not generate RSA private key"); + info!("Done generating RSA key"); + ret + }) + .await + .context("could not join blocking task")??; + + let span = tracing::info_span!("ecdsa"); + let ecdsa_key = task::spawn_blocking(move || { + let _entered = span.enter(); + let rng = rand::thread_rng(); + let ret = k256::SecretKey::random(rng); + info!("Done generating ECDSA key"); + ret + }) + .await + .context("could not join blocking task")?; + + Ok(Self { + issuer: default_oauth2_issuer(), + clients: Vec::new(), + keys: KeySet(vec![Key::from_rsa(rsa_key), Key::from_ecdsa(ecdsa_key)]), + }) + } + + fn test() -> Self { let rsa_key = Key::from_rsa_pem(indoc::indoc! {r#" -----BEGIN PRIVATE KEY----- MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEAymS2RkeIZo7pUeEN @@ -374,47 +414,6 @@ impl OAuth2Config { } } -#[async_trait] -impl ConfigurationSection<'_> for OAuth2Config { - fn path() -> &'static str { - "oauth2" - } - - #[tracing::instrument] - async fn generate() -> anyhow::Result { - info!("Generating keys..."); - - let span = tracing::info_span!("rsa"); - let rsa_key = task::spawn_blocking(move || { - let _entered = span.enter(); - let mut rng = rand::thread_rng(); - let ret = - RsaPrivateKey::new(&mut rng, 2048).context("could not generate RSA private key"); - info!("Done generating RSA key"); - ret - }); - - let span = tracing::info_span!("ecdsa"); - let ecdsa_key = task::spawn_blocking(move || { - let _entered = span.enter(); - let rng = rand::thread_rng(); - let ret = k256::SecretKey::random(rng); - info!("Done generating ECDSA key"); - ret - }); - - let (ecdsa_key, rsa_key) = tokio::join!(ecdsa_key, rsa_key); - let rsa_key = rsa_key.context("could not join blocking task")??; - let ecdsa_key = ecdsa_key.context("could not join blocking task")?; - - Ok(Self { - issuer: default_oauth2_issuer(), - clients: Vec::new(), - keys: KeySet(vec![Key::from_rsa(rsa_key), Key::from_ecdsa(ecdsa_key)]), - }) - } -} - #[cfg(test)] mod tests { use figment::Jail; diff --git a/matrix-authentication-service/src/config/util.rs b/crates/config/src/util.rs similarity index 97% rename from matrix-authentication-service/src/config/util.rs rename to crates/config/src/util.rs index b45d5b0c..b9fc9108 100644 --- a/matrix-authentication-service/src/config/util.rs +++ b/crates/config/src/util.rs @@ -66,4 +66,7 @@ pub trait ConfigurationSection<'a>: Sized + Deserialize<'a> + Serialize { .merge(Yaml::file(path)) .extract_inner(Self::path()) } + + /// Generate config used in unit tests + fn test() -> Self; } diff --git a/matrix-authentication-service/Cargo.toml b/crates/core/Cargo.toml similarity index 90% rename from matrix-authentication-service/Cargo.toml rename to crates/core/Cargo.toml index 28501ec2..e463d04c 100644 --- a/matrix-authentication-service/Cargo.toml +++ b/crates/core/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "matrix-authentication-service" +name = "mas-core" version = "0.1.0" authors = ["Quentin Gliech "] edition = "2018" @@ -14,7 +14,6 @@ futures-util = "0.3.17" # Logging and tracing tracing = "0.1.27" -tracing-subscriber = "0.2.22" # Error management thiserror = "1.0.29" @@ -22,8 +21,6 @@ anyhow = "1.0.44" # Web server warp = "0.3.1" -tower = { version = "0.4.8", features = ["full"] } -tower-http = { version = "0.1.1", features = ["full"] } hyper = { version = "0.14.12", features = ["full"] } # Template engine @@ -40,10 +37,8 @@ serde_json = "1.0.68" serde_urlencoded = "0.7.0" # Argument & config parsing -clap = "3.0.0-beta.4" figment = { version = "0.10.6", features = ["env", "yaml", "test"] } schemars = { version = "0.8.3", features = ["url", "chrono"] } -dotenv = "0.15.0" # Password hashing argon2 = { version = "0.3.1", features = ["password-hash"] } @@ -70,6 +65,7 @@ headers = "0.3.4" cookie = "0.15.1" oauth2-types = { path = "../oauth2-types", features = ["sqlx_type"] } +mas-config = { path = "../config" } [dependencies.jwt-compact] # Waiting on the next release because of the bump of the `rsa` dependency diff --git a/matrix-authentication-service/migrations/20210709203828_timestamp_trigger.down.sql b/crates/core/migrations/20210709203828_timestamp_trigger.down.sql similarity index 100% rename from matrix-authentication-service/migrations/20210709203828_timestamp_trigger.down.sql rename to crates/core/migrations/20210709203828_timestamp_trigger.down.sql diff --git a/matrix-authentication-service/migrations/20210709203828_timestamp_trigger.up.sql b/crates/core/migrations/20210709203828_timestamp_trigger.up.sql similarity index 100% rename from matrix-authentication-service/migrations/20210709203828_timestamp_trigger.up.sql rename to crates/core/migrations/20210709203828_timestamp_trigger.up.sql diff --git a/matrix-authentication-service/migrations/20210716213724_users.down.sql b/crates/core/migrations/20210716213724_users.down.sql similarity index 100% rename from matrix-authentication-service/migrations/20210716213724_users.down.sql rename to crates/core/migrations/20210716213724_users.down.sql diff --git a/matrix-authentication-service/migrations/20210716213724_users.up.sql b/crates/core/migrations/20210716213724_users.up.sql similarity index 100% rename from matrix-authentication-service/migrations/20210716213724_users.up.sql rename to crates/core/migrations/20210716213724_users.up.sql diff --git a/matrix-authentication-service/migrations/20210722072901_user_sessions.down.sql b/crates/core/migrations/20210722072901_user_sessions.down.sql similarity index 100% rename from matrix-authentication-service/migrations/20210722072901_user_sessions.down.sql rename to crates/core/migrations/20210722072901_user_sessions.down.sql diff --git a/matrix-authentication-service/migrations/20210722072901_user_sessions.up.sql b/crates/core/migrations/20210722072901_user_sessions.up.sql similarity index 100% rename from matrix-authentication-service/migrations/20210722072901_user_sessions.up.sql rename to crates/core/migrations/20210722072901_user_sessions.up.sql diff --git a/matrix-authentication-service/migrations/20210731130515_oauth2_sessions.down.sql b/crates/core/migrations/20210731130515_oauth2_sessions.down.sql similarity index 100% rename from matrix-authentication-service/migrations/20210731130515_oauth2_sessions.down.sql rename to crates/core/migrations/20210731130515_oauth2_sessions.down.sql diff --git a/matrix-authentication-service/migrations/20210731130515_oauth2_sessions.up.sql b/crates/core/migrations/20210731130515_oauth2_sessions.up.sql similarity index 100% rename from matrix-authentication-service/migrations/20210731130515_oauth2_sessions.up.sql rename to crates/core/migrations/20210731130515_oauth2_sessions.up.sql diff --git a/matrix-authentication-service/migrations/20210813080413_oauth2_access_tokens.down.sql b/crates/core/migrations/20210813080413_oauth2_access_tokens.down.sql similarity index 100% rename from matrix-authentication-service/migrations/20210813080413_oauth2_access_tokens.down.sql rename to crates/core/migrations/20210813080413_oauth2_access_tokens.down.sql diff --git a/matrix-authentication-service/migrations/20210813080413_oauth2_access_tokens.up.sql b/crates/core/migrations/20210813080413_oauth2_access_tokens.up.sql similarity index 100% rename from matrix-authentication-service/migrations/20210813080413_oauth2_access_tokens.up.sql rename to crates/core/migrations/20210813080413_oauth2_access_tokens.up.sql diff --git a/matrix-authentication-service/migrations/20210827112032_oauth2_refresh_tokens.down.sql b/crates/core/migrations/20210827112032_oauth2_refresh_tokens.down.sql similarity index 100% rename from matrix-authentication-service/migrations/20210827112032_oauth2_refresh_tokens.down.sql rename to crates/core/migrations/20210827112032_oauth2_refresh_tokens.down.sql diff --git a/matrix-authentication-service/migrations/20210827112032_oauth2_refresh_tokens.up.sql b/crates/core/migrations/20210827112032_oauth2_refresh_tokens.up.sql similarity index 100% rename from matrix-authentication-service/migrations/20210827112032_oauth2_refresh_tokens.up.sql rename to crates/core/migrations/20210827112032_oauth2_refresh_tokens.up.sql diff --git a/matrix-authentication-service/sqlx-data.json b/crates/core/sqlx-data.json similarity index 100% rename from matrix-authentication-service/sqlx-data.json rename to crates/core/sqlx-data.json diff --git a/matrix-authentication-service/src/errors.rs b/crates/core/src/errors.rs similarity index 98% rename from matrix-authentication-service/src/errors.rs rename to crates/core/src/errors.rs index a0df2911..007a029e 100644 --- a/matrix-authentication-service/src/errors.rs +++ b/crates/core/src/errors.rs @@ -93,7 +93,7 @@ pub struct ErroredForm { } impl ErroredForm { - pub fn new() -> Self { + #[must_use] pub fn new() -> Self { Self { form: Vec::new(), fields: Vec::new(), diff --git a/matrix-authentication-service/src/filters/authenticate.rs b/crates/core/src/filters/authenticate.rs similarity index 100% rename from matrix-authentication-service/src/filters/authenticate.rs rename to crates/core/src/filters/authenticate.rs diff --git a/matrix-authentication-service/src/filters/client.rs b/crates/core/src/filters/client.rs similarity index 98% rename from matrix-authentication-service/src/filters/client.rs rename to crates/core/src/filters/client.rs index 21c0666c..b5751200 100644 --- a/matrix-authentication-service/src/filters/client.rs +++ b/crates/core/src/filters/client.rs @@ -28,11 +28,13 @@ pub enum ClientAuthentication { } impl ClientAuthentication { + #[must_use] pub fn public(&self) -> bool { matches!(self, &Self::None) } } +#[must_use] pub fn with_client_auth( oauth2_config: &OAuth2Config, ) -> impl Filter @@ -132,6 +134,8 @@ struct ClientAuthForm { #[cfg(test)] mod tests { + use mas_config::ConfigurationSection; + use super::*; fn oauth2_config() -> OAuth2Config { diff --git a/matrix-authentication-service/src/filters/cookies.rs b/crates/core/src/filters/cookies.rs similarity index 97% rename from matrix-authentication-service/src/filters/cookies.rs rename to crates/core/src/filters/cookies.rs index c43d8680..c877fc95 100644 --- a/matrix-authentication-service/src/filters/cookies.rs +++ b/crates/core/src/filters/cookies.rs @@ -69,7 +69,7 @@ impl EncryptedCookie { } } -pub fn maybe_encrypted( +#[must_use] pub fn maybe_encrypted( options: &CookiesConfig, ) -> impl Filter,), Error = Infallible> + Clone + Send + Sync + 'static where @@ -83,7 +83,7 @@ where }) } -pub fn encrypted( +#[must_use] pub fn encrypted( options: &CookiesConfig, ) -> impl Filter + Clone + Send + Sync + 'static where @@ -97,7 +97,7 @@ where }) } -pub fn with_cookie_saver( +#[must_use] pub fn with_cookie_saver( options: &CookiesConfig, ) -> impl Filter + Clone + Send + Sync + 'static { diff --git a/matrix-authentication-service/src/filters/csrf.rs b/crates/core/src/filters/csrf.rs similarity index 96% rename from matrix-authentication-service/src/filters/csrf.rs rename to crates/core/src/filters/csrf.rs index 48ee5d6d..6d2b8153 100644 --- a/matrix-authentication-service/src/filters/csrf.rs +++ b/crates/core/src/filters/csrf.rs @@ -66,7 +66,7 @@ impl CsrfToken { } /// Get the value to include in HTML forms - pub fn form_value(&self) -> String { + #[must_use] pub fn form_value(&self) -> String { BASE64URL_NOPAD.encode(&self.token[..]) } @@ -112,7 +112,7 @@ impl CsrfForm { } } -pub fn csrf_token( +#[must_use] pub fn csrf_token( cookies_config: &CookiesConfig, ) -> impl Filter + Clone + Send + Sync + 'static { super::cookies::encrypted(cookies_config).and_then(move |token: CsrfToken| async move { @@ -121,7 +121,7 @@ pub fn csrf_token( }) } -pub fn updated_csrf_token( +#[must_use] pub fn updated_csrf_token( cookies_config: &CookiesConfig, csrf_config: &CsrfConfig, ) -> impl Filter + Clone + Send + Sync + 'static { @@ -144,7 +144,7 @@ pub fn updated_csrf_token( ) } -pub fn protected_form( +#[must_use] pub fn protected_form( cookies_config: &CookiesConfig, ) -> impl Filter + Clone + Send + Sync + 'static where diff --git a/matrix-authentication-service/src/filters/database.rs b/crates/core/src/filters/database.rs similarity index 100% rename from matrix-authentication-service/src/filters/database.rs rename to crates/core/src/filters/database.rs diff --git a/matrix-authentication-service/src/filters/errors.rs b/crates/core/src/filters/errors.rs similarity index 100% rename from matrix-authentication-service/src/filters/errors.rs rename to crates/core/src/filters/errors.rs diff --git a/matrix-authentication-service/src/filters/headers.rs b/crates/core/src/filters/headers.rs similarity index 100% rename from matrix-authentication-service/src/filters/headers.rs rename to crates/core/src/filters/headers.rs diff --git a/matrix-authentication-service/src/filters/mod.rs b/crates/core/src/filters/mod.rs similarity index 95% rename from matrix-authentication-service/src/filters/mod.rs rename to crates/core/src/filters/mod.rs index 8099c0a5..ac51d00a 100644 --- a/matrix-authentication-service/src/filters/mod.rs +++ b/crates/core/src/filters/mod.rs @@ -33,14 +33,14 @@ use crate::{ templates::Templates, }; -pub fn with_templates( +#[must_use] pub fn with_templates( templates: &Templates, ) -> impl Filter + Clone + Send + Sync + 'static { let templates = templates.clone(); warp::any().map(move || templates.clone()) } -pub fn with_keys( +#[must_use] pub fn with_keys( oauth2_config: &OAuth2Config, ) -> impl Filter + Clone + Send + Sync + 'static { let keyset = oauth2_config.keys.clone(); diff --git a/matrix-authentication-service/src/filters/session.rs b/crates/core/src/filters/session.rs similarity index 94% rename from matrix-authentication-service/src/filters/session.rs rename to crates/core/src/filters/session.rs index dd2fbcb0..9f58425a 100644 --- a/matrix-authentication-service/src/filters/session.rs +++ b/crates/core/src/filters/session.rs @@ -32,7 +32,7 @@ pub struct SessionCookie { } impl SessionCookie { - pub fn from_session_info(info: &SessionInfo) -> Self { + #[must_use] pub fn from_session_info(info: &SessionInfo) -> Self { Self { current: info.key(), } @@ -52,7 +52,7 @@ impl EncryptableCookieValue for SessionCookie { } } -pub fn with_optional_session( +#[must_use] pub fn with_optional_session( pool: &PgPool, cookies_config: &CookiesConfig, ) -> impl Filter,), Error = Rejection> + Clone + Send + Sync + 'static @@ -71,7 +71,7 @@ pub fn with_optional_session( ) } -pub fn with_session( +#[must_use] pub fn with_session( pool: &PgPool, cookies_config: &CookiesConfig, ) -> impl Filter + Clone + Send + Sync + 'static { diff --git a/matrix-authentication-service/src/handlers/health.rs b/crates/core/src/handlers/health.rs similarity index 100% rename from matrix-authentication-service/src/handlers/health.rs rename to crates/core/src/handlers/health.rs diff --git a/matrix-authentication-service/src/handlers/mod.rs b/crates/core/src/handlers/mod.rs similarity index 98% rename from matrix-authentication-service/src/handlers/mod.rs rename to crates/core/src/handlers/mod.rs index 07bbcec3..edbb0bb5 100644 --- a/matrix-authentication-service/src/handlers/mod.rs +++ b/crates/core/src/handlers/mod.rs @@ -25,7 +25,7 @@ mod views; use self::{health::filter as health, oauth2::filter as oauth2, views::filter as views}; -pub fn root( +#[must_use] pub fn root( pool: &PgPool, templates: &Templates, config: &RootConfig, diff --git a/matrix-authentication-service/src/handlers/oauth2/authorization.rs b/crates/core/src/handlers/oauth2/authorization.rs similarity index 100% rename from matrix-authentication-service/src/handlers/oauth2/authorization.rs rename to crates/core/src/handlers/oauth2/authorization.rs diff --git a/matrix-authentication-service/src/handlers/oauth2/discovery.rs b/crates/core/src/handlers/oauth2/discovery.rs similarity index 100% rename from matrix-authentication-service/src/handlers/oauth2/discovery.rs rename to crates/core/src/handlers/oauth2/discovery.rs diff --git a/matrix-authentication-service/src/handlers/oauth2/introspection.rs b/crates/core/src/handlers/oauth2/introspection.rs similarity index 100% rename from matrix-authentication-service/src/handlers/oauth2/introspection.rs rename to crates/core/src/handlers/oauth2/introspection.rs diff --git a/matrix-authentication-service/src/handlers/oauth2/keys.rs b/crates/core/src/handlers/oauth2/keys.rs similarity index 100% rename from matrix-authentication-service/src/handlers/oauth2/keys.rs rename to crates/core/src/handlers/oauth2/keys.rs diff --git a/matrix-authentication-service/src/handlers/oauth2/mod.rs b/crates/core/src/handlers/oauth2/mod.rs similarity index 100% rename from matrix-authentication-service/src/handlers/oauth2/mod.rs rename to crates/core/src/handlers/oauth2/mod.rs diff --git a/matrix-authentication-service/src/handlers/oauth2/token.rs b/crates/core/src/handlers/oauth2/token.rs similarity index 100% rename from matrix-authentication-service/src/handlers/oauth2/token.rs rename to crates/core/src/handlers/oauth2/token.rs diff --git a/matrix-authentication-service/src/handlers/oauth2/userinfo.rs b/crates/core/src/handlers/oauth2/userinfo.rs similarity index 100% rename from matrix-authentication-service/src/handlers/oauth2/userinfo.rs rename to crates/core/src/handlers/oauth2/userinfo.rs diff --git a/matrix-authentication-service/src/handlers/views/index.rs b/crates/core/src/handlers/views/index.rs similarity index 100% rename from matrix-authentication-service/src/handlers/views/index.rs rename to crates/core/src/handlers/views/index.rs diff --git a/matrix-authentication-service/src/handlers/views/login.rs b/crates/core/src/handlers/views/login.rs similarity index 100% rename from matrix-authentication-service/src/handlers/views/login.rs rename to crates/core/src/handlers/views/login.rs diff --git a/matrix-authentication-service/src/handlers/views/logout.rs b/crates/core/src/handlers/views/logout.rs similarity index 100% rename from matrix-authentication-service/src/handlers/views/logout.rs rename to crates/core/src/handlers/views/logout.rs diff --git a/matrix-authentication-service/src/handlers/views/mod.rs b/crates/core/src/handlers/views/mod.rs similarity index 100% rename from matrix-authentication-service/src/handlers/views/mod.rs rename to crates/core/src/handlers/views/mod.rs diff --git a/matrix-authentication-service/src/handlers/views/reauth.rs b/crates/core/src/handlers/views/reauth.rs similarity index 100% rename from matrix-authentication-service/src/handlers/views/reauth.rs rename to crates/core/src/handlers/views/reauth.rs diff --git a/matrix-authentication-service/src/handlers/views/register.rs b/crates/core/src/handlers/views/register.rs similarity index 100% rename from matrix-authentication-service/src/handlers/views/register.rs rename to crates/core/src/handlers/views/register.rs diff --git a/crates/core/src/lib.rs b/crates/core/src/lib.rs new file mode 100644 index 00000000..78fce9d7 --- /dev/null +++ b/crates/core/src/lib.rs @@ -0,0 +1,31 @@ +// Copyright 2021 The Matrix.org Foundation C.I.C. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#![forbid(unsafe_code)] +#![deny(clippy::all)] +#![warn(clippy::pedantic)] +#![allow(clippy::module_name_repetitions)] +#![allow(clippy::missing_panics_doc)] +#![allow(clippy::missing_errors_doc)] +#![allow(clippy::implicit_hasher)] + +pub(crate) use mas_config as config; + +pub mod errors; +pub mod filters; +pub mod handlers; +pub mod storage; +pub mod tasks; +pub mod templates; +pub mod tokens; diff --git a/matrix-authentication-service/src/storage/mod.rs b/crates/core/src/storage/mod.rs similarity index 100% rename from matrix-authentication-service/src/storage/mod.rs rename to crates/core/src/storage/mod.rs diff --git a/matrix-authentication-service/src/storage/oauth2/access_token.rs b/crates/core/src/storage/oauth2/access_token.rs similarity index 98% rename from matrix-authentication-service/src/storage/oauth2/access_token.rs rename to crates/core/src/storage/oauth2/access_token.rs index d6e7d3c9..29ed1137 100644 --- a/matrix-authentication-service/src/storage/oauth2/access_token.rs +++ b/crates/core/src/storage/oauth2/access_token.rs @@ -67,7 +67,7 @@ pub struct OAuth2AccessTokenLookup { } impl OAuth2AccessTokenLookup { - pub fn exp(&self) -> DateTime { + #[must_use] pub fn exp(&self) -> DateTime { self.created_at + Duration::seconds(i64::from(self.expires_after)) } } diff --git a/matrix-authentication-service/src/storage/oauth2/authorization_code.rs b/crates/core/src/storage/oauth2/authorization_code.rs similarity index 100% rename from matrix-authentication-service/src/storage/oauth2/authorization_code.rs rename to crates/core/src/storage/oauth2/authorization_code.rs diff --git a/matrix-authentication-service/src/storage/oauth2/mod.rs b/crates/core/src/storage/oauth2/mod.rs similarity index 100% rename from matrix-authentication-service/src/storage/oauth2/mod.rs rename to crates/core/src/storage/oauth2/mod.rs diff --git a/matrix-authentication-service/src/storage/oauth2/refresh_token.rs b/crates/core/src/storage/oauth2/refresh_token.rs similarity index 100% rename from matrix-authentication-service/src/storage/oauth2/refresh_token.rs rename to crates/core/src/storage/oauth2/refresh_token.rs diff --git a/matrix-authentication-service/src/storage/oauth2/session.rs b/crates/core/src/storage/oauth2/session.rs similarity index 98% rename from matrix-authentication-service/src/storage/oauth2/session.rs rename to crates/core/src/storage/oauth2/session.rs index 18b30bc0..eea672ea 100644 --- a/matrix-authentication-service/src/storage/oauth2/session.rs +++ b/crates/core/src/storage/oauth2/session.rs @@ -103,7 +103,7 @@ impl OAuth2Session { } } - pub fn max_auth_time(&self) -> Option> { + #[must_use] pub fn max_auth_time(&self) -> Option> { self.max_age .map(|d| Duration::seconds(i64::from(d))) .map(|d| self.created_at - d) diff --git a/matrix-authentication-service/src/storage/user.rs b/crates/core/src/storage/user.rs similarity index 99% rename from matrix-authentication-service/src/storage/user.rs rename to crates/core/src/storage/user.rs index 937235ac..b5c71d19 100644 --- a/matrix-authentication-service/src/storage/user.rs +++ b/crates/core/src/storage/user.rs @@ -44,7 +44,7 @@ pub struct SessionInfo { } impl SessionInfo { - pub fn key(&self) -> i64 { + #[must_use] pub fn key(&self) -> i64 { self.id } diff --git a/matrix-authentication-service/src/tasks/database.rs b/crates/core/src/tasks/database.rs similarity index 94% rename from matrix-authentication-service/src/tasks/database.rs rename to crates/core/src/tasks/database.rs index 3a0473d0..4d3751c2 100644 --- a/matrix-authentication-service/src/tasks/database.rs +++ b/crates/core/src/tasks/database.rs @@ -38,6 +38,6 @@ impl Task for CleanupExpired { } } -pub fn cleanup_expired(pool: &Pool) -> impl Task + Clone { +#[must_use] pub fn cleanup_expired(pool: &Pool) -> impl Task + Clone { CleanupExpired(pool.clone()) } diff --git a/matrix-authentication-service/src/tasks/mod.rs b/crates/core/src/tasks/mod.rs similarity index 100% rename from matrix-authentication-service/src/tasks/mod.rs rename to crates/core/src/tasks/mod.rs diff --git a/matrix-authentication-service/src/templates.rs b/crates/core/src/templates.rs similarity index 95% rename from matrix-authentication-service/src/templates.rs rename to crates/core/src/templates.rs index 754ad996..49b00178 100644 --- a/matrix-authentication-service/src/templates.rs +++ b/crates/core/src/templates.rs @@ -212,7 +212,7 @@ pub struct IndexContext { } impl IndexContext { - pub fn new(discovery_url: Url) -> Self { + #[must_use] pub fn new(discovery_url: Url) -> Self { Self { discovery_url } } } @@ -230,7 +230,7 @@ pub struct LoginContext { } impl LoginContext { - pub fn with_form_error(form: ErroredForm) -> Self { + #[must_use] pub fn with_form_error(form: ErroredForm) -> Self { Self { form } } } @@ -267,22 +267,22 @@ pub struct ErrorContext { } impl ErrorContext { - pub fn new() -> Self { + #[must_use] pub fn new() -> Self { Self::default() } - pub fn with_code(mut self, code: &'static str) -> Self { + #[must_use] pub fn with_code(mut self, code: &'static str) -> Self { self.code = Some(code); self } - pub fn with_description(mut self, description: String) -> Self { + #[must_use] pub fn with_description(mut self, description: String) -> Self { self.description = Some(description); self } #[allow(dead_code)] - pub fn with_details(mut self, details: String) -> Self { + #[must_use] pub fn with_details(mut self, details: String) -> Self { self.details = Some(details); self } diff --git a/matrix-authentication-service/src/tokens.rs b/crates/core/src/tokens.rs similarity index 100% rename from matrix-authentication-service/src/tokens.rs rename to crates/core/src/tokens.rs diff --git a/matrix-authentication-service/templates/base.html b/crates/core/templates/base.html similarity index 100% rename from matrix-authentication-service/templates/base.html rename to crates/core/templates/base.html diff --git a/matrix-authentication-service/templates/error.html b/crates/core/templates/error.html similarity index 100% rename from matrix-authentication-service/templates/error.html rename to crates/core/templates/error.html diff --git a/matrix-authentication-service/templates/error.txt b/crates/core/templates/error.txt similarity index 100% rename from matrix-authentication-service/templates/error.txt rename to crates/core/templates/error.txt diff --git a/matrix-authentication-service/templates/form_post.html b/crates/core/templates/form_post.html similarity index 100% rename from matrix-authentication-service/templates/form_post.html rename to crates/core/templates/form_post.html diff --git a/matrix-authentication-service/templates/index.html b/crates/core/templates/index.html similarity index 100% rename from matrix-authentication-service/templates/index.html rename to crates/core/templates/index.html diff --git a/matrix-authentication-service/templates/login.html b/crates/core/templates/login.html similarity index 100% rename from matrix-authentication-service/templates/login.html rename to crates/core/templates/login.html diff --git a/matrix-authentication-service/templates/reauth.html b/crates/core/templates/reauth.html similarity index 100% rename from matrix-authentication-service/templates/reauth.html rename to crates/core/templates/reauth.html diff --git a/matrix-authentication-service/templates/register.html b/crates/core/templates/register.html similarity index 100% rename from matrix-authentication-service/templates/register.html rename to crates/core/templates/register.html diff --git a/oauth2-types/Cargo.toml b/crates/oauth2-types/Cargo.toml similarity index 100% rename from oauth2-types/Cargo.toml rename to crates/oauth2-types/Cargo.toml diff --git a/oauth2-types/src/errors.rs b/crates/oauth2-types/src/errors.rs similarity index 100% rename from oauth2-types/src/errors.rs rename to crates/oauth2-types/src/errors.rs diff --git a/oauth2-types/src/lib.rs b/crates/oauth2-types/src/lib.rs similarity index 100% rename from oauth2-types/src/lib.rs rename to crates/oauth2-types/src/lib.rs diff --git a/oauth2-types/src/oidc.rs b/crates/oauth2-types/src/oidc.rs similarity index 100% rename from oauth2-types/src/oidc.rs rename to crates/oauth2-types/src/oidc.rs diff --git a/oauth2-types/src/pkce.rs b/crates/oauth2-types/src/pkce.rs similarity index 100% rename from oauth2-types/src/pkce.rs rename to crates/oauth2-types/src/pkce.rs diff --git a/oauth2-types/src/requests.rs b/crates/oauth2-types/src/requests.rs similarity index 100% rename from oauth2-types/src/requests.rs rename to crates/oauth2-types/src/requests.rs diff --git a/oauth2-types/src/test_utils.rs b/crates/oauth2-types/src/test_utils.rs similarity index 100% rename from oauth2-types/src/test_utils.rs rename to crates/oauth2-types/src/test_utils.rs diff --git a/matrix-authentication-service/src/main.rs b/matrix-authentication-service/src/main.rs deleted file mode 100644 index 2931f19c..00000000 --- a/matrix-authentication-service/src/main.rs +++ /dev/null @@ -1,60 +0,0 @@ -// Copyright 2021 The Matrix.org Foundation C.I.C. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#![forbid(unsafe_code)] -#![deny(clippy::all)] -#![warn(clippy::pedantic)] -#![allow(clippy::module_name_repetitions)] - -use anyhow::Context; -use clap::Clap; -use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Registry}; - -mod cli; -mod config; -mod errors; -mod filters; -mod handlers; -mod storage; -mod tasks; -mod templates; -mod tokens; - -use self::cli::RootCommand; - -#[tokio::main] -async fn main() -> anyhow::Result<()> { - // Load environment variables from .env files - if let Err(e) = dotenv::dotenv() { - // Display the error if it is something other than the .env file not existing - if !e.not_found() { - return Err(e).context("could not load .env file"); - } - } - - // Setup logging & tracing - let fmt_layer = tracing_subscriber::fmt::layer().with_writer(std::io::stderr); - let filter_layer = EnvFilter::try_from_default_env().or_else(|_| EnvFilter::try_new("info"))?; - - let subscriber = Registry::default().with(filter_layer).with(fmt_layer); - subscriber - .try_init() - .context("could not initialize logging")?; - - // Parse the CLI arguments - let opts = RootCommand::parse(); - - // And run the command - opts.run().await -}