mirror of
https://github.com/actix/actix-extras.git
synced 2025-08-30 19:10:20 +02:00
Make it work with actix-web 4.0.0-beta.7
This commit is contained in:
@@ -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"
|
||||
|
@@ -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<CookieValue>,
|
||||
) -> 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()
|
||||
}
|
||||
|
@@ -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<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
|
||||
S::Future: 'static,
|
||||
T: IdentityPolicy,
|
||||
B: 'static,
|
||||
B: MessageBody + 'static,
|
||||
B::Error: StdError,
|
||||
{
|
||||
type Response = ServiceResponse<B>;
|
||||
type Response = ServiceResponse;
|
||||
type Error = Error;
|
||||
type InitError = ();
|
||||
type Transform = IdentityServiceMiddleware<S, T>;
|
||||
@@ -73,12 +75,13 @@ impl<S, T> Clone for IdentityServiceMiddleware<S, T> {
|
||||
|
||||
impl<S, T, B> Service<ServiceRequest> for IdentityServiceMiddleware<S, T>
|
||||
where
|
||||
B: 'static,
|
||||
B: MessageBody + 'static,
|
||||
B::Error: StdError,
|
||||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
|
||||
S::Future: 'static,
|
||||
T: IdentityPolicy,
|
||||
{
|
||||
type Response = ServiceResponse<B>;
|
||||
type Response = ServiceResponse;
|
||||
type Error = Error;
|
||||
type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;
|
||||
|
||||
@@ -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)),
|
||||
|
Reference in New Issue
Block a user