diff --git a/.gitignore b/.gitignore index cab0673..7e5fef8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /target /.vscode -/config.toml \ No newline at end of file +/config.toml +*.db \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index f804b68..00201a4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -795,6 +795,39 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "diesel" +version = "2.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03fc05c17098f21b89bc7d98fe1dd3cce2c11c2ad8e145f2a44fe08ed28eb559" +dependencies = [ + "chrono", + "diesel_derives", + "libsqlite3-sys", + "time", +] + +[[package]] +name = "diesel_derives" +version = "2.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d02eecb814ae714ffe61ddc2db2dd03e6c49a42e269b5001355500d431cce0c" +dependencies = [ + "diesel_table_macro_syntax", + "proc-macro2", + "quote", + "syn 2.0.58", +] + +[[package]] +name = "diesel_table_macro_syntax" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc5557efc453706fed5e4fa85006fe9817c224c3f480a34c7e5959fd700921c5" +dependencies = [ + "syn 2.0.58", +] + [[package]] name = "digest" version = "0.10.7" @@ -1449,6 +1482,16 @@ version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4ec2a862134d2a7d32d7983ddcdd1c4923530833c9f2ea1a44fc5fa473989058" +[[package]] +name = "libsqlite3-sys" +version = "0.28.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c10584274047cb335c23d3e61bcef8e323adae7c5c8c760540f73610177fc3f" +dependencies = [ + "pkg-config", + "vcpkg", +] + [[package]] name = "linux-raw-sys" version = "0.4.13" @@ -2434,9 +2477,11 @@ version = "0.1.0" dependencies = [ "actix-session", "actix-web", + "chrono", "clap", "clio", "color-eyre", + "diesel", "env_logger", "eyre", "futures", diff --git a/Cargo.toml b/Cargo.toml index 807610c..db2d81d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,9 +8,11 @@ edition = "2021" [dependencies] actix-session = { version = "0.9.0", features = ["cookie-session"] } actix-web = { version = "4.5.1", features = ["secure-cookies"] } +chrono = { version = "0.4.37", features = ["now"] } clap = { version = "4.5.4", features = ["derive"] } clio = { version = "0.3.5", features = ["clap-parse"] } color-eyre = "0.6.3" +diesel = { version = "2.1.5", features = ["sqlite", "chrono"] } env_logger = "0.11.3" eyre = "0.6.12" futures = "0.3.30" diff --git a/config.toml.sample b/config.toml.sample index da14b88..649d1ae 100644 --- a/config.toml.sample +++ b/config.toml.sample @@ -1,3 +1,5 @@ +sqlite_db = "subscribe-list.db" + [oidc.google] pretty_name = "Google" issuer_url = "https://accounts.google.com" @@ -12,4 +14,4 @@ issuer_url = "https://salsa.debian.org" client_id = "" secret = "" redirect_url = "https://my-site/subscription/callback/debian-salsa" -scopes = ["email"] +scopes = ["email", "openid"] diff --git a/diesel.toml b/diesel.toml new file mode 100644 index 0000000..c028f4a --- /dev/null +++ b/diesel.toml @@ -0,0 +1,9 @@ +# For documentation on how to configure this file, +# see https://diesel.rs/guides/configuring-diesel-cli + +[print_schema] +file = "src/schema.rs" +custom_type_derives = ["diesel::query_builder::QueryId"] + +[migrations_directory] +dir = "migrations" diff --git a/migrations/.keep b/migrations/.keep new file mode 100644 index 0000000..e69de29 diff --git a/migrations/2024-04-08-223926_create_accounts/down.sql b/migrations/2024-04-08-223926_create_accounts/down.sql new file mode 100644 index 0000000..b882e7c --- /dev/null +++ b/migrations/2024-04-08-223926_create_accounts/down.sql @@ -0,0 +1 @@ +DROP TABLE "accounts" \ No newline at end of file diff --git a/migrations/2024-04-08-223926_create_accounts/up.sql b/migrations/2024-04-08-223926_create_accounts/up.sql new file mode 100644 index 0000000..db9059a --- /dev/null +++ b/migrations/2024-04-08-223926_create_accounts/up.sql @@ -0,0 +1,4 @@ +CREATE TABLE "accounts" ( + "email" TEXT NOT NULL PRIMARY KEY ASC, + "date" TIMESTAMP NOT NULL +) WITHOUT ROWID; \ No newline at end of file diff --git a/src/app.rs b/src/app.rs index 7186e7d..92ebac5 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,7 +1,4 @@ -use crate::{ - client::async_http_client, - config::{Config, OIDCProvider}, -}; +use crate::{client::async_http_client, config::Config, db::DbThread, models::Account}; use actix_session::Session; use actix_web::{ get, @@ -9,10 +6,10 @@ use actix_web::{ web::{self, ServiceConfig}, HttpResponse, Responder, }; -use eyre::{ensure, Context, OptionExt}; +use eyre::{ensure, OptionExt}; use openidconnect::{ core::{CoreAuthenticationFlow, CoreUserInfoClaims}, - AuthorizationCode, CsrfToken, EndUserEmail, Nonce, OAuth2TokenResponse, + AuthorizationCode, CsrfToken, EndUserEmail, Nonce, OAuth2TokenResponse, TokenResponse, }; use serde::{Deserialize, Serialize}; use std::marker::PhantomData; @@ -74,6 +71,8 @@ pub struct SubscriptionLoggedOutTemplateProviders { templates! { #[text = r#"Subscription +

Register or Sign In:

+As an anti-spam measure, you need to register using a 3rd-party account: