Skip to content

Commit

Permalink
[Feature] Add support for shareable user resources.
Browse files Browse the repository at this point in the history
  • Loading branch information
azasypkin committed Oct 2, 2023
1 parent 53e4b24 commit fa013b1
Show file tree
Hide file tree
Showing 31 changed files with 2,406 additions and 137 deletions.

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

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

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

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

8 changes: 8 additions & 0 deletions migrations/20230927212931_user_shares.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- Table to store user public shares (content security policies, certificate templates etc.).
CREATE TABLE IF NOT EXISTS user_shares
(
id TEXT PRIMARY KEY NOT NULL COLLATE NOCASE,
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
resource BLOB NOT NULL,
created_at INTEGER NOT NULL
) STRICT;
22 changes: 16 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ mod tests {
users::{User, UserId},
utils::{WebPageResource, WebPageResourceContent, WebPageResourceContentData},
};
use anyhow::anyhow;
use cron::Schedule;
use lettre::transport::stub::AsyncStubTransport;
use std::{
Expand All @@ -252,7 +253,7 @@ mod tests {
use trust_dns_resolver::proto::rr::Record;
use url::Url;

pub use crate::{network::tests::*, utils::tests::*};
pub use crate::{network::tests::*, server::tests::*, utils::tests::*};
use crate::{search::SearchIndex, templates::create_templates};

pub struct MockUserBuilder {
Expand Down Expand Up @@ -386,11 +387,20 @@ mod tests {
Database::open(|| Ok("sqlite::memory:".to_string())).await
}

pub fn mock_search_index() -> anyhow::Result<SearchIndex> {
SearchIndex::open(|schema| Ok(Index::create_in_ram(schema)))
}

pub fn mock_user() -> anyhow::Result<User> {
mock_user_with_id(1)
}

pub fn mock_user_with_id<I: TryInto<UserId>>(id: I) -> anyhow::Result<User> {
let id = id.try_into().map_err(|_| anyhow!("err"))?;
Ok(MockUserBuilder::new(
1.try_into()?,
"dev@secutils.dev",
"dev-handle",
id,
&format!("dev-{}@secutils.dev", *id),
&format!("dev-handle-{}", *id),
StoredCredentials {
password_hash: Some("hash".to_string()),
..Default::default()
Expand Down Expand Up @@ -446,7 +456,7 @@ mod tests {
Ok(Api::new(
config,
mock_db().await?,
SearchIndex::open(|schema| Ok(Index::create_in_ram(schema)))?,
mock_search_index()?,
mock_network(),
create_templates()?,
))
Expand All @@ -458,7 +468,7 @@ mod tests {
Ok(Api::new(
mock_config()?,
mock_db().await?,
SearchIndex::open(|schema| Ok(Index::create_in_ram(schema)))?,
mock_search_index()?,
network,
create_templates()?,
))
Expand Down
6 changes: 3 additions & 3 deletions src/notifications/api_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,8 @@ mod tests {
Envelope {
forward_path: [
Address {
serialized: "dev@secutils.dev",
at_start: 3,
serialized: "dev-1@secutils.dev",
at_start: 5,
},
],
reverse_path: Some(
Expand All @@ -352,7 +352,7 @@ mod tests {
},
),
},
"From: dev@secutils.dev\r\nReply-To: dev@secutils.dev\r\nTo: dev@secutils.dev\r\nSubject: [NO SUBJECT]\r\nDate: Sat, 01 Jan 2000 09:58:20 +0000\r\nContent-Transfer-Encoding: 7bit\r\n\r\nabc",
"From: dev@secutils.dev\r\nReply-To: dev@secutils.dev\r\nTo: dev-1@secutils.dev\r\nSubject: [NO SUBJECT]\r\nDate: Sat, 01 Jan 2000 09:58:20 +0000\r\nContent-Transfer-Encoding: 7bit\r\n\r\nabc",
),
(
Envelope {
Expand Down
4 changes: 2 additions & 2 deletions src/notifications/notification_content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@ mod tests {
.await?, @r###"
EmailNotificationContent {
subject: "Activate you Secutils.dev account",
text: "To activate your Secutils.dev account, please click the following link: http://localhost:1234/activate?code=some-code&email=dev%40secutils.dev",
text: "To activate your Secutils.dev account, please click the following link: http://localhost:1234/activate?code=some-code&email=dev-1%40secutils.dev",
html: Some(
"<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n <title>Activate your Secutils.dev account</title>\n <meta charset=\"utf-8\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n <style>\n body {\n font-family: Arial, sans-serif;\n background-color: #f1f1f1;\n margin: 0;\n padding: 0;\n }\n .container {\n max-width: 600px;\n margin: 0 auto;\n background-color: #fff;\n padding: 20px;\n border-radius: 5px;\n box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);\n }\n h1 {\n font-size: 24px;\n margin-top: 0;\n }\n p {\n font-size: 16px;\n line-height: 1.5;\n margin-bottom: 20px;\n }\n .button-link {\n color: #fff;\n background-color: #2196F3;\n padding: 10px 20px;\n text-decoration: none;\n border-radius: 5px;\n }\n </style>\n</head>\n<body>\n<div class=\"container\">\n <h1>Activate your Secutils.dev account</h1>\n <p>Thanks for signing up! To activate your account, please click the link below:</p>\n <a class=\"button-link\" href=\"http://localhost:1234/activate?code=some-code&email=dev%40secutils.dev\">Activate my account</a>\n <p>If the button above doesn't work, you can also copy and paste the following URL into your browser:</p>\n <p>http://localhost:1234/activate?code=some-code&email=dev%40secutils.dev</p>\n <p>If you have any trouble activating your account, please contact us at <a href=\"mailto: contact@secutils.dev\">contact@secutils.dev</a>.</p>\n</div>\n</body>\n</html>\n",
"<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n <title>Activate your Secutils.dev account</title>\n <meta charset=\"utf-8\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n <style>\n body {\n font-family: Arial, sans-serif;\n background-color: #f1f1f1;\n margin: 0;\n padding: 0;\n }\n .container {\n max-width: 600px;\n margin: 0 auto;\n background-color: #fff;\n padding: 20px;\n border-radius: 5px;\n box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);\n }\n h1 {\n font-size: 24px;\n margin-top: 0;\n }\n p {\n font-size: 16px;\n line-height: 1.5;\n margin-bottom: 20px;\n }\n .button-link {\n color: #fff;\n background-color: #2196F3;\n padding: 10px 20px;\n text-decoration: none;\n border-radius: 5px;\n }\n </style>\n</head>\n<body>\n<div class=\"container\">\n <h1>Activate your Secutils.dev account</h1>\n <p>Thanks for signing up! To activate your account, please click the link below:</p>\n <a class=\"button-link\" href=\"http://localhost:1234/activate?code=some-code&email=dev-1%40secutils.dev\">Activate my account</a>\n <p>If the button above doesn't work, you can also copy and paste the following URL into your browser:</p>\n <p>http://localhost:1234/activate?code=some-code&email=dev-1%40secutils.dev</p>\n <p>If you have any trouble activating your account, please contact us at <a href=\"mailto: contact@secutils.dev\">contact@secutils.dev</a>.</p>\n</div>\n</body>\n</html>\n",
),
attachments: None,
}
Expand Down
8 changes: 4 additions & 4 deletions src/notifications/notification_content_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ mod tests {
.await?, @r###"
EmailNotificationContent {
subject: "Activate you Secutils.dev account",
text: "To activate your Secutils.dev account, please click the following link: http://localhost:1234/activate?code=some-code&email=dev%40secutils.dev",
text: "To activate your Secutils.dev account, please click the following link: http://localhost:1234/activate?code=some-code&email=dev-1%40secutils.dev",
html: Some(
"<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n <title>Activate your Secutils.dev account</title>\n <meta charset=\"utf-8\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n <style>\n body {\n font-family: Arial, sans-serif;\n background-color: #f1f1f1;\n margin: 0;\n padding: 0;\n }\n .container {\n max-width: 600px;\n margin: 0 auto;\n background-color: #fff;\n padding: 20px;\n border-radius: 5px;\n box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);\n }\n h1 {\n font-size: 24px;\n margin-top: 0;\n }\n p {\n font-size: 16px;\n line-height: 1.5;\n margin-bottom: 20px;\n }\n .button-link {\n color: #fff;\n background-color: #2196F3;\n padding: 10px 20px;\n text-decoration: none;\n border-radius: 5px;\n }\n </style>\n</head>\n<body>\n<div class=\"container\">\n <h1>Activate your Secutils.dev account</h1>\n <p>Thanks for signing up! To activate your account, please click the link below:</p>\n <a class=\"button-link\" href=\"http://localhost:1234/activate?code=some-code&email=dev%40secutils.dev\">Activate my account</a>\n <p>If the button above doesn't work, you can also copy and paste the following URL into your browser:</p>\n <p>http://localhost:1234/activate?code=some-code&email=dev%40secutils.dev</p>\n <p>If you have any trouble activating your account, please contact us at <a href=\"mailto: contact@secutils.dev\">contact@secutils.dev</a>.</p>\n</div>\n</body>\n</html>\n",
"<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n <title>Activate your Secutils.dev account</title>\n <meta charset=\"utf-8\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n <style>\n body {\n font-family: Arial, sans-serif;\n background-color: #f1f1f1;\n margin: 0;\n padding: 0;\n }\n .container {\n max-width: 600px;\n margin: 0 auto;\n background-color: #fff;\n padding: 20px;\n border-radius: 5px;\n box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);\n }\n h1 {\n font-size: 24px;\n margin-top: 0;\n }\n p {\n font-size: 16px;\n line-height: 1.5;\n margin-bottom: 20px;\n }\n .button-link {\n color: #fff;\n background-color: #2196F3;\n padding: 10px 20px;\n text-decoration: none;\n border-radius: 5px;\n }\n </style>\n</head>\n<body>\n<div class=\"container\">\n <h1>Activate your Secutils.dev account</h1>\n <p>Thanks for signing up! To activate your account, please click the link below:</p>\n <a class=\"button-link\" href=\"http://localhost:1234/activate?code=some-code&email=dev-1%40secutils.dev\">Activate my account</a>\n <p>If the button above doesn't work, you can also copy and paste the following URL into your browser:</p>\n <p>http://localhost:1234/activate?code=some-code&email=dev-1%40secutils.dev</p>\n <p>If you have any trouble activating your account, please contact us at <a href=\"mailto: contact@secutils.dev\">contact@secutils.dev</a>.</p>\n</div>\n</body>\n</html>\n",
),
attachments: None,
}
Expand Down Expand Up @@ -115,9 +115,9 @@ mod tests {
.await?, @r###"
EmailNotificationContent {
subject: "Reset password for your Secutils.dev account",
text: "To reset your Secutils.dev password, please click the following link: http://localhost:1234/reset_credentials?code=some-code&email=dev%40secutils.dev",
text: "To reset your Secutils.dev password, please click the following link: http://localhost:1234/reset_credentials?code=some-code&email=dev-1%40secutils.dev",
html: Some(
"<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n <title>Reset password for your Secutils.dev account</title>\n <meta charset=\"utf-8\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n <style>\n body {\n font-family: Arial, sans-serif;\n background-color: #f1f1f1;\n margin: 0;\n padding: 0;\n }\n .container {\n max-width: 600px;\n margin: 0 auto;\n background-color: #fff;\n padding: 20px;\n border-radius: 5px;\n box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);\n }\n h1 {\n font-size: 24px;\n margin-top: 0;\n }\n p {\n font-size: 16px;\n line-height: 1.5;\n margin-bottom: 20px;\n }\n .button-link {\n color: #fff;\n background-color: #2196F3;\n padding: 10px 20px;\n text-decoration: none;\n border-radius: 5px;\n }\n </style>\n</head>\n<body>\n<div class=\"container\">\n <h1>Reset password for your Secutils.dev account</h1>\n <p>You recently requested to reset your password. To reset your password, please click the link below:</p>\n <a class=\"button-link\" href=\"http://localhost:1234/reset_credentials?code=some-code&email=dev%40secutils.dev\">Reset your password</a>\n <p>If the button above doesn't work, you can also copy and paste the following URL into your browser:</p>\n <p>http://localhost:1234/reset_credentials?code=some-code&email=dev%40secutils.dev</p>\n <p>If you did not request to reset your password, please ignore this email and your password will not be changed.</p>\n <p>If you have any trouble resetting your password, please contact us at <a href=\"mailto: contact@secutils.dev\">contact@secutils.dev</a>.</p>\n</div>\n</body>\n</html>\n",
"<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n <title>Reset password for your Secutils.dev account</title>\n <meta charset=\"utf-8\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n <style>\n body {\n font-family: Arial, sans-serif;\n background-color: #f1f1f1;\n margin: 0;\n padding: 0;\n }\n .container {\n max-width: 600px;\n margin: 0 auto;\n background-color: #fff;\n padding: 20px;\n border-radius: 5px;\n box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);\n }\n h1 {\n font-size: 24px;\n margin-top: 0;\n }\n p {\n font-size: 16px;\n line-height: 1.5;\n margin-bottom: 20px;\n }\n .button-link {\n color: #fff;\n background-color: #2196F3;\n padding: 10px 20px;\n text-decoration: none;\n border-radius: 5px;\n }\n </style>\n</head>\n<body>\n<div class=\"container\">\n <h1>Reset password for your Secutils.dev account</h1>\n <p>You recently requested to reset your password. To reset your password, please click the link below:</p>\n <a class=\"button-link\" href=\"http://localhost:1234/reset_credentials?code=some-code&email=dev-1%40secutils.dev\">Reset your password</a>\n <p>If the button above doesn't work, you can also copy and paste the following URL into your browser:</p>\n <p>http://localhost:1234/reset_credentials?code=some-code&email=dev-1%40secutils.dev</p>\n <p>If you did not request to reset your password, please ignore this email and your password will not be changed.</p>\n <p>If you have any trouble resetting your password, please contact us at <a href=\"mailto: contact@secutils.dev\">contact@secutils.dev</a>.</p>\n</div>\n</body>\n</html>\n",
),
attachments: None,
}
Expand Down
6 changes: 3 additions & 3 deletions src/scheduler/scheduler_jobs/notifications_send_job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,8 @@ mod tests {
Envelope {
forward_path: [
Address {
serialized: "dev@secutils.dev",
at_start: 3,
serialized: "dev-1@secutils.dev",
at_start: 5,
},
],
reverse_path: Some(
Expand All @@ -283,7 +283,7 @@ mod tests {
},
),
},
"From: dev@secutils.dev\r\nReply-To: dev@secutils.dev\r\nTo: dev@secutils.dev\r\nSubject: [NO SUBJECT]\r\nDate: Sat, 01 Jan 2000 10:00:00 +0000\r\nContent-Transfer-Encoding: 7bit\r\n\r\nmessage 0",
"From: dev@secutils.dev\r\nReply-To: dev@secutils.dev\r\nTo: dev-1@secutils.dev\r\nSubject: [NO SUBJECT]\r\nDate: Sat, 01 Jan 2000 10:00:00 +0000\r\nContent-Transfer-Encoding: 7bit\r\n\r\nmessage 0",
)
"###);

Expand Down
6 changes: 5 additions & 1 deletion src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use crate::{
scheduler::Scheduler,
search::{populate_search_index, SearchIndex},
security::{create_webauthn, Security},
server::app_state::AppState,
templates::create_templates,
users::builtin_users_initializer,
};
Expand All @@ -24,6 +23,11 @@ use anyhow::Context;
use lettre::{transport::smtp::authentication::Credentials, AsyncSmtpTransport, Tokio1Executor};
use std::sync::Arc;

#[cfg(test)]
pub use self::app_state::tests;

pub use app_state::AppState;

#[actix_rt::main]
pub async fn run(
config: Config,
Expand Down
Loading

0 comments on commit fa013b1

Please sign in to comment.