23 releases

0.7.1 Mar 2, 2024
0.6.0 Sep 16, 2023
0.5.2 Jul 19, 2022
0.4.0 Mar 1, 2022
0.1.0 Jun 12, 2019

#299 in HTTP server

Download history 2045/week @ 2023-12-23 3862/week @ 2023-12-30 4883/week @ 2024-01-06 5823/week @ 2024-01-13 5195/week @ 2024-01-20 4590/week @ 2024-01-27 4248/week @ 2024-02-03 3196/week @ 2024-02-10 3734/week @ 2024-02-17 4578/week @ 2024-02-24 5037/week @ 2024-03-02 3676/week @ 2024-03-09 5104/week @ 2024-03-16 5247/week @ 2024-03-23 5401/week @ 2024-03-30 3933/week @ 2024-04-06

20,189 downloads per month
Used in 12 crates (11 directly)

MIT/Apache

155KB
2.5K SLoC

actix-identity

Identity management for Actix Web.

crates.io Documentation Apache 2.0 or MIT licensed Dependency Status

Identity management for Actix Web.

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.

Getting started

To start using identity management in your Actix Web application you must register IdentityMiddleware and SessionMiddleware as middleware on your App:

use actix_web::{cookie::Key, App, HttpServer, HttpResponse};
use actix_identity::IdentityMiddleware;
use actix_session::{storage::RedisSessionStore, SessionMiddleware};

#[actix_web::main]
async fn main() {
    // When using `Key::generate()` it is important to initialize outside of the
    // `HttpServer::new` closure. When deployed the secret key should be read from a
    // configuration file or environment variables.
    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 [...]
    })
}

User identities can be created, accessed and destroyed using the Identity extractor in your request handlers:

use actix_web::{get, post, HttpResponse, Responder, HttpRequest, HttpMessage};
use actix_identity::Identity;
use actix_session::storage::RedisSessionStore;

#[get("/")]
async fn index(user: Option<Identity>) -> impl Responder {
    if let Some(user) = user {
        format!("Welcome! {}", user.id().unwrap())
    } else {
        "Welcome Anonymous!".to_owned()
    }
}

#[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()
}

#[post("/logout")]
async fn logout(user: Identity) -> impl Responder {
    user.logout();
    HttpResponse::Ok()
}

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.

In particular, you can automatically log out users who:

Dependencies

~17–31MB
~542K SLoC