From fcd013fcde9222a957517cdfc738ade24c255c88 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sun, 28 Aug 2022 18:39:28 +0100 Subject: [PATCH] add session ttl customization example --- auth/cookie-auth/src/main.rs | 3 +-- auth/cookie-session/src/main.rs | 14 ++++++++++++-- auth/simple-auth-server/src/auth_handler.rs | 11 ++++++----- auth/simple-auth-server/src/invitation_handler.rs | 6 ++++-- auth/simple-auth-server/src/main.rs | 6 ++++-- auth/simple-auth-server/src/register_handler.rs | 14 +++++++++----- basics/hello-world/src/main.rs | 4 +--- basics/state/src/main.rs | 12 ++++++++---- data-factory/src/main.rs | 3 +-- forms/form/src/main.rs | 15 ++++++++++----- forms/multipart-s3/src/main.rs | 13 ++++--------- graphql/juniper-advanced/src/db.rs | 6 ++++-- graphql/juniper/src/schema.rs | 3 +-- json/jsonrpc/src/convention.rs | 4 ++-- middleware/middleware-ext-mut/src/add_msg.rs | 3 +-- middleware/middleware/src/redirect.rs | 9 +++++---- rustfmt.toml | 3 ++- templating/tinytemplate/src/main.rs | 15 +++++++++------ 18 files changed, 84 insertions(+), 60 deletions(-) diff --git a/auth/cookie-auth/src/main.rs b/auth/cookie-auth/src/main.rs index cb0490b..76aecda 100644 --- a/auth/cookie-auth/src/main.rs +++ b/auth/cookie-auth/src/main.rs @@ -1,5 +1,4 @@ -use actix_identity::Identity; -use actix_identity::{CookieIdentityPolicy, IdentityService}; +use actix_identity::{CookieIdentityPolicy, Identity, IdentityService}; use actix_web::{middleware, web, App, HttpResponse, HttpServer}; use rand::Rng; diff --git a/auth/cookie-session/src/main.rs b/auth/cookie-session/src/main.rs index 19f82f8..96d4e7e 100644 --- a/auth/cookie-session/src/main.rs +++ b/auth/cookie-session/src/main.rs @@ -5,8 +5,14 @@ //! //! [User guide](https://actix.rs/docs/middleware/#user-sessions) -use actix_session::{storage::CookieSessionStore, Session, SessionMiddleware}; -use actix_web::{cookie::Key, middleware::Logger, web, App, HttpRequest, HttpServer, Result}; +use actix_session::{ + config::PersistentSession, storage::CookieSessionStore, Session, SessionMiddleware, +}; +use actix_web::{ + cookie::{self, Key}, + middleware::Logger, + web, App, HttpRequest, HttpServer, Result, +}; /// simple index handler with session async fn index(session: Session, req: HttpRequest) -> Result<&'static str> { @@ -39,6 +45,10 @@ async fn main() -> std::io::Result<()> { .wrap( SessionMiddleware::builder(CookieSessionStore::default(), Key::from(&[0; 64])) .cookie_secure(false) + // customize session and cookie expiration + .session_lifecycle( + PersistentSession::default().session_ttl(cookie::time::Duration::hours(2)), + ) .build(), ) .service(web::resource("/").to(index)) diff --git a/auth/simple-auth-server/src/auth_handler.rs b/auth/simple-auth-server/src/auth_handler.rs index b4b0942..b0d3f84 100644 --- a/auth/simple-auth-server/src/auth_handler.rs +++ b/auth/simple-auth-server/src/auth_handler.rs @@ -2,13 +2,14 @@ use std::future::{ready, Ready}; use actix_identity::Identity; use actix_web::{dev::Payload, web, Error, FromRequest, HttpRequest, HttpResponse}; -use diesel::prelude::*; -use diesel::PgConnection; +use diesel::{prelude::*, PgConnection}; use serde::Deserialize; -use crate::errors::ServiceError; -use crate::models::{Pool, SlimUser, User}; -use crate::utils::verify; +use crate::{ + errors::ServiceError, + models::{Pool, SlimUser, User}, + utils::verify, +}; #[derive(Debug, Deserialize)] pub struct AuthData { diff --git a/auth/simple-auth-server/src/invitation_handler.rs b/auth/simple-auth-server/src/invitation_handler.rs index ed9c68c..dc94a9e 100644 --- a/auth/simple-auth-server/src/invitation_handler.rs +++ b/auth/simple-auth-server/src/invitation_handler.rs @@ -2,8 +2,10 @@ use actix_web::{web, HttpResponse}; use diesel::{prelude::*, PgConnection}; use serde::Deserialize; -use crate::email_service::send_invitation; -use crate::models::{Invitation, Pool}; +use crate::{ + email_service::send_invitation, + models::{Invitation, Pool}, +}; #[derive(Deserialize)] pub struct InvitationData { diff --git a/auth/simple-auth-server/src/main.rs b/auth/simple-auth-server/src/main.rs index df835c5..0016ee4 100644 --- a/auth/simple-auth-server/src/main.rs +++ b/auth/simple-auth-server/src/main.rs @@ -3,8 +3,10 @@ extern crate diesel; use actix_identity::{CookieIdentityPolicy, IdentityService}; use actix_web::{middleware, web, App, HttpServer}; -use diesel::prelude::*; -use diesel::r2d2::{self, ConnectionManager}; +use diesel::{ + prelude::*, + r2d2::{self, ConnectionManager}, +}; use time::Duration; mod auth_handler; diff --git a/auth/simple-auth-server/src/register_handler.rs b/auth/simple-auth-server/src/register_handler.rs index d92df16..7c4086b 100644 --- a/auth/simple-auth-server/src/register_handler.rs +++ b/auth/simple-auth-server/src/register_handler.rs @@ -2,9 +2,11 @@ use actix_web::{web, HttpResponse}; use diesel::prelude::*; use serde::Deserialize; -use crate::errors::ServiceError; -use crate::models::{Invitation, Pool, SlimUser, User}; -use crate::utils::hash_password; +use crate::{ + errors::ServiceError, + models::{Invitation, Pool, SlimUser, User}, + utils::hash_password, +}; // UserData is used to extract data from a post request by the client #[derive(Debug, Deserialize)] @@ -34,8 +36,10 @@ fn query( password: String, pool: web::Data, ) -> Result { - use crate::schema::invitations::dsl::{id, invitations}; - use crate::schema::users::dsl::users; + use crate::schema::{ + invitations::dsl::{id, invitations}, + users::dsl::users, + }; let invitation_id = uuid::Uuid::parse_str(&invitation_id)?; let conn: &PgConnection = &pool.get().unwrap(); diff --git a/basics/hello-world/src/main.rs b/basics/hello-world/src/main.rs index 50960f0..e3f6514 100644 --- a/basics/hello-world/src/main.rs +++ b/basics/hello-world/src/main.rs @@ -24,9 +24,7 @@ async fn main() -> std::io::Result<()> { #[cfg(test)] mod tests { - use actix_web::body::to_bytes; - use actix_web::dev::Service; - use actix_web::{http, test, web, App, Error}; + use actix_web::{body::to_bytes, dev::Service, http, test, web, App, Error}; use super::*; diff --git a/basics/state/src/main.rs b/basics/state/src/main.rs index 0d37bdf..3b90681 100644 --- a/basics/state/src/main.rs +++ b/basics/state/src/main.rs @@ -18,10 +18,14 @@ //! //! Check [user guide](https://actix.rs/docs/application/#state) for more info. -use std::cell::Cell; -use std::io; -use std::sync::atomic::{AtomicUsize, Ordering}; -use std::sync::Mutex; +use std::{ + cell::Cell, + io, + sync::{ + atomic::{AtomicUsize, Ordering}, + Mutex, + }, +}; use actix_web::{ middleware, diff --git a/data-factory/src/main.rs b/data-factory/src/main.rs index a71d409..6f878bc 100644 --- a/data-factory/src/main.rs +++ b/data-factory/src/main.rs @@ -1,5 +1,4 @@ -use actix_web::web::Data; -use actix_web::{get, App, HttpServer}; +use actix_web::{get, web::Data, App, HttpServer}; use redis_tang::{Builder, Pool, RedisManager}; #[actix_web::main] diff --git a/forms/form/src/main.rs b/forms/form/src/main.rs index 7922ec5..e300ad7 100644 --- a/forms/form/src/main.rs +++ b/forms/form/src/main.rs @@ -70,11 +70,16 @@ async fn handle_post_3(req: HttpRequest, params: web::Form) -> impl Re #[cfg(test)] mod tests { - use actix_web::body::to_bytes; - use actix_web::dev::{Service, ServiceResponse}; - use actix_web::http::{header::HeaderValue, header::CONTENT_TYPE, StatusCode}; - use actix_web::test::{self, TestRequest}; - use actix_web::web::{Bytes, Form}; + use actix_web::{ + body::to_bytes, + dev::{Service, ServiceResponse}, + http::{ + header::{HeaderValue, CONTENT_TYPE}, + StatusCode, + }, + test::{self, TestRequest}, + web::{Bytes, Form}, + }; use super::*; diff --git a/forms/multipart-s3/src/main.rs b/forms/multipart-s3/src/main.rs index 5634cf3..0f92394 100644 --- a/forms/multipart-s3/src/main.rs +++ b/forms/multipart-s3/src/main.rs @@ -1,13 +1,11 @@ use std::fs; use actix_multipart::Multipart; -use actix_web::body::SizedStream; -use actix_web::{delete, error}; use actix_web::{ - get, middleware::Logger, post, web, App, Error, HttpResponse, HttpServer, Responder, + body::SizedStream, delete, error, get, middleware::Logger, post, web, App, Error, HttpResponse, + HttpServer, Responder, }; -use actix_web_lab::extract::Path; -use actix_web_lab::respond::Html; +use actix_web_lab::{extract::Path, respond::Html}; use aws_config::meta::region::RegionProviderChain; use dotenv::dotenv; use serde::{Deserialize, Serialize}; @@ -18,10 +16,7 @@ mod temp_file; mod upload_file; mod utils; -use self::client::Client; -use self::temp_file::TempFile; -use self::upload_file::UploadedFile; -use self::utils::split_payload; +use self::{client::Client, temp_file::TempFile, upload_file::UploadedFile, utils::split_payload}; #[derive(Debug, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] diff --git a/graphql/juniper-advanced/src/db.rs b/graphql/juniper-advanced/src/db.rs index e0983dd..60fad57 100644 --- a/graphql/juniper-advanced/src/db.rs +++ b/graphql/juniper-advanced/src/db.rs @@ -1,5 +1,7 @@ -use r2d2_mysql::mysql::{Opts, OptsBuilder}; -use r2d2_mysql::MysqlConnectionManager; +use r2d2_mysql::{ + mysql::{Opts, OptsBuilder}, + MysqlConnectionManager, +}; pub type Pool = r2d2::Pool; diff --git a/graphql/juniper/src/schema.rs b/graphql/juniper/src/schema.rs index 8200d60..9f7b47c 100644 --- a/graphql/juniper/src/schema.rs +++ b/graphql/juniper/src/schema.rs @@ -1,5 +1,4 @@ -use juniper::FieldResult; -use juniper::{EmptySubscription, RootNode}; +use juniper::{EmptySubscription, FieldResult, RootNode}; #[derive(GraphQLEnum)] enum Episode { diff --git a/json/jsonrpc/src/convention.rs b/json/jsonrpc/src/convention.rs index e3aca49..26e8f2c 100644 --- a/json/jsonrpc/src/convention.rs +++ b/json/jsonrpc/src/convention.rs @@ -1,7 +1,7 @@ //! JSON-RPC 2.0 Specification //! See: https://www.jsonrpc.org/specification -use std::error; -use std::fmt; + +use std::{error, fmt}; use serde::{Deserialize, Serialize}; use serde_json::Value; diff --git a/middleware/middleware-ext-mut/src/add_msg.rs b/middleware/middleware-ext-mut/src/add_msg.rs index e8e36e3..5aa1423 100644 --- a/middleware/middleware-ext-mut/src/add_msg.rs +++ b/middleware/middleware-ext-mut/src/add_msg.rs @@ -3,10 +3,9 @@ use std::{ task::{Context, Poll}, }; -use actix_web::Error; use actix_web::{ dev::{Service, ServiceRequest, ServiceResponse, Transform}, - HttpMessage, + Error, HttpMessage, }; #[derive(Debug, Clone)] diff --git a/middleware/middleware/src/redirect.rs b/middleware/middleware/src/redirect.rs index dffd42d..e28f386 100644 --- a/middleware/middleware/src/redirect.rs +++ b/middleware/middleware/src/redirect.rs @@ -1,9 +1,10 @@ use std::future::{ready, Ready}; -use actix_web::body::EitherBody; -use actix_web::dev::{self, ServiceRequest, ServiceResponse}; -use actix_web::dev::{Service, Transform}; -use actix_web::{http, Error, HttpResponse}; +use actix_web::{ + body::EitherBody, + dev::{self, Service, ServiceRequest, ServiceResponse, Transform}, + http, Error, HttpResponse, +}; use futures_util::future::LocalBoxFuture; pub struct CheckLogin; diff --git a/rustfmt.toml b/rustfmt.toml index 38b9e24..71b9be3 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -1,2 +1,3 @@ -reorder_imports = true group_imports = "StdExternalCrate" +imports_granularity = "Crate" +use_field_init_shorthand = true diff --git a/templating/tinytemplate/src/main.rs b/templating/tinytemplate/src/main.rs index 97187e6..4d34276 100644 --- a/templating/tinytemplate/src/main.rs +++ b/templating/tinytemplate/src/main.rs @@ -1,11 +1,14 @@ use std::collections::HashMap; -use actix_web::body::BoxBody; -use actix_web::dev::ServiceResponse; -use actix_web::http::header::ContentType; -use actix_web::http::StatusCode; -use actix_web::middleware::{ErrorHandlerResponse, ErrorHandlers}; -use actix_web::{error, middleware, web, App, Error, HttpResponse, HttpServer, Result}; +use actix_web::{ + body::BoxBody, + dev::ServiceResponse, + error, + http::{header::ContentType, StatusCode}, + middleware, + middleware::{ErrorHandlerResponse, ErrorHandlers}, + web, App, Error, HttpResponse, HttpServer, Result, +}; use serde_json::json; use tinytemplate::TinyTemplate;