diff --git a/.travis.yml b/.travis.yml index 84057bb8..7d10d027 100644 --- a/.travis.yml +++ b/.travis.yml @@ -38,6 +38,7 @@ script: cd async_ex1 && cargo check && cd .. cd basics && cargo check && cd .. cd cookie-auth && cargo check && cd .. + cd cookie-auth-full && cargo check && cd .. cd cookie-session && cargo check && cd .. cd diesel && cargo check && cd .. cd form && cargo check && cd .. diff --git a/Cargo.lock b/Cargo.lock index d8f67cfe..59ee193a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -380,6 +380,15 @@ dependencies = [ [[package]] name = "cookie-auth" version = "0.1.0" +dependencies = [ + "actix 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", + "actix-web 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", + "env_logger 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "cookie-auth-full" +version = "0.1.0" dependencies = [ "actix 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", "actix-web 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index c78478ca..fb89e0a7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,7 @@ members = [ "async_ex1", "basics", "cookie-auth", + "cookie-auth-full", "cookie-session", "diesel", "form", diff --git a/cookie-auth-full/Cargo.toml b/cookie-auth-full/Cargo.toml new file mode 100644 index 00000000..5098bfdd --- /dev/null +++ b/cookie-auth-full/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "cookie-auth-full" +version = "0.1.0" +authors = ["Nikolay Kim "] +workspace = "../" + +[dependencies] +actix = "0.7" +actix-web = "0.7" + +cookie = { version="0.10", features=["percent-encode", "secure"] } +futures = "0.1" +time = "0.1" +env_logger = "0.5" diff --git a/cookie-auth/src/auth.rs b/cookie-auth-full/src/auth.rs similarity index 100% rename from cookie-auth/src/auth.rs rename to cookie-auth-full/src/auth.rs diff --git a/cookie-auth-full/src/main.rs b/cookie-auth-full/src/main.rs new file mode 100644 index 00000000..f7a0c09c --- /dev/null +++ b/cookie-auth-full/src/main.rs @@ -0,0 +1,49 @@ +extern crate actix; +extern crate actix_web; +extern crate cookie; +extern crate env_logger; +extern crate futures; +extern crate time; + +use actix_web::{middleware, server, App, HttpRequest, HttpResponse}; + +mod auth; +use auth::{CookieIdentityPolicy, IdentityService, RequestIdentity}; + +fn index(req: &HttpRequest) -> String { + format!("Hello {}", req.identity().unwrap_or("Anonymous".to_owned())) +} + +fn login(req: &HttpRequest) -> HttpResponse { + req.remember("user1".to_owned()); + HttpResponse::Found().header("location", "/").finish() +} + +fn logout(req: &HttpRequest) -> HttpResponse { + req.forget(); + HttpResponse::Found().header("location", "/").finish() +} + +fn main() { + ::std::env::set_var("RUST_LOG", "actix_web=info"); + env_logger::init(); + let sys = actix::System::new("cookie-auth"); + + server::new(|| { + App::new() + .middleware(middleware::Logger::default()) + .middleware(IdentityService::new( + CookieIdentityPolicy::new(&[0; 32]) + .name("auth-example") + .secure(false), + )) + .resource("/login", |r| r.f(login)) + .resource("/logout", |r| r.f(logout)) + .resource("/", |r| r.f(index)) + }).bind("127.0.0.1:8080") + .unwrap() + .start(); + + println!("Started http server: 127.0.0.1:8080"); + let _ = sys.run(); +} diff --git a/cookie-auth/Cargo.toml b/cookie-auth/Cargo.toml index c44474eb..ceda654d 100644 --- a/cookie-auth/Cargo.toml +++ b/cookie-auth/Cargo.toml @@ -7,8 +7,4 @@ workspace = "../" [dependencies] actix = "0.7" actix-web = "0.7" - -cookie = { version="0.10", features=["percent-encode", "secure"] } -futures = "0.1" -time = "0.1" env_logger = "0.5" diff --git a/cookie-auth/src/main.rs b/cookie-auth/src/main.rs index f7a0c09c..a7444793 100644 --- a/cookie-auth/src/main.rs +++ b/cookie-auth/src/main.rs @@ -1,14 +1,10 @@ extern crate actix; extern crate actix_web; -extern crate cookie; extern crate env_logger; -extern crate futures; -extern crate time; use actix_web::{middleware, server, App, HttpRequest, HttpResponse}; - -mod auth; -use auth::{CookieIdentityPolicy, IdentityService, RequestIdentity}; +use actix_web::middleware::identity::RequestIdentity; +use actix_web::middleware::identity::{CookieIdentityPolicy, IdentityService}; fn index(req: &HttpRequest) -> String { format!("Hello {}", req.identity().unwrap_or("Anonymous".to_owned()))