Skip to content

Commit

Permalink
feat: auth cache (#643)
Browse files Browse the repository at this point in the history
* feat: initial commit of auth cache

currently called directly in `convert_key`, should be a generic layer usable by other handlers as well

* fix: expiration logic

* refactor: switch dashmap to ttlc_cache

* feat: rewrite the cache as a tower service

* feat: add cache layer to convert_cookie

* feat: cachemanagement trait

* feat: refactor layer to be applied to router not specific handlers

* refactor: move comment

* feat: set cache in cachelayer, invalidate cached jwt on logout

* feat: error handling in the cache layer

* feat: implement cache layer on gateway

* refactor: remove the cache from auth

* refactor: revert changes needed for cache in auth

* feat: invalidate jwt on logout calls

* refactor: clean up logout cache invalidation

* refactor: remove cache from shared state

* refactor: remove comment

* feat: add prepare.sh to auth

* feat: move cache to auth layer

* feat: invalidate cache on logout

also comment out broken test and add TODO to it, and revert auth manifest changes

* refactor: error handling in extract expiration

* refactor: remove cache-layer, add comment

* docs: add comment about logout cache invalidation

* refactor: cachemanager new fn, remove comment

* fix: make sure cookie is shuttle cookie

* feat: add buffer to cache expiration

* fix: fmt
  • Loading branch information
oddgrd authored Feb 27, 2023
1 parent 5187f6a commit 6686657
Show file tree
Hide file tree
Showing 10 changed files with 281 additions and 113 deletions.
25 changes: 13 additions & 12 deletions Cargo.lock

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

8 changes: 4 additions & 4 deletions auth/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ axum = { workspace = true, features = ["headers"] }
axum-sessions = "0.4.1"
clap = { workspace = true }
http = { workspace = true }
jsonwebtoken = { workspace = true}
jsonwebtoken = { workspace = true }
opentelemetry = { workspace = true }
opentelemetry-datadog = { workspace = true }
rand = { workspace = true }
ring = { workspace = true }
serde = { workspace = true, features = [ "derive" ] }
sqlx = { version = "0.6.2", features = [ "sqlite", "json", "runtime-tokio-native-tls", "migrate" ] }
serde = { workspace = true, features = ["derive"] }
sqlx = { version = "0.6.2", features = ["sqlite", "json", "runtime-tokio-native-tls", "migrate"] }
strum = { workspace = true }
thiserror = { workspace = true }
tokio = { version = "1.22.0", features = [ "full" ] }
tokio = { version = "1.22.0", features = ["full"] }
tracing = { workspace = true }
tracing-opentelemetry = { workspace = true }
tracing-subscriber = { workspace = true, features = ["env-filter"] }
Expand Down
9 changes: 6 additions & 3 deletions auth/src/api/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use tracing::field;
use crate::{
secrets::{EdDsaManager, KeyManager},
user::{UserManagement, UserManager},
COOKIE_EXPIRATION,
};

use super::handlers::{
Expand Down Expand Up @@ -102,7 +103,7 @@ impl ApiBuilder {
self.session_layer = Some(
SessionLayer::new(store, &secret)
.with_cookie_name("shuttle.sid")
.with_session_ttl(Some(std::time::Duration::from_secs(60 * 60 * 24))) // One day
.with_session_ttl(Some(COOKIE_EXPIRATION))
.with_secure(true),
);

Expand All @@ -116,10 +117,12 @@ impl ApiBuilder {
let user_manager = UserManager { pool };
let key_manager = EdDsaManager::new();

self.router.layer(session_layer).with_state(RouterState {
let state = RouterState {
user_manager: Arc::new(Box::new(user_manager)),
key_manager: Arc::new(Box::new(key_manager)),
})
};

self.router.layer(session_layer).with_state(state)
}
}

Expand Down
16 changes: 8 additions & 8 deletions auth/src/api/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub(crate) async fn login(
.expect("to set account name");
session
.insert("account_tier", user.account_tier)
.expect("to set account name");
.expect("to set account tier");

Ok(Json(user.into()))
}
Expand All @@ -74,9 +74,9 @@ pub(crate) async fn convert_cookie(

let claim = Claim::new(account_name, account_tier.into());

let response = shuttle_common::backends::auth::ConvertResponse {
token: claim.into_token(key_manager.private_key())?,
};
let token = claim.into_token(key_manager.private_key())?;

let response = shuttle_common::backends::auth::ConvertResponse { token };

Ok(Json(response))
}
Expand All @@ -92,15 +92,15 @@ pub(crate) async fn convert_key(
let User {
name, account_tier, ..
} = user_manager
.get_user_by_key(key)
.get_user_by_key(key.clone())
.await
.map_err(|_| StatusCode::UNAUTHORIZED)?;

let claim = Claim::new(name.to_string(), account_tier.into());

let response = shuttle_common::backends::auth::ConvertResponse {
token: claim.into_token(key_manager.private_key())?,
};
let token = claim.into_token(key_manager.private_key())?;

let response = shuttle_common::backends::auth::ConvertResponse { token };

Ok(Json(response))
}
Expand Down
4 changes: 3 additions & 1 deletion auth/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod error;
mod secrets;
mod user;

use std::{io, str::FromStr};
use std::{io, str::FromStr, time::Duration};

use args::StartArgs;
use sqlx::{
Expand All @@ -22,6 +22,8 @@ use crate::{
pub use api::ApiBuilder;
pub use args::{Args, Commands, InitArgs};

pub const COOKIE_EXPIRATION: Duration = Duration::from_secs(60 * 60 * 24); // One day

pub static MIGRATIONS: Migrator = sqlx::migrate!("./migrations");

pub async fn start(pool: SqlitePool, args: StartArgs) -> io::Result<()> {
Expand Down
2 changes: 1 addition & 1 deletion common/src/backends/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ pub struct ConvertResponse {
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
pub struct Claim {
/// Expiration time (as UTC timestamp).
exp: usize,
pub exp: usize,
/// Issued at (as UTC timestamp).
iat: usize,
/// Issuer.
Expand Down
Loading

0 comments on commit 6686657

Please sign in to comment.