Repository navigation
Expand file tree
/
Copy pathpresentation.rs
More file actions
134 lines (125 loc) · 4.23 KB
/
Copy pathpresentation.rs
File metadata and controls
134 lines (125 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
use std::io::{stdin, stdout, BufReader, BufWriter, Read};
use anyhow::Result;
use clap::{Args, Subcommand};
use didkit::{
generate_proof, ContextLoader, LinkedDataProofOptions, ProofFormat, VerifiablePresentation, JWK,
};
use tracing::warn;
use crate::{get_ssh_agent_sock, opts::ResolverOptions, KeyArg, ProofOptions};
#[derive(Subcommand)]
pub enum PresentationCmd {
/// Issue Presentation
Issue(Box<PresentationIssueArgs>),
/// Verify Presentation
Verify(PresentationVerifyArgs),
}
#[derive(Args)]
pub struct PresentationIssueArgs {
#[clap(flatten)]
key: KeyArg,
#[clap(flatten)]
proof_options: ProofOptions,
#[clap(flatten)]
resolver_options: ResolverOptions,
}
#[derive(Args)]
pub struct PresentationVerifyArgs {
#[clap(flatten)]
resolver_options: ResolverOptions,
#[clap(flatten)]
proof_options: ProofOptions,
}
pub async fn cli(cmd: PresentationCmd) -> Result<()> {
match cmd {
PresentationCmd::Issue(cmd_issue) => issue(*cmd_issue).await?,
PresentationCmd::Verify(cmd_verify) => verify(cmd_verify).await?,
};
Ok(())
}
pub async fn issue(args: PresentationIssueArgs) -> Result<()> {
let resolver = args.resolver_options.to_resolver();
let mut context_loader = ContextLoader::default();
let presentation_reader = BufReader::new(stdin());
let mut presentation: VerifiablePresentation =
serde_json::from_reader(presentation_reader).unwrap();
let jwk_opt: Option<JWK> = args.key.get_jwk_opt();
let ssh_agent_sock_opt = if args.key.ssh_agent {
Some(get_ssh_agent_sock())
} else {
None
};
let proof_format = args.proof_options.proof_format.clone();
let options = LinkedDataProofOptions::from(args.proof_options);
match proof_format {
ProofFormat::JWT => {
if ssh_agent_sock_opt.is_some() {
todo!("ssh-agent for JWT not implemented");
}
let jwt = presentation
.generate_jwt(jwk_opt.as_ref(), &options, &resolver)
.await
.unwrap();
print!("{jwt}");
}
ProofFormat::LDP => {
let proof = generate_proof(
&presentation,
jwk_opt.as_ref(),
options,
&resolver,
&mut context_loader,
ssh_agent_sock_opt.as_deref(),
)
.await
.unwrap();
presentation.add_proof(proof);
let stdout_writer = BufWriter::new(stdout());
serde_json::to_writer(stdout_writer, &presentation).unwrap();
}
_ => {
panic!("Unexpected proof format: {:?}", proof_format);
}
}
Ok(())
}
pub async fn verify(args: PresentationVerifyArgs) -> Result<()> {
let resolver = args.resolver_options.to_resolver();
let mut context_loader = ContextLoader::default();
let mut presentation_reader = BufReader::new(stdin());
let proof_format = args.proof_options.proof_format.clone();
let options = LinkedDataProofOptions::from(args.proof_options);
let result = match proof_format {
ProofFormat::JWT => {
let mut jwt = String::new();
presentation_reader.read_to_string(&mut jwt).unwrap();
let trimmed_jwt = jwt.trim();
if jwt != trimmed_jwt {
warn!("JWT was trimmed for extraneous whitespaces and new lines.");
}
VerifiablePresentation::verify_jwt(
trimmed_jwt,
Some(options),
&resolver,
&mut context_loader,
)
.await
}
ProofFormat::LDP => {
let presentation: VerifiablePresentation =
serde_json::from_reader(presentation_reader).unwrap();
presentation.validate_unsigned().unwrap();
presentation
.verify(Some(options), &resolver, &mut context_loader)
.await
}
_ => {
panic!("Unexpected proof format: {:?}", proof_format);
}
};
let stdout_writer = BufWriter::new(stdout());
serde_json::to_writer(stdout_writer, &result).unwrap();
if !result.errors.is_empty() {
std::process::exit(2);
}
Ok(())
}