From 6682fc826fdaa0abe2765c08286ed033edef3d02 Mon Sep 17 00:00:00 2001 From: Andrey Kutejko Date: Mon, 21 Jun 2021 18:00:20 +0200 Subject: [PATCH] Make it work with actix-web 4.0.0-beta.7 --- .github/workflows/ci.yml | 4 +- actix-cors/Cargo.toml | 4 +- actix-cors/src/builder.rs | 11 +++-- actix-cors/src/middleware.rs | 29 ++++++----- actix-identity/Cargo.toml | 6 +-- actix-identity/src/cookie.rs | 30 ++++++------ actix-identity/src/middleware.rs | 19 +++++--- actix-protobuf/Cargo.toml | 2 +- .../examples/prost-example/Cargo.toml | 2 +- actix-protobuf/src/lib.rs | 24 +++++----- actix-redis/Cargo.toml | 18 +++---- actix-redis/examples/authentication.rs | 40 +++++++++------- actix-redis/src/session.rs | 20 ++++---- actix-session/Cargo.toml | 2 +- actix-session/src/cookie.rs | 48 ++++++++++++------- actix-session/src/lib.rs | 7 ++- actix-web-httpauth/Cargo.toml | 2 +- actix-web-httpauth/src/middleware.rs | 19 ++++++-- 18 files changed, 168 insertions(+), 119 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6ea2bb858..e58880b3e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -101,7 +101,7 @@ jobs: - name: Clear the cargo caches run: | - cargo install cargo-cache --no-default-features --features ci-autoclean + cargo install cargo-cache --version 0.6.2 --no-default-features --features ci-autoclean cargo-cache build_and_test_other: @@ -180,5 +180,5 @@ jobs: - name: Clear the cargo caches run: | - cargo install cargo-cache --no-default-features --features ci-autoclean + cargo install cargo-cache --version 0.6.2 --no-default-features --features ci-autoclean cargo-cache diff --git a/actix-cors/Cargo.toml b/actix-cors/Cargo.toml index db3f40bfc..c657d01d4 100644 --- a/actix-cors/Cargo.toml +++ b/actix-cors/Cargo.toml @@ -19,8 +19,8 @@ name = "actix_cors" path = "src/lib.rs" [dependencies] -actix-web = { version = "4.0.0-beta.5", default-features = false } -actix-service = "2.0.0-beta.5" +actix-web = { version = "4.0.0-beta.7", default-features = false } +actix-service = "2" derive_more = "0.99.5" futures-util = { version = "0.3.7", default-features = false } diff --git a/actix-cors/src/builder.rs b/actix-cors/src/builder.rs index c66b7307f..0cf61da75 100644 --- a/actix-cors/src/builder.rs +++ b/actix-cors/src/builder.rs @@ -1,6 +1,10 @@ -use std::{collections::HashSet, convert::TryInto, iter::FromIterator, rc::Rc}; +use std::{ + collections::HashSet, convert::TryInto, error::Error as StdError, + iter::FromIterator, rc::Rc, +}; use actix_web::{ + body::MessageBody, dev::{RequestHead, Service, ServiceRequest, ServiceResponse, Transform}, error::{Error, Result}, http::{self, header::HeaderName, Error as HttpError, HeaderValue, Method, Uri}, @@ -487,9 +491,10 @@ impl Transform for Cors where S: Service, Error = Error>, S::Future: 'static, - B: 'static, + B: MessageBody + 'static, + B::Error: StdError, { - type Response = ServiceResponse; + type Response = ServiceResponse; type Error = Error; type InitError = (); type Transform = CorsMiddleware; diff --git a/actix-cors/src/middleware.rs b/actix-cors/src/middleware.rs index ae522bfab..0de2d0c0e 100644 --- a/actix-cors/src/middleware.rs +++ b/actix-cors/src/middleware.rs @@ -1,6 +1,7 @@ -use std::{convert::TryInto, rc::Rc}; +use std::{convert::TryInto, error::Error as StdError, rc::Rc}; use actix_web::{ + body::{Body, MessageBody}, dev::{Service, ServiceRequest, ServiceResponse}, error::{Error, Result}, http::{ @@ -25,8 +26,13 @@ pub struct CorsMiddleware { pub(crate) inner: Rc, } -impl CorsMiddleware { - fn handle_preflight(inner: &Inner, req: ServiceRequest) -> ServiceResponse { +impl CorsMiddleware +where + S: Service, Error = Error>, + B: MessageBody + 'static, + B::Error: StdError, +{ + fn handle_preflight(inner: &Inner, req: ServiceRequest) -> ServiceResponse { if let Err(err) = inner .validate_origin(req.head()) .and_then(|_| inner.validate_allowed_method(req.head())) @@ -69,11 +75,10 @@ impl CorsMiddleware { } let res = res.finish(); - let res = res.into_body(); req.into_response(res) } - fn augment_response( + fn augment_response( inner: &Inner, mut res: ServiceResponse, ) -> ServiceResponse { @@ -112,20 +117,21 @@ impl CorsMiddleware { } } -type CorsMiddlewareServiceFuture = Either< - Ready, Error>>, - LocalBoxFuture<'static, Result, Error>>, +type CorsMiddlewareServiceFuture = Either< + Ready>, + LocalBoxFuture<'static, Result>, >; impl Service for CorsMiddleware where S: Service, Error = Error>, S::Future: 'static, - B: 'static, + B: MessageBody + 'static, + B::Error: StdError, { - type Response = ServiceResponse; + type Response = ServiceResponse; type Error = Error; - type Future = CorsMiddlewareServiceFuture; + type Future = CorsMiddlewareServiceFuture; actix_service::forward_ready!(service); @@ -157,6 +163,7 @@ where } else { res } + .map(|res| res.map_body(|_, body| Body::from_message(body))) } .boxed_local(); diff --git a/actix-identity/Cargo.toml b/actix-identity/Cargo.toml index a7f123006..69fc11ed9 100644 --- a/actix-identity/Cargo.toml +++ b/actix-identity/Cargo.toml @@ -15,13 +15,13 @@ name = "actix_identity" path = "src/lib.rs" [dependencies] -actix-service = "2.0.0-beta.5" -actix-web = { version = "4.0.0-beta.5", default-features = false, features = ["cookies", "secure-cookies"] } +actix-service = "2" +actix-web = { version = "4.0.0-beta.7", default-features = false, features = ["cookies", "secure-cookies"] } futures-util = { version = "0.3.7", default-features = false } serde = "1.0" serde_json = "1.0" time = "0.2.23" [dev-dependencies] -actix-http = "3.0.0-beta.4" +actix-http = "3.0.0-beta.7" actix-rt = "2" diff --git a/actix-identity/src/cookie.rs b/actix-identity/src/cookie.rs index a78dcc6fb..27a02c369 100644 --- a/actix-identity/src/cookie.rs +++ b/actix-identity/src/cookie.rs @@ -7,7 +7,7 @@ use time::Duration; use actix_web::{ cookie::{Cookie, CookieJar, Key, SameSite}, dev::{ServiceRequest, ServiceResponse}, - error::{Error, Result}, + error::{Error, ErrorInternalServerError, Result}, http::header::{self, HeaderValue}, HttpMessage, }; @@ -69,16 +69,18 @@ impl CookieIdentityInner { value: Option, ) -> Result<()> { let add_cookie = value.is_some(); - let val = value.map(|val| { - if !self.legacy_supported() { - serde_json::to_string(&val) - } else { - Ok(val.identity) - } - }); + let val = value + .map(|val| { + if !self.legacy_supported() { + serde_json::to_string(&val) + } else { + Ok(val.identity) + } + }) + .transpose() + .map_err(ErrorInternalServerError)?; - let mut cookie = - Cookie::new(self.name.clone(), val.unwrap_or_else(|| Ok(String::new()))?); + let mut cookie = Cookie::new(self.name.clone(), val.unwrap_or_default()); cookie.set_path(self.path.clone()); cookie.set_secure(self.secure); cookie.set_http_only(true); @@ -108,10 +110,10 @@ impl CookieIdentityInner { }; if add_cookie { - jar.private(&key).add(cookie); + jar.private_mut(&key).add(cookie); } else { jar.add_original(cookie.clone()); - jar.private(&key).remove(cookie); + jar.private_mut(&key).remove(cookie); } for cookie in jar.delta() { @@ -391,7 +393,7 @@ mod tests { .copied() .collect(); - jar.private(&Key::derive_from(&key)).add(Cookie::new( + jar.private_mut(&Key::derive_from(&key)).add(Cookie::new( COOKIE_NAME, serde_json::to_string(&CookieValue { identity: identity.to_string(), @@ -575,7 +577,7 @@ mod tests { fn legacy_login_cookie(identity: &'static str) -> Cookie<'static> { let mut jar = CookieJar::new(); - jar.private(&Key::derive_from(&COOKIE_KEY_MASTER)) + jar.private_mut(&Key::derive_from(&COOKIE_KEY_MASTER)) .add(Cookie::new(COOKIE_NAME, identity)); jar.get(COOKIE_NAME).unwrap().clone() } diff --git a/actix-identity/src/middleware.rs b/actix-identity/src/middleware.rs index 676d62f57..56998de5a 100644 --- a/actix-identity/src/middleware.rs +++ b/actix-identity/src/middleware.rs @@ -1,6 +1,7 @@ -use std::rc::Rc; +use std::{error::Error as StdError, rc::Rc}; use actix_web::{ + body::{Body, MessageBody}, dev::{Service, ServiceRequest, ServiceResponse, Transform}, Error, HttpMessage, Result, }; @@ -41,9 +42,10 @@ where S: Service, Error = Error> + 'static, S::Future: 'static, T: IdentityPolicy, - B: 'static, + B: MessageBody + 'static, + B::Error: StdError, { - type Response = ServiceResponse; + type Response = ServiceResponse; type Error = Error; type InitError = (); type Transform = IdentityServiceMiddleware; @@ -73,12 +75,13 @@ impl Clone for IdentityServiceMiddleware { impl Service for IdentityServiceMiddleware where - B: 'static, + B: MessageBody + 'static, + B::Error: StdError, S: Service, Error = Error> + 'static, S::Future: 'static, T: IdentityPolicy, { - type Response = ServiceResponse; + type Response = ServiceResponse; type Error = Error; type Future = LocalBoxFuture<'static, Result>; @@ -100,11 +103,13 @@ where if let Some(id) = id { match backend.to_response(id.id, id.changed, &mut res).await { - Ok(_) => Ok(res), + Ok(_) => { + Ok(res.map_body(|_, body| Body::from_message(body))) + } Err(e) => Ok(res.error_response(e)), } } else { - Ok(res) + Ok(res.map_body(|_, body| Body::from_message(body))) } } Err(err) => Ok(req.error_response(err)), diff --git a/actix-protobuf/Cargo.toml b/actix-protobuf/Cargo.toml index 867b0100e..f50030738 100644 --- a/actix-protobuf/Cargo.toml +++ b/actix-protobuf/Cargo.toml @@ -20,7 +20,7 @@ path = "src/lib.rs" [dependencies] actix-rt = "2" -actix-web = { version = "4.0.0-beta.5", default_features = false } +actix-web = { version = "4.0.0-beta.7", default_features = false } derive_more = "0.99.5" futures-util = { version = "0.3.7", default-features = false } prost = "0.7" diff --git a/actix-protobuf/examples/prost-example/Cargo.toml b/actix-protobuf/examples/prost-example/Cargo.toml index 7e5728fff..af986968a 100644 --- a/actix-protobuf/examples/prost-example/Cargo.toml +++ b/actix-protobuf/examples/prost-example/Cargo.toml @@ -8,7 +8,7 @@ authors = [ ] [dependencies] -actix-web = "4.0.0-beta.5" +actix-web = "4.0.0-beta.7" actix-protobuf = { path = "../../" } env_logger = "0.8" diff --git a/actix-protobuf/src/lib.rs b/actix-protobuf/src/lib.rs index 3100c2858..4b24bce18 100644 --- a/actix-protobuf/src/lib.rs +++ b/actix-protobuf/src/lib.rs @@ -12,11 +12,13 @@ use prost::DecodeError as ProtoBufDecodeError; use prost::EncodeError as ProtoBufEncodeError; use prost::Message; -use actix_web::dev::{HttpResponseBuilder, Payload}; -use actix_web::error::{Error, PayloadError, ResponseError}; +use actix_web::dev::Payload; +use actix_web::error::{Error, ErrorBadRequest, ErrorPayloadTooLarge, PayloadError}; use actix_web::http::header::{CONTENT_LENGTH, CONTENT_TYPE}; use actix_web::web::BytesMut; -use actix_web::{FromRequest, HttpMessage, HttpRequest, HttpResponse, Responder}; +use actix_web::{ + FromRequest, HttpMessage, HttpRequest, HttpResponse, HttpResponseBuilder, Responder, +}; use futures_util::future::{FutureExt, LocalBoxFuture}; use futures_util::StreamExt; @@ -39,11 +41,11 @@ pub enum ProtoBufPayloadError { Payload(PayloadError), } -impl ResponseError for ProtoBufPayloadError { - fn error_response(&self) -> HttpResponse { - match *self { - ProtoBufPayloadError::Overflow => HttpResponse::PayloadTooLarge().into(), - _ => HttpResponse::BadRequest().into(), +impl Into for ProtoBufPayloadError { + fn into(self) -> Error { + match self { + ProtoBufPayloadError::Overflow => ErrorPayloadTooLarge(self).into(), + _ => ErrorBadRequest(self).into(), } } } @@ -143,9 +145,7 @@ impl Responder for ProtoBuf { Ok(()) => HttpResponse::Ok() .content_type("application/protobuf") .body(buf), - Err(err) => HttpResponse::from_error(Error::from( - ProtoBufPayloadError::Serialize(err), - )), + Err(err) => HttpResponse::from_error(ProtoBufPayloadError::Serialize(err)), } } } @@ -255,7 +255,7 @@ impl ProtoBufResponseBuilder for HttpResponseBuilder { let mut body = Vec::new(); value .encode(&mut body) - .map_err(ProtoBufPayloadError::Serialize)?; + .map_err(|err| Into::::into(ProtoBufPayloadError::Serialize(err)))?; Ok(self.body(body)) } } diff --git a/actix-redis/Cargo.toml b/actix-redis/Cargo.toml index 1454e9e8f..a41f701d8 100644 --- a/actix-redis/Cargo.toml +++ b/actix-redis/Cargo.toml @@ -31,31 +31,31 @@ web = [ ] [dependencies] -actix = { version = "0.11.0", default-features = false } +actix = { version = "0.12.0", default-features = false } actix-rt = { version = "2.1", default-features = false } -actix-service = "2.0.0-beta.5" +actix-service = "2" actix-tls = { version = "3.0.0-beta.5", default-features = false, features = ["connect"] } log = "0.4.6" -backoff = "0.2.1" +backoff = "0.3.0" derive_more = "0.99.2" futures-core = { version = "0.3.7", default-features = false } -redis2 = { package = "redis", version = "0.19.0", features = ["tokio-comp", "tokio-native-tls-comp"] } -redis-async = { version = "0.8", default-features = false, features = ["tokio10"] } +redis2 = { package = "redis", version = "0.20.0", features = ["tokio-comp", "tokio-native-tls-comp"] } +redis-async = { version = "0.9.2", default-features = false, features = ["tokio10"] } time = "0.2.23" tokio = { version = "1", features = ["sync"] } tokio-util = "0.6.1" # actix-session -actix-web = { version = "4.0.0-beta.5", default_features = false, optional = true } +actix-web = { version = "4.0.0-beta.7", default_features = false, optional = true } actix-session = { version = "0.5.0-beta.1", optional = true } rand = { version = "0.8.0", optional = true } serde = { version = "1.0.101", optional = true } serde_json = { version = "1.0.40", optional = true } [dev-dependencies] -actix-test = "0.1.0-beta.1" -actix-http = "3.0.0-beta.5" +actix-test = "0.1.0-beta.3" +actix-http = "3.0.0-beta.7" actix-rt = "2.1" -env_logger = "0.7" +env_logger = "0.8" serde_derive = "1.0" diff --git a/actix-redis/examples/authentication.rs b/actix-redis/examples/authentication.rs index 16f9f0c8d..31df41cce 100644 --- a/actix-redis/examples/authentication.rs +++ b/actix-redis/examples/authentication.rs @@ -1,7 +1,8 @@ use actix_redis::RedisSession; use actix_session::Session; use actix_web::{ - cookie, middleware, web, App, Error, HttpResponse, HttpServer, Responder, + cookie, error::InternalError, middleware, web, App, Error, HttpResponse, HttpServer, + Responder, }; use serde::{Deserialize, Serialize}; @@ -19,10 +20,10 @@ struct User { } impl User { - fn authenticate(credentials: Credentials) -> Result { + fn authenticate(credentials: Credentials) -> Result { // TODO: figure out why I keep getting hacked if &credentials.password != "hunter2" { - return Err(HttpResponse::Unauthorized().json("Unauthorized")); + return Err(unauthorized()); } Ok(User { @@ -33,29 +34,32 @@ impl User { } } -pub fn validate_session(session: &Session) -> Result { - let user_id: Option = session.get("user_id").unwrap_or(None); +fn unauthorized() -> Error { + InternalError::from_response( + "Unauthorized", + HttpResponse::Unauthorized().json("Unauthorized").into(), + ) + .into() +} - match user_id { - Some(id) => { - // keep the user's session alive - session.renew(); - Ok(id) - } - None => Err(HttpResponse::Unauthorized().json("Unauthorized")), - } +pub fn validate_session(session: &Session) -> Result { + let user_id: i64 = session + .get("user_id") + .unwrap_or(None) + .ok_or_else(unauthorized)?; + // keep the user's session alive + session.renew(); + Ok(user_id) } async fn login( credentials: web::Json, session: Session, -) -> Result { +) -> Result { let credentials = credentials.into_inner(); - match User::authenticate(credentials) { - Ok(user) => session.insert("user_id", user.id).unwrap(), - Err(_) => return Err(HttpResponse::Unauthorized().json("Unauthorized")), - }; + let user = User::authenticate(credentials)?; + session.insert("user_id", user.id).unwrap(); Ok("Welcome!") } diff --git a/actix-redis/src/session.rs b/actix-redis/src/session.rs index 55a25e87f..f4fb151b7 100644 --- a/actix-redis/src/session.rs +++ b/actix-redis/src/session.rs @@ -6,7 +6,7 @@ use actix_session::{Session, SessionStatus}; use actix_web::cookie::{Cookie, CookieJar, Key, SameSite}; use actix_web::dev::{ServiceRequest, ServiceResponse}; use actix_web::http::header::{self, HeaderValue}; -use actix_web::{error, Error, HttpMessage}; +use actix_web::{error, Error}; use futures_core::future::LocalBoxFuture; use rand::{distributions::Alphanumeric, rngs::OsRng, Rng}; use redis_async::resp::RespValue; @@ -311,7 +311,7 @@ impl Inner { // set cookie let mut jar = CookieJar::new(); - jar.signed(&self.key).add(cookie); + jar.signed_mut(&self.key).add(cookie); (value, Some(jar)) }; @@ -320,10 +320,8 @@ impl Inner { let state: HashMap<_, _> = state.collect(); - let body = match serde_json::to_string(&state) { - Err(e) => return Err(e.into()), - Ok(body) => body, - }; + let body = + serde_json::to_string(&state).map_err(error::ErrorInternalServerError)?; let cmd = Command(resp_array!["SET", cache_key, body, "EX", &self.ttl]); @@ -444,9 +442,9 @@ mod test { let id: Option = session.get("user_id")?; if let Some(x) = id { session.purge(); - Ok(format!("Logged out: {}", x).into()) + Ok(HttpResponse::Ok().body(format!("Logged out: {}", x))) } else { - Ok("Could not log out anonymous user".into()) + Ok(HttpResponse::Ok().body("Could not log out anonymous user")) } } @@ -648,7 +646,11 @@ mod test { .unwrap(); assert_ne!( OffsetDateTime::now_utc().year(), - cookie_4.expires().map(|t| t.year()).unwrap() + cookie_4 + .expires() + .and_then(|e| e.datetime()) + .map(|t| t.year()) + .unwrap() ); // Step 10: GET index, including session cookie #2 in request diff --git a/actix-session/Cargo.toml b/actix-session/Cargo.toml index 670a92265..691f940ba 100644 --- a/actix-session/Cargo.toml +++ b/actix-session/Cargo.toml @@ -20,7 +20,7 @@ default = ["cookie-session"] cookie-session = ["actix-web/secure-cookies"] [dependencies] -actix-web = { version = "4.0.0-beta.5", default_features = false, features = ["cookies"] } +actix-web = { version = "4.0.0-beta.7", default_features = false, features = ["cookies"] } actix-service = "2.0.0-beta.5" derive_more = "0.99.5" diff --git a/actix-session/src/cookie.rs b/actix-session/src/cookie.rs index b656e494f..57898c560 100644 --- a/actix-session/src/cookie.rs +++ b/actix-session/src/cookie.rs @@ -1,12 +1,13 @@ //! Cookie based sessions. See docs for [`CookieSession`]. -use std::{collections::HashMap, rc::Rc}; +use std::{collections::HashMap, error::Error as StdError, rc::Rc}; use actix_service::{Service, Transform}; +use actix_web::body::{Body, MessageBody}; use actix_web::cookie::{Cookie, CookieJar, Key, SameSite}; use actix_web::dev::{ServiceRequest, ServiceResponse}; use actix_web::http::{header::SET_COOKIE, HeaderValue}; -use actix_web::{Error, HttpMessage, ResponseError}; +use actix_web::{Error, ResponseError}; use derive_more::Display; use futures_util::future::{ok, LocalBoxFuture, Ready}; use serde_json::error::Error as JsonError; @@ -106,8 +107,8 @@ impl CookieSessionInner { let mut jar = CookieJar::new(); match self.security { - CookieSecurity::Signed => jar.signed(&self.key).add(cookie), - CookieSecurity::Private => jar.private(&self.key).add(cookie), + CookieSecurity::Signed => jar.signed_mut(&self.key).add(cookie), + CookieSecurity::Private => jar.private_mut(&self.key).add(cookie), } for cookie in jar.delta() { @@ -292,13 +293,15 @@ impl CookieSession { } } -impl Transform for CookieSession +impl Transform for CookieSession where S: Service>, S::Future: 'static, - S::Error: 'static, + S::Error: StdError + 'static, + B: MessageBody + 'static, + B::Error: StdError, { - type Response = ServiceResponse; + type Response = ServiceResponse; type Error = S::Error; type InitError = (); type Transform = CookieSessionMiddleware; @@ -318,13 +321,15 @@ pub struct CookieSessionMiddleware { inner: Rc, } -impl Service for CookieSessionMiddleware +impl Service for CookieSessionMiddleware where S: Service>, S::Future: 'static, S::Error: 'static, + B: MessageBody + 'static, + B::Error: StdError, { - type Response = ServiceResponse; + type Response = ServiceResponse; type Error = S::Error; type Future = LocalBoxFuture<'static, Result>; @@ -346,32 +351,35 @@ where Box::pin(async move { let mut res = fut.await?; - let res = match Session::get_changes(&mut res) { + let result = match Session::get_changes(&mut res) { (SessionStatus::Changed, state) | (SessionStatus::Renewed, state) => { - res.checked_expr(|res| inner.set_cookie(res, state)) + inner.set_cookie(&mut res, state) } (SessionStatus::Unchanged, state) if prolong_expiration => { - res.checked_expr(|res| inner.set_cookie(res, state)) + inner.set_cookie(&mut res, state) } // set a new session cookie upon first request (new client) (SessionStatus::Unchanged, _) => { if is_new { let state: HashMap = HashMap::new(); - res.checked_expr(|res| inner.set_cookie(res, state.into_iter())) + inner.set_cookie(&mut res, state.into_iter()) } else { - res + Ok(()) } } (SessionStatus::Purged, _) => { let _ = inner.remove_cookie(&mut res); - res + Ok(()) } }; - Ok(res) + match result { + Ok(_) => Ok(res.map_body(|_, body| Body::from_message(body))), + Err(error) => Ok(res.error_response(error)), + } }) } } @@ -533,7 +541,9 @@ mod tests { .find(|c| c.name() == "actix-session") .expect("Cookie is set") .expires() - .expect("Expiration is set"); + .expect("Expiration is set") + .datetime() + .expect("Expiration is a datetime"); actix_rt::time::sleep(std::time::Duration::from_secs(1)).await; @@ -545,7 +555,9 @@ mod tests { .find(|c| c.name() == "actix-session") .expect("Cookie is set") .expires() - .expect("Expiration is set"); + .expect("Expiration is set") + .datetime() + .expect("Expiration is a datetime"); assert!(expires_2 - expires_1 >= Duration::seconds(1)); } diff --git a/actix-session/src/lib.rs b/actix-session/src/lib.rs index db500003a..ac9e38b3e 100644 --- a/actix-session/src/lib.rs +++ b/actix-session/src/lib.rs @@ -51,6 +51,7 @@ use std::{ use actix_web::{ dev::{Extensions, Payload, RequestHead, ServiceRequest, ServiceResponse}, + error::ErrorInternalServerError, Error, FromRequest, HttpMessage, HttpRequest, }; use futures_util::future::{ok, Ready}; @@ -148,7 +149,9 @@ impl Session { /// Get a `value` from the session. pub fn get(&self, key: &str) -> Result, Error> { if let Some(s) = self.0.borrow().state.get(key) { - Ok(Some(serde_json::from_str(s)?)) + Ok(Some( + serde_json::from_str(s).map_err(ErrorInternalServerError)?, + )) } else { Ok(None) } @@ -174,7 +177,7 @@ impl Session { if inner.status != SessionStatus::Purged { inner.status = SessionStatus::Changed; - let val = serde_json::to_string(&value)?; + let val = serde_json::to_string(&value).map_err(ErrorInternalServerError)?; inner.state.insert(key.into(), val); } diff --git a/actix-web-httpauth/Cargo.toml b/actix-web-httpauth/Cargo.toml index db45f9650..99934cf95 100644 --- a/actix-web-httpauth/Cargo.toml +++ b/actix-web-httpauth/Cargo.toml @@ -20,7 +20,7 @@ name = "actix_web_httpauth" path = "src/lib.rs" [dependencies] -actix-web = { version = "4.0.0-beta.5", default_features = false } +actix-web = { version = "4.0.0-beta.7", default_features = false } actix-service = "2.0.0-beta.5" base64 = "0.13" futures-util = { version = "0.3.7", default-features = false } diff --git a/actix-web-httpauth/src/middleware.rs b/actix-web-httpauth/src/middleware.rs index 6f42299fa..b62946a70 100644 --- a/actix-web-httpauth/src/middleware.rs +++ b/actix-web-httpauth/src/middleware.rs @@ -1,8 +1,12 @@ //! HTTP Authentication middleware. -use std::{future::Future, marker::PhantomData, pin::Pin, rc::Rc, sync::Arc}; +use std::{ + error::Error as StdError, future::Future, marker::PhantomData, pin::Pin, rc::Rc, + sync::Arc, +}; use actix_web::{ + body::{Body, MessageBody}, dev::{Service, ServiceRequest, ServiceResponse, Transform}, Error, }; @@ -120,8 +124,10 @@ where F: Fn(ServiceRequest, T) -> O + 'static, O: Future> + 'static, T: AuthExtractor + 'static, + B: MessageBody + 'static, + B::Error: StdError, { - type Response = ServiceResponse; + type Response = ServiceResponse; type Error = Error; type Transform = AuthenticationMiddleware; type InitError = (); @@ -153,10 +159,12 @@ where F: Fn(ServiceRequest, T) -> O + 'static, O: Future> + 'static, T: AuthExtractor + 'static, + B: MessageBody + 'static, + B::Error: StdError, { - type Response = ServiceResponse; + type Response = ServiceResponse; type Error = S::Error; - type Future = LocalBoxFuture<'static, Result, Error>>; + type Future = LocalBoxFuture<'static, Result>; actix_service::forward_ready!(service); @@ -177,7 +185,8 @@ where // middleware to do their thing (eg. cors adding headers) let req = process_fn(req, credentials).await?; - service.call(req).await + let res = service.call(req).await?; + Ok(res.map_body(|_, body| Body::from_message(body))) } .boxed_local() }