json-proof-token 0.4.1

A library implementation based on the new JOSE Working Group draft specifications: JSON Web Proof (JWP), JSON Proof Token (JPT) and JSON Proof Algorithm (JPA).
Documentation
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
// Copyright 2025 Fondazione LINKS

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

//     http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use serde::{Deserialize, Serialize};

use crate::{
    jpa::algs::{PresentationProofAlgorithm, ProofAlgorithm},
    jpt::claims::Claims, jwk::key::Jwk,
};

#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
/// JWP Issuer Protected Header, defined in https://datatracker.ietf.org/doc/html/draft-ietf-jose-json-web-proof-08#name-issuer-protected-header
pub struct IssuerProtectedHeader {
    /// JWP type (JPT)
    #[serde(skip_serializing_if = "Option::is_none")]
    typ: Option<String>,
    /// Algorithm used for the JWP
    alg: ProofAlgorithm,
    /// ID for the key used for the JWP.
    #[serde(skip_serializing_if = "Option::is_none")]
    kid: Option<String>,
    /// ClaimsID, identifier for a set of claim names without explicitly listing 
    /// them in order to ensure externally resolve of claims. Application dependent.
    #[serde(skip_serializing_if = "Option::is_none")]
    cid: Option<String>,
    /// if you want you can put the claims directly into the header
    #[serde(skip_serializing_if = "Option::is_none")]
    claims: Option<Claims>,
    /// Critical Header Parameter indicates that extensions to the json-web-proof specification and/or json-proof-algorithms 
    /// are being used that MUST be understood and processed
    /// see https://datatracker.ietf.org/doc/html/draft-ietf-jose-json-web-proof-08#name-crit-critical-header-parame
    #[serde(skip_serializing_if = "Option::is_none")]
    crit: Option<Vec<String>>,
    /// Issuer Header Parameter identifies the principal that issued the JWP.
    /// The processing of this claim is generally application specific.
    #[serde(skip_serializing_if = "Option::is_none")]
    iss: Option<String>,
    /// Proof Key represents the public key used by the issuer for proof of possession within certain algorithms.
    /// This is an ephemeral key that MUST be unique for each issued JWP
    /// See https://datatracker.ietf.org/doc/html/draft-ietf-jose-json-web-proof-08#name-proof_key-proof-key-header-
    #[serde(skip_serializing_if = "Option::is_none")]
    proof_key: Option<Jwk>,
}

impl IssuerProtectedHeader {
    /// Constructor for IssuerProtectedHeader, sets the ProofAlgorithm and the typ as JPT
    pub fn new(alg: ProofAlgorithm) -> Self {
        Self {
            typ: Some("JPT".to_owned()),
            alg,
            kid: None,
            cid: None,
            claims: None,
            crit: None,
            iss: None,
            proof_key: None,
        }
    }

    /// Getter for alg
    pub fn alg(&self) -> ProofAlgorithm {
        self.alg
    }

    /// Getter for typ
    pub fn typ(&self) -> Option<&String> {
        self.typ.as_ref()
    }

    /// Setter for typ
    pub fn set_typ(&mut self, value: Option<String>) {
        self.typ = value;
    }

    /// Getter for kid
    pub fn kid(&self) -> Option<&String> {
        self.kid.as_ref()
    }

    /// Setter for kid
    pub fn set_kid(&mut self, value: Option<String>) {
        self.kid = value;
    }

    /// Getter for cid
    pub fn cid(&self) -> Option<&String> {
        self.cid.as_ref()
    }

    /// Setter for cid
    pub fn set_cid(&mut self, value: Option<String>) {
        self.cid = value;
    }

    /// Getter for claims
    pub fn claims(&self) -> Option<&Claims> {
        self.claims.as_ref()
    }

    /// Setter for claims
    pub(crate) fn set_claims(&mut self, value: Option<Claims>) {
        self.claims = value;
    }

    /// Getter for crit
    pub fn crit(&self) -> Option<&Vec<String>> {
        self.crit.as_ref()
    }

    /// Setter for claims
    pub fn set_crit(&mut self, value: Option<Vec<String>>) {
        self.crit = value;
    }

    /// Getter for iss
    pub fn iss(&self) -> Option<&String> {
        self.iss.as_ref()
    }

    /// Setter for iss
    pub fn set_iss(&mut self, value: Option<String>) {
        self.iss = value;
    }

    /// Getter for proof_key
    pub fn proof_key(&self) -> Option<Jwk> {
        self.proof_key.clone()
    }

    /// Setter for proof_key
    pub fn set_proof_key(&mut self, value: Option<Jwk>) {
        self.proof_key = value;
    }
    
}

