From a83a099f345cb095063f933872494deab6228e58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Berthaud-M=C3=BCller?= Date: Wed, 27 Apr 2022 19:54:40 +0200 Subject: [PATCH] only use std duration in config --- src/config.rs | 21 +++++---------------- src/models/auth.rs | 9 +++++---- 2 files changed, 10 insertions(+), 20 deletions(-) diff --git a/src/config.rs b/src/config.rs index 8921197..adc9a91 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,8 +1,7 @@ use std::net::SocketAddr; -use std::time::Duration as StdDuration; +use std::time::Duration; use serde::{Deserialize, Deserializer}; -use chrono::Duration as ChronoDuration; use crate::models::name::SerdeName; use crate::dns::TsigAlgorithm; @@ -18,7 +17,7 @@ pub struct Config { pub struct DnsClientConfig { pub server: SocketAddr, #[serde(deserialize_with = "from_std_duration")] - pub timeout: StdDuration, + pub timeout: Duration, pub tsig: Option, } @@ -33,12 +32,12 @@ pub struct TsigConfig { #[derive(Debug, Deserialize)] pub struct WebAppConfig { - #[serde(deserialize_with = "from_chrono_duration")] - pub token_duration: ChronoDuration, + #[serde(deserialize_with = "from_std_duration")] + pub token_duration: Duration, } -fn from_std_duration<'de, D>(deserializer: D) -> Result +fn from_std_duration<'de, D>(deserializer: D) -> Result where D: Deserializer<'de> { use serde::de::Error; @@ -46,16 +45,6 @@ fn from_std_duration<'de, D>(deserializer: D) -> Result .and_then(|string| humantime::parse_duration(&string).map_err(|err| Error::custom(err.to_string()))) } -fn from_chrono_duration<'de, D>(deserializer: D) -> Result - where D: Deserializer<'de> -{ - use serde::de::Error; - String::deserialize(deserializer) - .and_then(|string| humantime::parse_duration(&string).map_err(|err| Error::custom(err.to_string()))) - .and_then(|duration| ChronoDuration::from_std(duration).map_err(|err| Error::custom(err.to_string()))) -} - - fn from_base64<'de, D>(deserializer: D) -> Result, D::Error> where D: Deserializer<'de> { diff --git a/src/models/auth.rs b/src/models/auth.rs index 288695b..b614624 100644 --- a/src/models/auth.rs +++ b/src/models/auth.rs @@ -1,7 +1,8 @@ +use std::time::Duration as StdDuration; + use serde::{Serialize, Deserialize}; use chrono::naive::serde::ts_seconds::serialize as ts_seconds_naive; -use chrono::{Duration, NaiveDateTime, Utc, DateTime}; - +use chrono::{Duration as ChronoDuration, NaiveDateTime, Utc, DateTime}; use diesel::prelude::*; use rand::Rng; use rand::rngs::OsRng; @@ -55,10 +56,10 @@ impl Session { }) } - pub fn new(conn: &diesel::SqliteConnection, user_info: &UserInfo, token_duration: Duration) -> Result { + pub fn new(conn: &diesel::SqliteConnection, user_info: &UserInfo, token_duration: StdDuration) -> Result { use crate::schema::session::dsl::*; - let expires = Utc::now() + token_duration; + let expires = Utc::now() + ChronoDuration::from_std(token_duration).unwrap(); let user_session = Session { session_id: Session::generate_id(),