Format code
This commit is contained in:
parent
113b69505e
commit
10a2780b42
|
@ -55,7 +55,7 @@ fn main() {
|
||||||
rocket_csrf::CsrfConfig::default()
|
rocket_csrf::CsrfConfig::default()
|
||||||
.with_cookie_name("foobar")
|
.with_cookie_name("foobar")
|
||||||
.with_cookie_len(64)
|
.with_cookie_len(64)
|
||||||
.with_lifetime(time::Duration::days(3))
|
.with_lifetime(time::Duration::days(3)),
|
||||||
))
|
))
|
||||||
.attach(Template::fairing())
|
.attach(Template::fairing())
|
||||||
.mount("/", routes![new, create])
|
.mount("/", routes![new, create])
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
#![feature(decl_macro)]
|
#![feature(decl_macro)]
|
||||||
|
|
||||||
#[macro_use] extern crate rocket;
|
#[macro_use]
|
||||||
#[macro_use] extern crate serde_derive;
|
extern crate rocket;
|
||||||
|
#[macro_use]
|
||||||
|
extern crate serde_derive;
|
||||||
|
|
||||||
use rocket::response::{Flash, Redirect};
|
|
||||||
use rocket::request::{FlashMessage, Form};
|
use rocket::request::{FlashMessage, Form};
|
||||||
|
use rocket::response::{Flash, Redirect};
|
||||||
use rocket_contrib::templates::Template;
|
use rocket_contrib::templates::Template;
|
||||||
use rocket_csrf::CsrfToken;
|
use rocket_csrf::CsrfToken;
|
||||||
|
|
||||||
|
@ -46,10 +48,7 @@ fn new(csrf_token: CsrfToken, flash: Option<FlashMessage>) -> Template {
|
||||||
#[post("/comments", data = "<form>")]
|
#[post("/comments", data = "<form>")]
|
||||||
fn create(csrf_token: CsrfToken, form: Form<Comment>) -> Flash<Redirect> {
|
fn create(csrf_token: CsrfToken, form: Form<Comment>) -> Flash<Redirect> {
|
||||||
if let Err(_) = csrf_token.verify(&form.authenticity_token) {
|
if let Err(_) = csrf_token.verify(&form.authenticity_token) {
|
||||||
return Flash::error(
|
return Flash::error(Redirect::to(uri!(new)), "invalid authenticity token");
|
||||||
Redirect::to(uri!(new)),
|
|
||||||
"invalid authenticity token",
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Flash::success(
|
Flash::success(
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#![feature(decl_macro)]
|
#![feature(decl_macro)]
|
||||||
|
|
||||||
#[macro_use] extern crate rocket;
|
#[macro_use]
|
||||||
|
extern crate rocket;
|
||||||
|
|
||||||
const COOKIE_NAME: &str = "foobar";
|
const COOKIE_NAME: &str = "foobar";
|
||||||
const COOKIE_LEN: usize = 64;
|
const COOKIE_LEN: usize = 64;
|
||||||
|
@ -15,18 +16,25 @@ fn rocket() -> rocket::Rocket {
|
||||||
rocket_csrf::CsrfConfig::default()
|
rocket_csrf::CsrfConfig::default()
|
||||||
.with_cookie_name(COOKIE_NAME)
|
.with_cookie_name(COOKIE_NAME)
|
||||||
.with_cookie_len(COOKIE_LEN)
|
.with_cookie_len(COOKIE_LEN)
|
||||||
.with_lifetime(time::Duration::days(3))
|
.with_lifetime(time::Duration::days(3)),
|
||||||
))
|
))
|
||||||
.mount("/", routes![index])
|
.mount("/", routes![index])
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
fn index() {
|
fn index() {}
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn add_csrf_token_to_cookies() {
|
fn add_csrf_token_to_cookies() {
|
||||||
base64::decode(client().get("/").dispatch().cookies().iter().find(|cookie| {
|
base64::decode(
|
||||||
cookie.name() == COOKIE_NAME
|
client()
|
||||||
}).unwrap().value()).unwrap();
|
.get("/")
|
||||||
|
.dispatch()
|
||||||
|
.cookies()
|
||||||
|
.iter()
|
||||||
|
.find(|cookie| cookie.name() == COOKIE_NAME)
|
||||||
|
.unwrap()
|
||||||
|
.value(),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#![feature(decl_macro)]
|
#![feature(decl_macro)]
|
||||||
|
|
||||||
#[macro_use] extern crate rocket;
|
#[macro_use]
|
||||||
|
extern crate rocket;
|
||||||
|
|
||||||
fn client() -> rocket::local::Client {
|
fn client() -> rocket::local::Client {
|
||||||
rocket::local::Client::new(rocket()).unwrap()
|
rocket::local::Client::new(rocket()).unwrap()
|
||||||
|
@ -13,12 +14,19 @@ fn rocket() -> rocket::Rocket {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
fn index() {
|
fn index() {}
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn add_csrf_token_to_cookies() {
|
fn add_csrf_token_to_cookies() {
|
||||||
base64::decode(client().get("/").dispatch().cookies().iter().find(|cookie| {
|
base64::decode(
|
||||||
cookie.name() == "csrf_token"
|
client()
|
||||||
}).unwrap().value()).unwrap();
|
.get("/")
|
||||||
|
.dispatch()
|
||||||
|
.cookies()
|
||||||
|
.iter()
|
||||||
|
.find(|cookie| cookie.name() == "csrf_token")
|
||||||
|
.unwrap()
|
||||||
|
.value(),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
#![feature(decl_macro)]
|
#![feature(decl_macro)]
|
||||||
|
|
||||||
#[macro_use] extern crate rocket;
|
#[macro_use]
|
||||||
|
extern crate rocket;
|
||||||
|
|
||||||
|
use bcrypt::verify;
|
||||||
use rand::RngCore;
|
use rand::RngCore;
|
||||||
use rocket::http::Cookie;
|
use rocket::http::Cookie;
|
||||||
use rocket_csrf::CsrfToken;
|
use rocket_csrf::CsrfToken;
|
||||||
use bcrypt::verify;
|
|
||||||
|
|
||||||
const COOKIE_NAME: &str = "foobar";
|
const COOKIE_NAME: &str = "foobar";
|
||||||
const COOKIE_LEN: usize = 64;
|
const COOKIE_LEN: usize = 64;
|
||||||
|
@ -20,7 +21,7 @@ fn rocket() -> rocket::Rocket {
|
||||||
rocket_csrf::CsrfConfig::default()
|
rocket_csrf::CsrfConfig::default()
|
||||||
.with_cookie_name(COOKIE_NAME)
|
.with_cookie_name(COOKIE_NAME)
|
||||||
.with_cookie_len(COOKIE_LEN)
|
.with_cookie_len(COOKIE_LEN)
|
||||||
.with_lifetime(time::Duration::days(3))
|
.with_lifetime(time::Duration::days(3)),
|
||||||
))
|
))
|
||||||
.mount("/", routes![index])
|
.mount("/", routes![index])
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
#![feature(decl_macro)]
|
#![feature(decl_macro)]
|
||||||
|
|
||||||
#[macro_use] extern crate rocket;
|
#[macro_use]
|
||||||
|
extern crate rocket;
|
||||||
|
|
||||||
|
use bcrypt::verify;
|
||||||
use rand::RngCore;
|
use rand::RngCore;
|
||||||
use rocket::http::Cookie;
|
use rocket::http::Cookie;
|
||||||
use rocket_csrf::CsrfToken;
|
use rocket_csrf::CsrfToken;
|
||||||
use bcrypt::verify;
|
|
||||||
|
|
||||||
fn client() -> rocket::local::Client {
|
fn client() -> rocket::local::Client {
|
||||||
rocket::local::Client::new(rocket()).unwrap()
|
rocket::local::Client::new(rocket()).unwrap()
|
||||||
|
|
Loading…
Reference in New Issue