Skip to content

Commit

Permalink
Do what chrono does (#341)
Browse files Browse the repository at this point in the history
* Do what chrono does

* Target specific dependencies
  • Loading branch information
BrandonDyer64 authored Nov 7, 2023
1 parent 004a188 commit d4f3300
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
9 changes: 8 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,19 @@ rust-version = "1.67.0"
[dependencies]
serde_json = "1.0"
serde = {version = "1.0", features = ["derive"] }
ring = { version = "0.17.4", features = ["std"] }
base64 = "0.21.0"
# For PEM decoding
pem = {version = "3", optional = true}
simple_asn1 = {version = "0.6", optional = true}

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
ring = { version = "0.17.4", features = ["std"] }


[target.'cfg(target_arch = "wasm32")'.dependencies]
js-sys = "0.3"
ring = { version = "0.17.4", features = ["std", "wasm32_unknown_unknown_js"] }

[dev-dependencies]
# For the custom time example
time = "0.3"
Expand Down
14 changes: 11 additions & 3 deletions src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::borrow::Cow;
use std::collections::HashSet;
use std::fmt;
use std::marker::PhantomData;
use std::time::{SystemTime, UNIX_EPOCH};

use serde::de::{self, Visitor};
use serde::{Deserialize, Deserializer};
Expand Down Expand Up @@ -137,9 +136,18 @@ impl Default for Validation {
}

/// Gets the current timestamp in the format expected by JWTs.
#[cfg(not(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi")))))]
#[must_use]
pub fn get_current_timestamp() -> u64 {
let start = SystemTime::now();
start.duration_since(UNIX_EPOCH).expect("Time went backwards").as_secs()
let start = std::time::SystemTime::now();
start.duration_since(std::time::UNIX_EPOCH).expect("Time went backwards").as_secs()
}

/// Gets the current timestamp in the format expected by JWTs.
#[cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))]
#[must_use]
pub fn get_current_timestamp() -> u64 {
js_sys::Date::new_0().get_time() as u64 / 1000
}

#[derive(Deserialize)]
Expand Down

0 comments on commit d4f3300

Please sign in to comment.