diff options
author | pennae <github@quasiparticle.net> | 2022-07-25 02:26:35 +0200 |
---|---|---|
committer | pennae <github@quasiparticle.net> | 2022-07-25 02:45:55 +0200 |
commit | 9e6572fa282a18fecfb31a2c35c17c0e8c23e371 (patch) | |
tree | c4ead0f54601fe010e2e17b0a8b43f6362b1c1a4 /src/api | |
parent | 9aacaaf079373f8bc25f0533d7f6218e691c5de7 (diff) | |
download | minor-skulk-9e6572fa282a18fecfb31a2c35c17c0e8c23e371.tar.gz minor-skulk-9e6572fa282a18fecfb31a2c35c17c0e8c23e371.tar.xz minor-skulk-9e6572fa282a18fecfb31a2c35c17c0e8c23e371.zip |
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.
Diffstat (limited to 'src/api')
-rw-r--r-- | src/api/auth/account.rs | 12 | ||||
-rw-r--r-- | src/api/auth/device.rs | 17 | ||||
-rw-r--r-- | src/api/auth/invite.rs | 4 | ||||
-rw-r--r-- | src/api/auth/oauth.rs | 12 | ||||
-rw-r--r-- | src/api/mod.rs | 22 |
5 files changed, 24 insertions, 43 deletions
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<KeyFetchToken>, - #[serde(serialize_with = "serialize_dt")] - authAt: DateTime<Utc>, + #[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<Utc>, + #[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<String>, @@ -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<String>, name: Option<String>, - #[serde(serialize_with = "serialize_dt_opt")] - createdTime: Option<DateTime<Utc>>, + #[serde(with = "time::serde::timestamp::option")] + createdTime: Option<OffsetDateTime>, // MISSING createdTimeFormatted - #[serde(serialize_with = "serialize_dt_opt")] - lastAccessTime: Option<DateTime<Utc>>, + #[serde(with = "time::serde::timestamp::option")] + lastAccessTime: Option<OffsetDateTime>, // 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<Reference<'static>> { 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<Utc>, + #[serde(with = "time::serde::timestamp")] + auth_at: OffsetDateTime, #[serde(skip_serializing_if = "Option::is_none")] keys_jwe: Option<String>, } @@ -328,7 +328,7 @@ pub(crate) async fn token_unauthenticated( async fn token_impl( db: &DbConn, user_id: Option<UserID>, - auth_at: Option<DateTime<Utc>>, + auth_at: Option<OffsetDateTime>, req: TokenReq, parent_refresh: Option<OauthTokenID>, parent_session: Option<SessionID>, @@ -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<S, TZ>(dt: &DateTime<TZ>, ser: S) -> Result<S::Ok, S::Error> -where - S: Serializer, - TZ: TimeZone, -{ - ser.serialize_i64(dt.timestamp()) -} - -pub fn serialize_dt_opt<S, TZ>(dt: &Option<DateTime<TZ>>, ser: S) -> Result<S::Ok, S::Error> -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 {} |