Rename Guard to CsrfToken
This commit is contained in:
parent
28c5a75e95
commit
f07620dd8c
|
@ -50,14 +50,15 @@ it in forms) or verify it (e.g. to validate form):
|
|||
use rocket::response::Redirect;
|
||||
use rocket::request::Form;
|
||||
use rocket_contrib::templates::Template;
|
||||
use rocket_csrf::CsrfToken;
|
||||
|
||||
#[get("/comments/new")]
|
||||
fn new(csrf: rocket_csrf::Guard) -> Template {
|
||||
fn new(csrf: CsrfToken) -> Template {
|
||||
// your code
|
||||
}
|
||||
|
||||
#[post("/comments", data = "<form>")]
|
||||
fn create(csrf: rocket_csrf::Guard, form: Form<Comment>) -> Redirect {
|
||||
fn create(csrf: CsrfToken, form: Form<Comment>) -> Redirect {
|
||||
// your code
|
||||
}
|
||||
```
|
||||
|
@ -68,7 +69,7 @@ to use it in [templates](https://rocket.rs/v0.4/guide/responses/#templates):
|
|||
|
||||
```rust
|
||||
#[get("/comments/new")]
|
||||
fn new(csrf: rocket_csrf::Guard) -> Template {
|
||||
fn new(csrf: CsrfToken) -> Template {
|
||||
let csrf_token: String = csrf.0;
|
||||
|
||||
// your code
|
||||
|
@ -101,7 +102,7 @@ authenticity token:
|
|||
|
||||
```rust
|
||||
#[post("/comments", data = "<form>")]
|
||||
fn create(csrf: rocket_csrf::Guard, form: Form<Comment>) -> Redirect {
|
||||
fn create(csrf: CsrfToken, form: Form<Comment>) -> Redirect {
|
||||
if let Err(_) = csrf.verify(&form.authenticity_token) {
|
||||
return Redirect::to(uri!(new));
|
||||
}
|
||||
|
|
|
@ -865,7 +865,7 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "rocket_csrf"
|
||||
version = "0.0.1"
|
||||
version = "0.0.2"
|
||||
dependencies = [
|
||||
"base64 0.13.0",
|
||||
"rand",
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
use rocket::response::{Flash, Redirect};
|
||||
use rocket::request::{FlashMessage, Form};
|
||||
use rocket_contrib::templates::Template;
|
||||
use rocket_csrf::CsrfToken;
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct TemplateContext {
|
||||
|
@ -33,7 +34,7 @@ fn index() -> Redirect {
|
|||
}
|
||||
|
||||
#[get("/comments/new")]
|
||||
fn new(csrf: rocket_csrf::Guard, flash: Option<FlashMessage>) -> Template {
|
||||
fn new(csrf: CsrfToken, flash: Option<FlashMessage>) -> Template {
|
||||
let template_context = TemplateContext {
|
||||
csrf_token: csrf.0,
|
||||
flash: flash.map(|msg| format!("{}! {}", msg.name(), msg.msg())),
|
||||
|
@ -43,7 +44,7 @@ fn new(csrf: rocket_csrf::Guard, flash: Option<FlashMessage>) -> Template {
|
|||
}
|
||||
|
||||
#[post("/comments", data = "<form>")]
|
||||
fn create(csrf: rocket_csrf::Guard, form: Form<Comment>) -> Flash<Redirect> {
|
||||
fn create(csrf: CsrfToken, form: Form<Comment>) -> Flash<Redirect> {
|
||||
if let Err(_) = csrf.verify(&form.authenticity_token) {
|
||||
return Flash::error(
|
||||
Redirect::to(uri!(new)),
|
||||
|
|
|
@ -13,7 +13,7 @@ const RAW_TOKEN_LENGTH: usize = 32;
|
|||
|
||||
pub struct Fairing;
|
||||
|
||||
pub struct Guard(pub String);
|
||||
pub struct CsrfToken(pub String);
|
||||
|
||||
pub struct VerificationFailure;
|
||||
|
||||
|
@ -23,7 +23,7 @@ impl Fairing {
|
|||
}
|
||||
}
|
||||
|
||||
impl Guard {
|
||||
impl CsrfToken {
|
||||
pub fn verify(&self, form_authenticity_token: &String)
|
||||
-> Result<(), VerificationFailure>
|
||||
{
|
||||
|
@ -56,7 +56,7 @@ impl RocketFairing for Fairing {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a, 'r> FromRequest<'a, 'r> for Guard {
|
||||
impl<'a, 'r> FromRequest<'a, 'r> for CsrfToken {
|
||||
type Error = ();
|
||||
|
||||
fn from_request(request: &'a Request<'r>) -> Outcome<Self, Self::Error> {
|
||||
|
|
Loading…
Reference in New Issue