1
0
mirror of https://github.com/actix/actix-extras.git synced 2025-02-21 10:04:49 +01:00

103 lines
3.8 KiB
Rust
Raw Normal View History

//! Identity management for Actix Web.
2019-03-09 18:04:40 -08:00
//!
//! `actix-identity` can be used to track identity of a user across multiple requests. It is built
//! on top of HTTP sessions, via [`actix-session`](https://docs.rs/actix-session).
2019-03-09 18:04:40 -08:00
//!
//! # Getting started
//! To start using identity management in your Actix Web application you must register
//! [`IdentityMiddleware`] and `SessionMiddleware` as middleware on your `App`:
2019-03-09 18:04:40 -08:00
//!
//! ```no_run
//! # use actix_web::web;
//! use actix_web::{cookie::Key, App, HttpServer, HttpResponse};
//! use actix_identity::IdentityMiddleware;
//! use actix_session::{storage::RedisSessionStore, SessionMiddleware};
2019-03-09 18:04:40 -08:00
//!
//! #[actix_web::main]
//! async fn main() {
//! let secret_key = Key::generate();
//! let redis_store = RedisSessionStore::new("redis://127.0.0.1:6379")
//! .await
//! .unwrap();
//!
//! HttpServer::new(move || {
//! App::new()
//! // Install the identity framework first.
//! .wrap(IdentityMiddleware::default())
//! // The identity system is built on top of sessions. You must install the session
//! // middleware to leverage `actix-identity`. The session middleware must be mounted
//! // AFTER the identity middleware: `actix-web` invokes middleware in the OPPOSITE
//! // order of registration when it receives an incoming request.
//! .wrap(SessionMiddleware::new(
//! redis_store.clone(),
//! secret_key.clone()
//! ))
//! // Your request handlers [...]
//! # .default_service(web::to(|| HttpResponse::Ok()))
//! })
//! # ;
//! }
2022-03-01 04:23:51 +00:00
//! ```
//!
//! User identities can be created, accessed and destroyed using the [`Identity`] extractor in your
//! request handlers:
//!
//! ```no_run
//! use actix_web::{get, post, HttpResponse, Responder, HttpRequest, HttpMessage};
//! use actix_identity::Identity;
//! use actix_session::storage::RedisSessionStore;
2019-03-09 18:04:40 -08:00
//!
2021-03-23 05:05:03 +00:00
//! #[get("/")]
//! async fn index(user: Option<Identity>) -> impl Responder {
//! if let Some(user) = user {
//! format!("Welcome! {}", user.id().unwrap())
2019-03-09 18:04:40 -08:00
//! } else {
2019-03-09 20:40:09 -08:00
//! "Welcome Anonymous!".to_owned()
2019-03-09 18:04:40 -08:00
//! }
//! }
//!
2021-03-23 05:05:03 +00:00
//! #[post("/login")]
//! async fn login(request: HttpRequest) -> impl Responder {
//! // Some kind of authentication should happen here
//! // e.g. password-based, biometric, etc.
//! // [...]
//!
//! // attach a verified user identity to the active session
//! Identity::login(&request.extensions(), "User1".into()).unwrap();
//!
//! HttpResponse::Ok()
2019-03-09 18:04:40 -08:00
//! }
//!
2021-03-23 05:05:03 +00:00
//! #[post("/logout")]
//! async fn logout(user: Identity) -> impl Responder {
//! user.logout();
//! HttpResponse::Ok()
2019-03-09 18:04:40 -08:00
//! }
//! ```
2019-03-09 18:04:40 -08:00
//!
//! # Advanced configuration
//! By default, `actix-identity` does not automatically log out users. You can change this behaviour
//! by customising the configuration for [`IdentityMiddleware`] via [`IdentityMiddleware::builder`].
2022-03-01 04:23:51 +00:00
//!
//! In particular, you can automatically log out users who:
//! - have been inactive for a while (see [`IdentityMiddlewareBuilder::visit_deadline`];
//! - logged in too long ago (see [`IdentityMiddlewareBuilder::login_deadline`]).
//!
//! [`IdentityMiddlewareBuilder::visit_deadline`]: config::IdentityMiddlewareBuilder::visit_deadline
//! [`IdentityMiddlewareBuilder::login_deadline`]: config::IdentityMiddlewareBuilder::login_deadline
2020-07-06 15:47:22 +09:00
2022-07-09 20:05:47 +01:00
#![forbid(unsafe_code)]
#![deny(rust_2018_idioms, nonstandard_style, missing_docs)]
2021-12-08 06:11:13 +00:00
#![warn(future_incompatible)]
2023-11-03 19:46:12 +00:00
#![doc(html_logo_url = "https://actix.rs/img/logo.png")]
#![doc(html_favicon_url = "https://actix.rs/favicon.ico")]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
2020-07-06 15:47:22 +09:00
pub mod config;
Feature: Add IdentityError to actix-identity crate. (#296) * Add IdentityError to actix-identity crate. In order to let crates in the actix web ecosystem interact correctly with `actix_web::Error`, this commit introduces its own error type, replacing the previous usage of `anyhow::Error`. * Mend some clippy warnings on IdentityError. * Split identity error into more granular versions. - `MissingIdentityError` occurs whenever we attempt to gather information about an identity from a session, and fail. - `LoginError` occurs whenever we attempt to login via an identity, and fail. * Feedback for identity error implementation. - `IdentityError` -> `GetIdentityError` - Move error messages into Display impl where appropriate - Split `id` and `get_identity` errors into two types - Implement `source` on custom errors * Expand identity error types with struct markers. In order to get a little more future compatibility and reduce abstraction leaking, this commit introduces some contextual structs to our identity errors package. * Improve doc message for SessionExpiryError. Co-authored-by: Luca Palmieri <20745048+LukeMathWalker@users.noreply.github.com> * Improve identity error docs and messaging. Co-authored-by: Luca Palmieri <20745048+LukeMathWalker@users.noreply.github.com> * Expand LostIdentityError with placeholder. Adds a placeholder unit struct to the LostIdentityError variant of GetIdentityError, which should let us expand on that variant with extra context later if we like. * Add From coercion for LostIdentityError. Improve the ergonomics of using the LostIdentityError unit struct. * Update Cargo.toml * Update CHANGES.md * expose identity error module * fix error impl Co-authored-by: Luca Palmieri <20745048+LukeMathWalker@users.noreply.github.com> Co-authored-by: Rob Ede <robjtede@icloud.com>
2023-01-06 20:05:12 -06:00
pub mod error;
2021-03-23 05:05:03 +00:00
mod identity;
mod identity_ext;
2021-03-23 05:05:03 +00:00
mod middleware;
2019-03-09 18:04:40 -08:00
2022-09-11 21:55:40 +01:00
pub use self::{identity::Identity, identity_ext::IdentityExt, middleware::IdentityMiddleware};