Skip to content

Commit e7afadc

Browse files
Be more careful with timestamps
1 parent 11364e5 commit e7afadc

File tree

2 files changed

+3
-15
lines changed

2 files changed

+3
-15
lines changed

src/cookie.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use std::{
2-
sync::Arc,
3-
time::{Duration, SystemTime},
4-
};
1+
use std::sync::Arc;
52

63
use axum::{
74
extract::State,
@@ -10,6 +7,7 @@ use axum::{
107
Json, Router,
118
};
129
use base64::{prelude::BASE64_STANDARD, Engine};
10+
use jsonwebtoken::get_current_timestamp;
1311
use log::info;
1412
use ring::rand::{SecureRandom, SystemRandom};
1513
use serde::{Deserialize, Serialize};
@@ -54,9 +52,7 @@ fn set_cookie(
5452
const QUERY: &str =
5553
"INSERT OR REPLACE INTO Auth (AccountID, Cookie, Expires) VALUES (?, ?, ?);";
5654

57-
let valid_for = Duration::from_secs(valid_secs);
58-
let expires = SystemTime::now() + valid_for;
59-
let expires_timestamp = util::as_timestamp(expires);
55+
let expires_timestamp = get_current_timestamp() + valid_secs;
6056

6157
let mut stmt = db.prepare(QUERY)?;
6258
stmt.bind((1, account_id)).unwrap();

src/util.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::time::SystemTime;
2-
31
use axum::http::HeaderMap;
42
use log::info;
53
use sqlite::{Connection, State};
@@ -86,12 +84,6 @@ pub fn parse_csv(data: &str) -> Vec<Vec<String>> {
8684
.collect()
8785
}
8886

89-
pub fn as_timestamp(st: SystemTime) -> u64 {
90-
st.duration_since(SystemTime::UNIX_EPOCH)
91-
.unwrap_or_default()
92-
.as_secs()
93-
}
94-
9587
pub fn validate_authed_request(headers: &HeaderMap) -> Result<i64, String> {
9688
let auth_header = headers.get("authorization").ok_or("No auth header")?;
9789
// auth header uses the Bearer scheme

0 commit comments

Comments
 (0)