diff options
author | Martin Fischer <martin@push-f.com> | 2021-01-19 22:49:09 +0100 |
---|---|---|
committer | Martin Fischer <martin@push-f.com> | 2021-01-19 23:17:45 +0100 |
commit | c55c4a49414f9dbcb637ba5ea765f4af9aebf807 (patch) | |
tree | 691a4a6838a19d5793002f4350c35302561d9152 /src/security.rs | |
parent | e63998de1eb6c348438ecbf6334eb11ec9236bcb (diff) |
request: improve error handling with thiserrorv0.2.1
bump version to 0.2.1
Diffstat (limited to 'src/security.rs')
-rw-r--r-- | src/security.rs | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/src/security.rs b/src/security.rs index 4a17fe3..c12a97f 100644 --- a/src/security.rs +++ b/src/security.rs @@ -3,10 +3,11 @@ use rand::{Rng, distributions::Alphanumeric}; use ::cookie::Cookie; use time::{Duration, OffsetDateTime}; +use thiserror::Error; pub use crate::signed::Key; -use crate::{Error, request::Parts, response::Response}; +use crate::{request::Parts, response::Response}; /// A cookie-based CSRF token to be used with [`crate::request::Body::into_form_csrf`]. pub struct CsrfToken { @@ -14,6 +15,15 @@ pub struct CsrfToken { from_client: bool, } +#[derive(Error, Debug)] +pub enum CsrfError { + #[error("expected csrf cookie")] + NoCookie, + + #[error("csrf parameter doesn't match csrf cookie")] + Mismatch, +} + impl CsrfToken { /// Retrieves the CSRF token from a `csrf` cookie or generates /// a new token and stores it as a cookie if it doesn't exist. @@ -34,12 +44,12 @@ impl CsrfToken { format!("<input name=csrf type=hidden value=\"{}\">", self.token) } - pub(crate) fn matches(&self, str: String) -> Result<(), Error> { + pub(crate) fn matches(&self, str: String) -> Result<(), CsrfError> { if !self.from_client { - return Err(Error::bad_request("expected csrf cookie".to_string())) + return Err(CsrfError::NoCookie) } if self.token != str { - return Err(Error::bad_request("csrf parameter doesn't match csrf cookie".to_string())) + return Err(CsrfError::Mismatch) } Ok(()) } |