From 23ec2d61f18703e4efe71a4be9b987e60be3ff2d Mon Sep 17 00:00:00 2001 From: Paul Makles <paulmakles@gmail.com> Date: Tue, 26 Jan 2021 17:29:03 +0000 Subject: [PATCH] Push env variables to rAuth + cargo fmt. --- src/database/entities/channel.rs | 8 ++- src/database/entities/message.rs | 84 +++++++++++++++-------------- src/database/guards/user.rs | 5 +- src/database/migrations/scripts.rs | 4 +- src/main.rs | 27 +++++++++- src/notifications/events.rs | 4 +- src/notifications/websocket.rs | 12 +++-- src/routes/channels/group_create.rs | 2 +- src/routes/onboard/complete.rs | 2 +- src/routes/users/open_dm.rs | 2 +- 10 files changed, 94 insertions(+), 56 deletions(-) diff --git a/src/database/entities/channel.rs b/src/database/entities/channel.rs index 63759f8..3ee927a 100644 --- a/src/database/entities/channel.rs +++ b/src/database/entities/channel.rs @@ -79,8 +79,12 @@ impl Channel { pub async fn publish_update(&self, data: JsonValue) -> Result<()> { let id = self.id().to_string(); ClientboundNotification::ChannelUpdate { - id: id.clone(), data - }.publish(id).await.ok(); + id: id.clone(), + data, + } + .publish(id) + .await + .ok(); Ok(()) } diff --git a/src/database/entities/message.rs b/src/database/entities/message.rs index 91349a4..8f8fbe7 100644 --- a/src/database/entities/message.rs +++ b/src/database/entities/message.rs @@ -48,46 +48,48 @@ impl Message { let channels = get_collection("channels"); match channel { Channel::DirectMessage { id, .. } => { - channels.update_one( - doc! { "_id": id }, - doc! { - "$set": { - "active": true, - "last_message": { - "_id": self.id.clone(), - "author": self.author.clone(), - "short": self.content.chars().take(24).collect::<String>() + channels + .update_one( + doc! { "_id": id }, + doc! { + "$set": { + "active": true, + "last_message": { + "_id": self.id.clone(), + "author": self.author.clone(), + "short": self.content.chars().take(24).collect::<String>() + } } - } - }, - None - ) - .await - .map_err(|_| Error::DatabaseError { - operation: "update_one", - with: "channel", - })?; - }, + }, + None, + ) + .await + .map_err(|_| Error::DatabaseError { + operation: "update_one", + with: "channel", + })?; + } Channel::Group { id, .. } => { - channels.update_one( - doc! { "_id": id }, - doc! { - "$set": { - "last_message": { - "_id": self.id.clone(), - "author": self.author.clone(), - "short": self.content.chars().take(24).collect::<String>() + channels + .update_one( + doc! { "_id": id }, + doc! { + "$set": { + "last_message": { + "_id": self.id.clone(), + "author": self.author.clone(), + "short": self.content.chars().take(24).collect::<String>() + } } - } - }, - None - ) - .await - .map_err(|_| Error::DatabaseError { - operation: "update_one", - with: "channel", - })?; - }, + }, + None, + ) + .await + .map_err(|_| Error::DatabaseError { + operation: "update_one", + with: "channel", + })?; + } _ => {} } @@ -102,8 +104,12 @@ impl Message { pub async fn publish_update(&self, data: JsonValue) -> Result<()> { let channel = self.channel.clone(); ClientboundNotification::MessageUpdate { - id: self.id.clone(), data - }.publish(channel).await.ok(); + id: self.id.clone(), + data, + } + .publish(channel) + .await + .ok(); Ok(()) } diff --git a/src/database/guards/user.rs b/src/database/guards/user.rs index e021101..1f44115 100644 --- a/src/database/guards/user.rs +++ b/src/database/guards/user.rs @@ -29,7 +29,10 @@ impl<'a, 'r> FromRequest<'a, 'r> for User { } else { Outcome::Failure(( Status::InternalServerError, - rauth::util::Error::DatabaseError { operation: "find_one", with: "user" }, + rauth::util::Error::DatabaseError { + operation: "find_one", + with: "user", + }, )) } } diff --git a/src/database/migrations/scripts.rs b/src/database/migrations/scripts.rs index 7ebcb5c..2c8d6ea 100644 --- a/src/database/migrations/scripts.rs +++ b/src/database/migrations/scripts.rs @@ -1,9 +1,7 @@ -use super::super::{get_collection, get_db}; +use crate::database::get_collection; -use crate::rocket::futures::StreamExt; use log::info; use mongodb::bson::{doc, from_document}; -use mongodb::options::FindOptions; use serde::{Deserialize, Serialize}; #[derive(Serialize, Deserialize)] diff --git a/src/main.rs b/src/main.rs index 79109f2..690a8ab 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,10 +18,13 @@ pub mod notifications; pub mod routes; pub mod util; +use chrono::Duration; use futures::join; use log::info; -use rauth::{self, options::Options}; +use rauth::auth::Auth; +use rauth::options::{EmailVerification, Options, SMTP}; use rocket_cors::AllowedOrigins; +use util::variables::{PUBLIC_URL, SMTP_FROM, SMTP_HOST, SMTP_PASSWORD, SMTP_USERNAME, USE_EMAIL}; #[async_std::main] async fn main() { @@ -55,7 +58,27 @@ async fn launch_web() { .to_cors() .expect("Failed to create CORS."); - let auth = rauth::auth::Auth::new(database::get_collection("accounts"), Options::new()); + let auth = Auth::new( + database::get_collection("accounts"), + Options::new() + .base_url(format!("{}/auth", *PUBLIC_URL)) + .email_verification(if *USE_EMAIL { + EmailVerification::Enabled { + success_redirect_uri: format!("{}/welcome", *PUBLIC_URL), + verification_expiry: Duration::days(1), + verification_ratelimit: Duration::minutes(1), + + smtp: SMTP { + from: (*SMTP_FROM).to_string(), + host: (*SMTP_HOST).to_string(), + username: (*SMTP_USERNAME).to_string(), + password: (*SMTP_PASSWORD).to_string(), + }, + } + } else { + EmailVerification::Disabled + }), + ); routes::mount(rocket::ignite()) .mount("/", rocket_cors::catch_all_options_routes()) diff --git a/src/notifications/events.rs b/src/notifications/events.rs index 92c161e..b21ffca 100644 --- a/src/notifications/events.rs +++ b/src/notifications/events.rs @@ -41,7 +41,7 @@ pub enum ClientboundNotification { Message(Message), MessageUpdate { id: String, - data: JsonValue + data: JsonValue, }, MessageDelete { id: String, @@ -50,7 +50,7 @@ pub enum ClientboundNotification { ChannelCreate(Channel), ChannelUpdate { id: String, - data: JsonValue + data: JsonValue, }, ChannelGroupJoin { id: String, diff --git a/src/notifications/websocket.rs b/src/notifications/websocket.rs index df4099b..841c437 100644 --- a/src/notifications/websocket.rs +++ b/src/notifications/websocket.rs @@ -12,7 +12,10 @@ use futures::{pin_mut, prelude::*}; use hive_pubsub::PubSub; use log::{debug, info}; use many_to_many::ManyToMany; -use rauth::{auth::{Auth, Session}, options::Options}; +use rauth::{ + auth::{Auth, Session}, + options::Options, +}; use std::collections::HashMap; use std::net::SocketAddr; use std::sync::{Arc, Mutex, RwLock}; @@ -84,9 +87,10 @@ async fn accept(stream: TcpStream) { } } - if let Ok(validated_session) = Auth::new(get_collection("accounts"), Options::new()) - .verify_session(new_session) - .await + if let Ok(validated_session) = + Auth::new(get_collection("accounts"), Options::new()) + .verify_session(new_session) + .await { let id = validated_session.user_id.clone(); if let Ok(user) = (Ref { id: id.clone() }).fetch_user().await { diff --git a/src/routes/channels/group_create.rs b/src/routes/channels/group_create.rs index 65d6a16..c979b01 100644 --- a/src/routes/channels/group_create.rs +++ b/src/routes/channels/group_create.rs @@ -70,7 +70,7 @@ pub async fn req(user: User, info: Json<Data>) -> Result<JsonValue> { .unwrap_or_else(|| "A group.".to_string()), owner: user.id, recipients: set.into_iter().collect::<Vec<String>>(), - last_message: None + last_message: None, }; channel.clone().publish().await?; diff --git a/src/routes/onboard/complete.rs b/src/routes/onboard/complete.rs index 1711c12..45f30d7 100644 --- a/src/routes/onboard/complete.rs +++ b/src/routes/onboard/complete.rs @@ -27,7 +27,7 @@ pub async fn req(session: Session, user: Option<User>, data: Json<Data>) -> Resu data.validate() .map_err(|error| Error::FailedValidation { error })?; - + if data.username == "revolt" { Err(Error::UsernameTaken)? } diff --git a/src/routes/users/open_dm.rs b/src/routes/users/open_dm.rs index d36b664..4712cba 100644 --- a/src/routes/users/open_dm.rs +++ b/src/routes/users/open_dm.rs @@ -40,7 +40,7 @@ pub async fn req(user: User, target: Ref) -> Result<JsonValue> { id, active: false, recipients: vec![user.id, target.id], - last_message: None + last_message: None, } }; -- GitLab