#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct PresentationProtectedHeader {
    alg: PresentationProofAlgorithm,
    /// ID for the key used for the JWP.
    #[serde(skip_serializing_if = "Option::is_none")]
    kid: Option<String>,
    /// Audience Header Parameter identifies the recipients that the JWP is intended, generally application specific.
    /// See https://datatracker.ietf.org/doc/html/draft-ietf-jose-json-web-proof-08#name-aud-audience-header-paramet.
    #[serde(skip_serializing_if = "Option::is_none")]
    aud: Option<String>,
    /// For replay attacks
    #[serde(skip_serializing_if = "Option::is_none")]
    nonce: Option<String>,
    /// JWP type (JPT)
    #[serde(skip_serializing_if = "Option::is_none")]
    typ: Option<String>,
    /// Critical Header Parameter indicates that extensions to the json-web-proof specification and/or json-proof-algorithms 
    /// are being used that MUST be understood and processed
    /// see https://datatracker.ietf.org/doc/html/draft-ietf-jose-json-web-proof-08#name-crit-critical-header-parame
    #[serde(skip_serializing_if = "Option::is_none")]
    crit: Option<Vec<String>>,
    /// Issuer Header Parameter identifies the principal that issued the JWP.
    /// The processing of this claim is generally application specific.
    #[serde(skip_serializing_if = "Option::is_none")]
    iss: Option<String>,
    /// Presentation Key represents the public key is used by the holder for proof of possession and integrity
    /// protection of the presented protected header.
    /// See https://datatracker.ietf.org/doc/html/draft-ietf-jose-json-web-proof-08#name-presentation_key-presentati
    #[serde(skip_serializing_if = "Option::is_none")]
    presentation_key: Option<Jwk>,
}

impl PresentationProtectedHeader {
    /// Constructor for PresentationProtectedHeader, sets the PresentationProofAlgorithm 
    pub fn new(alg: PresentationProofAlgorithm) -> Self {
        Self {
            alg,
            kid: None,
            aud: None,
            nonce: None,
            typ: None,
            crit: None,
            iss: None,
            presentation_key: None,
        }
    }

    /// Getter for alg
    pub fn alg(&self) -> PresentationProofAlgorithm {
        self.alg
    }

    /// Getter for kid
    pub fn kid(&self) -> Option<&String> {
        self.kid.as_ref()
    }

    /// Setter for kid
    pub fn set_kid(&mut self, value: Option<String>) {
        self.kid = value;
    }

    /// Getter for aud
    pub fn aud(&self) -> Option<&String> {
        self.aud.as_ref()
    }

    /// Setter for aud
    pub fn set_aud(&mut self, value: Option<String>) {
        self.aud = value;
    }

    /// Getter for nonce
    pub fn nonce(&self) -> Option<&String> {
        self.nonce.as_ref()
    }

    /// Setter for nonce
    pub fn set_nonce(&mut self, value: Option<String>) {
        self.nonce = value;
    }

    /// Getter for typ
    pub fn typ(&self) -> Option<&String> {
        self.typ.as_ref()
    }

    /// Setter for typ
    pub fn set_typ(&mut self, value: Option<String>) {
        self.typ = value;
    }

    /// Getter for crit
    pub fn crit(&self) -> Option<&Vec<String>> {
        self.crit.as_ref()
    }

    /// Setter for claims
    pub fn set_crit(&mut self, value: Option<Vec<String>>) {
        self.crit = value;
    }

    /// Getter for iss
    pub fn iss(&self) -> Option<&String> {
        self.iss.as_ref()
    }

    /// Setter for iss
    pub fn set_iss(&mut self, value: Option<String>) {
        self.iss = value;
    }

    /// Getter for presentation_key
    pub fn presentation_key(&self) -> Option<Jwk> {
        self.presentation_key.clone()
    }

    /// Setter for presentation_key
    pub fn set_presentation_key(&mut self, value: Option<Jwk>) {
        self.presentation_key = value;
    }

}

#[cfg(test)]
mod tests {
    use serde_json::json;

    use super::*;

    #[test]
    fn test_default_issuer_protected_header() {
        let header = IssuerProtectedHeader::new(ProofAlgorithm::BBS);
        assert_eq!(header.alg(), ProofAlgorithm::BBS);
        assert_eq!(header.typ(), Some(&"JPT".to_owned()));
        assert_eq!(header.kid(), None);
        assert_eq!(header.cid(), None);
        assert_eq!(header.claims(), None);
        assert_eq!(header.crit(), None);
        assert_eq!(header.iss(), None);
        assert_eq!(header.proof_key(), None);
    }

