Skip to content

Commit

Permalink
Update base64 crate and use it in place of base64-url
Browse files Browse the repository at this point in the history
  • Loading branch information
paolobarbolini authored and Jarema committed Apr 5, 2023
1 parent 6da6e93 commit 87d7f04
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 17 deletions.
3 changes: 1 addition & 2 deletions async-nats/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ categories = ["network-programming", "api-bindings"]

[dependencies]
memchr = "2.4"
base64-url = "1.4.13"
bytes = "1.4.0"
futures = { version = "0.3.26", default-features = false, features = ["std", "async-await"] }
nkeys = "0.2.0"
Expand All @@ -36,7 +35,7 @@ time = { version = "0.3.20", features = ["parsing", "formatting", "serde", "serd
rustls-native-certs = "0.6"
tracing = "0.1"
thiserror = "1.0"
base64 = "0.13"
base64 = "0.21"
tokio-retry = "0.3"
ring = "0.16"
rand = "0.8"
Expand Down
4 changes: 3 additions & 1 deletion async-nats/src/connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ use crate::SocketAddr;
use crate::ToServerAddrs;
use crate::LANG;
use crate::VERSION;
use base64::engine::general_purpose::URL_SAFE_NO_PAD;
use base64::engine::Engine;
use bytes::BytesMut;
use rand::seq::SliceRandom;
use rand::thread_rng;
Expand Down Expand Up @@ -183,7 +185,7 @@ impl Connector {
Ok(signed) => {
connect_info.nkey = Some(key_pair.public_key());
connect_info.signature =
Some(base64_url::encode(&signed));
Some(URL_SAFE_NO_PAD.encode(signed));
}
Err(_) => {
return Err(ConnectError::new(
Expand Down
16 changes: 7 additions & 9 deletions async-nats/src/jetstream/object_store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ use std::{
};

use crate::{HeaderMap, HeaderValue};
use base64::URL_SAFE;
use base64::engine::general_purpose::{STANDARD, URL_SAFE};
use base64::engine::Engine;
use once_cell::sync::Lazy;
use ring::digest::SHA256;
use tokio::io::AsyncReadExt;

use base64_url::base64;
use futures::{Stream, StreamExt};
use regex::Regex;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -57,7 +57,7 @@ pub(crate) fn is_valid_object_name(object_name: &str) -> bool {
}

pub(crate) fn encode_object_name(object_name: &str) -> String {
base64::encode_config(object_name, base64::URL_SAFE)
URL_SAFE.encode(object_name)
}

/// Configuration values for object store buckets.
Expand Down Expand Up @@ -205,7 +205,8 @@ impl ObjectStore {
.stream
.get_last_raw_message_by_subject(subject.as_str())
.await?;
let decoded_payload = base64::decode(message.payload)
let decoded_payload = STANDARD
.decode(message.payload)
.map_err(|err| Box::new(std::io::Error::new(ErrorKind::Other, err)))?;
let object_info = serde_json::from_slice::<ObjectInfo>(&decoded_payload)?;

Expand Down Expand Up @@ -291,10 +292,7 @@ impl ObjectStore {
nuid: object_nuid,
chunks: object_chunks,
size: object_size,
digest: format!(
"SHA-256={}",
base64::encode_config(digest, base64::URL_SAFE)
),
digest: format!("SHA-256={}", URL_SAFE.encode(digest)),
modified: OffsetDateTime::now_utc(),
deleted: false,
};
Expand Down Expand Up @@ -566,7 +564,7 @@ impl tokio::io::AsyncRead for Object<'_> {
if info.pending == 0 {
let digest = self.digest.take().map(|context| context.finish());
if let Some(digest) = digest {
if format!("SHA-256={}", base64::encode_config(digest, URL_SAFE))
if format!("SHA-256={}", URL_SAFE.encode(digest))
!= self.info.digest
{
return Poll::Ready(Err(io::Error::new(
Expand Down
7 changes: 5 additions & 2 deletions async-nats/src/jetstream/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ use std::{

use crate::{header::HeaderName, HeaderMap, HeaderValue};
use crate::{Error, StatusCode};
use base64::engine::general_purpose::STANDARD;
use base64::engine::Engine;
use bytes::Bytes;
use futures::{future::BoxFuture, TryFutureExt};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -1090,11 +1092,12 @@ impl TryFrom<RawMessage> for crate::Message {
type Error = Error;

fn try_from(value: RawMessage) -> Result<Self, Self::Error> {
let decoded_payload = base64::decode(value.payload)
let decoded_payload = STANDARD
.decode(value.payload)
.map_err(|err| Box::new(std::io::Error::new(ErrorKind::Other, err)))?;
let decoded_headers = value
.headers
.map(base64::decode)
.map(|header| STANDARD.decode(header))
.map_or(Ok(None), |v| v.map(Some))?;

let length = decoded_headers
Expand Down
4 changes: 3 additions & 1 deletion async-nats/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// limitations under the License.

use crate::{Authorization, Client, ConnectError, Event, ToServerAddrs};
use base64::engine::general_purpose::URL_SAFE_NO_PAD;
use base64::engine::Engine;
use futures::Future;
use std::fmt::Formatter;
use std::{fmt, path::PathBuf, pin::Pin, sync::Arc, time::Duration};
Expand Down Expand Up @@ -251,7 +253,7 @@ impl ConnectOptions {
let sig = sign_cb(nonce.as_bytes().to_vec())
.await
.map_err(AuthError::new)?;
Ok(base64_url::encode(&sig))
Ok(URL_SAFE_NO_PAD.encode(sig))
})
})),
),
Expand Down
7 changes: 5 additions & 2 deletions async-nats/tests/object_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mod object_store {
use std::{io, time::Duration};

use async_nats::jetstream::object_store::ObjectMeta;
use base64::URL_SAFE;
use base64::Engine;
use futures::StreamExt;
use rand::RngCore;
use ring::digest::SHA256;
Expand Down Expand Up @@ -60,7 +60,10 @@ mod object_store {
}
}
assert_eq!(
format!("SHA-256={}", base64::encode_config(digest, URL_SAFE)),
format!(
"SHA-256={}",
base64::engine::general_purpose::URL_SAFE.encode(digest)
),
object.info.digest
);
assert_eq!(result, bytes);
Expand Down

0 comments on commit 87d7f04

Please sign in to comment.