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-service",
|
||||||
"actix-utils",
|
"actix-utils",
|
||||||
"ahash 0.7.6",
|
"ahash 0.7.6",
|
||||||
"base64",
|
"base64 0.13.1",
|
||||||
"bitflags",
|
"bitflags",
|
||||||
"brotli",
|
"brotli",
|
||||||
"bytes",
|
"bytes",
|
||||||
|
@ -297,6 +297,12 @@ version = "0.13.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8"
|
checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "base64"
|
||||||
|
version = "0.21.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a4a4ddaa51a5bc52a6948f74c06d20aaaddb71924eab79b8c97a8c556e942d6a"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "base64ct"
|
name = "base64ct"
|
||||||
version = "1.5.3"
|
version = "1.5.3"
|
||||||
|
@ -1286,7 +1292,7 @@ version = "8.2.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "09f4f04699947111ec1733e71778d763555737579e44b85844cae8e1940a1828"
|
checksum = "09f4f04699947111ec1733e71778d763555737579e44b85844cae8e1940a1828"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"base64",
|
"base64 0.13.1",
|
||||||
"ring",
|
"ring",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
|
@ -1614,6 +1620,7 @@ dependencies = [
|
||||||
"actix-web",
|
"actix-web",
|
||||||
"argon2",
|
"argon2",
|
||||||
"async-trait",
|
"async-trait",
|
||||||
|
"base64 0.21.0",
|
||||||
"bytes",
|
"bytes",
|
||||||
"chrono",
|
"chrono",
|
||||||
"dotenvy",
|
"dotenvy",
|
||||||
|
@ -1991,7 +1998,7 @@ version = "0.11.13"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "68cc60575865c7831548863cc02356512e3f1dc2f3f82cb837d7fc4cc8f3c97c"
|
checksum = "68cc60575865c7831548863cc02356512e3f1dc2f3f82cb837d7fc4cc8f3c97c"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"base64",
|
"base64 0.13.1",
|
||||||
"bytes",
|
"bytes",
|
||||||
"encoding_rs",
|
"encoding_rs",
|
||||||
"futures-core",
|
"futures-core",
|
||||||
|
@ -2086,7 +2093,7 @@ version = "1.0.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "0864aeff53f8c05aa08d86e5ef839d3dfcf07aeba2db32f12db0ef716e87bd55"
|
checksum = "0864aeff53f8c05aa08d86e5ef839d3dfcf07aeba2db32f12db0ef716e87bd55"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"base64",
|
"base64 0.13.1",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -2340,7 +2347,7 @@ checksum = "dcbc16ddba161afc99e14d1713a453747a2b07fc097d2009f4c300ec99286105"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"ahash 0.7.6",
|
"ahash 0.7.6",
|
||||||
"atoi",
|
"atoi",
|
||||||
"base64",
|
"base64 0.13.1",
|
||||||
"bitflags",
|
"bitflags",
|
||||||
"byteorder",
|
"byteorder",
|
||||||
"bytes",
|
"bytes",
|
||||||
|
|
|
@ -8,6 +8,7 @@ actix-rt = "2.7"
|
||||||
actix-web = "4"
|
actix-web = "4"
|
||||||
argon2 = "0.4"
|
argon2 = "0.4"
|
||||||
async-trait = "0.1.59"
|
async-trait = "0.1.59"
|
||||||
|
base64 = "0.21"
|
||||||
bytes = "1.3"
|
bytes = "1.3"
|
||||||
chrono = { version = "0.4", features = [ "alloc", "clock", "serde" ] }
|
chrono = { version = "0.4", features = [ "alloc", "clock", "serde" ] }
|
||||||
dotenvy = "0.15.6"
|
dotenvy = "0.15.6"
|
||||||
|
|
|
@ -17,6 +17,7 @@ pub enum Error {
|
||||||
BadRequest,
|
BadRequest,
|
||||||
BadSignature,
|
BadSignature,
|
||||||
BadToken(jsonwebtoken::errors::Error),
|
BadToken(jsonwebtoken::errors::Error),
|
||||||
|
Base64,
|
||||||
Crypto(crypto::Error),
|
Crypto(crypto::Error),
|
||||||
Database(sqlx::Error),
|
Database(sqlx::Error),
|
||||||
Invalid(validate::Error),
|
Invalid(validate::Error),
|
||||||
|
@ -35,6 +36,7 @@ impl ResponseError for Error {
|
||||||
Error::BadRequest => StatusCode::BAD_REQUEST,
|
Error::BadRequest => StatusCode::BAD_REQUEST,
|
||||||
Error::BadSignature => StatusCode::UNAUTHORIZED,
|
Error::BadSignature => StatusCode::UNAUTHORIZED,
|
||||||
Error::BadToken(_) => StatusCode::UNAUTHORIZED,
|
Error::BadToken(_) => StatusCode::UNAUTHORIZED,
|
||||||
|
Error::Base64 => StatusCode::BAD_REQUEST,
|
||||||
Error::Invalid(_) => StatusCode::UNPROCESSABLE_ENTITY,
|
Error::Invalid(_) => StatusCode::UNPROCESSABLE_ENTITY,
|
||||||
Error::MalformedApub(_) => StatusCode::UNPROCESSABLE_ENTITY,
|
Error::MalformedApub(_) => StatusCode::UNPROCESSABLE_ENTITY,
|
||||||
Error::MalformedHeader(_) => StatusCode::BAD_REQUEST,
|
Error::MalformedHeader(_) => StatusCode::BAD_REQUEST,
|
||||||
|
@ -56,6 +58,7 @@ impl fmt::Display for Error {
|
||||||
Error::BadRequest => write!(f, "Bad request"),
|
Error::BadRequest => write!(f, "Bad request"),
|
||||||
Error::BadSignature => write!(f, "Bad signature"),
|
Error::BadSignature => write!(f, "Bad signature"),
|
||||||
Error::BadToken(jwt_error) => jwt_error.fmt(f),
|
Error::BadToken(jwt_error) => jwt_error.fmt(f),
|
||||||
|
Error::Base64 => write!(f, "Invalid base64 encoding"),
|
||||||
Error::Crypto(crypto_error) => crypto_error.fmt(f),
|
Error::Crypto(crypto_error) => crypto_error.fmt(f),
|
||||||
Error::Database(sqlx_error) => sqlx_error.fmt(f),
|
Error::Database(sqlx_error) => sqlx_error.fmt(f),
|
||||||
Error::Invalid(validate_error) => validate_error.fmt(f),
|
Error::Invalid(validate_error) => validate_error.fmt(f),
|
||||||
|
|
|
@ -3,4 +3,5 @@ pub mod crypto;
|
||||||
pub mod http;
|
pub mod http;
|
||||||
pub mod password;
|
pub mod password;
|
||||||
pub mod token;
|
pub mod token;
|
||||||
|
pub mod transcode;
|
||||||
pub mod validate;
|
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