    #[test]
    fn test_custom_issuer_protected_header(){
        let json = json!({
            "kid": "HjfcpyjuZQ-O8Ye2hQnNbT9RbbnrobptdnExR0DUjU8",
            "alg": "BBS",
            "typ": "JPT",
            "iss": "example.com",
            "cid": "example.com/cid/123",
            "claims": [
                "iat",
                "exp",
                "family_name",
                "given_name",
                "email",
                "address",
                "age_over_21"
            ],
            "crit": [
                "critical_extension_1",
                "critical_extension_2",
            ],
            "proof_key" : {
                "kty": "EC",
                "crv": "BLS12381G2",
                "x": "AizYfy-snuLWBAQjzm5UJcmXkNe4DPVbcqFha7i7hgmpiDgGHVUdqqM8YWmWkzi-DBSTXPozzlvnB1TZXcgXtYPla9M1iyK3evsD3Eoyo3ClR1_I_Pfmlk_signHOz9i",
                "y": "A8PoKJou9-4t93kYDlIX_BGMgAqjIaZIW5TRQwusD4lDhcmSZy9hY5Sl2NxERhA8ERq2NLklV6dethvprgZ3hKfzrjU97MtkcY2ql-390o08o_C475nIAXqtgDqZwg-X",
                "d": "UFjZc6H5vhHAmPcchdlRLnfNKmSCbnqDylT3aKZYSW4"
            }
        });
        let claims: Claims = serde_json::from_value(json!(["iat", "exp", "family_name", "given_name", "email", "address", "age_over_21"]))
            .expect("Failed to deserialize Claims");
        let jwk: Jwk = serde_json::from_value(json!({
            "kty": "EC",
            "crv": "BLS12381G2",
            "x": "AizYfy-snuLWBAQjzm5UJcmXkNe4DPVbcqFha7i7hgmpiDgGHVUdqqM8YWmWkzi-DBSTXPozzlvnB1TZXcgXtYPla9M1iyK3evsD3Eoyo3ClR1_I_Pfmlk_signHOz9i",
            "y": "A8PoKJou9-4t93kYDlIX_BGMgAqjIaZIW5TRQwusD4lDhcmSZy9hY5Sl2NxERhA8ERq2NLklV6dethvprgZ3hKfzrjU97MtkcY2ql-390o08o_C475nIAXqtgDqZwg-X",
            "d": "UFjZc6H5vhHAmPcchdlRLnfNKmSCbnqDylT3aKZYSW4"
        })).expect("Failed to deserialize Jwk");
        let header: IssuerProtectedHeader = serde_json::from_value(json).expect("Failed to deserialize IssuerProtectedHeader");
        assert_eq!(header.alg(), ProofAlgorithm::BBS);
        assert_eq!(header.typ(), Some(&"JPT".to_owned()));
        assert_eq!(header.kid(), Some(&"HjfcpyjuZQ-O8Ye2hQnNbT9RbbnrobptdnExR0DUjU8".to_owned()));
        assert_eq!(header.cid(), Some(&"example.com/cid/123".to_owned()));
        assert_eq!(header.claims(), Some(&claims));
        assert_eq!(header.crit(), Some(&vec!["critical_extension_1".to_owned(), "critical_extension_2".to_owned()]));
        assert_eq!(header.proof_key(), Some(jwk));
        assert_eq!(header.iss(), Some(&"example.com".to_owned()));

    }

    #[test]
    fn test_default_presentation_protected_header() {
        let header = PresentationProtectedHeader::new(PresentationProofAlgorithm::BBS_SHAKE256);
        assert_eq!(header.alg(), PresentationProofAlgorithm::BBS_SHAKE256);
        assert_eq!(header.kid(), None);
        assert_eq!(header.aud(), None);
        assert_eq!(header.nonce(), None);
        assert_eq!(header.typ(), None);
        assert_eq!(header.crit(), None);
        assert_eq!(header.iss(), None);
        assert_eq!(header.presentation_key(), None);
    }

