Skip to content

Commit

Permalink
Switch to tower-cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
lnicola committed Oct 14, 2023
1 parent 4388f1a commit 92857ac
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 21 deletions.
150 changes: 137 additions & 13 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ tracing-subscriber = "0.2"
url = "2.2"
walkdir = "2.3"
async-trait = "0.1.73"
tower-cookies = "0.9.0"

[features]
default = ["bundled-windows"]
Expand Down
16 changes: 8 additions & 8 deletions src/authentication.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::convert::Infallible;
use std::sync::Arc;

use crate::db::SqliteStore;
Expand All @@ -7,9 +6,10 @@ use crate::RustyShare;
use async_trait::async_trait;
use axum::extract::{FromRef, FromRequestParts};
use axum::response::Response;
use headers::{Cookie, HeaderMapExt};
use http::request::Parts;
use http::StatusCode;
use http::Uri;
use tower_cookies::Cookies;

pub enum Authentication {
User(i32, String),
Expand All @@ -19,12 +19,12 @@ pub enum Authentication {
fn check_session(
store: &Option<SqliteStore>,
uri: &Uri,
cookie: Option<Cookie>,
cookies: &Cookies,
) -> Result<(i32, String), Response> {
let r = if let Some(store) = store {
let redirect = || response::login_redirect(uri, false);
let cookie = cookie.ok_or_else(redirect)?;
let session_id = cookie.get("sid").ok_or_else(redirect)?;
let cookie = cookies.get("sid").ok_or_else(redirect)?;
let session_id = cookie.value();
let session_id = hex::decode(session_id).map_err(|e| {
tracing::error!("{}", e);
redirect()
Expand All @@ -48,13 +48,13 @@ where
Arc<RustyShare>: FromRef<S>,
S: Send + Sync,
{
type Rejection = Infallible;
type Rejection = (StatusCode, &'static str);

async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
let rusty_share = Arc::<RustyShare>::from_ref(state);
let store = &rusty_share.store;
let cookie = parts.headers.typed_get::<Cookie>();
let authentication = match check_session(store, &parts.uri, cookie) {
let cookies = Cookies::from_request_parts(parts, state).await?;
let authentication = match check_session(store, &parts.uri, &cookies) {
Ok((user_id, name)) => Authentication::User(user_id, name),
Err(response) => Authentication::Error(response),
};
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use tokio::io::AsyncWriteExt;
use tokio::runtime::Runtime;
use tokio::sync::mpsc;
use tokio::task;
use tower_cookies::CookieManagerLayer;
use tower_http::services::ServeFile;
use tower_http::trace::{DefaultOnResponse, TraceLayer};
use tracing::Level;
Expand Down Expand Up @@ -634,6 +635,7 @@ async fn run() -> Result<(), Error> {
.put(upload),
)
.with_state(state)
.layer(CookieManagerLayer::new())
.layer(
TraceLayer::new_for_http()
.on_response(DefaultOnResponse::new().level(Level::INFO)),
Expand Down

0 comments on commit 92857ac

Please sign in to comment.