Skip to content

Commit

Permalink
Update base64 to 0.21.0 (#278)
Browse files Browse the repository at this point in the history
* chore: update base64 to 0.21.0
  • Loading branch information
Alexey-N-Chernyshov committed Jan 21, 2023
1 parent 4008c8b commit 968a10a
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ include = ["src/**/*", "benches/**/*", "tests/**/*", "LICENSE", "README.md", "CH
serde_json = "1.0"
serde = {version = "1.0", features = ["derive"] }
ring = { version = "0.16.5", features = ["std"] }
base64 = "0.13"
base64 = "0.21.0"
# For PEM decoding
pem = {version = "1", optional = true}
simple_asn1 = {version = "0.6", optional = true}
Expand Down
3 changes: 2 additions & 1 deletion src/decoding.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use base64::{engine::general_purpose::STANDARD, Engine};
use serde::de::DeserializeOwned;

use crate::algorithms::AlgorithmFamily;
Expand Down Expand Up @@ -56,7 +57,7 @@ impl DecodingKey {

/// If you're using HMAC with a base64 encoded secret, use this.
pub fn from_base64_secret(secret: &str) -> Result<Self> {
let out = base64::decode(secret)?;
let out = STANDARD.decode(secret)?;
Ok(DecodingKey { family: AlgorithmFamily::Hmac, kind: DecodingKeyKind::SecretOrDer(out) })
}

Expand Down
3 changes: 2 additions & 1 deletion src/encoding.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use base64::{engine::general_purpose::STANDARD, Engine};
use serde::ser::Serialize;

use crate::algorithms::AlgorithmFamily;
Expand All @@ -24,7 +25,7 @@ impl EncodingKey {

/// If you have a base64 HMAC secret, use that.
pub fn from_base64_secret(secret: &str) -> Result<Self> {
let out = base64::decode(secret)?;
let out = STANDARD.decode(secret)?;
Ok(EncodingKey { family: AlgorithmFamily::Hmac, content: out })
}

Expand Down
5 changes: 4 additions & 1 deletion src/header.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::result;

use base64::{engine::general_purpose::STANDARD, Engine};
use serde::{Deserialize, Serialize};

use crate::algorithms::Algorithm;
Expand Down Expand Up @@ -93,7 +94,9 @@ impl Header {
Ok(self
.x5c
.as_ref()
.map(|b64_certs| b64_certs.iter().map(base64::decode).collect::<result::Result<_, _>>())
.map(|b64_certs| {
b64_certs.iter().map(|x| STANDARD.decode(x)).collect::<result::Result<_, _>>()
})
.transpose()?)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/jwk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,13 +370,13 @@ impl JwkSet {
#[cfg(test)]
mod tests {
use crate::jwk::{AlgorithmParameters, JwkSet, OctetKeyType};
use crate::serialization::b64_encode;
use crate::Algorithm;
use serde_json::json;

#[test]
fn check_hs256() {
let key =
base64::encode_config("abcdefghijklmnopqrstuvwxyz012345", base64::URL_SAFE_NO_PAD);
let key = b64_encode("abcdefghijklmnopqrstuvwxyz012345");
let jwks_json = json!({
"keys": [
{
Expand Down
5 changes: 3 additions & 2 deletions src/serialization.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine};
use serde::{Deserialize, Serialize};

use crate::errors::Result;

pub(crate) fn b64_encode<T: AsRef<[u8]>>(input: T) -> String {
base64::encode_config(input, base64::URL_SAFE_NO_PAD)
URL_SAFE_NO_PAD.encode(input)
}

pub(crate) fn b64_decode<T: AsRef<[u8]>>(input: T) -> Result<Vec<u8>> {
base64::decode_config(input, base64::URL_SAFE_NO_PAD).map_err(|e| e.into())
URL_SAFE_NO_PAD.decode(input).map_err(|e| e.into())
}

/// Serializes a struct to JSON and encodes it in base64
Expand Down
3 changes: 2 additions & 1 deletion tests/header/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use base64::{engine::general_purpose::STANDARD, Engine};
use jsonwebtoken::Header;

static CERT_CHAIN: [&str; 3] = include!("cert_chain.json");
Expand All @@ -14,7 +15,7 @@ fn x5c_der_empty_chain() {
#[test]
fn x5c_der_valid_chain() {
let der_chain: Vec<Vec<u8>> =
CERT_CHAIN.iter().map(base64::decode).collect::<Result<_, _>>().unwrap();
CERT_CHAIN.iter().map(|x| STANDARD.decode(x)).collect::<Result<_, _>>().unwrap();

let x5c = Some(CERT_CHAIN.iter().map(ToString::to_string).collect());
let header = Header { x5c, ..Default::default() };
Expand Down

0 comments on commit 968a10a

Please sign in to comment.