-
Notifications
You must be signed in to change notification settings - Fork 57
/
client.rs
162 lines (143 loc) · 5.87 KB
/
client.rs
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright 2022 Adobe. All rights reserved.
// This file is licensed to you under the Apache License,
// Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
// or the MIT license (http://opensource.org/licenses/MIT),
// at your option.
// Unless required by applicable law or agreed to in writing,
// this software is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or
// implied. See the LICENSE-MIT and LICENSE-APACHE files for the
// specific language governing permissions and limitations under
// each license.
//! Example C2PA client application
use std::path::PathBuf;
use anyhow::Result;
use c2pa::{
assertions::{c2pa_action, labels, Action, Actions, CreativeWork, Exif, SchemaDotOrgPerson},
create_signer, Builder, ClaimGeneratorInfo, Ingredient, Reader, Relationship,
};
use c2pa_crypto::SigningAlg;
const GENERATOR: &str = "test_app/0.1";
const INDENT_SPACE: usize = 2;
// Example for reading the contents of a manifest store, recursively showing nested manifests
fn show_manifest(reader: &Reader, manifest_label: &str, level: usize) -> Result<()> {
let indent = " ".repeat(level * INDENT_SPACE);
println!("{indent}manifest_label: {manifest_label}");
if let Some(manifest) = reader.get_manifest(manifest_label) {
println!(
"{}title: {} , format: {}, instance_id: {}",
indent,
manifest.title().unwrap_or_default(),
manifest.format(),
manifest.instance_id()
);
for assertion in manifest.assertions().iter() {
println!("{}", assertion.label_with_instance());
match assertion.label() {
labels::ACTIONS => {
let actions: Actions = assertion.to_assertion()?;
for action in actions.actions {
println!("{}{}", indent, action.action());
}
}
labels::CREATIVE_WORK => {
let creative_work: CreativeWork = assertion.to_assertion()?;
if let Some(authors) = creative_work.author() {
for author in authors {
if let Some(name) = author.name() {
println!("{indent}author = {name} ");
}
}
}
if let Some(url) = creative_work.get::<String>("url") {
println!("{indent}url = {url} ");
}
}
_ => {}
}
}
for ingredient in manifest.ingredients().iter() {
println!("{}Ingredient title:{}", indent, ingredient.title());
if let Some(validation_status) = ingredient.validation_status() {
for status in validation_status {
println!(
"Ingredient validation status: {}: {}",
status.code(),
status.explanation().unwrap_or_default()
);
}
}
if let Some(label) = ingredient.active_manifest() {
show_manifest(reader, label, level + 1)?;
}
}
}
Ok(())
}
pub fn main() -> Result<()> {
let args: Vec<String> = std::env::args().collect();
// allow passing in source and dest paths or use defaults
let (src, dst) = match args.len() >= 3 {
true => (args[1].as_str(), args[2].as_str()),
false => (
"sdk/tests/fixtures/earth_apollo17.jpg",
"target/tmp/client.jpg",
),
};
let source = PathBuf::from(src);
let dest = PathBuf::from(dst);
// if a filepath was provided on the command line, read it as a parent file
let mut parent = Ingredient::from_file(source.as_path())?;
parent.set_relationship(Relationship::ParentOf);
// create an action assertion stating that we imported this file
let actions = Actions::new().add_action(
Action::new(c2pa_action::OPENED)
.set_parameter("identifier", parent.instance_id().to_owned())?,
);
// build a creative work assertion
let creative_work =
CreativeWork::new().add_author(SchemaDotOrgPerson::new().set_name("me")?)?;
let exif = Exif::from_json_str(
r#"{
"@context" : {
"exif": "http://ns.adobe.com/exif/1.0/"
},
"exif:GPSVersionID": "2.2.0.0",
"exif:GPSLatitude": "39,21.102N",
"exif:GPSLongitude": "74,26.5737W",
"exif:GPSAltitudeRef": 0,
"exif:GPSAltitude": "100963/29890",
"exif:GPSTimeStamp": "2019-09-22T18:22:57Z"
}"#,
)?;
// create a new Manifest
let mut builder = Builder::new();
builder
.set_claim_generator_info(ClaimGeneratorInfo::new(GENERATOR))
.add_ingredient(parent)
.add_assertion(Actions::LABEL, &actions)?
.add_assertion(CreativeWork::LABEL, &creative_work)?
.add_assertion(Exif::LABEL, &exif)?;
// sign and embed into the target file
let signcert_path = "sdk/tests/fixtures/certs/es256.pub";
let pkey_path = "sdk/tests/fixtures/certs/es256.pem";
let signer = create_signer::from_files(signcert_path, pkey_path, SigningAlg::Es256, None)?;
builder.sign_file(&*signer, &source, &dest)?;
let reader = Reader::from_file(&dest)?;
// example of how to print out the whole manifest as json
println!("{reader}\n");
// walk through the manifests and show the contents
if let Some(manifest_label) = reader.active_label() {
show_manifest(&reader, manifest_label, 0)?;
}
if let Some(validation_status) = reader.validation_status() {
for status in validation_status {
println!(
"Validation status: {}: {}",
status.code(),
status.explanation().unwrap_or_default()
);
}
}
Ok(())
}