create new token if none exists
This commit is contained in:
parent
e3bd32c985
commit
eed2dad08f
18
src/cli.rs
18
src/cli.rs
@ -12,7 +12,7 @@ use std::io::Write;
|
||||
use std::process::exit;
|
||||
use std::thread;
|
||||
|
||||
use crate::luks::LuksDevice;
|
||||
use crate::luks::{Fido2LuksToken, LuksDevice};
|
||||
use crate::util::sha256;
|
||||
use std::borrow::Cow;
|
||||
use std::collections::HashSet;
|
||||
@ -27,6 +27,12 @@ impl Display for HexEncoded {
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<[u8]> for HexEncoded {
|
||||
fn as_ref(&self) -> &[u8] {
|
||||
&self.0[..]
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for HexEncoded {
|
||||
type Err = hex::FromHexError;
|
||||
|
||||
@ -628,10 +634,12 @@ pub fn run_cli() -> Fido2LuksResult<()> {
|
||||
tokens.push((id, token));
|
||||
}
|
||||
}
|
||||
if tokens.is_empty() {
|
||||
unimplemented!("// TODO: create new token")
|
||||
}
|
||||
let count = tokens.len();
|
||||
let count = if tokens.is_empty() {
|
||||
dev.add_token(&Fido2LuksToken::with_credentials(&credentials.ids.0, *slot))?;
|
||||
1
|
||||
} else {
|
||||
tokens.len()
|
||||
};
|
||||
for (id, mut token) in tokens {
|
||||
token
|
||||
.credential
|
||||
|
23
src/luks.rs
23
src/luks.rs
@ -87,6 +87,13 @@ impl LuksDevice {
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
pub fn add_token(&mut self, data: &Fido2LuksToken) -> Fido2LuksResult<()> {
|
||||
self.device
|
||||
.token_handle()
|
||||
.json_set(TokenInput::AddToken(&serde_json::to_value(&data).unwrap()))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn remove_token(&mut self, token: u32) -> Fido2LuksResult<()> {
|
||||
self.device
|
||||
.token_handle()
|
||||
@ -274,14 +281,24 @@ pub struct Fido2LuksToken {
|
||||
}
|
||||
|
||||
impl Fido2LuksToken {
|
||||
fn new(credential_id: impl AsRef<[u8]>, slot: u32) -> Self {
|
||||
pub fn new(credential_id: impl AsRef<[u8]>, slot: u32) -> Self {
|
||||
Self::with_credentials(std::iter::once(credential_id), slot)
|
||||
}
|
||||
|
||||
pub fn with_credentials<I: IntoIterator<Item = B>, B: AsRef<[u8]>>(
|
||||
credentials: I,
|
||||
slot: u32,
|
||||
) -> Self {
|
||||
Self {
|
||||
credential: vec![hex::encode(credential_id)].into_iter().collect(),
|
||||
credential: credentials
|
||||
.into_iter()
|
||||
.map(|cred| hex::encode(cred.as_ref()))
|
||||
.collect(),
|
||||
keyslots: vec![slot.to_string()].into_iter().collect(),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
fn default_type() -> &'static str {
|
||||
pub fn default_type() -> &'static str {
|
||||
"fido2luks"
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user