refactor(router): make routes public and move crypto module to common utils (#176)

Co-authored-by: Arun Raj M <jarnura47@gmail.com>
This commit is contained in:
ItsMeShashank
2022-12-19 19:34:24 +05:30
committed by GitHub
parent 9a4b1d023e
commit bf322c9535
8 changed files with 55 additions and 26 deletions

2
Cargo.lock generated
View File

@ -919,11 +919,13 @@ dependencies = [
"bytes", "bytes",
"error-stack", "error-stack",
"fake", "fake",
"hex",
"masking", "masking",
"nanoid", "nanoid",
"once_cell", "once_cell",
"proptest", "proptest",
"regex", "regex",
"ring",
"router_env", "router_env",
"serde", "serde",
"serde_json", "serde_json",

View File

@ -10,9 +10,11 @@ license = "Apache-2.0"
[dependencies] [dependencies]
bytes = "1.3.0" bytes = "1.3.0"
error-stack = "0.2.4" error-stack = "0.2.4"
hex = "0.4.3"
nanoid = "0.4.0" nanoid = "0.4.0"
once_cell = "1.16.0" once_cell = "1.16.0"
regex = "1.7.0" regex = "1.7.0"
ring = "0.16.20"
serde = { version = "1.0.149", features = ["derive"] } serde = { version = "1.0.149", features = ["derive"] }
serde_json = "1.0.89" serde_json = "1.0.89"
serde_urlencoded = "0.7.1" serde_urlencoded = "0.7.1"

View File

@ -1,11 +1,14 @@
//! Utilities for cryptographic algorithms
use error_stack::{IntoReport, ResultExt}; use error_stack::{IntoReport, ResultExt};
use ring::{aead, hmac}; use ring::{aead, hmac};
use crate::core::errors::{self, CustomResult}; use crate::errors::{self, CustomResult};
const RING_ERR_UNSPECIFIED: &str = "ring::error::Unspecified"; const RING_ERR_UNSPECIFIED: &str = "ring::error::Unspecified";
/// Trait for cryptographically signing messages
pub trait SignMessage { pub trait SignMessage {
/// Takes in a secret and a message and returns the calculated signature as bytes
fn sign_message( fn sign_message(
&self, &self,
_secret: &[u8], _secret: &[u8],
@ -13,7 +16,10 @@ pub trait SignMessage {
) -> CustomResult<Vec<u8>, errors::CryptoError>; ) -> CustomResult<Vec<u8>, errors::CryptoError>;
} }
/// Trait for cryptographically verifying a message against a signature
pub trait VerifySignature { pub trait VerifySignature {
/// Takes in a secret, the signature and the message and verifies the message
/// against the signature
fn verify_signature( fn verify_signature(
&self, &self,
_secret: &[u8], _secret: &[u8],
@ -22,7 +28,9 @@ pub trait VerifySignature {
) -> CustomResult<bool, errors::CryptoError>; ) -> CustomResult<bool, errors::CryptoError>;
} }
/// Trait for cryptographically encoding a message
pub trait EncodeMessage { pub trait EncodeMessage {
/// Takes in a secret and the message and encodes it, returning bytes
fn encode_message( fn encode_message(
&self, &self,
_secret: &[u8], _secret: &[u8],
@ -30,7 +38,9 @@ pub trait EncodeMessage {
) -> CustomResult<(Vec<u8>, Vec<u8>), errors::CryptoError>; ) -> CustomResult<(Vec<u8>, Vec<u8>), errors::CryptoError>;
} }
/// Trait for cryptographically decoding a message
pub trait DecodeMessage { pub trait DecodeMessage {
/// Takes in a secret, an encoded messages and attempts to decode it, returning bytes
fn decode_message( fn decode_message(
&self, &self,
_secret: &[u8], _secret: &[u8],
@ -38,6 +48,9 @@ pub trait DecodeMessage {
) -> CustomResult<Vec<u8>, errors::CryptoError>; ) -> CustomResult<Vec<u8>, errors::CryptoError>;
} }
/// Represents no cryptographic algorithm.
/// Implements all crypto traits and acts like a Nop
#[derive(Debug)]
pub struct NoAlgorithm; pub struct NoAlgorithm;
impl SignMessage for NoAlgorithm { impl SignMessage for NoAlgorithm {
@ -81,6 +94,8 @@ impl DecodeMessage for NoAlgorithm {
} }
} }
/// Represents the HMAC-SHA-256 algorithm
#[derive(Debug)]
pub struct HmacSha256; pub struct HmacSha256;
impl SignMessage for HmacSha256 { impl SignMessage for HmacSha256 {
@ -107,6 +122,8 @@ impl VerifySignature for HmacSha256 {
} }
} }
/// Represents the HMAC-SHA-512 algorithm
#[derive(Debug)]
pub struct HmacSha512; pub struct HmacSha512;
impl SignMessage for HmacSha512 { impl SignMessage for HmacSha512 {
@ -133,6 +150,8 @@ impl VerifySignature for HmacSha512 {
} }
} }
/// Represents the GCM-AES-256 algorithm
#[derive(Debug)]
pub struct GcmAes256 { pub struct GcmAes256 {
nonce: Vec<u8>, nonce: Vec<u8>,
} }

View File

@ -54,3 +54,20 @@ pub enum ValidationError {
#[error("{message}")] #[error("{message}")]
InvalidValue { message: String }, InvalidValue { message: String },
} }
/// Cryptograpic algorithm errors
#[derive(Debug, thiserror::Error)]
pub enum CryptoError {
/// The cryptographic algorithm was unable to encode the message
#[error("Failed to encode given message")]
EncodingFailed,
/// The cryptographic algorithm was unable to decode the message
#[error("Failed to decode given message")]
DecodingFailed,
/// The cryptographic algorithm was unable to sign the message
#[error("Failed to sign message")]
MessageSigningFailed,
/// The cryptographic algorithm was unable to verify the given signature
#[error("Failed to verify signature")]
SignatureVerificationFailed,
}

View File

@ -15,6 +15,7 @@
#![doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR" ), "/", "README.md"))] #![doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR" ), "/", "README.md"))]
pub mod consts; pub mod consts;
pub mod crypto;
pub mod custom_serde; pub mod custom_serde;
pub mod errors; pub mod errors;
pub mod ext_traits; pub mod ext_traits;

View File

@ -451,15 +451,3 @@ pub enum WebhooksFlowError {
#[error("Webhook not received by merchant")] #[error("Webhook not received by merchant")]
NotReceivedByMerchant, NotReceivedByMerchant,
} }
#[derive(Debug, thiserror::Error)]
pub enum CryptoError {
#[error("Failed to encode given message")]
EncodingFailed,
#[error("Failed to decode given message")]
DecodingFailed,
#[error("Failed to sign message")]
MessageSigningFailed,
#[error("Failed to verify signature")]
SignatureVerificationFailed,
}

View File

@ -1,15 +1,15 @@
mod admin; pub mod admin;
mod app; pub mod app;
mod customers; pub mod customers;
mod ephemeral_key; pub mod ephemeral_key;
mod health; pub mod health;
mod mandates; pub mod mandates;
mod metrics; pub mod metrics;
mod payment_methods; pub mod payment_methods;
pub(crate) mod payments; pub mod payments;
mod payouts; pub mod payouts;
mod refunds; pub mod refunds;
mod webhooks; pub mod webhooks;
pub use self::app::{ pub use self::app::{
AppState, Customers, EphemeralKey, Health, Mandates, MerchantAccount, MerchantConnectorAccount, AppState, Customers, EphemeralKey, Health, Mandates, MerchantAccount, MerchantConnectorAccount,

View File

@ -1,4 +1,3 @@
pub(crate) mod crypto;
pub(crate) mod custom_serde; pub(crate) mod custom_serde;
pub(crate) mod db_utils; pub(crate) mod db_utils;
mod ext_traits; mod ext_traits;
@ -8,6 +7,7 @@ mod fp_utils;
pub(crate) mod storage_partitioning; pub(crate) mod storage_partitioning;
pub(crate) use common_utils::{ pub(crate) use common_utils::{
crypto,
ext_traits::{ByteSliceExt, BytesExt, Encode, StringExt, ValueExt}, ext_traits::{ByteSliceExt, BytesExt, Encode, StringExt, ValueExt},
validation::validate_email, validation::validate_email,
}; };