@contentauth/c2pa-node 0.4.2 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/settings.rs DELETED
@@ -1,520 +0,0 @@
1
- // Copyright 2025 Adobe. All rights reserved.
2
- // This file is licensed to you under the Apache License,
3
- // Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
4
- // or the MIT license (http://opensource.org/licenses/MIT),
5
- // at your option.
6
-
7
- // Unless required by applicable law or agreed to in writing,
8
- // this software is distributed on an "AS IS" BASIS, WITHOUT
9
- // WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or
10
- // implied. See the LICENSE-MIT and LICENSE-APACHE files for the
11
- // specific language governing permissions and limitations under
12
- // each license.
13
-
14
- use c2pa::settings::Settings;
15
- use neon::prelude::*;
16
- use std::sync::{OnceLock, RwLock};
17
-
18
- use crate::runtime::reload_runtime;
19
-
20
- // Global JSON snapshot used to initialize each Tokio worker thread
21
- static GLOBAL_SETTINGS_TOML: OnceLock<RwLock<Option<String>>> = OnceLock::new();
22
-
23
- fn global_toml() -> &'static RwLock<Option<String>> {
24
- GLOBAL_SETTINGS_TOML.get_or_init(|| RwLock::new(None))
25
- }
26
-
27
- fn set_global_settings_toml(toml: Option<String>) {
28
- *global_toml().write().unwrap() = toml;
29
- }
30
-
31
- pub(crate) fn get_global_settings_toml() -> Option<String> {
32
- global_toml().read().unwrap().clone()
33
- }
34
-
35
- /// Apply settings to the current thread from the global TOML snapshot.
36
- /// This should be called whenever settings might be needed, as settings may have changed.
37
- pub(crate) fn apply_settings_to_current_thread() {
38
- if let Some(toml) = get_global_settings_toml() {
39
- let _ = Settings::from_toml(&toml);
40
- }
41
- }
42
-
43
- /// Parse a JSON string (argument 0) into c2pa-rs Settings and apply them globally.
44
- /// Returns undefined; the JSON snapshot is stored globally for new worker threads.
45
- pub fn load_settings(mut cx: FunctionContext) -> JsResult<JsUndefined> {
46
- let json_arg = cx.argument::<JsString>(0)?;
47
- let json_string = json_arg.value(&mut cx);
48
-
49
- // Apply settings globally using deprecated Settings::from_string with JSON format
50
- #[allow(deprecated)]
51
- match Settings::from_string(&json_string, "json") {
52
- Ok(_) => {
53
- // Settings are now applied globally, return the TOML representation
54
- let toml_string = Settings::to_toml()
55
- .or_else(|e| cx.throw_error(format!("Failed to get settings as TOML: {}", e)))?;
56
- // Save the JSON snapshot for new worker threads
57
- set_global_settings_toml(Some(toml_string));
58
- // Apply settings to current thread
59
- apply_settings_to_current_thread();
60
- reload_runtime();
61
- Ok(cx.undefined())
62
- }
63
- Err(e) => cx.throw_error(format!("Failed to apply settings: {}", e)),
64
- }
65
- }
66
-
67
- /// Parse a TOML string (argument 0) into c2pa-rs Settings and apply them globally.
68
- /// Returns undefined; the TOML snapshot is stored globally for new worker threads.
69
- pub fn load_settings_toml(mut cx: FunctionContext) -> JsResult<JsUndefined> {
70
- let toml_arg = cx.argument::<JsString>(0)?;
71
- let toml_string = toml_arg.value(&mut cx);
72
-
73
- // Apply settings globally using Settings::from_toml
74
- match Settings::from_toml(&toml_string) {
75
- Ok(_) => {
76
- // Settings are now applied globally, save the TOML representation
77
- let toml_string = Settings::to_toml()
78
- .or_else(|e| cx.throw_error(format!("Failed to get settings as TOML: {}", e)))?;
79
- // Save the TOML snapshot for new worker threads
80
- set_global_settings_toml(Some(toml_string));
81
- // Apply settings to current thread
82
- apply_settings_to_current_thread();
83
- reload_runtime();
84
- Ok(cx.undefined())
85
- }
86
- Err(e) => cx.throw_error(format!("Failed to apply TOML settings: {}", e)),
87
- }
88
- }
89
-
90
- /// Returns the JSON settings snapshot currently stored by the Node SDK.
91
- /// This value represents the canonical settings source in this crate and
92
- /// should match the thread-local SETTINGS used internally by the `c2pa`
93
- /// crate on each worker thread (applied in `runtime.rs` using `on_thread_start`).
94
- pub fn get_settings_json(mut cx: FunctionContext) -> JsResult<JsValue> {
95
- match get_global_settings_toml() {
96
- Some(toml_string) => {
97
- // Convert TOML -> JSON string for SDK consumption
98
- let toml_val: toml::Value = match toml::from_str(&toml_string) {
99
- Ok(v) => v,
100
- Err(e) => return cx.throw_error(format!("Failed to parse TOML: {}", e)),
101
- };
102
- let json = match serde_json::to_string(&toml_val) {
103
- Ok(s) => s,
104
- Err(e) => return cx.throw_error(format!("Failed to serialize JSON: {}", e)),
105
- };
106
- Ok(cx.string(json).upcast())
107
- }
108
- None => {
109
- let obj = cx.empty_object();
110
- Ok(obj.upcast())
111
- }
112
- }
113
- }
114
-
115
- /// Get the raw TOML snapshot of the current global settings. Used for debugging.
116
- #[allow(dead_code)]
117
- pub fn get_settings_toml(mut cx: FunctionContext) -> JsResult<JsValue> {
118
- match get_global_settings_toml() {
119
- Some(t) => Ok(cx.string(t).upcast()),
120
- None => Ok(cx.undefined().upcast()),
121
- }
122
- }
123
-
124
- /// Load trust configuration into the main trust settings.
125
- /// Takes a JSON string representing a TrustConfig object and applies it to the trust field.
126
- pub fn load_trust_config(mut cx: FunctionContext) -> JsResult<JsUndefined> {
127
- let json_arg = cx.argument::<JsString>(0)?;
128
- let json_string = json_arg.value(&mut cx);
129
-
130
- // Parse the JSON to validate it's a valid TrustConfig
131
- let trust_config: serde_json::Value = match serde_json::from_str(&json_string) {
132
- Ok(v) => v,
133
- Err(e) => return cx.throw_error(format!("Invalid trust config JSON: {}", e)),
134
- };
135
-
136
- // Get current settings as TOML to preserve all existing settings
137
- let current_toml = get_global_settings_toml().unwrap_or_else(|| {
138
- // If no current settings, start with default settings
139
- Settings::to_toml().unwrap_or_default()
140
- });
141
-
142
- // Parse current settings to merge with new trust config
143
- let mut current_settings: serde_json::Value = match toml::from_str::<toml::Value>(&current_toml)
144
- {
145
- Ok(toml_val) => serde_json::to_value(toml_val)
146
- .unwrap_or_else(|_| serde_json::Value::Object(serde_json::Map::new())),
147
- Err(_) => serde_json::Value::Object(serde_json::Map::new()),
148
- };
149
-
150
- // Update only the trust section
151
- if let serde_json::Value::Object(ref mut map) = current_settings {
152
- map.insert("trust".to_string(), trust_config);
153
- }
154
-
155
- // Convert back to JSON string for Settings::from_string
156
- let settings_json = serde_json::to_string(&current_settings)
157
- .or_else(|e| cx.throw_error(format!("Failed to serialize settings: {}", e)))?;
158
-
159
- // Use the existing load_settings function with merged settings
160
- #[allow(deprecated)]
161
- match Settings::from_string(&settings_json, "json") {
162
- Ok(_) => {
163
- // Update global settings
164
- let full_toml = Settings::to_toml()
165
- .or_else(|e| cx.throw_error(format!("Failed to get settings as TOML: {}", e)))?;
166
- set_global_settings_toml(Some(full_toml));
167
- apply_settings_to_current_thread();
168
- reload_runtime();
169
- Ok(cx.undefined())
170
- }
171
- Err(e) => cx.throw_error(format!("Failed to apply trust config: {}", e)),
172
- }
173
- }
174
-
175
- /// Load trust configuration into the CAWG trust settings.
176
- /// Takes a JSON string representing a TrustConfig object and applies it to the cawg_trust field.
177
- pub fn load_cawg_trust_config(mut cx: FunctionContext) -> JsResult<JsUndefined> {
178
- let json_arg = cx.argument::<JsString>(0)?;
179
- let json_string = json_arg.value(&mut cx);
180
-
181
- // Parse the JSON to validate it's a valid TrustConfig
182
- let trust_config: serde_json::Value = match serde_json::from_str(&json_string) {
183
- Ok(v) => v,
184
- Err(e) => return cx.throw_error(format!("Invalid trust config JSON: {}", e)),
185
- };
186
-
187
- // Get current settings as TOML to preserve all existing settings
188
- let current_toml = get_global_settings_toml().unwrap_or_else(|| {
189
- // If no current settings, start with default settings
190
- Settings::to_toml().unwrap_or_default()
191
- });
192
-
193
- // Parse current settings to merge with new trust config
194
- let mut current_settings: serde_json::Value = match toml::from_str::<toml::Value>(&current_toml)
195
- {
196
- Ok(toml_val) => serde_json::to_value(toml_val)
197
- .unwrap_or_else(|_| serde_json::Value::Object(serde_json::Map::new())),
198
- Err(_) => serde_json::Value::Object(serde_json::Map::new()),
199
- };
200
-
201
- // Update only the cawg_trust section
202
- if let serde_json::Value::Object(ref mut map) = current_settings {
203
- map.insert("cawg_trust".to_string(), trust_config);
204
- }
205
-
206
- // Convert back to JSON string for Settings::from_string
207
- let settings_json = serde_json::to_string(&current_settings)
208
- .or_else(|e| cx.throw_error(format!("Failed to serialize settings: {}", e)))?;
209
-
210
- // Use the existing load_settings function with merged settings
211
- #[allow(deprecated)]
212
- match Settings::from_string(&settings_json, "json") {
213
- Ok(_) => {
214
- // Update global settings
215
- let full_toml = Settings::to_toml()
216
- .or_else(|e| cx.throw_error(format!("Failed to get settings as TOML: {}", e)))?;
217
- set_global_settings_toml(Some(full_toml));
218
- apply_settings_to_current_thread();
219
- reload_runtime();
220
- Ok(cx.undefined())
221
- }
222
- Err(e) => cx.throw_error(format!("Failed to apply CAWG trust config: {}", e)),
223
- }
224
- }
225
-
226
- /// Get the current trust configuration as JSON.
227
- pub fn get_trust_config(mut cx: FunctionContext) -> JsResult<JsValue> {
228
- match get_global_settings_toml() {
229
- Some(toml_string) => {
230
- // Convert TOML -> JSON for processing
231
- let toml_val: toml::Value = match toml::from_str(&toml_string) {
232
- Ok(v) => v,
233
- Err(e) => return cx.throw_error(format!("Failed to parse TOML: {}", e)),
234
- };
235
- let settings: serde_json::Value = match serde_json::to_value(&toml_val) {
236
- Ok(v) => v,
237
- Err(e) => return cx.throw_error(format!("Failed to convert TOML to JSON: {}", e)),
238
- };
239
-
240
- // Extract trust section
241
- let default_trust_config = serde_json::json!({
242
- "verify_trust_list": true,
243
- "user_anchors": null,
244
- "trust_anchors": null,
245
- "trust_config": null,
246
- "allowed_list": null
247
- });
248
- let trust_section = settings.get("trust").unwrap_or(&default_trust_config);
249
-
250
- let json = match serde_json::to_string(trust_section) {
251
- Ok(s) => s,
252
- Err(e) => {
253
- return cx.throw_error(format!("Failed to serialize trust config: {}", e))
254
- }
255
- };
256
- Ok(cx.string(json).upcast())
257
- }
258
- None => {
259
- // Return default trust config
260
- let default_trust = serde_json::json!({
261
- "verify_trust_list": true,
262
- "user_anchors": null,
263
- "trust_anchors": null,
264
- "trust_config": null,
265
- "allowed_list": null
266
- });
267
- Ok(cx.string(default_trust.to_string()).upcast())
268
- }
269
- }
270
- }
271
-
272
- /// Get the current CAWG trust configuration as JSON.
273
- pub fn get_cawg_trust_config(mut cx: FunctionContext) -> JsResult<JsValue> {
274
- match get_global_settings_toml() {
275
- Some(toml_string) => {
276
- // Convert TOML -> JSON for processing
277
- let toml_val: toml::Value = match toml::from_str(&toml_string) {
278
- Ok(v) => v,
279
- Err(e) => return cx.throw_error(format!("Failed to parse TOML: {}", e)),
280
- };
281
- let settings: serde_json::Value = match serde_json::to_value(&toml_val) {
282
- Ok(v) => v,
283
- Err(e) => return cx.throw_error(format!("Failed to convert TOML to JSON: {}", e)),
284
- };
285
-
286
- // Extract cawg_trust section
287
- let default_cawg_trust_config = serde_json::json!({
288
- "verify_trust_list": true,
289
- "user_anchors": null,
290
- "trust_anchors": null,
291
- "trust_config": null,
292
- "allowed_list": null
293
- });
294
- let cawg_trust_section = settings
295
- .get("cawg_trust")
296
- .unwrap_or(&default_cawg_trust_config);
297
-
298
- let json = match serde_json::to_string(cawg_trust_section) {
299
- Ok(s) => s,
300
- Err(e) => {
301
- return cx.throw_error(format!("Failed to serialize CAWG trust config: {}", e))
302
- }
303
- };
304
- Ok(cx.string(json).upcast())
305
- }
306
- None => {
307
- // Return default trust config
308
- let default_trust = serde_json::json!({
309
- "verify_trust_list": true,
310
- "user_anchors": null,
311
- "trust_anchors": null,
312
- "trust_config": null,
313
- "allowed_list": null
314
- });
315
- Ok(cx.string(default_trust.to_string()).upcast())
316
- }
317
- }
318
- }
319
-
320
- /// Load verify configuration into the verify settings.
321
- /// Takes a JSON string representing a VerifyConfig object and applies it to the verify field.
322
- pub fn load_verify_config(mut cx: FunctionContext) -> JsResult<JsUndefined> {
323
- let json_arg = cx.argument::<JsString>(0)?;
324
- let json_string = json_arg.value(&mut cx);
325
-
326
- // Parse the JSON to get the verify config
327
- let verify_config: serde_json::Value = match serde_json::from_str(&json_string) {
328
- Ok(v) => v,
329
- Err(e) => return cx.throw_error(format!("Invalid JSON: {}", e)),
330
- };
331
-
332
- // Get current settings and merge with verify config
333
- let current_toml = get_global_settings_toml().unwrap_or_else(|| {
334
- // If no current settings, create default settings
335
- Settings::to_toml().unwrap_or_else(|_| String::new())
336
- });
337
-
338
- // Parse current settings
339
- let mut current_settings: serde_json::Value = if current_toml.is_empty() {
340
- serde_json::json!({
341
- "version_major": 1,
342
- "version_minor": 0,
343
- "trust": {
344
- "verify_trust_list": true,
345
- "user_anchors": null,
346
- "trust_anchors": null,
347
- "trust_config": null,
348
- "allowed_list": null
349
- },
350
- "cawg_trust": {
351
- "verify_trust_list": true,
352
- "user_anchors": null,
353
- "trust_anchors": null,
354
- "trust_config": null,
355
- "allowed_list": null
356
- },
357
- "core": {
358
- "debug": false,
359
- "hash_alg": "sha256",
360
- "salt_jumbf_boxes": true,
361
- "prefer_box_hash": false,
362
- "merkle_tree_max_proofs": 5,
363
- "compress_manifests": true,
364
- "backing_store_memory_threshold_in_mb": 512
365
- },
366
- "verify": {
367
- "verify_after_reading": true,
368
- "verify_after_sign": true,
369
- "verify_trust": false,
370
- "verify_timestamp_trust": true,
371
- "ocsp_fetch": false,
372
- "remote_manifest_fetch": true,
373
- "skip_ingredient_conflict_resolution": false,
374
- "strict_v1_validation": false
375
- },
376
- "builder": {
377
- "thumbnail": {
378
- "enabled": true
379
- }
380
- }
381
- })
382
- } else {
383
- // Parse existing TOML to JSON
384
- let toml_val: toml::Value = match toml::from_str(&current_toml) {
385
- Ok(v) => v,
386
- Err(e) => return cx.throw_error(format!("Failed to parse current TOML: {}", e)),
387
- };
388
- match serde_json::to_value(&toml_val) {
389
- Ok(v) => v,
390
- Err(e) => return cx.throw_error(format!("Failed to convert TOML to JSON: {}", e)),
391
- }
392
- };
393
-
394
- // Update the verify section with new values
395
- if let Some(verify_section) = current_settings.get_mut("verify") {
396
- if let Some(obj) = verify_section.as_object_mut() {
397
- if let Some(val) = verify_config.get("verify_after_reading") {
398
- obj.insert("verify_after_reading".to_string(), val.clone());
399
- }
400
- if let Some(val) = verify_config.get("verify_after_sign") {
401
- obj.insert("verify_after_sign".to_string(), val.clone());
402
- }
403
- if let Some(val) = verify_config.get("verify_trust") {
404
- obj.insert("verify_trust".to_string(), val.clone());
405
- }
406
- if let Some(val) = verify_config.get("verify_timestamp_trust") {
407
- obj.insert("verify_timestamp_trust".to_string(), val.clone());
408
- }
409
- if let Some(val) = verify_config.get("ocsp_fetch") {
410
- obj.insert("ocsp_fetch".to_string(), val.clone());
411
- }
412
- if let Some(val) = verify_config.get("remote_manifest_fetch") {
413
- obj.insert("remote_manifest_fetch".to_string(), val.clone());
414
- }
415
- if let Some(val) = verify_config.get("skip_ingredient_conflict_resolution") {
416
- obj.insert(
417
- "skip_ingredient_conflict_resolution".to_string(),
418
- val.clone(),
419
- );
420
- }
421
- if let Some(val) = verify_config.get("strict_v1_validation") {
422
- obj.insert("strict_v1_validation".to_string(), val.clone());
423
- }
424
- }
425
- }
426
-
427
- // Convert back to TOML and apply
428
- let updated_toml = match toml::to_string(&current_settings) {
429
- Ok(t) => t,
430
- Err(e) => return cx.throw_error(format!("Failed to convert to TOML: {}", e)),
431
- };
432
-
433
- // Apply the updated settings
434
- match Settings::from_toml(&updated_toml) {
435
- Ok(_) => {
436
- // Settings are now applied globally, save the TOML representation
437
- let toml_string = Settings::to_toml()
438
- .or_else(|e| cx.throw_error(format!("Failed to get settings as TOML: {}", e)))?;
439
- // Save the TOML snapshot for new worker threads
440
- set_global_settings_toml(Some(toml_string));
441
- apply_settings_to_current_thread();
442
- reload_runtime();
443
- Ok(cx.undefined())
444
- }
445
- Err(e) => cx.throw_error(format!("Failed to apply verify settings: {}", e)),
446
- }
447
- }
448
-
449
- /// Get the current verify configuration as JSON.
450
- pub fn get_verify_config(mut cx: FunctionContext) -> JsResult<JsValue> {
451
- match get_global_settings_toml() {
452
- Some(toml_string) => {
453
- // Convert TOML -> JSON for processing
454
- let toml_val: toml::Value = match toml::from_str(&toml_string) {
455
- Ok(v) => v,
456
- Err(e) => return cx.throw_error(format!("Failed to parse TOML: {}", e)),
457
- };
458
- let settings: serde_json::Value = match serde_json::to_value(&toml_val) {
459
- Ok(v) => v,
460
- Err(e) => return cx.throw_error(format!("Failed to convert TOML to JSON: {}", e)),
461
- };
462
-
463
- // Extract verify section
464
- let default_verify_config = serde_json::json!({
465
- "verify_after_reading": true,
466
- "verify_after_sign": true,
467
- "verify_trust": false,
468
- "verify_timestamp_trust": true,
469
- "ocsp_fetch": false,
470
- "remote_manifest_fetch": true,
471
- "skip_ingredient_conflict_resolution": false,
472
- "strict_v1_validation": false
473
- });
474
- let verify_section = settings.get("verify").unwrap_or(&default_verify_config);
475
-
476
- let json = match serde_json::to_string(verify_section) {
477
- Ok(s) => s,
478
- Err(e) => {
479
- return cx.throw_error(format!("Failed to serialize verify config: {}", e))
480
- }
481
- };
482
- Ok(cx.string(json).upcast())
483
- }
484
- None => {
485
- // Return default verify config
486
- let default_verify = serde_json::json!({
487
- "verify_after_reading": true,
488
- "verify_after_sign": true,
489
- "verify_trust": false,
490
- "verify_timestamp_trust": true,
491
- "ocsp_fetch": false,
492
- "remote_manifest_fetch": true,
493
- "skip_ingredient_conflict_resolution": false,
494
- "strict_v1_validation": false
495
- });
496
- Ok(cx.string(default_verify.to_string()).upcast())
497
- }
498
- }
499
- }
500
-
501
- /// Reset settings to their default values.
502
- /// This replicates the private reset() function from c2pa-rs Settings.
503
- pub fn reset_settings(mut cx: FunctionContext) -> JsResult<JsUndefined> {
504
- // Create default settings and get their TOML representation
505
- let default_settings = Settings::default();
506
- let default_toml = toml::to_string(&default_settings)
507
- .or_else(|e| cx.throw_error(format!("Failed to serialize default settings: {}", e)))?;
508
-
509
- // Apply the default settings
510
- match Settings::from_toml(&default_toml) {
511
- Ok(_) => {
512
- // Store the default TOML for worker threads
513
- set_global_settings_toml(Some(default_toml));
514
- apply_settings_to_current_thread();
515
- reload_runtime();
516
- Ok(cx.undefined())
517
- }
518
- Err(e) => cx.throw_error(format!("Failed to apply default settings: {}", e)),
519
- }
520
- }