mirror of
https://github.com/actix/examples
synced 2025-06-28 18:00:37 +02:00
Restructure folders (#411)
This commit is contained in:
committed by
GitHub
parent
9db98162b2
commit
c3407627d0
11
session/cookie-auth/Cargo.toml
Normal file
11
session/cookie-auth/Cargo.toml
Normal file
@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "cookie-auth"
|
||||
version = "2.0.0"
|
||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
actix-web = "3"
|
||||
actix-identity = "0.3"
|
||||
env_logger = "0.8"
|
||||
rand = "0.7"
|
26
session/cookie-auth/README.md
Normal file
26
session/cookie-auth/README.md
Normal file
@ -0,0 +1,26 @@
|
||||
# cookie-auth
|
||||
|
||||
Testing with cookie auth with [curl](https://curl.haxx.se).
|
||||
|
||||
Login:
|
||||
|
||||
curl -v -b "auth-example=user1" -X POST http://localhost:8080/login
|
||||
< HTTP/1.1 302 Found
|
||||
< set-cookie: auth-example=GRm2Vku0UpFbJ3CNTKbndzIYHVGi8wc8eoXm/Axtf2BO; HttpOnly; Path=/
|
||||
< location: /
|
||||
|
||||
Uses a POST request with a Useridentity `user1`. A cookie is set and a redirect to home `/` follows.
|
||||
|
||||
Get:
|
||||
|
||||
Now with the cookie `auth-example` sent in a GET request, the `user1` is recognized.
|
||||
|
||||
curl -v -b "auth-example=GRm2Vku0UpFbJ3CNTKbndzIYHVGi8wc8eoXm/Axtf2BO" http://localhost:8080/
|
||||
* Connected to localhost (127.0.0.1) port 8080 (#0)
|
||||
> GET / HTTP/1.1
|
||||
> Host: localhost:8080
|
||||
> Cookie: auth-example=GRm2Vku0UpFbJ3CNTKbndzIYHVGi8wc8eoXm/Axtf2BO
|
||||
>
|
||||
< HTTP/1.1 200 OK
|
||||
<
|
||||
Hello user1
|
48
session/cookie-auth/src/main.rs
Normal file
48
session/cookie-auth/src/main.rs
Normal file
@ -0,0 +1,48 @@
|
||||
use actix_identity::Identity;
|
||||
use actix_identity::{CookieIdentityPolicy, IdentityService};
|
||||
use actix_web::{middleware, web, App, HttpResponse, HttpServer};
|
||||
use rand::Rng;
|
||||
|
||||
async fn index(id: Identity) -> String {
|
||||
format!(
|
||||
"Hello {}",
|
||||
id.identity().unwrap_or_else(|| "Anonymous".to_owned())
|
||||
)
|
||||
}
|
||||
|
||||
async fn login(id: Identity) -> HttpResponse {
|
||||
id.remember("user1".to_owned());
|
||||
HttpResponse::Found().header("location", "/").finish()
|
||||
}
|
||||
|
||||
async fn logout(id: Identity) -> HttpResponse {
|
||||
id.forget();
|
||||
HttpResponse::Found().header("location", "/").finish()
|
||||
}
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
std::env::set_var("RUST_LOG", "actix_web=info");
|
||||
env_logger::init();
|
||||
|
||||
// Generate a random 32 byte key. Note that it is important to use a unique
|
||||
// private key for every project. Anyone with access to the key can generate
|
||||
// authentication cookies for any user!
|
||||
let private_key = rand::thread_rng().gen::<[u8; 32]>();
|
||||
HttpServer::new(move || {
|
||||
App::new()
|
||||
.wrap(IdentityService::new(
|
||||
CookieIdentityPolicy::new(&private_key)
|
||||
.name("auth-example")
|
||||
.secure(false),
|
||||
))
|
||||
// enable logger - always register actix-web Logger middleware last
|
||||
.wrap(middleware::Logger::default())
|
||||
.service(web::resource("/login").route(web::post().to(login)))
|
||||
.service(web::resource("/logout").to(logout))
|
||||
.service(web::resource("/").route(web::get().to(index)))
|
||||
})
|
||||
.bind("127.0.0.1:8080")?
|
||||
.run()
|
||||
.await
|
||||
}
|
Reference in New Issue
Block a user