iron-csrf 0.0.9

CSRF protection for the web framework Iron
Documentation
//! Module containing the CSRF error types.

use std::error::Error;
use std::fmt;

use iron::prelude::*;
use iron::status;

/// An `enum` of all CSRF related errors.
#[derive(Debug)]
pub enum CsrfError {
    /// The necessary pieces to validate a request were missing. This could mean the either the
    /// cookie or the token, query string, or form field are missing.
    CriteriaMissing,
    /// Input was not able to be converted from Base64.
    NotBase64,
    /// Random data was unable to be generated.
    RngError,
    /// Generic error case. Uncatchable by consumers of this crate.
    Undefined(String),
    /// The CSRF validation failed.
    ValidationFailed,
}

impl Error for CsrfError {
    fn description(&self) -> &str {
        "CSRF Error"
    }
}

impl fmt::Display for CsrfError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self)
    }
}

impl From<CsrfError> for IronError {
    fn from(err: CsrfError) -> IronError {
        IronError {
            response: Response::with((status::Forbidden, format!("{}", err))),
            error: Box::new(err),
        }
    }
}