From 9e6572fa282a18fecfb31a2c35c17c0e8c23e371 Mon Sep 17 00:00:00 2001 From: pennae Date: Mon, 25 Jul 2022 02:26:35 +0200 Subject: remove dependency on chrono prompted by a cargo audit run. time works just as well and is better maintained. web-push still uses chrono, but from the looks of things it won't be affected. --- src/api/auth/account.rs | 12 ++++++------ src/api/auth/device.rs | 17 +++++++++-------- src/api/auth/invite.rs | 4 ++-- src/api/auth/oauth.rs | 12 ++++++------ src/api/mod.rs | 22 +--------------------- 5 files changed, 24 insertions(+), 43 deletions(-) (limited to 'src/api') diff --git a/src/api/auth/account.rs b/src/api/auth/account.rs index 8f3ac55..d96229d 100644 --- a/src/api/auth/account.rs +++ b/src/api/auth/account.rs @@ -1,13 +1,13 @@ use std::sync::Arc; use anyhow::Result; -use chrono::{DateTime, Utc}; use password_hash::SaltString; use rand::{thread_rng, Rng}; use rocket::request::FromRequest; use rocket::State; use rocket::{serde::json::Json, Request}; use serde::{Deserialize, Serialize}; +use time::OffsetDateTime; use validator::Validate; use crate::api::{Empty, EMPTY}; @@ -19,7 +19,7 @@ use crate::types::{AccountResetID, HawkKey}; use crate::utils::DeferAction; use crate::Config; use crate::{ - api::{auth, serialize_dt}, + api::auth, auth::{AuthSource, Authenticated}, crypto::{AuthPW, KeyBundle, KeyFetchReq, SessionCredentials}, types::{KeyFetchID, OauthToken, SecretKey, User, UserID, VerifyHash}, @@ -56,8 +56,8 @@ pub(crate) struct CreateResp { sessionToken: SessionToken, #[serde(skip_serializing_if = "Option::is_none")] keyFetchToken: Option, - #[serde(serialize_with = "serialize_dt")] - authAt: DateTime, + #[serde(with = "time::serde::timestamp")] + authAt: OffsetDateTime, // MISSING verificationMethod } @@ -165,8 +165,8 @@ pub(crate) struct LoginResp { // NOTE this is the *account* verified status, not the session status. // the spec doesn't say. verified: bool, - #[serde(serialize_with = "serialize_dt")] - authAt: DateTime, + #[serde(with = "time::serde::timestamp")] + authAt: OffsetDateTime, // MISSING metricsEnabled } diff --git a/src/api/auth/device.rs b/src/api/auth/device.rs index 44fbd2a..5201073 100644 --- a/src/api/auth/device.rs +++ b/src/api/auth/device.rs @@ -1,11 +1,11 @@ use std::time::Duration; use std::{collections::HashMap, sync::Arc}; -use chrono::{DateTime, Utc}; use futures::future::join_all; use rocket::{serde::json::Json, State}; use serde::{Deserialize, Serialize}; use serde_json::Value; +use time::OffsetDateTime; use crate::api::auth::{WithSession, WithVerifiedFxaLogin, WithVerifiedSession}; use crate::api::{Empty, EMPTY}; @@ -13,7 +13,7 @@ use crate::db::DbConn; use crate::push::PushClient; use crate::utils::DeferAction; use crate::{ - api::{auth, serialize_dt_opt}, + api::auth, auth::Authenticated, db::Db, types::{ @@ -39,7 +39,8 @@ fn map_error(e: sqlx::Error) -> auth::Error { pub(crate) struct Info { isCurrentDevice: bool, id: DeviceID, - lastAccessTime: i64, + #[serde(with = "time::serde::timestamp")] + lastAccessTime: OffsetDateTime, name: String, r#type: String, pushCallback: Option, @@ -62,7 +63,7 @@ fn device_to_json(current: Option<&DeviceID>, dev: Device) -> Info { Info { isCurrentDevice: Some(&dev.device_id) == current, id: dev.device_id, - lastAccessTime: dev.last_active.timestamp(), + lastAccessTime: dev.last_active, name: dev.name, r#type: dev.type_, pushCallback: pcb, @@ -356,11 +357,11 @@ pub(crate) struct AttachedClient { isCurrentSession: bool, deviceType: Option, name: Option, - #[serde(serialize_with = "serialize_dt_opt")] - createdTime: Option>, + #[serde(with = "time::serde::timestamp::option")] + createdTime: Option, // MISSING createdTimeFormatted - #[serde(serialize_with = "serialize_dt_opt")] - lastAccessTime: Option>, + #[serde(with = "time::serde::timestamp::option")] + lastAccessTime: Option, // MISSING lastAccessTimeFormatted // MISSING approximateLastAccessTime // MISSING approximateLastAccessTimeFormatted diff --git a/src/api/auth/invite.rs b/src/api/auth/invite.rs index e70c3d6..ecd39f9 100644 --- a/src/api/auth/invite.rs +++ b/src/api/auth/invite.rs @@ -1,7 +1,7 @@ use base64::URL_SAFE_NO_PAD; -use chrono::{Duration, Utc}; use rocket::{http::uri::Reference, serde::json::Json, State}; use serde::{Deserialize, Serialize}; +use time::{Duration, OffsetDateTime}; use crate::{api::auth, auth::Authenticated, crypto::random_bytes, db::DbConn, Config}; @@ -13,7 +13,7 @@ pub(crate) async fn generate_invite_link( ttl: Duration, ) -> anyhow::Result> { let code = base64::encode_config(&random_bytes::<32>(), URL_SAFE_NO_PAD); - db.add_invite_code(&code, Utc::now() + ttl).await?; + db.add_invite_code(&code, OffsetDateTime::now_utc() + ttl).await?; Reference::parse_owned(format!("{}/#/register/{}", cfg.location, code)) .map_err(|e| anyhow!("url building failed at {e}")) } diff --git a/src/api/auth/oauth.rs b/src/api/auth/oauth.rs index 384d4b4..25d6150 100644 --- a/src/api/auth/oauth.rs +++ b/src/api/auth/oauth.rs @@ -1,11 +1,11 @@ use std::collections::HashMap; -use chrono::{DateTime, Duration, Local, Utc}; use rocket::serde::json::Json; use serde::{Deserialize, Serialize}; use serde_json::Value; use sha2::Digest; use subtle::ConstantTimeEq; +use time::{Duration, OffsetDateTime}; use crate::api::auth::WithVerifiedFxaLogin; use crate::api::{Empty, EMPTY}; @@ -13,7 +13,7 @@ use crate::crypto::SessionToken; use crate::db::DbConn; use crate::types::oauth::{Scope, ScopeSet}; use crate::{ - api::{auth, serialize_dt}, + api::auth, auth::Authenticated, crypto::SessionCredentials, types::{ @@ -283,8 +283,8 @@ pub(crate) struct TokenResp { scope: ScopeSet, token_type: TokenType, expires_in: u32, - #[serde(serialize_with = "serialize_dt")] - auth_at: DateTime, + #[serde(with = "time::serde::timestamp")] + auth_at: OffsetDateTime, #[serde(skip_serializing_if = "Option::is_none")] keys_jwe: Option, } @@ -328,7 +328,7 @@ pub(crate) async fn token_unauthenticated( async fn token_impl( db: &DbConn, user_id: Option, - auth_at: Option>, + auth_at: Option, req: TokenReq, parent_refresh: Option, parent_session: Option, @@ -385,7 +385,7 @@ async fn token_impl( scope: scope.clone(), parent_refresh, parent_session, - expires_at: (Local::now() + Duration::seconds(ttl.into())).into(), + expires_at: OffsetDateTime::now_utc() + Duration::seconds(ttl.into()), }, ) .await?; diff --git a/src/api/mod.rs b/src/api/mod.rs index 1831659..d5997dc 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -1,30 +1,10 @@ -use chrono::{DateTime, TimeZone}; use rocket::serde::json::Json; -use serde::{Deserialize, Serialize, Serializer}; +use serde::{Deserialize, Serialize}; pub(crate) mod auth; pub(crate) mod oauth; pub(crate) mod profile; -pub fn serialize_dt(dt: &DateTime, ser: S) -> Result -where - S: Serializer, - TZ: TimeZone, -{ - ser.serialize_i64(dt.timestamp()) -} - -pub fn serialize_dt_opt(dt: &Option>, ser: S) -> Result -where - S: Serializer, - TZ: TimeZone, -{ - match dt { - Some(dt) => serialize_dt(dt, ser), - None => ser.serialize_unit(), - } -} - #[derive(Clone, Copy, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct Empty {} -- cgit v1.2.3