mod constrained;
mod credential;
pub use constrained::*;
pub use credential::*;
use secrecy::ExposeSecret as _;
use secrecy::SecretString;
use ssh_encoding::{self, CheckedSum, Decode, Encode, Reader, Writer};
use ssh_key::public::KeyData;
use crate::proto::{Error, Result};
#[derive(Clone, PartialEq, Debug)]
pub struct AddIdentity {
pub credential: Credential,
}
impl Decode for AddIdentity {
type Error = Error;
fn decode(reader: &mut impl Reader) -> Result<Self> {
let credential = Credential::decode(reader)?;
Ok(Self { credential })
}
}
impl Encode for AddIdentity {
fn encoded_len(&self) -> ssh_encoding::Result<usize> {
self.credential.encoded_len()
}
fn encode(&self, writer: &mut impl Writer) -> ssh_encoding::Result<()> {
self.credential.encode(writer)
}
}
#[derive(Clone, Debug)]
pub struct SmartcardKey {
pub id: String,
pub pin: SecretString,
}
impl Decode for SmartcardKey {
type Error = Error;
fn decode(reader: &mut impl Reader) -> Result<Self> {
let id = String::decode(reader)?;
let pin = String::decode(reader)?.into();
Ok(Self { id, pin })
}
}
impl Encode for SmartcardKey {
fn encoded_len(&self) -> ssh_encoding::Result<usize> {
[
self.id.encoded_len()?,
self.pin.expose_secret().encoded_len()?,
]
.checked_sum()
}
fn encode(&self, writer: &mut impl Writer) -> ssh_encoding::Result<()> {
self.id.encode(writer)?;
self.pin.expose_secret().encode(writer)?;
Ok(())
}
}
impl PartialEq for SmartcardKey {
fn eq(&self, other: &Self) -> bool {
self.id == other.id && self.pin.expose_secret() == other.pin.expose_secret()
}
}
#[derive(Clone, PartialEq, Debug)]
pub struct RemoveIdentity {
pub pubkey: KeyData,
}
impl Decode for RemoveIdentity {
type Error = Error;
fn decode(reader: &mut impl Reader) -> Result<Self> {
let pubkey = reader.read_prefixed(KeyData::decode)?;
Ok(Self { pubkey })
}
}
impl Encode for RemoveIdentity {
fn encoded_len(&self) -> ssh_encoding::Result<usize> {
self.pubkey.encoded_len_prefixed()
}
fn encode(&self, writer: &mut impl Writer) -> ssh_encoding::Result<()> {
self.pubkey.encode_prefixed(writer)
}
}