util: add base64 transcoding wrappers
This commit is contained in:
parent
f54a172e07
commit
c7d5f6d78c
5 changed files with 37 additions and 5 deletions
17
Cargo.lock
generated
17
Cargo.lock
generated
|
@ -30,7 +30,7 @@ dependencies = [
|
|||
"actix-service",
|
||||
"actix-utils",
|
||||
"ahash 0.7.6",
|
||||
"base64",
|
||||
"base64 0.13.1",
|
||||
"bitflags",
|
||||
"brotli",
|
||||
"bytes",
|
||||
|
@ -297,6 +297,12 @@ version = "0.13.1"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8"
|
||||
|
||||
[[package]]
|
||||
name = "base64"
|
||||
version = "0.21.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a4a4ddaa51a5bc52a6948f74c06d20aaaddb71924eab79b8c97a8c556e942d6a"
|
||||
|
||||
[[package]]
|
||||
name = "base64ct"
|
||||
version = "1.5.3"
|
||||
|
@ -1286,7 +1292,7 @@ version = "8.2.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "09f4f04699947111ec1733e71778d763555737579e44b85844cae8e1940a1828"
|
||||
dependencies = [
|
||||
"base64",
|
||||
"base64 0.13.1",
|
||||
"ring",
|
||||
"serde",
|
||||
"serde_json",
|
||||
|
@ -1614,6 +1620,7 @@ dependencies = [
|
|||
"actix-web",
|
||||
"argon2",
|
||||
"async-trait",
|
||||
"base64 0.21.0",
|
||||
"bytes",
|
||||
"chrono",
|
||||
"dotenvy",
|
||||
|
@ -1991,7 +1998,7 @@ version = "0.11.13"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "68cc60575865c7831548863cc02356512e3f1dc2f3f82cb837d7fc4cc8f3c97c"
|
||||
dependencies = [
|
||||
"base64",
|
||||
"base64 0.13.1",
|
||||
"bytes",
|
||||
"encoding_rs",
|
||||
"futures-core",
|
||||
|
@ -2086,7 +2093,7 @@ version = "1.0.1"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0864aeff53f8c05aa08d86e5ef839d3dfcf07aeba2db32f12db0ef716e87bd55"
|
||||
dependencies = [
|
||||
"base64",
|
||||
"base64 0.13.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -2340,7 +2347,7 @@ checksum = "dcbc16ddba161afc99e14d1713a453747a2b07fc097d2009f4c300ec99286105"
|
|||
dependencies = [
|
||||
"ahash 0.7.6",
|
||||
"atoi",
|
||||
"base64",
|
||||
"base64 0.13.1",
|
||||
"bitflags",
|
||||
"byteorder",
|
||||
"bytes",
|
||||
|
|
|
@ -8,6 +8,7 @@ actix-rt = "2.7"
|
|||
actix-web = "4"
|
||||
argon2 = "0.4"
|
||||
async-trait = "0.1.59"
|
||||
base64 = "0.21"
|
||||
bytes = "1.3"
|
||||
chrono = { version = "0.4", features = [ "alloc", "clock", "serde" ] }
|
||||
dotenvy = "0.15.6"
|
||||
|
|
|
@ -17,6 +17,7 @@ pub enum Error {
|
|||
BadRequest,
|
||||
BadSignature,
|
||||
BadToken(jsonwebtoken::errors::Error),
|
||||
Base64,
|
||||
Crypto(crypto::Error),
|
||||
Database(sqlx::Error),
|
||||
Invalid(validate::Error),
|
||||
|
@ -35,6 +36,7 @@ impl ResponseError for Error {
|
|||
Error::BadRequest => StatusCode::BAD_REQUEST,
|
||||
Error::BadSignature => StatusCode::UNAUTHORIZED,
|
||||
Error::BadToken(_) => StatusCode::UNAUTHORIZED,
|
||||
Error::Base64 => StatusCode::BAD_REQUEST,
|
||||
Error::Invalid(_) => StatusCode::UNPROCESSABLE_ENTITY,
|
||||
Error::MalformedApub(_) => StatusCode::UNPROCESSABLE_ENTITY,
|
||||
Error::MalformedHeader(_) => StatusCode::BAD_REQUEST,
|
||||
|
@ -56,6 +58,7 @@ impl fmt::Display for Error {
|
|||
Error::BadRequest => write!(f, "Bad request"),
|
||||
Error::BadSignature => write!(f, "Bad signature"),
|
||||
Error::BadToken(jwt_error) => jwt_error.fmt(f),
|
||||
Error::Base64 => write!(f, "Invalid base64 encoding"),
|
||||
Error::Crypto(crypto_error) => crypto_error.fmt(f),
|
||||
Error::Database(sqlx_error) => sqlx_error.fmt(f),
|
||||
Error::Invalid(validate_error) => validate_error.fmt(f),
|
||||
|
|
|
@ -3,4 +3,5 @@ pub mod crypto;
|
|||
pub mod http;
|
||||
pub mod password;
|
||||
pub mod token;
|
||||
pub mod transcode;
|
||||
pub mod validate;
|
||||
|
|
20
src/util/transcode.rs
Normal file
20
src/util/transcode.rs
Normal file
|
@ -0,0 +1,20 @@
|
|||
use base64::{
|
||||
alphabet,
|
||||
engine::{GeneralPurpose, GeneralPurposeConfig},
|
||||
Engine,
|
||||
};
|
||||
use bytes::Bytes;
|
||||
|
||||
use crate::core::*;
|
||||
|
||||
static ENGINE: GeneralPurpose =
|
||||
GeneralPurpose::new(&alphabet::STANDARD, GeneralPurposeConfig::new());
|
||||
|
||||
pub fn base64_decode(s: &str) -> Result<Bytes> {
|
||||
let bytes = ENGINE.decode(s).map_err(|_| Error::Base64)?;
|
||||
Ok(Bytes::from(bytes))
|
||||
}
|
||||
|
||||
pub fn base64_encode(b: &[u8]) -> String {
|
||||
ENGINE.encode(b)
|
||||
}
|
Loading…
Reference in a new issue