Skip to content

Commit

Permalink
Expose whether a given client contains initialized data
Browse files Browse the repository at this point in the history
  • Loading branch information
sosthene-nitrokey committed Apr 4, 2024
1 parent 4916324 commit 0e73519
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
38 changes: 33 additions & 5 deletions src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ mod data;
use core::fmt;

use hkdf::Hkdf;
use littlefs2::{
path,
path::{Path, PathBuf},
};
use rand_core::{CryptoRng, RngCore};
use sha2::Sha256;
use trussed::{
Expand All @@ -14,9 +18,9 @@ use trussed::{
key::{Kind, Secrecy},
platform::Platform,
serde_extensions::ExtensionImpl,
service::{Keystore, ServiceResources},
store::filestore::Filestore,
types::{CoreContext, Location, PathBuf},
service::{ClientFilestore, Keystore, ServiceResources},
store::{filestore::Filestore, Store},
types::{CoreContext, Location},
Bytes,
};

Expand Down Expand Up @@ -211,6 +215,31 @@ impl AuthBackend {
}
}

impl AuthBackend {
/// Returns whether a client is active (has auth data such has PINs)
pub fn is_client_active(
layout: FilesystemLayout,
location: Location,
client: &Path,
store: impl Store,
) -> Result<bool> {
let backend_path = client.join(BACKEND_DIR);
let mut fs;
match layout {
FilesystemLayout::V0 => {
fs = ClientFilestore::new(backend_path, store);
}
FilesystemLayout::V1 => {
fs = ClientFilestore::new_raw(backend_path, store);
}
}

Ok(fs
.read_dir_first(path!(""), location, &trussed::api::NotBefore::None)?
.is_some())
}
}

/// Per-client context for [`AuthBackend`][]
#[derive(Default, Debug)]
pub struct AuthContext {
Expand All @@ -230,8 +259,7 @@ impl ExtensionImpl<AuthExtension> for AuthBackend {
resources: &mut ServiceResources<P>,
) -> Result<AuthReply> {
// FIXME: Have a real implementation from trussed
let mut backend_path = core_ctx.path.clone();
backend_path.push(&PathBuf::from(BACKEND_DIR));
let backend_path = core_ctx.path.join(BACKEND_DIR);
let mut fs;
let mut global_fs;
match self.layout {
Expand Down
11 changes: 6 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,12 @@ pub mod migrate;

use core::str::FromStr;

use serde::{Deserialize, Serialize};
use trussed::{
config::MAX_SHORT_DATA_LENGTH,
types::{Bytes, PathBuf},
use littlefs2::{
path,
path::{Path, PathBuf},
};
use serde::{Deserialize, Serialize};
use trussed::{config::MAX_SHORT_DATA_LENGTH, types::Bytes};

pub use backend::{AuthBackend, AuthContext, FilesystemLayout, MAX_HW_KEY_LEN};
pub use extension::{
Expand All @@ -84,7 +85,7 @@ pub const MAX_PIN_LENGTH: usize = MAX_SHORT_DATA_LENGTH;
/// A PIN.
pub type Pin = Bytes<MAX_PIN_LENGTH>;

const BACKEND_DIR: &str = "backend-auth";
const BACKEND_DIR: &Path = path!("backend-auth");

/// The ID of a PIN within the namespace of a client.
///
Expand Down

0 comments on commit 0e73519

Please sign in to comment.