use super::put_object::PutObjectParams;
use super::{Command, Response};
use session::SessionErrorKind::ProtocolError;
use {
Adapter, Capability, CommandType, Domain, HmacAlg, ObjectId, ObjectLabel, Session, SessionError,
};
pub const HMAC_MIN_KEY_SIZE: usize = 8;
pub fn put_hmac_key<A: Adapter, T: Into<Vec<u8>>>(
session: &mut Session<A>,
key_id: ObjectId,
label: ObjectLabel,
domains: Domain,
capabilities: Capability,
algorithm: HmacAlg,
key_bytes: T,
) -> Result<ObjectId, SessionError> {
let hmac_key = key_bytes.into();
if hmac_key.len() < HMAC_MIN_KEY_SIZE || hmac_key.len() > algorithm.max_key_len() {
fail!(
ProtocolError,
"invalid key length for {:?}: {} (min {}, max {})",
algorithm,
hmac_key.len(),
HMAC_MIN_KEY_SIZE,
algorithm.max_key_len()
);
}
session
.send_command(PutHMACKeyCommand {
params: PutObjectParams {
id: key_id,
label,
domains,
capabilities,
algorithm: algorithm.into(),
},
hmac_key,
}).map(|response| response.key_id)
}
#[derive(Serialize, Deserialize, Debug)]
pub(crate) struct PutHMACKeyCommand {
pub params: PutObjectParams,
pub hmac_key: Vec<u8>,
}
impl Command for PutHMACKeyCommand {
type ResponseType = PutHMACKeyResponse;
}
#[derive(Serialize, Deserialize, Debug)]
pub(crate) struct PutHMACKeyResponse {
pub key_id: ObjectId,
}
impl Response for PutHMACKeyResponse {
const COMMAND_TYPE: CommandType = CommandType::PutHMACKey;
}