Skip to content

Commit e38aad2

Browse files
committed
1. Fixes Token Expiration Issue
2. Updates possible token values to be those allowed in the bsky client
1 parent 24befb0 commit e38aad2

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

rsky-pds/src/account_manager/helpers/email_token.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::apis::com::atproto::server::get_random_token;
22
use crate::common;
3-
use crate::common::time::{from_str_to_utc, less_than_ago_ms, MINUTE};
3+
use crate::common::time::{from_str_to_utc, less_than_ago_s, MINUTE};
44
use crate::db::establish_connection;
55
use crate::models::models::EmailTokenPurpose;
66
use crate::models::EmailToken;
@@ -49,7 +49,7 @@ pub async fn assert_valid_token(
4949
.optional()?;
5050
if let Some(res) = res {
5151
let requested_at = from_str_to_utc(&res.requested_at);
52-
let expired = !less_than_ago_ms(requested_at, expiration_len);
52+
let expired = !less_than_ago_s(requested_at, expiration_len);
5353
if expired {
5454
bail!("Token is expired")
5555
}
@@ -76,7 +76,7 @@ pub async fn assert_valid_token_and_find_did(
7676
.optional()?;
7777
if let Some(res) = res {
7878
let requested_at = from_str_to_utc(&res.requested_at);
79-
let expired = !less_than_ago_ms(requested_at, expiration_len);
79+
let expired = !less_than_ago_s(requested_at, expiration_len);
8080
if expired {
8181
bail!("Token is expired")
8282
}

rsky-pds/src/apis/com/atproto/server/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,12 @@ pub struct AssertionContents {
6969
pub fn get_random_token() -> String {
7070
let token: String = rand::thread_rng()
7171
.sample_iter(&Alphanumeric)
72-
.take(32)
72+
.take(50)
7373
.map(char::from)
7474
.collect();
75-
token[0..5].to_owned() + "-" + &token[5..10]
75+
//Bluesky Client doesn't support 1,8,9,0 in the email verification tokens
76+
let allowed_token = token.replace(&['1', '8', '9', '0'][..], "");
77+
allowed_token[0..5].to_owned() + "-" + &allowed_token[5..10]
7678
}
7779

7880
pub async fn safe_resolve_did_doc(

rsky-pds/src/common/time.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ pub const MINUTE: i32 = SECOND * 60;
99
pub const HOUR: i32 = MINUTE * 60;
1010
pub const DAY: i32 = HOUR * 24;
1111

12-
pub fn less_than_ago_ms(time: DateTime<UtcOffset>, range: i32) -> bool {
12+
pub fn less_than_ago_s(time: DateTime<UtcOffset>, range: i32) -> bool {
1313
let now = SystemTime::now()
1414
.duration_since(SystemTime::UNIX_EPOCH)
1515
.expect("timestamp in micros since UNIX epoch")
16-
.as_micros() as usize;
17-
now < (time.timestamp() as usize + range as usize)
16+
.as_secs() as usize;
17+
let x = time.timestamp() as usize + range as usize;
18+
now < x
1819
}
1920

2021
pub fn from_str_to_micros(str: &String) -> i64 {

0 commit comments

Comments
 (0)