1
0
mirror of https://github.com/actix/examples synced 2024-11-27 16:02:57 +01:00
examples/auth/cookie-auth/src/main.rs
2023-03-14 02:40:02 +00:00

76 lines
2.6 KiB
Rust

use actix_identity::{Identity, IdentityMiddleware};
use actix_session::{config::PersistentSession, storage::CookieSessionStore, SessionMiddleware};
use actix_web::{
cookie::{time::Duration, Key},
error,
http::StatusCode,
middleware, web, App, HttpMessage as _, HttpRequest, HttpServer, Responder,
};
const ONE_MINUTE: Duration = Duration::minutes(1);
async fn index(identity: Option<Identity>) -> actix_web::Result<impl Responder> {
let id = match identity.map(|id| id.id()) {
None => "anonymous".to_owned(),
Some(Ok(id)) => id,
Some(Err(err)) => return Err(error::ErrorInternalServerError(err)),
};
Ok(format!("Hello {id}"))
}
async fn login(req: HttpRequest) -> impl Responder {
Identity::login(&req.extensions(), "user1".to_owned()).unwrap();
web::Redirect::to("/").using_status_code(StatusCode::FOUND)
}
async fn logout(id: Identity) -> impl Responder {
id.logout();
web::Redirect::to("/").using_status_code(StatusCode::FOUND)
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
// Generate a random secret key. Note that it is important to use a unique
// secret key for every project. Anyone with access to the key can generate
// authentication cookies for any user!
//
// If the secret key is read from a file or the environment, make sure it is generated securely.
// For example, a secure random key (in base64 format) can be generated with the OpenSSL CLI:
// ```
// openssl rand -base64 64
// ```
//
// Then decoded and used converted to a Key:
// ```
// let secret_key = Key::from(base64::decode(&private_key_base64).unwrap());
// ```
let secret_key = Key::generate();
log::info!("starting HTTP server at http://localhost:8080");
HttpServer::new(move || {
App::new()
.service(web::resource("/login").route(web::post().to(login)))
.service(web::resource("/logout").route(web::post().to(logout)))
.service(web::resource("/").route(web::get().to(index)))
.wrap(IdentityMiddleware::default())
.wrap(
SessionMiddleware::builder(CookieSessionStore::default(), secret_key.clone())
.cookie_name("auth-example".to_owned())
.cookie_secure(false)
.session_lifecycle(PersistentSession::default().session_ttl(ONE_MINUTE))
.build(),
)
.wrap(middleware::NormalizePath::trim())
.wrap(middleware::Logger::default())
})
.bind(("127.0.0.1", 8080))?
.run()
.await
}