1
0
mirror of https://github.com/actix/actix-extras.git synced 2025-06-26 10:27:42 +02:00

session, redis, and httpauth pre-v4 releases (#162)

This commit is contained in:
Rob Ede
2021-03-21 09:38:29 +00:00
committed by GitHub
parent 8d635f71fb
commit 5a72dd33d5
18 changed files with 113 additions and 159 deletions

View File

@ -1,8 +1,14 @@
# Changes
## Unreleased - 2020-xx-xx
* `Session::set_session` takes a `IntoIterator` instead of `Iterator`
* Fix calls to `session.purge()` from paths other than the one specified in the cookie
## 0.4.1 - 2020-03-21
* `Session::set_session` takes a `IntoIterator` instead of `Iterator`. [#105]
* Fix calls to `session.purge()` from paths other than the one specified in the cookie. [#129]
[#105]: https://github.com/actix/actix-extras/pull/105
[#129]: https://github.com/actix/actix-extras/pull/129
## 0.4.0 - 2020-09-11

View File

@ -1,6 +1,6 @@
[package]
name = "actix-session"
version = "0.4.0"
version = "0.4.1"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Sessions for Actix web"
readme = "README.md"
@ -23,10 +23,10 @@ cookie-session = ["actix-web/secure-cookies"]
actix-web = { version = "3.0.0", default_features = false }
actix-service = "1.0.6"
derive_more = "0.99.2"
futures-util = { version = "0.3.4", default-features = false }
futures-util = { version = "0.3.7", default-features = false }
serde = "1.0"
serde_json = "1.0"
time = { version = "0.2.7", default-features = false, features = ["std"] }
time = { version = "0.2.23", default-features = false, features = ["std"] }
[dev-dependencies]
actix-rt = "1"

View File

@ -1,19 +1,15 @@
# actix-session
[![crates.io](https://img.shields.io/crates/v/actix-session)](https://crates.io/crates/actix-session)
[![Documentation](https://docs.rs/actix-session/badge.svg)](https://docs.rs/actix-session)
[![Dependency Status](https://deps.rs/crate/actix-session/0.3.0/status.svg)](https://deps.rs/crate/actix-session/0.3.0)
[![Build Status](https://travis-ci.org/actix/actix-session.svg?branch=master)](https://travis-ci.org/actix/actix-session)
[![codecov](https://codecov.io/gh/actix/actix-session/branch/master/graph/badge.svg)](https://codecov.io/gh/actix/actix-session)
> Sessions for Actix Web.
[![crates.io](https://img.shields.io/crates/v/actix-session?label=latest)](https://crates.io/crates/actix-session)
[![Documentation](https://docs.rs/actix-session/badge.svg?version=0.4.1)](https://docs.rs/actix-session/0.4.1)
![Apache 2.0 or MIT licensed](https://img.shields.io/crates/l/actix-session)
[![Join the chat at https://gitter.im/actix/actix](https://badges.gitter.im/actix/actix.svg)](https://gitter.im/actix/actix?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[![Dependency Status](https://deps.rs/crate/actix-session/0.4.1/status.svg)](https://deps.rs/crate/actix-session/0.4.1)
> Session for actix-web framework.
## Documentation & community resources
## Documentation & Resources
* [User Guide](https://actix.rs/docs/)
* [API Documentation](https://docs.rs/actix-session/)
* [Chat on gitter](https://gitter.im/actix/actix)
* Cargo package: [actix-session](https://crates.io/crates/actix-session)
* Minimum supported Rust version: 1.40 or later
- [API Documentation](https://docs.rs/actix-session)
- [Example Projects](https://github.com/actix/examples/tree/HEAD/session)
- Minimum Supported Rust Version (MSRV): 1.42.0

View File

@ -1,19 +1,4 @@
//! Cookie session.
//!
//! [**CookieSession**](struct.CookieSession.html)
//! uses cookies as session storage. `CookieSession` creates sessions
//! which are limited to storing fewer than 4000 bytes of data, as the payload
//! must fit into a single cookie. An internal server error is generated if a
//! session contains more than 4000 bytes.
//!
//! A cookie may have a security policy of *signed* or *private*. Each has
//! a respective `CookieSession` constructor.
//!
//! A *signed* cookie may be viewed but not modified by the client. A *private*
//! cookie may neither be viewed nor modified by the client.
//!
//! The constructors take a key as an argument. This is the private key
//! for cookie session - when this value is changed, all session data is lost.
//! Cookie based sessions. See docs for [`CookieSession`].
use std::collections::HashMap;
use std::rc::Rc;
@ -180,7 +165,7 @@ impl CookieSessionInner {
/// than 4000 bytes.
///
/// A cookie may have a security policy of *signed* or *private*. Each has a
/// respective `CookieSessionBackend` constructor.
/// respective `CookieSession` constructor.
///
/// A *signed* cookie is stored on the client as plaintext alongside
/// a signature such that the cookie may be viewed but not modified by the
@ -198,9 +183,8 @@ impl CookieSessionInner {
/// By default all cookies are percent encoded, but certain symbols may
/// cause troubles when reading cookie, if they are not properly percent encoded.
///
/// # Example
///
/// ```rust
/// # Examples
/// ```
/// use actix_session::CookieSession;
/// use actix_web::{web, App, HttpResponse, HttpServer};
///
@ -215,7 +199,7 @@ impl CookieSessionInner {
pub struct CookieSession(Rc<CookieSessionInner>);
impl CookieSession {
/// Construct new *signed* `CookieSessionBackend` instance.
/// Construct new *signed* `CookieSession` instance.
///
/// Panics if key length is less than 32 bytes.
pub fn signed(key: &[u8]) -> CookieSession {
@ -225,7 +209,7 @@ impl CookieSession {
)))
}
/// Construct new *private* `CookieSessionBackend` instance.
/// Construct new *private* `CookieSession` instance.
///
/// Panics if key length is less than 32 bytes.
pub fn private(key: &[u8]) -> CookieSession {

View File

@ -1,18 +1,16 @@
//! User sessions.
//! Sessions for Actix Web.
//!
//! Actix provides a general solution for session management. Session
//! middlewares could provide different implementations which could
//! be accessed via general session api.
//! Provides a general solution for session management. Session middleware could provide different
//! implementations which could be accessed via general session API.
//!
//! By default, only cookie session backend is implemented. Other
//! backend implementations can be added.
//! This crate provides a general solution for session management and includes a cookie backend.
//! Other backend implementations can be built to use persistent or key-value stores, for example.
//!
//! In general, you insert a *session* middleware and initialize it
//! , such as a `CookieSessionBackend`. To access session data,
//! [*Session*](struct.Session.html) extractor must be used. Session
//! extractor allows us to get or set session data.
//! In general, some session middleware, such as a [`CookieSession`] is initialized and applied.
//! To access session data, the [`Session`] extractor must be used. This extractor allows reading
//! modifying session data.
//!
//! ```rust,no_run
//! ```no_run
//! use actix_web::{web, App, HttpServer, HttpResponse, Error};
//! use actix_session::{Session, CookieSession};
//!
@ -20,7 +18,7 @@
//! // access session data
//! if let Some(count) = session.get::<i32>("counter")? {
//! println!("SESSION value: {}", count);
//! session.set("counter", count+1)?;
//! session.set("counter", count + 1)?;
//! } else {
//! session.set("counter", 1)?;
//! }
@ -31,12 +29,11 @@
//! #[actix_rt::main]
//! async fn main() -> std::io::Result<()> {
//! HttpServer::new(
//! || App::new().wrap(
//! CookieSession::signed(&[0; 32]) // <- create cookie based session middleware
//! .secure(false)
//! )
//! .service(web::resource("/").to(|| HttpResponse::Ok())))
//! .bind("127.0.0.1:59880")?
//! || App::new()
//! // create cookie based session middleware
//! .wrap(CookieSession::signed(&[0; 32]).secure(false))
//! .default_service(web::to(|| HttpResponse::Ok())))
//! .bind(("127.0.0.1", 8080))?
//! .run()
//! .await
//! }
@ -44,17 +41,14 @@
#![deny(rust_2018_idioms)]
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
use std::{cell::RefCell, collections::HashMap, rc::Rc};
use actix_web::dev::{
Extensions, Payload, RequestHead, ServiceRequest, ServiceResponse,
};
use actix_web::{Error, FromRequest, HttpMessage, HttpRequest};
use futures_util::future::{ok, Ready};
use serde::de::DeserializeOwned;
use serde::Serialize;
use serde::{de::DeserializeOwned, Serialize};
#[cfg(feature = "cookie-session")]
mod cookie;
@ -63,16 +57,14 @@ pub use crate::cookie::CookieSession;
/// The high-level interface you use to modify session data.
///
/// Session object could be obtained with
/// [`UserSession::get_session`](trait.UserSession.html#tymethod.get_session)
/// method. The `UserSession` trait is implemented for `HttpRequest`, `ServiceRequest`, and
/// `RequestHead`.
/// Session object is obtained with [`UserSession::get_session`]. The [`UserSession`] trait is
/// implemented for `HttpRequest`, `ServiceRequest`, and `RequestHead`.
///
/// ```rust
/// ```
/// use actix_session::Session;
/// use actix_web::*;
/// use actix_web::Result;
///
/// fn index(session: Session) -> Result<&'static str> {
/// async fn index(session: Session) -> Result<&'static str> {
/// // access session data
/// if let Some(count) = session.get::<i32>("counter")? {
/// session.set("counter", count + 1)?;
@ -82,11 +74,10 @@ pub use crate::cookie::CookieSession;
///
/// Ok("Welcome!")
/// }
/// # fn main() {}
/// ```
pub struct Session(Rc<RefCell<SessionInner>>);
/// Helper trait that allows to get session
/// Extraction of a [`Session`] object.
pub trait UserSession {
fn get_session(&self) -> Session;
}
@ -188,12 +179,10 @@ impl Session {
/// Values that match keys already existing on the session will be overwritten. Values should
/// already be JSON serialized.
///
/// # Example
///
/// # Examples
/// ```
/// # use actix_session::Session;
/// # use actix_web::test;
/// #
/// let mut req = test::TestRequest::default().to_srv_request();
///
/// Session::set_session(