From 60d419921617164e499e8efa1402216ecdb4e339 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Thu, 14 Dec 2023 16:58:37 -0500 Subject: [PATCH] fix logout The reason this is so labyrinthine is because of the users and billing components. They bind dynamic behaviors that are defined in storyteller-web (one of them being clearing the redis session cache). Actually, the users component _didn't_, because it was defined in the billing component. So I hoisted it out into a new component library. In the fullness of time I imagine all of this will be consolidated back under storyteller-web so we don't have to worry about binding actions at a distance. Anyway, this is tested and works locally. --- Cargo.lock | 13 +++++++++ Cargo.toml | 1 + crates/service/plugins/billing/Cargo.toml | 1 + ...pe_create_checkout_session_json_handler.rs | 2 +- ...reate_checkout_session_redirect_handler.rs | 2 +- ...te_customer_portal_session_json_handler.rs | 2 +- ...ustomer_portal_session_redirect_handler.rs | 2 +- .../plugins/billing/src/stripe/traits/mod.rs | 1 - .../plugins/user_traits_component/Cargo.toml | 27 +++++++++++++++++++ .../plugins/user_traits_component/src/lib.rs | 21 +++++++++++++++ .../traits/internal_session_cache_purge.rs | 0 .../user_traits_component/src/traits/mod.rs | 1 + crates/service/plugins/users/Cargo.toml | 1 + .../users/src/endpoints/logout_handler.rs | 5 ++++ crates/service/web/storyteller_web/Cargo.toml | 1 + .../internal_session_cache_purge_impl.rs | 2 +- .../service/web/storyteller_web/src/main.rs | 16 +++++------ 17 files changed, 82 insertions(+), 16 deletions(-) create mode 100644 crates/service/plugins/user_traits_component/Cargo.toml create mode 100644 crates/service/plugins/user_traits_component/src/lib.rs rename crates/service/plugins/{billing/src/stripe => user_traits_component/src}/traits/internal_session_cache_purge.rs (100%) create mode 100644 crates/service/plugins/user_traits_component/src/traits/mod.rs diff --git a/Cargo.lock b/Cargo.lock index af9894f874..8d6d76535f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -813,6 +813,7 @@ dependencies = [ "tokio", "toml 0.5.11", "url_config", + "user_traits_component", ] [[package]] @@ -5954,6 +5955,7 @@ dependencies = [ "twitch_oauth2", "url_config", "user_input_common", + "user_traits_component", "users_component", "utoipa", "utoipa-swagger-ui", @@ -7066,6 +7068,16 @@ dependencies = [ "unicode-segmentation", ] +[[package]] +name = "user_traits_component" +version = "0.0.1" +dependencies = [ + "actix-web", + "errors", + "mockall", + "my-workspace-hack", +] + [[package]] name = "users_component" version = "0.0.1" @@ -7109,6 +7121,7 @@ dependencies = [ "tokens", "toml 0.5.11", "user_input_common", + "user_traits_component", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 1ea5d8fefc..411f131e53 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -83,6 +83,7 @@ members = [ # Service Plugins: complete subsystems that can be plugged into services (users, billing, etc.) "crates/service/plugins/billing", "crates/service/plugins/inference_workloads", + "crates/service/plugins/user_traits_component", "crates/service/plugins/users", # Vendored code diff --git a/crates/service/plugins/billing/Cargo.toml b/crates/service/plugins/billing/Cargo.toml index 869eee7790..56a8176786 100644 --- a/crates/service/plugins/billing/Cargo.toml +++ b/crates/service/plugins/billing/Cargo.toml @@ -20,6 +20,7 @@ http_server_common = { path = "../../../lib/deprecated/http_server_common" } mysql_queries = { path = "../../../lib/mysql_queries" } reusable_types = { path = "../../../_types/reusable_types" } url_config = { path = "../../../lib/url_config" } +user_traits_component = { path = "../user_traits_component" } # Workspace hack (faster compile times) my-workspace-hack = { version = "0.1", path = "../../../my-workspace-hack" } diff --git a/crates/service/plugins/billing/src/stripe/http_endpoints/checkout/create/stripe_create_checkout_session_json_handler.rs b/crates/service/plugins/billing/src/stripe/http_endpoints/checkout/create/stripe_create_checkout_session_json_handler.rs index bdc4de4af8..c81a63b39d 100644 --- a/crates/service/plugins/billing/src/stripe/http_endpoints/checkout/create/stripe_create_checkout_session_json_handler.rs +++ b/crates/service/plugins/billing/src/stripe/http_endpoints/checkout/create/stripe_create_checkout_session_json_handler.rs @@ -3,12 +3,12 @@ use actix_web::web::Json; use reusable_types::server_environment::ServerEnvironment; use url_config::third_party_url_redirector::ThirdPartyUrlRedirector; +use user_traits_component::traits::internal_session_cache_purge::InternalSessionCachePurge; use crate::stripe::http_endpoints::checkout::create::stripe_create_checkout_session_error::CreateCheckoutSessionError; use crate::stripe::http_endpoints::checkout::create::stripe_create_checkout_session_shared::stripe_create_checkout_session_shared; use crate::stripe::stripe_config::StripeConfig; use crate::stripe::traits::internal_product_to_stripe_lookup::InternalProductToStripeLookup; -use crate::stripe::traits::internal_session_cache_purge::InternalSessionCachePurge; use crate::stripe::traits::internal_user_lookup::InternalUserLookup; // =============== Request =============== diff --git a/crates/service/plugins/billing/src/stripe/http_endpoints/checkout/create/stripe_create_checkout_session_redirect_handler.rs b/crates/service/plugins/billing/src/stripe/http_endpoints/checkout/create/stripe_create_checkout_session_redirect_handler.rs index 3f6ccc221d..657761495a 100644 --- a/crates/service/plugins/billing/src/stripe/http_endpoints/checkout/create/stripe_create_checkout_session_redirect_handler.rs +++ b/crates/service/plugins/billing/src/stripe/http_endpoints/checkout/create/stripe_create_checkout_session_redirect_handler.rs @@ -4,12 +4,12 @@ use actix_web::web::Query; use reusable_types::server_environment::ServerEnvironment; use url_config::third_party_url_redirector::ThirdPartyUrlRedirector; +use user_traits_component::traits::internal_session_cache_purge::InternalSessionCachePurge; use crate::stripe::http_endpoints::checkout::create::stripe_create_checkout_session_error::CreateCheckoutSessionError; use crate::stripe::http_endpoints::checkout::create::stripe_create_checkout_session_shared::stripe_create_checkout_session_shared; use crate::stripe::stripe_config::StripeConfig; use crate::stripe::traits::internal_product_to_stripe_lookup::InternalProductToStripeLookup; -use crate::stripe::traits::internal_session_cache_purge::InternalSessionCachePurge; use crate::stripe::traits::internal_user_lookup::InternalUserLookup; // =============== Request =============== diff --git a/crates/service/plugins/billing/src/stripe/http_endpoints/customer_portal/stripe_create_customer_portal_session_json_handler.rs b/crates/service/plugins/billing/src/stripe/http_endpoints/customer_portal/stripe_create_customer_portal_session_json_handler.rs index 3e41b3b2b5..bb21b81621 100644 --- a/crates/service/plugins/billing/src/stripe/http_endpoints/customer_portal/stripe_create_customer_portal_session_json_handler.rs +++ b/crates/service/plugins/billing/src/stripe/http_endpoints/customer_portal/stripe_create_customer_portal_session_json_handler.rs @@ -2,12 +2,12 @@ use actix_web::{HttpRequest, HttpResponse, web}; use actix_web::web::Json; use url_config::third_party_url_redirector::ThirdPartyUrlRedirector; +use user_traits_component::traits::internal_session_cache_purge::InternalSessionCachePurge; use crate::stripe::http_endpoints::customer_portal::stripe_create_customer_portal_session_error::CreateCustomerPortalSessionError; use crate::stripe::http_endpoints::customer_portal::stripe_create_customer_portal_session_shared::stripe_create_customer_portal_session_shared; use crate::stripe::stripe_config::StripeConfig; use crate::stripe::traits::internal_product_to_stripe_lookup::InternalProductToStripeLookup; -use crate::stripe::traits::internal_session_cache_purge::InternalSessionCachePurge; use crate::stripe::traits::internal_user_lookup::InternalUserLookup; // =============== Request =============== diff --git a/crates/service/plugins/billing/src/stripe/http_endpoints/customer_portal/stripe_create_customer_portal_session_redirect_handler.rs b/crates/service/plugins/billing/src/stripe/http_endpoints/customer_portal/stripe_create_customer_portal_session_redirect_handler.rs index c4e69a3288..4aafd6e37c 100644 --- a/crates/service/plugins/billing/src/stripe/http_endpoints/customer_portal/stripe_create_customer_portal_session_redirect_handler.rs +++ b/crates/service/plugins/billing/src/stripe/http_endpoints/customer_portal/stripe_create_customer_portal_session_redirect_handler.rs @@ -3,12 +3,12 @@ use actix_web::{HttpRequest, HttpResponse, web}; use actix_web::web::Query; use url_config::third_party_url_redirector::ThirdPartyUrlRedirector; +use user_traits_component::traits::internal_session_cache_purge::InternalSessionCachePurge; use crate::stripe::http_endpoints::customer_portal::stripe_create_customer_portal_session_error::CreateCustomerPortalSessionError; use crate::stripe::http_endpoints::customer_portal::stripe_create_customer_portal_session_shared::stripe_create_customer_portal_session_shared; use crate::stripe::stripe_config::StripeConfig; use crate::stripe::traits::internal_product_to_stripe_lookup::InternalProductToStripeLookup; -use crate::stripe::traits::internal_session_cache_purge::InternalSessionCachePurge; use crate::stripe::traits::internal_user_lookup::InternalUserLookup; // =============== Request =============== diff --git a/crates/service/plugins/billing/src/stripe/traits/mod.rs b/crates/service/plugins/billing/src/stripe/traits/mod.rs index 2cf3b026fa..44d38b0eac 100644 --- a/crates/service/plugins/billing/src/stripe/traits/mod.rs +++ b/crates/service/plugins/billing/src/stripe/traits/mod.rs @@ -1,4 +1,3 @@ pub mod internal_product_to_stripe_lookup; -pub mod internal_session_cache_purge; pub mod internal_subscription_product_lookup; pub mod internal_user_lookup; \ No newline at end of file diff --git a/crates/service/plugins/user_traits_component/Cargo.toml b/crates/service/plugins/user_traits_component/Cargo.toml new file mode 100644 index 0000000000..6521233d9d --- /dev/null +++ b/crates/service/plugins/user_traits_component/Cargo.toml @@ -0,0 +1,27 @@ +[package] +publish = false +name = "user_traits_component" +edition = "2018" +version = "0.0.1" +authors = [ + "Brandon Thomas ", + "Brandon Thomas ", +] + +[lib] +name = "user_traits_component" +path = "src/lib.rs" + +[dependencies] + +# Internal +errors = { path = "../../../lib/errors" } + +# Workspace hack (faster compile times) +my-workspace-hack = { version = "0.1", path = "../../../my-workspace-hack" } + +# Web +actix-web = "4.4.0" + +[dev-dependencies] +mockall = "0.11.3" diff --git a/crates/service/plugins/user_traits_component/src/lib.rs b/crates/service/plugins/user_traits_component/src/lib.rs new file mode 100644 index 0000000000..a490ebd661 --- /dev/null +++ b/crates/service/plugins/user_traits_component/src/lib.rs @@ -0,0 +1,21 @@ +//! user_traits_component +//! +//! Expose some traits for other plugins / components. +//! This will probably go away once we start consolidating the plugins into the main service. +//! + +// Never allow these +#![forbid(private_in_public)] +#![forbid(unused_must_use)] // NB: It's unsafe to not close/check some things + +// Okay to toggle +#![forbid(unreachable_patterns)] +#![forbid(unused_imports)] +#![forbid(unused_mut)] +#![forbid(unused_variables)] + +// Always allow +#![allow(dead_code)] +#![allow(non_snake_case)] + +pub mod traits; diff --git a/crates/service/plugins/billing/src/stripe/traits/internal_session_cache_purge.rs b/crates/service/plugins/user_traits_component/src/traits/internal_session_cache_purge.rs similarity index 100% rename from crates/service/plugins/billing/src/stripe/traits/internal_session_cache_purge.rs rename to crates/service/plugins/user_traits_component/src/traits/internal_session_cache_purge.rs diff --git a/crates/service/plugins/user_traits_component/src/traits/mod.rs b/crates/service/plugins/user_traits_component/src/traits/mod.rs new file mode 100644 index 0000000000..01df49e0ee --- /dev/null +++ b/crates/service/plugins/user_traits_component/src/traits/mod.rs @@ -0,0 +1 @@ +pub mod internal_session_cache_purge; diff --git a/crates/service/plugins/users/Cargo.toml b/crates/service/plugins/users/Cargo.toml index 86db221476..7d2a8f616e 100644 --- a/crates/service/plugins/users/Cargo.toml +++ b/crates/service/plugins/users/Cargo.toml @@ -30,6 +30,7 @@ reusable_types = { path = "../../../_types/reusable_types" } server_environment = { path = "../../../lib/server_environment" } tokens = { path = "../../../_types/tokens" } user_input_common = { path = "../../../lib/user_input_common" } +user_traits_component = { path = "../user_traits_component" } # Workspace hack (faster compile times) my-workspace-hack = { version = "0.1", path = "../../../my-workspace-hack" } diff --git a/crates/service/plugins/users/src/endpoints/logout_handler.rs b/crates/service/plugins/users/src/endpoints/logout_handler.rs index 806b525d48..4c51375357 100644 --- a/crates/service/plugins/users/src/endpoints/logout_handler.rs +++ b/crates/service/plugins/users/src/endpoints/logout_handler.rs @@ -13,6 +13,7 @@ use sqlx::MySqlPool; use http_server_common::response::response_error_helpers::to_simple_json_error; use mysql_queries::queries::users::user_sessions::delete_user_session::delete_user_session; +use user_traits_component::traits::internal_session_cache_purge::InternalSessionCachePurge; use crate::cookies::session::session_cookie_manager::SessionCookieManager; @@ -53,8 +54,12 @@ pub async fn logout_handler( http_request: HttpRequest, session_cookie_manager: web::Data, mysql_pool: web::Data, + internal_session_cache_purge: web::Data, ) -> Result { + // Best effort to delete Redis session cache + internal_session_cache_purge.best_effort_purge_session_cache(&http_request); + let mut delete_cookie = match http_request.cookie("session") { Some(cookie) => { match session_cookie_manager.decode_session_token(&cookie) { diff --git a/crates/service/web/storyteller_web/Cargo.toml b/crates/service/web/storyteller_web/Cargo.toml index bc0e4b61bc..6767b33240 100644 --- a/crates/service/web/storyteller_web/Cargo.toml +++ b/crates/service/web/storyteller_web/Cargo.toml @@ -52,6 +52,7 @@ user_input_common = { path = "../../../lib/user_input_common" } # Internal Components billing_component = { path = "../../plugins/billing" } +user_traits_component = { path = "../../plugins/user_traits_component" } users_component = { path = "../../plugins/users" } # Workspace hack (faster compile times) diff --git a/crates/service/web/storyteller_web/src/billing/internal_session_cache_purge_impl.rs b/crates/service/web/storyteller_web/src/billing/internal_session_cache_purge_impl.rs index c296ed5296..2dcf6eb523 100644 --- a/crates/service/web/storyteller_web/src/billing/internal_session_cache_purge_impl.rs +++ b/crates/service/web/storyteller_web/src/billing/internal_session_cache_purge_impl.rs @@ -1,8 +1,8 @@ use actix_web::HttpRequest; -use billing_component::stripe::traits::internal_session_cache_purge::InternalSessionCachePurge; use redis_caching::redis_ttl_cache::RedisTtlCache; use redis_common::redis_cache_keys::RedisCacheKeys; +use user_traits_component::traits::internal_session_cache_purge::InternalSessionCachePurge; use users_component::utils::session_checker::SessionChecker; pub struct InternalSessionCachePurgeImpl { diff --git a/crates/service/web/storyteller_web/src/main.rs b/crates/service/web/storyteller_web/src/main.rs index 57f09a82c8..8b815cd6f3 100644 --- a/crates/service/web/storyteller_web/src/main.rs +++ b/crates/service/web/storyteller_web/src/main.rs @@ -20,15 +20,13 @@ use std::sync::Arc; use std::time::Duration; -use actix_cors::Cors; -use actix_web::{App, HttpResponse, HttpServer, middleware, web}; +use actix_web::{App, HttpServer, middleware, web}; use actix_web::middleware::{DefaultHeaders, Logger}; use anyhow::anyhow; use elasticsearch::Elasticsearch; use elasticsearch::http::transport::Transport; use futures::Future; -use limitation::Limiter; -use log::{error, info}; +use log::info; use r2d2_redis::r2d2; use r2d2_redis::redis::Commands; use r2d2_redis::RedisConnectionManager; @@ -48,7 +46,6 @@ use actix_helpers::middleware::disabled_endpoint_filter::disabled_endpoints::exa use actix_helpers::middleware::disabled_endpoint_filter::disabled_endpoints::prefix_disabled_endpoints::PrefixDisabledEndpoints; use billing_component::stripe::stripe_config::{FullUrlOrPath, StripeCheckoutConfigs, StripeConfig, StripeCustomerPortalConfigs, StripeSecrets}; use billing_component::stripe::traits::internal_product_to_stripe_lookup::InternalProductToStripeLookup; -use billing_component::stripe::traits::internal_session_cache_purge::InternalSessionCachePurge; use billing_component::stripe::traits::internal_subscription_product_lookup::InternalSubscriptionProductLookup; use billing_component::stripe::traits::internal_user_lookup::InternalUserLookup; use bootstrap::bootstrap::{bootstrap, BootstrapArgs}; @@ -56,10 +53,9 @@ use cloud_storage::bucket_client::BucketClient; use config::common_env::CommonEnv; use config::shared_constants::DEFAULT_MYSQL_CONNECTION_STRING; use config::shared_constants::DEFAULT_RUST_LOG; -use container_common::files::read_toml_file_to_struct::read_toml_file_to_struct; use email_sender::smtp_email_sender::SmtpEmailSender; use errors::AnyhowResult; -use http_server_common::cors::{build_cors_config, build_production_cors_config}; +use http_server_common::cors::build_cors_config; use memory_caching::single_item_ttl_cache::SingleItemTtlCache; use mysql_queries::mediators::badge_granter::BadgeGranter; use mysql_queries::mediators::firehose_publisher::FirehosePublisher; @@ -67,6 +63,7 @@ use redis_caching::redis_ttl_cache::RedisTtlCache; use reusable_types::server_environment::ServerEnvironment; use twitch_common::twitch_secrets::TwitchSecrets; use url_config::third_party_url_redirector::ThirdPartyUrlRedirector; +use user_traits_component::traits::internal_session_cache_purge::InternalSessionCachePurge; use users_component::cookies::anonymous_visitor_tracking::avt_cookie_manager::AvtCookieManager; use users_component::cookies::session::session_cookie_manager::SessionCookieManager; use users_component::utils::session_checker::SessionChecker; @@ -76,12 +73,11 @@ use crate::billing::internal_session_cache_purge_impl::InternalSessionCachePurge use crate::billing::stripe_internal_subscription_product_lookup_impl::StripeInternalSubscriptionProductLookupImpl; use crate::billing::stripe_internal_user_lookup_impl::StripeInternalUserLookupImpl; use crate::configs::app_startup::redis_rate_limiters::configure_redis_rate_limiters; -use crate::configs::static_api_tokens::{StaticApiTokenConfig, StaticApiTokens, StaticApiTokenSet}; +use crate::configs::static_api_tokens::StaticApiTokenSet; use crate::http_server::middleware::pushback_filter_middleware::PushbackFilter; -use crate::http_server::web_utils::redis_rate_limiter::RedisRateLimiter; use crate::memory_cache::model_token_to_info_cache::ModelTokenToInfoCache; use crate::routes::add_routes; -use crate::server_state::{DurableInMemoryCaches, EnvConfig, EphemeralInMemoryCaches, InMemoryCaches, RedisRateLimiters, ServerInfo, ServerState, StaticFeatureFlags, StripeSettings, TrollBans, TwitchOauth, TwitchOauthSecrets}; +use crate::server_state::{DurableInMemoryCaches, EnvConfig, EphemeralInMemoryCaches, InMemoryCaches, ServerInfo, ServerState, StaticFeatureFlags, StripeSettings, TrollBans, TwitchOauth, TwitchOauthSecrets}; use crate::threads::db_health_checker_thread::db_health_check_status::HealthCheckStatus; use crate::threads::db_health_checker_thread::db_health_checker_thread::db_health_checker_thread; use crate::threads::poll_ip_banlist_thread::poll_ip_bans;