Skip to content

Commit d4f3300

Browse files
authored
Do what chrono does (#341)
* Do what chrono does * Target specific dependencies
1 parent 004a188 commit d4f3300

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

Cargo.toml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,19 @@ rust-version = "1.67.0"
1515
[dependencies]
1616
serde_json = "1.0"
1717
serde = {version = "1.0", features = ["derive"] }
18-
ring = { version = "0.17.4", features = ["std"] }
1918
base64 = "0.21.0"
2019
# For PEM decoding
2120
pem = {version = "3", optional = true}
2221
simple_asn1 = {version = "0.6", optional = true}
2322

23+
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
24+
ring = { version = "0.17.4", features = ["std"] }
25+
26+
27+
[target.'cfg(target_arch = "wasm32")'.dependencies]
28+
js-sys = "0.3"
29+
ring = { version = "0.17.4", features = ["std", "wasm32_unknown_unknown_js"] }
30+
2431
[dev-dependencies]
2532
# For the custom time example
2633
time = "0.3"

src/validation.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::borrow::Cow;
22
use std::collections::HashSet;
33
use std::fmt;
44
use std::marker::PhantomData;
5-
use std::time::{SystemTime, UNIX_EPOCH};
65

76
use serde::de::{self, Visitor};
87
use serde::{Deserialize, Deserializer};
@@ -137,9 +136,18 @@ impl Default for Validation {
137136
}
138137

139138
/// Gets the current timestamp in the format expected by JWTs.
139+
#[cfg(not(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi")))))]
140+
#[must_use]
140141
pub fn get_current_timestamp() -> u64 {
141-
let start = SystemTime::now();
142-
start.duration_since(UNIX_EPOCH).expect("Time went backwards").as_secs()
142+
let start = std::time::SystemTime::now();
143+
start.duration_since(std::time::UNIX_EPOCH).expect("Time went backwards").as_secs()
144+
}
145+
146+
/// Gets the current timestamp in the format expected by JWTs.
147+
#[cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))]
148+
#[must_use]
149+
pub fn get_current_timestamp() -> u64 {
150+
js_sys::Date::new_0().get_time() as u64 / 1000
143151
}
144152

145153
#[derive(Deserialize)]

0 commit comments

Comments
 (0)