Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do what chrono does #341

Merged
merged 2 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
BrandonDyer64 marked this conversation as resolved.
Show resolved Hide resolved
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