Migrate to new age-plugin API

This commit is contained in:
Jack Grigg
2021-04-13 20:41:34 +12:00
parent 63c8d6c303
commit 33ab757025
4 changed files with 51 additions and 96 deletions
Generated
+2 -2
View File
@@ -18,7 +18,7 @@ dependencies = [
[[package]] [[package]]
name = "age-core" name = "age-core"
version = "0.5.0" version = "0.5.0"
source = "git+https://github.com/str4d/rage.git?rev=a9b5e88aa5816b284ebea23fc84d0203a3c4fdbb#a9b5e88aa5816b284ebea23fc84d0203a3c4fdbb" source = "git+https://github.com/str4d/rage.git?rev=4aa52a2dbb5feed86dcafa3afe8d554975ca5518#4aa52a2dbb5feed86dcafa3afe8d554975ca5518"
dependencies = [ dependencies = [
"base64", "base64",
"c2-chacha", "c2-chacha",
@@ -35,7 +35,7 @@ dependencies = [
[[package]] [[package]]
name = "age-plugin" name = "age-plugin"
version = "0.0.0" version = "0.0.0"
source = "git+https://github.com/str4d/rage.git?rev=a9b5e88aa5816b284ebea23fc84d0203a3c4fdbb#a9b5e88aa5816b284ebea23fc84d0203a3c4fdbb" source = "git+https://github.com/str4d/rage.git?rev=4aa52a2dbb5feed86dcafa3afe8d554975ca5518#4aa52a2dbb5feed86dcafa3afe8d554975ca5518"
dependencies = [ dependencies = [
"age-core", "age-core",
"bech32", "bech32",
+2 -2
View File
@@ -45,5 +45,5 @@ flate2 = "1"
man = "0.3" man = "0.3"
[patch.crates-io] [patch.crates-io]
age-core = { git = "https://github.com/str4d/rage.git", rev = "a9b5e88aa5816b284ebea23fc84d0203a3c4fdbb" } age-core = { git = "https://github.com/str4d/rage.git", rev = "4aa52a2dbb5feed86dcafa3afe8d554975ca5518" }
age-plugin = { git = "https://github.com/str4d/rage.git", rev = "a9b5e88aa5816b284ebea23fc84d0203a3c4fdbb" } age-plugin = { git = "https://github.com/str4d/rage.git", rev = "4aa52a2dbb5feed86dcafa3afe8d554975ca5518" }
+1
View File
@@ -19,6 +19,7 @@ mod yubikey;
use error::Error; use error::Error;
const PLUGIN_NAME: &str = "yubikey";
const BINARY_NAME: &str = "age-plugin-yubikey"; const BINARY_NAME: &str = "age-plugin-yubikey";
const RECIPIENT_PREFIX: &str = "age1yubikey"; const RECIPIENT_PREFIX: &str = "age1yubikey";
const IDENTITY_PREFIX: &str = "age-plugin-yubikey-"; const IDENTITY_PREFIX: &str = "age-plugin-yubikey-";
+46 -92
View File
@@ -4,11 +4,10 @@ use age_plugin::{
recipient::{self, RecipientPluginV1}, recipient::{self, RecipientPluginV1},
Callbacks, Callbacks,
}; };
use bech32::{FromBase32, Variant};
use std::collections::HashMap; use std::collections::HashMap;
use std::io; use std::io;
use crate::{format, p256::Recipient, yubikey, IDENTITY_PREFIX, RECIPIENT_PREFIX}; use crate::{format, p256::Recipient, yubikey, PLUGIN_NAME};
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub(crate) struct RecipientPlugin { pub(crate) struct RecipientPlugin {
@@ -17,75 +16,45 @@ pub(crate) struct RecipientPlugin {
} }
impl RecipientPluginV1 for RecipientPlugin { impl RecipientPluginV1 for RecipientPlugin {
fn add_recipients<'a, I: Iterator<Item = &'a str>>( fn add_recipient(
&mut self, &mut self,
recipients: I, index: usize,
) -> Result<(), Vec<recipient::Error>> { plugin_name: &str,
let errors: Vec<_> = recipients bytes: &[u8],
.enumerate() ) -> Result<(), recipient::Error> {
.filter_map(|(index, recipient)| { if let Some(pk) = if plugin_name == PLUGIN_NAME {
if let Some(pk) = bech32::decode(recipient) Recipient::from_bytes(&bytes)
.ok() } else {
.and_then(|(hrp, data, variant)| { None
if hrp == RECIPIENT_PREFIX && variant == Variant::Bech32 { } {
Some(data) self.recipients.push(pk);
} else {
None
}
})
.and_then(|data| Vec::from_base32(&data).ok())
.and_then(|bytes| Recipient::from_bytes(&bytes))
{
self.recipients.push(pk);
None
} else {
Some(recipient::Error::Recipient {
index,
message: "Invalid recipient".to_owned(),
})
}
})
.collect();
if errors.is_empty() {
Ok(()) Ok(())
} else { } else {
Err(errors) Err(recipient::Error::Recipient {
index,
message: "Invalid recipient".to_owned(),
})
} }
} }
fn add_identities<'a, I: Iterator<Item = &'a str>>( fn add_identity(
&mut self, &mut self,
identities: I, index: usize,
) -> Result<(), Vec<recipient::Error>> { plugin_name: &str,
let errors: Vec<_> = identities bytes: &[u8],
.enumerate() ) -> Result<(), recipient::Error> {
.filter_map(|(index, identity)| { if let Some(stub) = if plugin_name == PLUGIN_NAME {
if let Some(stub) = bech32::decode(identity) yubikey::Stub::from_bytes(&bytes, index)
.ok() } else {
.and_then(|(hrp, data, variant)| { None
if hrp == IDENTITY_PREFIX.to_lowercase() && variant == Variant::Bech32 { } {
Some(data) self.yubikeys.push(stub);
} else {
None
}
})
.and_then(|data| Vec::from_base32(&data).ok())
.and_then(|bytes| yubikey::Stub::from_bytes(&bytes, index))
{
self.yubikeys.push(stub);
None
} else {
Some(recipient::Error::Identity {
index,
message: "Invalid Yubikey stub".to_owned(),
})
}
})
.collect();
if errors.is_empty() {
Ok(()) Ok(())
} else { } else {
Err(errors) Err(recipient::Error::Identity {
index,
message: "Invalid Yubikey stub".to_owned(),
})
} }
} }
@@ -135,39 +104,24 @@ pub(crate) struct IdentityPlugin {
} }
impl IdentityPluginV1 for IdentityPlugin { impl IdentityPluginV1 for IdentityPlugin {
fn add_identities<'a, I: Iterator<Item = &'a str>>( fn add_identity(
&mut self, &mut self,
identities: I, index: usize,
) -> Result<(), Vec<identity::Error>> { plugin_name: &str,
let errors: Vec<_> = identities bytes: &[u8],
.enumerate() ) -> Result<(), identity::Error> {
.filter_map(|(index, identity)| { if let Some(stub) = if plugin_name == PLUGIN_NAME {
if let Some(stub) = bech32::decode(identity) yubikey::Stub::from_bytes(&bytes, index)
.ok() } else {
.and_then(|(hrp, data, variant)| { None
if hrp == IDENTITY_PREFIX.to_lowercase() && variant == Variant::Bech32 { } {
Some(data) self.yubikeys.push(stub);
} else {
None
}
})
.and_then(|data| Vec::from_base32(&data).ok())
.and_then(|bytes| yubikey::Stub::from_bytes(&bytes, index))
{
self.yubikeys.push(stub);
None
} else {
Some(identity::Error::Identity {
index,
message: "Invalid Yubikey stub".to_owned(),
})
}
})
.collect();
if errors.is_empty() {
Ok(()) Ok(())
} else { } else {
Err(errors) Err(identity::Error::Identity {
index,
message: "Invalid Yubikey stub".to_owned(),
})
} }
} }