|
1 |
| -//! Identity management for Actix Web. |
2 |
| -//! |
3 |
| -//! `actix-identity` can be used to track identity of a user across multiple requests. It is built |
4 |
| -//! on top of HTTP sessions, via [`actix-session`](https://docs.rs/actix-session). |
5 |
| -//! |
6 |
| -//! # Getting started |
7 |
| -//! To start using identity management in your Actix Web application you must register |
8 |
| -//! [`IdentityMiddleware`] and `SessionMiddleware` as middleware on your `App`: |
9 |
| -//! |
10 |
| -//! ```no_run |
11 |
| -//! # use actix_web::web; |
12 |
| -//! use actix_web::{cookie::Key, App, HttpServer, HttpResponse}; |
13 |
| -//! use actix_identity::IdentityMiddleware; |
14 |
| -//! use actix_session::{storage::RedisSessionStore, SessionMiddleware}; |
15 |
| -//! |
16 |
| -//! #[actix_web::main] |
17 |
| -//! async fn main() { |
18 |
| -//! let secret_key = Key::generate(); |
19 |
| -//! let redis_store = RedisSessionStore::new("redis://127.0.0.1:6379") |
20 |
| -//! .await |
21 |
| -//! .unwrap(); |
22 |
| -//! |
23 |
| -//! HttpServer::new(move || { |
24 |
| -//! App::new() |
25 |
| -//! // Install the identity framework first. |
26 |
| -//! .wrap(IdentityMiddleware::default()) |
27 |
| -//! // The identity system is built on top of sessions. You must install the session |
28 |
| -//! // middleware to leverage `actix-identity`. The session middleware must be mounted |
29 |
| -//! // AFTER the identity middleware: `actix-web` invokes middleware in the OPPOSITE |
30 |
| -//! // order of registration when it receives an incoming request. |
31 |
| -//! .wrap(SessionMiddleware::new( |
32 |
| -//! redis_store.clone(), |
33 |
| -//! secret_key.clone() |
34 |
| -//! )) |
35 |
| -//! // Your request handlers [...] |
36 |
| -//! # .default_service(web::to(|| HttpResponse::Ok())) |
37 |
| -//! }) |
38 |
| -//! # ; |
39 |
| -//! } |
40 |
| -//! ``` |
41 |
| -//! |
42 |
| -//! User identities can be created, accessed and destroyed using the [`Identity`] extractor in your |
43 |
| -//! request handlers: |
44 |
| -//! |
45 |
| -//! ```no_run |
46 |
| -//! use actix_web::{get, post, HttpResponse, Responder, HttpRequest, HttpMessage}; |
47 |
| -//! use actix_identity::Identity; |
48 |
| -//! use actix_session::storage::RedisSessionStore; |
49 |
| -//! |
50 |
| -//! #[get("/")] |
51 |
| -//! async fn index(user: Option<Identity>) -> impl Responder { |
52 |
| -//! if let Some(user) = user { |
53 |
| -//! format!("Welcome! {}", user.id().unwrap()) |
54 |
| -//! } else { |
55 |
| -//! "Welcome Anonymous!".to_owned() |
56 |
| -//! } |
57 |
| -//! } |
58 |
| -//! |
59 |
| -//! #[post("/login")] |
60 |
| -//! async fn login(request: HttpRequest) -> impl Responder { |
61 |
| -//! // Some kind of authentication should happen here |
62 |
| -//! // e.g. password-based, biometric, etc. |
63 |
| -//! // [...] |
64 |
| -//! |
65 |
| -//! // attach a verified user identity to the active session |
66 |
| -//! Identity::login(&request.extensions(), "User1".into()).unwrap(); |
67 |
| -//! |
68 |
| -//! HttpResponse::Ok() |
69 |
| -//! } |
70 |
| -//! |
71 |
| -//! #[post("/logout")] |
72 |
| -//! async fn logout(user: Identity) -> impl Responder { |
73 |
| -//! user.logout(); |
74 |
| -//! HttpResponse::Ok() |
75 |
| -//! } |
76 |
| -//! ``` |
77 |
| -//! |
78 |
| -//! # Advanced configuration |
79 |
| -//! By default, `actix-identity` does not automatically log out users. You can change this behaviour |
80 |
| -//! by customising the configuration for [`IdentityMiddleware`] via [`IdentityMiddleware::builder`]. |
81 |
| -//! |
82 |
| -//! In particular, you can automatically log out users who: |
83 |
| -//! - have been inactive for a while (see [`IdentityMiddlewareBuilder::visit_deadline`]; |
84 |
| -//! - logged in too long ago (see [`IdentityMiddlewareBuilder::login_deadline`]). |
85 |
| -//! |
86 |
| -//! [`IdentityMiddlewareBuilder::visit_deadline`]: config::IdentityMiddlewareBuilder::visit_deadline |
87 |
| -//! [`IdentityMiddlewareBuilder::login_deadline`]: config::IdentityMiddlewareBuilder::login_deadline |
| 1 | +/*! |
| 2 | +Identity management for Actix Web. |
| 3 | +
|
| 4 | +`actix-identity` can be used to track identity of a user across multiple requests. It is built |
| 5 | +on top of HTTP sessions, via [`actix-session`](https://docs.rs/actix-session). |
| 6 | +
|
| 7 | +# Getting started |
| 8 | +To start using identity management in your Actix Web application you must register |
| 9 | +[`IdentityMiddleware`] and `SessionMiddleware` as middleware on your `App`: |
| 10 | +
|
| 11 | +```no_run |
| 12 | +# use actix_web::web; |
| 13 | +use actix_web::{cookie::Key, App, HttpServer, HttpResponse}; |
| 14 | +use actix_identity::IdentityMiddleware; |
| 15 | +use actix_session::{storage::RedisSessionStore, SessionMiddleware}; |
| 16 | +
|
| 17 | +#[actix_web::main] |
| 18 | +async fn main() { |
| 19 | + // When using `Key::generate()` it is important to initialize outside of the |
| 20 | + // `HttpServer::new` closure. When deployed the secret key should be read from a |
| 21 | + // configuration file or environment variables. |
| 22 | + let secret_key = Key::generate(); |
| 23 | +
|
| 24 | + let redis_store = RedisSessionStore::new("redis://127.0.0.1:6379") |
| 25 | + .await |
| 26 | + .unwrap(); |
| 27 | +
|
| 28 | + HttpServer::new(move || { |
| 29 | + App::new() |
| 30 | + // Install the identity framework first. |
| 31 | + .wrap(IdentityMiddleware::default()) |
| 32 | + // The identity system is built on top of sessions. You must install the session |
| 33 | + // middleware to leverage `actix-identity`. The session middleware must be mounted |
| 34 | + // AFTER the identity middleware: `actix-web` invokes middleware in the OPPOSITE |
| 35 | + // order of registration when it receives an incoming request. |
| 36 | + .wrap(SessionMiddleware::new( |
| 37 | + redis_store.clone(), |
| 38 | + secret_key.clone(), |
| 39 | + )) |
| 40 | + // Your request handlers [...] |
| 41 | + # .default_service(web::to(|| HttpResponse::Ok())) |
| 42 | + }) |
| 43 | +# ; |
| 44 | +} |
| 45 | +``` |
| 46 | +
|
| 47 | +User identities can be created, accessed and destroyed using the [`Identity`] extractor in your |
| 48 | +request handlers: |
| 49 | +
|
| 50 | +```no_run |
| 51 | +use actix_web::{get, post, HttpResponse, Responder, HttpRequest, HttpMessage}; |
| 52 | +use actix_identity::Identity; |
| 53 | +use actix_session::storage::RedisSessionStore; |
| 54 | +
|
| 55 | +#[get("/")] |
| 56 | +async fn index(user: Option<Identity>) -> impl Responder { |
| 57 | + if let Some(user) = user { |
| 58 | + format!("Welcome! {}", user.id().unwrap()) |
| 59 | + } else { |
| 60 | + "Welcome Anonymous!".to_owned() |
| 61 | + } |
| 62 | +} |
| 63 | +
|
| 64 | +#[post("/login")] |
| 65 | +async fn login(request: HttpRequest) -> impl Responder { |
| 66 | + // Some kind of authentication should happen here |
| 67 | + // e.g. password-based, biometric, etc. |
| 68 | + // [...] |
| 69 | +
|
| 70 | + // attach a verified user identity to the active session |
| 71 | + Identity::login(&request.extensions(), "User1".into()).unwrap(); |
| 72 | +
|
| 73 | + HttpResponse::Ok() |
| 74 | +} |
| 75 | +
|
| 76 | +#[post("/logout")] |
| 77 | +async fn logout(user: Identity) -> impl Responder { |
| 78 | + user.logout(); |
| 79 | + HttpResponse::Ok() |
| 80 | +} |
| 81 | +``` |
| 82 | +
|
| 83 | +# Advanced configuration |
| 84 | +By default, `actix-identity` does not automatically log out users. You can change this behaviour |
| 85 | +by customising the configuration for [`IdentityMiddleware`] via [`IdentityMiddleware::builder`]. |
| 86 | +
|
| 87 | +In particular, you can automatically log out users who: |
| 88 | +- have been inactive for a while (see [`IdentityMiddlewareBuilder::visit_deadline`]; |
| 89 | +- logged in too long ago (see [`IdentityMiddlewareBuilder::login_deadline`]). |
| 90 | +
|
| 91 | +[`IdentityMiddlewareBuilder::visit_deadline`]: config::IdentityMiddlewareBuilder::visit_deadline |
| 92 | +[`IdentityMiddlewareBuilder::login_deadline`]: config::IdentityMiddlewareBuilder::login_deadline |
| 93 | +*/ |
88 | 94 |
|
89 | 95 | #![forbid(unsafe_code)]
|
90 | 96 | #![deny(rust_2018_idioms, nonstandard_style, missing_docs)]
|
|
0 commit comments