Rename Guard to CsrfToken

This commit is contained in:
Alex Kotov 2020-10-17 04:56:13 +05:00
parent 28c5a75e95
commit f07620dd8c
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
4 changed files with 12 additions and 10 deletions

View File

@ -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));
}

View File

@ -865,7 +865,7 @@ dependencies = [
[[package]]
name = "rocket_csrf"
version = "0.0.1"
version = "0.0.2"
dependencies = [
"base64 0.13.0",
"rand",

View File

@ -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)),

View File

@ -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> {