Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Produce JWT as assertion of DIC and DDIC payloads #71

Merged
merged 11 commits into from
Nov 22, 2023
8 changes: 7 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Rust CI

on:
on:
pull_request:
branches:
- main
Expand Down Expand Up @@ -42,3 +42,9 @@ jobs:
cd mediator-server
cargo build
cargo test

- name: Build and Test Plugin for Mediator Coordination
run: |
cd mediator-coordination
cargo build
cargo test
2 changes: 1 addition & 1 deletion did-endpoint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
axum = { version = "0.6.20" }
axum = { version = "0.6.20", features = ["macros"] }
chrono = { version = "0.4.26" }
did-utils = { path = "../did-utils"}
dotenv-flow = "0.15.0"
Expand Down
14 changes: 8 additions & 6 deletions did-endpoint/src/didgen.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::util::{didweb, KeyStore};
use crate::util::{didweb, filesystem::StdFileSystem, keystore::KeyStore};
use did_utils::{
didcore::{
AssertionMethod, Authentication, Document, KeyAgreement, KeyFormat, Service,
VerificationMethod,
},
ldmodel::Context,
key_jwk::jwk::Jwk,
ldmodel::Context,
};
use std::path::Path;

Expand All @@ -28,7 +28,8 @@ pub enum Error {
/// All persistence is handled at `storage_dirpath`.
pub fn didgen(storage_dirpath: &str, server_public_domain: &str) -> Result<Document, Error> {
// Create a new store, which is timestamp-aware
let mut store = KeyStore::new(storage_dirpath);
let mut fs = StdFileSystem;
let mut store = KeyStore::new(&mut fs, storage_dirpath);
tracing::info!("keystore: {}", store.path());

// Generate authentication key
Expand Down Expand Up @@ -168,8 +169,9 @@ pub fn validate_diddoc(storage_dirpath: &str) -> Result<(), String> {

// Validate that keystore exists

let store = KeyStore::latest(storage_dirpath);
if store.is_none() {
let mut fs = StdFileSystem;
let store = KeyStore::latest(&mut fs, storage_dirpath);
if store.is_err() {
return Err(String::from("Missing keystore"));
}

Expand Down Expand Up @@ -280,7 +282,7 @@ mod tests {
}),
prm: Parameters::default(),
};

let diddoc = gen_diddoc(
&storage_dirpath,
&server_public_domain,
Expand Down
5 changes: 2 additions & 3 deletions did-endpoint/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pub mod didgen;
pub mod web;
pub mod plugin;

mod util;
pub mod util;
pub mod web;
2 changes: 1 addition & 1 deletion did-endpoint/src/plugin.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use axum::Router;
use super::{didgen, web};
use axum::Router;
use server_plugin::{Plugin, PluginError};

#[derive(Default)]
Expand Down
89 changes: 89 additions & 0 deletions did-endpoint/src/util/filesystem.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use std::io::{Error as IoError, ErrorKind, Result as IoResult};

// Define a trait for file system operations
pub trait FileSystem: Send + 'static {
fn read_to_string(&self, path: &str) -> IoResult<String>;
fn write(&mut self, path: &str, content: &str) -> IoResult<()>;
fn read_dir_files(&self, path: &str) -> IoResult<Vec<String>>;
fn create_dir_all(&mut self, path: &str) -> IoResult<()>;
// Add other file system operations as needed
}

// Implement the trait for the actual file system
#[derive(Clone, Copy, Default)]
pub struct StdFileSystem;

impl FileSystem for StdFileSystem {
fn read_to_string(&self, path: &str) -> IoResult<String> {
std::fs::read_to_string(path)
}

fn write(&mut self, path: &str, content: &str) -> IoResult<()> {
std::fs::write(path, content)
}

fn read_dir_files(&self, path: &str) -> IoResult<Vec<String>> {
let mut files = vec![];
for entry in std::fs::read_dir(path)? {
let path = entry?.path();
if path.is_file() {
files.push(
path.to_str()
.ok_or(IoError::new(ErrorKind::Other, "InvalidPath"))?
.to_string(),
)
}
}

Ok(files)
}

fn create_dir_all(&mut self, path: &str) -> IoResult<()> {
std::fs::create_dir_all(path)
}

// Implement other file system operations as needed
}

#[cfg(test)]
mod tests {
use std::collections::HashMap;

use super::*;

// Now, for testing, create a mock implementation of the trait
#[derive(Default)]
struct MockFileSystem {
map: HashMap<String, String>,
}

impl FileSystem for MockFileSystem {
fn read_to_string(&self, path: &str) -> IoResult<String> {
Ok(self.map.get(path).cloned().unwrap_or_default())
}

fn write(&mut self, path: &str, content: &str) -> IoResult<()> {
self.map.insert(path.to_string(), content.to_string());
Ok(())
}

fn read_dir_files(&self, _path: &str) -> IoResult<Vec<String>> {
Ok(vec![])
}

fn create_dir_all(&mut self, _path: &str) -> IoResult<()> {
Ok(())
}
}

#[test]
fn can_mock_fs_operations() {
let mut mock_fs = MockFileSystem::default();

let res = mock_fs.write("/file.txt", "2456535e-a316-4d9e-8ab4-74a33d75d1fa");
assert!(res.is_ok());

let content = mock_fs.read_to_string("/file.txt").unwrap();
assert_eq!(&content, "2456535e-a316-4d9e-8ab4-74a33d75d1fa");
}
}
Loading
Loading