    #[test]
    fn test_custom_presentation_protected_header(){
        let json = json!({
            "kid": "HjfcpyjuZQ-O8Ye2hQnNbT9RbbnrobptdnExR0DUjU8",
            "alg": "BBS",
            "typ": "JPT",
            "iss": "example.com",
            "aud": "audience_example",
            "crit": [
                "critical_extension_1",
                "critical_extension_2",
            ],
            "presentation_key" : {
                "kty": "EC",
                "crv": "BLS12381G2",
                "x": "AizYfy-snuLWBAQjzm5UJcmXkNe4DPVbcqFha7i7hgmpiDgGHVUdqqM8YWmWkzi-DBSTXPozzlvnB1TZXcgXtYPla9M1iyK3evsD3Eoyo3ClR1_I_Pfmlk_signHOz9i",
                "y": "A8PoKJou9-4t93kYDlIX_BGMgAqjIaZIW5TRQwusD4lDhcmSZy9hY5Sl2NxERhA8ERq2NLklV6dethvprgZ3hKfzrjU97MtkcY2ql-390o08o_C475nIAXqtgDqZwg-X",
                "d": "UFjZc6H5vhHAmPcchdlRLnfNKmSCbnqDylT3aKZYSW4"
            },
            "nonce": "wrmBRkKtXjQ"
        });

        let jwk: Jwk = serde_json::from_value(json!({
            "kty": "EC",
            "crv": "BLS12381G2",
            "x": "AizYfy-snuLWBAQjzm5UJcmXkNe4DPVbcqFha7i7hgmpiDgGHVUdqqM8YWmWkzi-DBSTXPozzlvnB1TZXcgXtYPla9M1iyK3evsD3Eoyo3ClR1_I_Pfmlk_signHOz9i",
            "y": "A8PoKJou9-4t93kYDlIX_BGMgAqjIaZIW5TRQwusD4lDhcmSZy9hY5Sl2NxERhA8ERq2NLklV6dethvprgZ3hKfzrjU97MtkcY2ql-390o08o_C475nIAXqtgDqZwg-X",
            "d": "UFjZc6H5vhHAmPcchdlRLnfNKmSCbnqDylT3aKZYSW4"
        })).expect("Failed to deserialize Jwk");
        let header: PresentationProtectedHeader = serde_json::from_value(json).expect("Failed to deserialize IssuerProtectedHeader");
        assert_eq!(header.alg(), PresentationProofAlgorithm::BBS);
        assert_eq!(header.typ(), Some(&"JPT".to_owned()));
        assert_eq!(header.kid(), Some(&"HjfcpyjuZQ-O8Ye2hQnNbT9RbbnrobptdnExR0DUjU8".to_owned()));
        assert_eq!(header.crit(), Some(&vec!["critical_extension_1".to_owned(), "critical_extension_2".to_owned()]));
        assert_eq!(header.presentation_key(), Some(jwk));
        assert_eq!(header.iss(), Some(&"example.com".to_owned()));
        assert_eq!(header.aud(), Some(&"audience_example".to_owned()));
        assert_eq!(header.nonce(), Some(&"wrmBRkKtXjQ".to_owned()));

    }

    #[test]
    fn test_set_issuer_protected_header() {
        let mut  header = IssuerProtectedHeader::new(ProofAlgorithm::BBS);
        let claims: Claims = serde_json::from_value(json!(["iat", "exp", "family_name", "given_name", "email", "address", "age_over_21"]))
            .expect("Failed to deserialize Claims");
        header.set_kid(Some("HjfcpyjuZQ-O8Ye2hQnNbT9RbbnrobptdnExR0DUjU8".to_owned()));
        header.set_cid(Some("example.com/cid/123".to_owned()));
        header.set_claims(Some(claims));
        header.set_crit(Some(vec!["critical_extension_1".to_owned(), "critical_extension_2".to_owned()]));
        header.set_iss(Some("example.com".to_owned()));
        
        let json = json!({
            "kid": "HjfcpyjuZQ-O8Ye2hQnNbT9RbbnrobptdnExR0DUjU8",
            "alg": "BBS",
            "typ": "JPT",
            "iss": "example.com",
            "cid": "example.com/cid/123",
            "claims": [
                "iat",
                "exp",
                "family_name",
                "given_name",
                "email",
                "address",
                "age_over_21"
            ],
            "crit": [
                "critical_extension_1",
                "critical_extension_2",
            ],
        });

       let header_json: IssuerProtectedHeader = serde_json::from_value(json).expect("Failed to deserialize IssuerProtectedHeader");
       assert_eq!(header, header_json);

    }
    #[test]
    fn test_set_presentation_protected_header() {
        let mut  header = PresentationProtectedHeader::new(PresentationProofAlgorithm::BBS_SHAKE256);

        header.set_kid(Some("HjfcpyjuZQ-O8Ye2hQnNbT9RbbnrobptdnExR0DUjU8".to_owned()));
        header.set_crit(Some(vec!["critical_extension_1".to_owned(), "critical_extension_2".to_owned()]));
        header.set_iss(Some("example.com".to_owned()));
        header.set_aud(Some("audience_example".to_owned()));
        header.set_nonce(Some("wrmBRkKtXjQ".to_owned()));
        header.set_typ(Some("JPT".to_owned()));
        let json = json!({
            "kid": "HjfcpyjuZQ-O8Ye2hQnNbT9RbbnrobptdnExR0DUjU8",
            "alg": "BBS-SHAKE256",
            "typ": "JPT",
            "iss": "example.com",
            "aud": "audience_example",
            "crit": [
                "critical_extension_1",
                "critical_extension_2",
            ],
            "nonce": "wrmBRkKtXjQ"
        });

       let header_json: PresentationProtectedHeader = serde_json::from_value(json).expect("Failed to deserialize PresentationProtectedHeader");
       assert_eq!(header, header_json);

    }

}