From 5585855c7a2077f8cc552706da7cb091028c0b5b Mon Sep 17 00:00:00 2001 From: Bowen Ding Date: Wed, 21 Apr 2021 22:40:35 +0800 Subject: [PATCH 1/3] Fix panic getting timestamp on wasm32 target --- oauth1-request/Cargo.toml | 3 +++ oauth1-request/src/serializer/auth.rs | 23 +++++++++++++++++------ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/oauth1-request/Cargo.toml b/oauth1-request/Cargo.toml index 25dd9cf..1da0954 100644 --- a/oauth1-request/Cargo.toml +++ b/oauth1-request/Cargo.toml @@ -35,6 +35,9 @@ sha-1 = { version = "0.9", optional = true } oauth1-request = { version = "0.5", path = "", default-features = false } version-sync = "0.9" +[target.'cfg(all(target_arch = "wasm32", target_os = "unknown"))'.dependencies] +js-sys = "0.3" + [features] default = ["derive", "hmac-sha1"] derive = ["oauth1-request-derive"] diff --git a/oauth1-request/src/serializer/auth.rs b/oauth1-request/src/serializer/auth.rs index c71ec6c..bf7b678 100644 --- a/oauth1-request/src/serializer/auth.rs +++ b/oauth1-request/src/serializer/auth.rs @@ -3,7 +3,6 @@ use std::fmt::{Display, Write}; use std::num::NonZeroU64; use std::str; -use std::time::{SystemTime, UNIX_EPOCH}; use rand::prelude::*; @@ -235,11 +234,7 @@ impl<'a, SM: SignatureMethod> Serializer for Authorizer<'a, SM> { let t = if let Some(t) = self.options.timestamp { t.get() } else { - match SystemTime::now().duration_since(UNIX_EPOCH) { - Ok(d) => d.as_secs(), - #[cold] - Err(_) => 1, - } + get_current_timestamp() }; append_to_header!(self, encoded timestamp, t); } @@ -278,6 +273,22 @@ impl<'a, SM: SignatureMethod> Serializer for Authorizer<'a, SM> { } } +fn get_current_timestamp() -> u64 { + cfg_if::cfg_if! { + // `std::time::SystemTime::now` is not supported and panics on `wasm32-unknown-unknown` target + if #[cfg(all(target_arch = "wasm32", target_os = "unknown"))] { + (js_sys::Date::now() / 1000.0) as u64 + } else { + use std::time::{SystemTime, UNIX_EPOCH}; + match SystemTime::now().duration_since(UNIX_EPOCH) { + Ok(d) => d.as_secs(), + #[cold] + Err(_) => 1, + } + } + } +} + // This is worth 72 bits of entropy. The nonce is required to be unique across all requests with // the same timestamp, client and token. Even if you generate the nonce one million times a second // (which is unlikely unless you are DoS-ing the server or something), the expected time it takes From 381e9f65270c591212c691161c8a1899290ad3f5 Mon Sep 17 00:00:00 2001 From: Daiki Mizukami Date: Wed, 6 Oct 2021 21:10:50 +0900 Subject: [PATCH 2/3] Add tests for #9 --- .cargo/config.toml | 2 ++ .github/workflows/ci.yml | 23 ++++++++++++++++++++++- oauth1-request/Cargo.toml | 4 ++++ oauth1-request/tests/pull_9.rs | 14 ++++++++++++++ 4 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 .cargo/config.toml create mode 100644 oauth1-request/tests/pull_9.rs diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..10762f9 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,2 @@ +[target.wasm32-unknown-unknown] +runner = "wasm-bindgen-test-runner" diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d6743c6..564204a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -16,29 +16,50 @@ jobs: - beta - nightly - '1.46.0' + target: + - features: - - derive - hmac-sha1 + include: + - toolchain: stable + features: '' + target: wasm32-unknown-unknown steps: - uses: actions/checkout@v2 - name: Install Rust toolchain uses: actions-rs/toolchain@v1 with: toolchain: ${{ matrix.toolchain }} + target: ${{ matrix.target }} profile: minimal override: true id: toolchain - uses: Swatinem/rust-cache@v1 + with: + key: ${{ matrix.target }} + - name: Install `wasm-bindgen-test-runner` + if: matrix.target == 'wasm32-unknown-unknown' + run: | + VER=0.2.78 + NAME="wasm-bindgen-$VER-x86_64-unknown-linux-musl" + DIGEST=14f1b0ef9225370f0d270efbdbbfe2cf5eb191d57b8eec14ade69c98c71e226f + curl -fLOsS "https://github.com/rustwasm/wasm-bindgen/releases/download/$VER/$NAME.tar.gz" + sha256sum --check --quiet <<< "$DIGEST $NAME.tar.gz" + tar -xzf "$NAME.tar.gz" "$NAME/wasm-bindgen-test-runner" + mv "$NAME/wasm-bindgen-test-runner" /usr/local/bin/ - run: echo 'RUSTFLAGS=--allow unknown_lints' >> $GITHUB_ENV if: matrix.toolchain == '1.46.0' + - run: echo 'CARGO_BUILD_TARGET=${{ matrix.target }}' >> $GITHUB_ENV + if: matrix.target != '' - name: Build `oauth1-request` uses: actions-rs/cargo@v1 with: command: build args: --verbose --tests --manifest-path oauth1-request/Cargo.toml --features=${{ matrix.features }} - name: Build `examples` - if: ${{ matrix.toolchain != '1.46.0' }} + if: ${{ matrix.target == '' && matrix.toolchain != '1.46.0' }} uses: actions-rs/cargo@v1 with: command: build diff --git a/oauth1-request/Cargo.toml b/oauth1-request/Cargo.toml index 4bd8e3c..51fd69e 100644 --- a/oauth1-request/Cargo.toml +++ b/oauth1-request/Cargo.toml @@ -38,6 +38,10 @@ version-sync = "0.9" [target.'cfg(all(target_arch = "wasm32", target_os = "unknown"))'.dependencies] js-sys = "0.3" +[target.'cfg(all(target_arch = "wasm32", target_os = "unknown"))'.dev-dependencies] +getrandom = { version = "0.2", features = ["js"] } +wasm-bindgen-test = "0.3" + [features] default = ["derive", "hmac-sha1"] derive = ["oauth1-request-derive"] diff --git a/oauth1-request/tests/pull_9.rs b/oauth1-request/tests/pull_9.rs new file mode 100644 index 0000000..f51566b --- /dev/null +++ b/oauth1-request/tests/pull_9.rs @@ -0,0 +1,14 @@ +//! Test for . + +#[cfg(all(target_arch = "wasm32", target_os = "unknown"))] +use wasm_bindgen_test::wasm_bindgen_test as test; + +#[test] +fn pull_9() { + let _ = oauth1_request::get( + "", + &(), + &oauth_credentials::Token::from_parts("", "", "", ""), + oauth1_request::HmacSha1, + ); +} From 3bc307a8bc96901880a5940f089ad4ffb81413d9 Mon Sep 17 00:00:00 2001 From: Daiki Mizukami Date: Wed, 6 Oct 2021 21:16:04 +0900 Subject: [PATCH 3/3] Move `wasm32-unknown-unknown` with JS env support behind `js` feature --- .github/workflows/ci.yml | 8 ++++---- oauth1-request/Cargo.toml | 8 +++++--- oauth1-request/src/serializer/auth.rs | 2 +- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 564204a..2f8127b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -19,12 +19,12 @@ jobs: target: - features: - - - - derive - - hmac-sha1 + - js + - js,derive + - js,hmac-sha1 include: - toolchain: stable - features: '' + features: js target: wasm32-unknown-unknown steps: - uses: actions/checkout@v2 diff --git a/oauth1-request/Cargo.toml b/oauth1-request/Cargo.toml index 51fd69e..165d404 100644 --- a/oauth1-request/Cargo.toml +++ b/oauth1-request/Cargo.toml @@ -30,14 +30,15 @@ either = { version = "1.2", optional = true } hmac = { version = "0.11", optional = true } sha-1 = { version = "0.9", optional = true } +[target.'cfg(all(target_arch = "wasm32", target_os = "unknown"))'.dependencies] +# `js` feature +js-sys = { version = "0.3", optional = true } + [dev-dependencies] # Trick to make `proc-macro-crate` work in doctests. oauth1-request = { version = "0.5", path = "", default-features = false } version-sync = "0.9" -[target.'cfg(all(target_arch = "wasm32", target_os = "unknown"))'.dependencies] -js-sys = "0.3" - [target.'cfg(all(target_arch = "wasm32", target_os = "unknown"))'.dev-dependencies] getrandom = { version = "0.2", features = ["js"] } wasm-bindgen-test = "0.3" @@ -46,4 +47,5 @@ wasm-bindgen-test = "0.3" default = ["derive", "hmac-sha1"] derive = ["oauth1-request-derive"] hmac-sha1 = ["hmac", "sha-1"] +js = ["js-sys"] serde = ["oauth-credentials/serde"] diff --git a/oauth1-request/src/serializer/auth.rs b/oauth1-request/src/serializer/auth.rs index 89f70c2..750d139 100644 --- a/oauth1-request/src/serializer/auth.rs +++ b/oauth1-request/src/serializer/auth.rs @@ -276,7 +276,7 @@ impl<'a, SM: SignatureMethod> Serializer for Authorizer<'a, SM> { fn get_current_timestamp() -> u64 { cfg_if::cfg_if! { // `std::time::SystemTime::now` is not supported and panics on `wasm32-unknown-unknown` target - if #[cfg(all(target_arch = "wasm32", target_os = "unknown"))] { + if #[cfg(all(feature = "js", target_arch = "wasm32", target_os = "unknown"))] { (js_sys::Date::now() / 1000.0) as u64 } else { use std::time::{SystemTime, UNIX_EPOCH};