From 894a795ee7007d4351063c67d643974fae4a2fdb Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Wed, 18 Apr 2018 16:46:35 -0700 Subject: [PATCH] add cookie auth example --- Cargo.lock | 239 +++++++++++++++++----------------- Cargo.toml | 1 + cookie-auth/Cargo.toml | 13 ++ cookie-auth/src/auth.rs | 277 ++++++++++++++++++++++++++++++++++++++++ cookie-auth/src/main.rs | 53 ++++++++ 5 files changed, 469 insertions(+), 114 deletions(-) create mode 100644 cookie-auth/Cargo.toml create mode 100644 cookie-auth/src/auth.rs create mode 100644 cookie-auth/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index fd495011..f47804f6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ [[package]] name = "actix" -version = "0.5.5" +version = "0.5.6" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "actix_derive 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -17,7 +17,7 @@ dependencies = [ "tokio-io 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-signal 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "trust-dns-resolver 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", - "uuid 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", + "uuid 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -25,8 +25,8 @@ name = "actix-redis" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "actix 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", - "actix-web 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", + "actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", + "actix-web 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", "backoff 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "cookie 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)", "failure 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -35,18 +35,18 @@ dependencies = [ "log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)", "redis-async 0.0.7 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-core 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-io 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "actix-web" -version = "0.5.1" +version = "0.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "actix 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", + "actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", "base64 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", "bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "brotli2 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -75,11 +75,10 @@ dependencies = [ "percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "regex 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)", "serde_urlencoded 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "sha1 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", - "skeptic 0.13.2 (registry+https://github.com/rust-lang/crates.io-index)", "smallvec 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "time 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-core 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", @@ -94,15 +93,15 @@ dependencies = [ name = "actix-web-cors" version = "0.1.0" dependencies = [ - "actix 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", - "actix-web 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", + "actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", + "actix-web 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", "dotenv 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)", - "env_logger 0.5.7 (registry+https://github.com/rust-lang/crates.io-index)", + "env_logger 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", "http 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -212,9 +211,9 @@ dependencies = [ name = "basics" version = "0.1.0" dependencies = [ - "actix 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", - "actix-web 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", - "env_logger 0.5.7 (registry+https://github.com/rust-lang/crates.io-index)", + "actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", + "actix-web 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", + "env_logger 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -272,9 +271,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "semver 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -308,6 +307,18 @@ dependencies = [ "url 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "cookie-auth" +version = "0.1.0" +dependencies = [ + "actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", + "actix-web 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", + "cookie 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)", + "env_logger 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)", + "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "crossbeam-channel" version = "0.1.2" @@ -425,16 +436,16 @@ dependencies = [ name = "diesel-example" version = "0.1.0" dependencies = [ - "actix 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", - "actix-web 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", + "actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", + "actix-web 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", "diesel 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "dotenv 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)", - "env_logger 0.5.7 (registry+https://github.com/rust-lang/crates.io-index)", + "env_logger 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", "r2d2 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)", "uuid 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -527,7 +538,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "env_logger" -version = "0.5.7" +version = "0.5.8" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "atty 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", @@ -674,9 +685,9 @@ dependencies = [ name = "hello-world" version = "0.1.0" dependencies = [ - "actix 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", - "actix-web 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", - "env_logger 0.5.7 (registry+https://github.com/rust-lang/crates.io-index)", + "actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", + "actix-web 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", + "env_logger 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -701,9 +712,9 @@ dependencies = [ name = "http-proxy" version = "0.1.0" dependencies = [ - "actix 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", - "actix-web 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", - "env_logger 0.5.7 (registry+https://github.com/rust-lang/crates.io-index)", + "actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", + "actix-web 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", + "env_logger 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -760,7 +771,7 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "error-chain 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", - "socket2 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", + "socket2 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", "widestring 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", "winreg 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -793,15 +804,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" name = "json-example" version = "0.1.0" dependencies = [ - "actix 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", - "actix-web 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", + "actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", + "actix-web 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", "bytes 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "env_logger 0.5.7 (registry+https://github.com/rust-lang/crates.io-index)", + "env_logger 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", "json 0.11.13 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -813,8 +824,8 @@ dependencies = [ "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", "juniper_codegen 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)", "ordermap 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", "url 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "uuid 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -823,14 +834,14 @@ dependencies = [ name = "juniper-example" version = "0.1.0" dependencies = [ - "actix 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", - "actix-web 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", - "env_logger 0.5.7 (registry+https://github.com/rust-lang/crates.io-index)", + "actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", + "actix-web 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", + "env_logger 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", "juniper 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1007,9 +1018,9 @@ dependencies = [ name = "multipart-example" version = "0.1.0" dependencies = [ - "actix 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", - "actix-web 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", - "env_logger 0.5.7 (registry+https://github.com/rust-lang/crates.io-index)", + "actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", + "actix-web 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", + "env_logger 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1077,7 +1088,7 @@ name = "ordermap" version = "0.2.13" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "serde 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1208,10 +1219,10 @@ dependencies = [ name = "protobuf-example" version = "0.1.0" dependencies = [ - "actix 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", - "actix-web 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", + "actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", + "actix-web 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", "bytes 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "env_logger 0.5.7 (registry+https://github.com/rust-lang/crates.io-index)", + "env_logger 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)", "failure 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", "prost 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1266,16 +1277,16 @@ dependencies = [ name = "r2d2-example" version = "0.1.0" dependencies = [ - "actix 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", - "actix-web 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", - "env_logger 0.5.7 (registry+https://github.com/rust-lang/crates.io-index)", + "actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", + "actix-web 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", + "env_logger 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", "r2d2 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)", "r2d2_sqlite 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "rusqlite 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)", "uuid 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1343,10 +1354,10 @@ dependencies = [ name = "redis-session" version = "0.1.0" dependencies = [ - "actix 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", + "actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", "actix-redis 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", - "actix-web 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", - "env_logger 0.5.7 (registry+https://github.com/rust-lang/crates.io-index)", + "actix-web 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", + "env_logger 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1369,14 +1380,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "aho-corasick 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", "memchr 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)", - "regex-syntax 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", + "regex-syntax 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", "thread_local 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", "utf8-ranges 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "regex-syntax" -version = "0.5.3" +version = "0.5.5" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "ucd-util 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1465,7 +1476,7 @@ version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1475,23 +1486,23 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "serde" -version = "1.0.37" +version = "1.0.39" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "serde_derive" -version = "1.0.37" +version = "1.0.39" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "proc-macro2 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "quote 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive_internals 0.23.0 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive_internals 0.23.1 (registry+https://github.com/rust-lang/crates.io-index)", "syn 0.13.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "serde_derive_internals" -version = "0.23.0" +version = "0.23.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "proc-macro2 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1500,13 +1511,13 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.13" +version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "itoa 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", "num-traits 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1516,7 +1527,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "itoa 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", "url 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1540,7 +1551,7 @@ dependencies = [ "error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "pulldown-cmark 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)", "tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", "walkdir 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1565,7 +1576,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "socket2" -version = "0.3.4" +version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "cfg-if 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1582,9 +1593,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" name = "state" version = "0.1.0" dependencies = [ - "actix 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", - "actix-web 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", - "env_logger 0.5.7 (registry+https://github.com/rust-lang/crates.io-index)", + "actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", + "actix-web 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", + "env_logger 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1653,9 +1664,9 @@ dependencies = [ name = "template-tera" version = "0.1.0" dependencies = [ - "actix 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", - "actix-web 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", - "env_logger 0.5.7 (registry+https://github.com/rust-lang/crates.io-index)", + "actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", + "actix-web 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", + "env_logger 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)", "tera 0.11.6 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1672,8 +1683,8 @@ dependencies = [ "pest 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", "pest_derive 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)", "regex 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)", "slug 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "url 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1719,9 +1730,9 @@ dependencies = [ name = "tls-example" version = "0.1.0" dependencies = [ - "actix 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", - "actix-web 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", - "env_logger 0.5.7 (registry+https://github.com/rust-lang/crates.io-index)", + "actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", + "actix-web 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", + "env_logger 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)", "openssl 0.10.6 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1965,9 +1976,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" name = "unix-socket" version = "0.1.0" dependencies = [ - "actix 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", - "actix-web 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", - "env_logger 0.5.7 (registry+https://github.com/rust-lang/crates.io-index)", + "actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", + "actix-web 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", + "env_logger 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-uds 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -2006,12 +2017,12 @@ version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "rand 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "uuid" -version = "0.6.2" +version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "cfg-if 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2047,9 +2058,9 @@ dependencies = [ name = "websocket" version = "0.1.0" dependencies = [ - "actix 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", - "actix-web 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", - "env_logger 0.5.7 (registry+https://github.com/rust-lang/crates.io-index)", + "actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", + "actix-web 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", + "env_logger 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-core 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -2058,16 +2069,16 @@ dependencies = [ name = "websocket-example" version = "0.1.0" dependencies = [ - "actix 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", - "actix-web 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", + "actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", + "actix-web 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", "byteorder 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "bytes 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "env_logger 0.5.7 (registry+https://github.com/rust-lang/crates.io-index)", + "env_logger 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-core 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-io 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -2140,9 +2151,9 @@ dependencies = [ ] [metadata] -"checksum actix 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)" = "29d85dc76d055498ef65ecf556df154393f94f55d325a05be20c676432536ce7" +"checksum actix 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)" = "e767e3170dc7cdec50fe1b74d22fd9d2f4b78b97b2052a254b5acb07dae68634" "checksum actix-redis 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d2c35d0dae8a853584c66b2db88e61203cd0b690666aae3cc3451cfc345791c8" -"checksum actix-web 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "5c6fd8fa3f10cbca9b0050801181e739f045ecd47f3ebeacb150ef7cb81f5ab1" +"checksum actix-web 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "922d6bfd87a5b070cbb65c4731fb906963f64ee4619a78858494caf1598ec85a" "checksum actix_derive 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c4b1dc922654b9aca7a8a31eab875fde804fa9fbd67f220f2e457787b23590f2" "checksum aho-corasick 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "d6531d44de723825aa81398a6415283229725a00fa30713812ab9323faa82fc4" "checksum antidote 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "34fde25430d87a9388dadbe6e34d7f72a462c8b43ac8d309b42b0a8505d7e2a5" @@ -2188,7 +2199,7 @@ dependencies = [ "checksum encoding-index-singlebyte 1.20141219.5 (registry+https://github.com/rust-lang/crates.io-index)" = "3351d5acffb224af9ca265f435b859c7c01537c0849754d3db3fdf2bfe2ae84a" "checksum encoding-index-tradchinese 1.20141219.5 (registry+https://github.com/rust-lang/crates.io-index)" = "fd0e20d5688ce3cab59eb3ef3a2083a5c77bf496cb798dc6fcdb75f323890c18" "checksum encoding_index_tests 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "a246d82be1c9d791c5dfde9a2bd045fc3cbba3fa2b11ad558f27d01712f00569" -"checksum env_logger 0.5.7 (registry+https://github.com/rust-lang/crates.io-index)" = "0f475037312b91d34dbc3142a1ad3980ef0d070c7a855ce238afdd5e987cfecc" +"checksum env_logger 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)" = "be27f8ea102a7182093a80d98f0b78623b580eda8791cbe8e2345fe6e57567a6" "checksum error-chain 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "faa976b4fd2e4c2b2f3f486874b19e61944d3de3de8b61c9fcf835d583871bcc" "checksum error-chain 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d9435d864e017c3c6afeac1654189b06cdb491cf2ff73dbf0d73b0f292f42ff8" "checksum error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ff511d5dc435d703f4971bc399647c9bc38e20cb41452e3b9feb4765419ed3f3" @@ -2281,7 +2292,7 @@ dependencies = [ "checksum redox_syscall 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)" = "0d92eecebad22b767915e4d529f89f28ee96dbbf5a4810d2b844373f136417fd" "checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76" "checksum regex 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "aec3f58d903a7d2a9dc2bf0e41a746f4530e0cab6b615494e058f67a3ef947fb" -"checksum regex-syntax 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "b2550876c31dc914696a6c2e01cbce8afba79a93c8ae979d2fe051c0230b3756" +"checksum regex-syntax 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)" = "bd90079345f4a4c3409214734ae220fd773c6f2e8a543d07370c6c1c369cfbfb" "checksum remove_dir_all 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "dfc5b3ce5d5ea144bb04ebd093a9e14e9765bcfec866aecda9b6dec43b3d1e24" "checksum resolv-conf 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8e1b086bb6a2659d6ba66e4aa21bde8a53ec03587cd5c80b83bdc3a330f35cab" "checksum ring 0.12.1 (registry+https://github.com/rust-lang/crates.io-index)" = "6f7d28b30a72c01b458428e0ae988d4149c20d902346902be881e3edc4bb325c" @@ -2294,10 +2305,10 @@ dependencies = [ "checksum scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "94258f53601af11e6a49f722422f6e3425c52b06245a5cf9bc09908b174f5e27" "checksum semver 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bee2bc909ab2d8d60dab26e8cad85b25d795b14603a0dcb627b78b9d30b6454b" "checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" -"checksum serde 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)" = "d3bcee660dcde8f52c3765dd9ca5ee36b4bf35470a738eb0bd5a8752b0389645" -"checksum serde_derive 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)" = "f1711ab8b208541fa8de00425f6a577d90f27bb60724d2bb5fd911314af9668f" -"checksum serde_derive_internals 0.23.0 (registry+https://github.com/rust-lang/crates.io-index)" = "89b340a48245bc03ddba31d0ff1709c118df90edc6adabaca4aac77aea181cce" -"checksum serde_json 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)" = "5c508584d9913df116b91505eec55610a2f5b16e9ed793c46e4d0152872b3e74" +"checksum serde 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)" = "53e6b6859395f46cf528414659ce43e70902b2277519707c3bd91797b3320330" +"checksum serde_derive 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)" = "16e97f8dc5b2dabc0183e0cde24b1a53835e5bb3d2c9e0fdb077f895bba7f2a9" +"checksum serde_derive_internals 0.23.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9d30c4596450fd7bbda79ef15559683f9a79ac0193ea819db90000d7e1cae794" +"checksum serde_json 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7bf1cbb1387028a13739cb018ee0d9b3db534f22ca3c84a5904f7eadfde14e75" "checksum serde_urlencoded 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ce0fd303af908732989354c6f02e05e2e6d597152870f2c6990efb0577137480" "checksum sha1 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2579985fda508104f7587689507983eadd6a6e84dd35d6d115361f530916fa0d" "checksum siphasher 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "0df90a788073e8d0235a67e50441d47db7c8ad9debd91cbf43736a2a92d36537" @@ -2305,7 +2316,7 @@ dependencies = [ "checksum slab 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fdeff4cd9ecff59ec7e3744cbca73dfe5ac35c2aedb2cfba8a1c715a18912e9d" "checksum slug 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f6f5ff4b43cb07b86c5f9236c92714a22cdf9e5a27a7d85e398e2c9403328cb8" "checksum smallvec 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "44db0ecb22921ef790d17ae13a3f6d15784183ff5f2a01aa32098c7498d2b4b9" -"checksum socket2 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "71ebbe82fcdd697244ba7fe6e05e63b5c45910c3927e28469a04947494ff48d8" +"checksum socket2 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "ff606e0486e88f5fc6cfeb3966e434fb409abbc7a3ab495238f70a1ca97f789d" "checksum stable_deref_trait 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "15132e0e364248108c5e2c02e3ab539be8d6f5d52a01ca9bbf27ed657316f02b" "checksum string 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "31f98b200e7caca9efca50fc0aa69cd58a5ec81d5f6e75b2f3ecaad2e998972a" "checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad" @@ -2346,7 +2357,7 @@ dependencies = [ "checksum url 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f808aadd8cfec6ef90e4a14eb46f24511824d1ac596b9682703c87056c8678b7" "checksum utf8-ranges 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "662fab6525a98beff2921d7f61a39e7d59e0b425ebc7d0d9e66d316e55124122" "checksum uuid 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "bcc7e3b898aa6f6c08e5295b6c89258d1331e9ac578cc992fb818759951bdc22" -"checksum uuid 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d4670e1e935f7edd193a413f802e2ee52274aed62a09ccaab1656515c9c53a66" +"checksum uuid 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8630752f979f1b6b87c49830a5e3784082545de63920d59fbaac252474319447" "checksum vcpkg 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "7ed0f6789c8a85ca41bbc1c9d175422116a9869bd1cf31bb08e1493ecce60380" "checksum version_check 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "6b772017e347561807c1aa192438c5fd74242a670a6cffacc40f2defd1dc069d" "checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" diff --git a/Cargo.toml b/Cargo.toml index b5c55c1f..48bbe412 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,7 @@ members = [ "./", "basics", + "cookie-auth", "diesel", "hello-world", "http-proxy", diff --git a/cookie-auth/Cargo.toml b/cookie-auth/Cargo.toml new file mode 100644 index 00000000..7ffa728c --- /dev/null +++ b/cookie-auth/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "cookie-auth" +version = "0.1.0" +authors = ["Nikolay Kim "] +workspace = "../" + +[dependencies] +actix = "0.5" +actix-web = "^0.5" +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/src/auth.rs new file mode 100644 index 00000000..ed747caf --- /dev/null +++ b/cookie-auth/src/auth.rs @@ -0,0 +1,277 @@ +use std::rc::Rc; + +use cookie::{Cookie, CookieJar, Key}; +use futures::Future; +use futures::future::{FutureResult, err as FutErr, ok as FutOk}; +use time::Duration; + +use actix_web::{HttpRequest, HttpResponse, Error, Result}; +use actix_web::http::header::{self, HeaderValue}; +use actix_web::middleware::{Middleware, Response, Started}; + + +/// Trait provides identity service for the request. +pub trait RequestIdentity { + + /// Return the claimed identity of the user associated request or + /// ``None`` if no identity can be found associated with the request. + fn identity(&mut self) -> Option<&str>; + + /// Remember identity. + fn remember(&mut self, identity: String); + + /// This method is used to 'forget' the current identity on subsequent requests. + fn forget(&mut self); + +} + +impl RequestIdentity for HttpRequest { + fn identity(&mut self) -> Option<&str> { + if let Some(id) = self.extensions().get::() { + return id.0.identity() + } + None + } + + fn remember(&mut self, identity: String) { + if let Some(id) = self.extensions().get_mut::() { + return id.0.remember(identity) + } + } + + fn forget(&mut self) { + if let Some(id) = self.extensions().get_mut::() { + return id.0.forget() + } + } +} + + +/// An identity +pub trait Identity: 'static { + fn identity(&self) -> Option<&str>; + + fn remember(&mut self, key: String); + + fn forget(&mut self); + + /// Write session to storage backend. + fn write(&mut self, resp: HttpResponse) -> Result; +} + +/// Identity policy definition. +pub trait IdentityPolicy: Sized + 'static { + type Identity: Identity; + type Future: Future; + + /// Parse the session from request and load data from a service identity. + fn from_request(&self, request: &mut HttpRequest) -> Self::Future; +} + +/// Middleware that implements identity service +pub struct IdentityService { + backend: T, +} + +impl IdentityService { + /// Create new identity service with specified backend. + pub fn new(backend: T) -> Self { + IdentityService { backend } + } +} + +struct IdentityBox(Box); + +#[doc(hidden)] +unsafe impl Send for IdentityBox {} +#[doc(hidden)] +unsafe impl Sync for IdentityBox {} + + +impl> Middleware for IdentityService { + fn start(&self, req: &mut HttpRequest) -> Result { + let mut req = req.clone(); + + let fut = self.backend + .from_request(&mut req) + .then(move |res| match res { + Ok(id) => { + req.extensions().insert(IdentityBox(Box::new(id))); + FutOk(None) + } + Err(err) => FutErr(err), + }); + Ok(Started::Future(Box::new(fut))) + } + + fn response(&self, req: &mut HttpRequest, resp: HttpResponse) -> Result { + if let Some(mut id) = req.extensions().remove::() { + id.0.write(resp) + } else { + Ok(Response::Done(resp)) + } + } +} + +/// Identity that uses private cookies as identity storage +pub struct CookieIdentity { + changed: bool, + identity: Option, + inner: Rc, +} + +impl Identity for CookieIdentity { + fn identity(&self) -> Option<&str> { + self.identity.as_ref().map(|s| s.as_ref()) + } + + fn remember(&mut self, value: String) { + self.changed = true; + self.identity = Some(value); + } + + fn forget(&mut self) { + self.changed = true; + self.identity = None; + } + + fn write(&mut self, mut resp: HttpResponse) -> Result { + if self.changed { + let _ = self.inner.set_cookie(&mut resp, self.identity.take()); + } + Ok(Response::Done(resp)) + } +} + +struct CookieIdentityInner { + key: Key, + name: String, + path: String, + domain: Option, + secure: bool, + max_age: Option, +} + +impl CookieIdentityInner { + fn new(key: &[u8]) -> CookieIdentityInner { + CookieIdentityInner { + key: Key::from_master(key), + name: "actix-identity".to_owned(), + path: "/".to_owned(), + domain: None, + secure: true, + max_age: None, + } + } + + fn set_cookie(&self, resp: &mut HttpResponse, id: Option) -> Result<()> { + let some = id.is_some(); + { + let id = id.unwrap_or_else(|| String::new()); + let mut cookie = Cookie::new(self.name.clone(), id); + cookie.set_path(self.path.clone()); + cookie.set_secure(self.secure); + cookie.set_http_only(true); + + if let Some(ref domain) = self.domain { + cookie.set_domain(domain.clone()); + } + + if let Some(max_age) = self.max_age { + cookie.set_max_age(max_age); + } + + let mut jar = CookieJar::new(); + if some { + jar.private(&self.key).add(cookie); + } else { + jar.add_original(cookie.clone()); + jar.private(&self.key).remove(cookie); + } + + for cookie in jar.delta() { + let val = HeaderValue::from_str(&cookie.to_string())?; + resp.headers_mut().append(header::SET_COOKIE, val); + } + } + + Ok(()) + } + + fn load(&self, req: &mut HttpRequest) -> Option { + if let Ok(cookies) = req.cookies() { + for cookie in cookies { + if cookie.name() == self.name { + let mut jar = CookieJar::new(); + jar.add_original(cookie.clone()); + + let cookie_opt = jar.private(&self.key).get(&self.name); + if let Some(cookie) = cookie_opt { + return Some(cookie.value().into()) + } + } + } + } + None + } +} + +/// Use cookies for request identity. +pub struct CookieIdentityPolicy(Rc); + +impl CookieIdentityPolicy { + + /// Construct new `CookieIdentityPolicy` instance. + /// + /// Panics if key length is less than 32 bytes. + pub fn new(key: &[u8]) -> CookieIdentityPolicy { + CookieIdentityPolicy(Rc::new(CookieIdentityInner::new(key))) + } + + /// Sets the `path` field in the session cookie being built. + pub fn path>(mut self, value: S) -> CookieIdentityPolicy { + Rc::get_mut(&mut self.0).unwrap().path = value.into(); + self + } + + /// Sets the `name` field in the session cookie being built. + pub fn name>(mut self, value: S) -> CookieIdentityPolicy { + Rc::get_mut(&mut self.0).unwrap().name = value.into(); + self + } + + /// Sets the `domain` field in the session cookie being built. + pub fn domain>(mut self, value: S) -> CookieIdentityPolicy { + Rc::get_mut(&mut self.0).unwrap().domain = Some(value.into()); + self + } + + /// Sets the `secure` field in the session cookie being built. + /// + /// If the `secure` field is set, a cookie will only be transmitted when the + /// connection is secure - i.e. `https` + pub fn secure(mut self, value: bool) -> CookieIdentityPolicy { + Rc::get_mut(&mut self.0).unwrap().secure = value; + self + } + + /// Sets the `max-age` field in the session cookie being built. + pub fn max_age(mut self, value: Duration) -> CookieIdentityPolicy { + Rc::get_mut(&mut self.0).unwrap().max_age = Some(value); + self + } +} + +impl IdentityPolicy for CookieIdentityPolicy { + type Identity = CookieIdentity; + type Future = FutureResult; + + fn from_request(&self, req: &mut HttpRequest) -> Self::Future { + let identity = self.0.load(req); + FutOk(CookieIdentity { + identity, + changed: false, + inner: Rc::clone(&self.0), + }) + } +} diff --git a/cookie-auth/src/main.rs b/cookie-auth/src/main.rs new file mode 100644 index 00000000..7c178a87 --- /dev/null +++ b/cookie-auth/src/main.rs @@ -0,0 +1,53 @@ +extern crate actix; +extern crate actix_web; +extern crate cookie; +extern crate time; +extern crate futures; +extern crate env_logger; + +use actix_web::{server, middleware, App, HttpRequest, HttpResponse}; + +mod auth; +use auth::{RequestIdentity, IdentityService, CookieIdentityPolicy}; + + +fn index(mut req: HttpRequest) -> String { + format!("Hello {}", req.identity().unwrap_or("Anonymous")) +} + +fn login(mut req: HttpRequest) -> HttpResponse { + req.remember("user1".to_owned()); + HttpResponse::Found() + .header("location", "/") + .finish() +} + +fn logout(mut 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(); +}