101 lines
2.7 KiB
Rust
101 lines
2.7 KiB
Rust
use std::fmt::Debug;
|
|
|
|
use hex::FromHex;
|
|
use rand::{thread_rng, Rng};
|
|
use secrecy::{ExposeSecret, SecretString, SecretVec};
|
|
use serde::{Deserialize, Serialize};
|
|
use thiserror::Error;
|
|
|
|
#[derive(Debug, Clone, Deserialize)]
|
|
pub struct Password(pub SecretString);
|
|
impl From<String> for Password {
|
|
fn from(value: String) -> Self {
|
|
Self(SecretString::new(value))
|
|
}
|
|
}
|
|
|
|
impl PartialEq for Password {
|
|
fn eq(&self, other: &Self) -> bool {
|
|
self.0.expose_secret() == other.0.expose_secret()
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone, Deserialize)]
|
|
pub struct UserToken(pub SecretString);
|
|
impl From<String> for UserToken {
|
|
fn from(value: String) -> Self {
|
|
Self(SecretString::new(value))
|
|
}
|
|
}
|
|
|
|
impl PartialEq for UserToken {
|
|
fn eq(&self, other: &Self) -> bool {
|
|
self.0.expose_secret() == other.0.expose_secret()
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone, Deserialize)]
|
|
pub struct UserServerKey(pub SecretString);
|
|
impl From<String> for UserServerKey {
|
|
fn from(value: String) -> Self {
|
|
Self(SecretString::new(value))
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone, Deserialize)]
|
|
pub struct ServerPadlock(pub SecretString);
|
|
impl From<String> for ServerPadlock {
|
|
fn from(value: String) -> Self {
|
|
Self(SecretString::new(value))
|
|
}
|
|
}
|
|
|
|
/// Not actually a hash, just a random value handed out to servers on request.
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct ServerHash(pub String);
|
|
|
|
pub struct PadlockGenerationSecret(pub SecretVec<u8>);
|
|
|
|
impl PadlockGenerationSecret {
|
|
/// Entirely arbitrary
|
|
const MIN_LENGTH_BYTES: usize = 32;
|
|
|
|
fn get_random_secret() -> Vec<u8> {
|
|
let mut rng = thread_rng();
|
|
(0..Self::MIN_LENGTH_BYTES).map(|_| rng.gen()).collect()
|
|
}
|
|
}
|
|
|
|
impl Debug for PadlockGenerationSecret {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
f.debug_tuple("PadlockGenerationSecret")
|
|
.field(&"[REDACTED Vec<u8>]")
|
|
.finish()
|
|
}
|
|
}
|
|
impl Clone for PadlockGenerationSecret {
|
|
fn clone(&self) -> Self {
|
|
PadlockGenerationSecret(self.0.expose_secret().clone().into())
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone, Error)]
|
|
#[error(
|
|
"Padlock secret too short, must be at least {} bytes - here's a fresh secret for you: {}",
|
|
PadlockGenerationSecret::MIN_LENGTH_BYTES,
|
|
hex::encode(PadlockGenerationSecret::get_random_secret())
|
|
)]
|
|
pub struct PadlockSecretTooShort;
|
|
impl FromHex for PadlockGenerationSecret {
|
|
type Error = PadlockSecretTooShort;
|
|
|
|
fn from_hex<T: AsRef<[u8]>>(hex: T) -> Result<Self, Self::Error> {
|
|
let hex = hex.as_ref();
|
|
if hex.len() < Self::MIN_LENGTH_BYTES {
|
|
Err(PadlockSecretTooShort)
|
|
} else {
|
|
Ok(Self(hex.to_vec().into()))
|
|
}
|
|
}
|
|
}
|