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
// Copyright 2023 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 std::collections::HashMap;
use jsonprooftoken::{jpt::claims::JptClaims, jwp::{header::{IssuerProtectedHeader, PresentationProtectedHeader}, issued::JwpIssued, presented::JwpPresented}, jpa::algs::ProofAlgorithm, encoding::{base64url_encode, SerializationType}, jwk::{key::Jwk, types::KeyPairSubtype, alg_parameters::JwkAlgorithmParameters}};
use serde::Serialize;
use serde_json::Value;
fn main() {
// let jpt_claims = JptClaims {
// sub: Some("user123".to_string()),
// exp: Some(1633756800),
// nbf: Some(1633670400),
// iat: Some(1633666800),
// jti: Some("123456".to_string()),
// custom: Some(serde_json::json!({
// "degree": {
// "type": "BachelorDegree",
// "name": "Bachelor of Science and Arts",
// "ciao": [
// {"u1": "value1"},
// {"u2": "value2"}
// ]
// },
// "name": "John Doe"
// })),
// };
// let custom_claims = serde_json::json!({
// "family_name": "Doe",
// "given_name": "Jay",
// "email": "jaydoe@example.org",
// "age": 42
// });
let custom_claims = serde_json::json!({
"degree": {
"type": "BachelorDegree",
"name": "Bachelor of Science and Arts",
"ciao": [
{"u1": "value1"},
{"u2": "value2"}
]
},
"name": "John Doe"
});
let mut jpt_claims = JptClaims::new();
// jpt_claims.set_claim(Some("family_name"), "Doe");
// jpt_claims.set_claim(Some("given_name"), "Jay");
// jpt_claims.set_claim(Some("email"), "jaydoe@example.org");
// jpt_claims.set_claim(Some("age"), 42);
jpt_claims.set_iss("https://issuer.example".to_owned());
jpt_claims.set_claim(None, custom_claims, true);
println!("{:?}", jpt_claims);
let (claims, payloads) = jpt_claims.get_claims_and_payloads();
println!("Claims: {:?}", claims);
println!("Payloads: {:?}", payloads);
let issued_header = IssuerProtectedHeader{
typ: Some("JPT".to_owned()),
alg: ProofAlgorithm::BLS12381_SHA256,
kid: None,
cid: None,
claims: Some(claims),
};
println!("Issued Header: {:?}", issued_header);
let issued_jwp = JwpIssued::new(issued_header, payloads);
println!("ISSUED JWP: \n{:?}", issued_jwp);
let bbs_jwk = Jwk::generate(KeyPairSubtype::BLS12381SHA256).unwrap();
println!("BBS Jwk: {:?}", bbs_jwk);
let compact_issued_jwp = issued_jwp.encode(SerializationType::COMPACT, &bbs_jwk).unwrap();
println!("Compact JWP: {}", compact_issued_jwp);
let decoded_issued_jwp = JwpIssued::decode(compact_issued_jwp, SerializationType::COMPACT, &bbs_jwk.to_public().unwrap()).unwrap();
println!("DECODED ISSUED JWP \n{:?}", decoded_issued_jwp);
let presentation_header = PresentationProtectedHeader{
alg: ProofAlgorithm::BLS12381_SHA256_PROOF,
kid: None,
aud: Some("https://recipient.example.com".to_owned()),
nonce: Some("wrmBRkKtXjQ".to_owned())
};
// This is an alternative
// let presentation_jwp = decoded_issued_jwp.present(SerializationType::COMPACT, &bbs_jwk.to_public().unwrap(), presentation_header);
let mut presentation_jwp = JwpPresented::new(decoded_issued_jwp.get_issuer_protected_header().clone(),presentation_header, decoded_issued_jwp.get_payloads().clone());
presentation_jwp.set_disclosed(1, false).unwrap();
presentation_jwp.set_disclosed(3, false).unwrap();
let compact_presented_jwp = presentation_jwp.encode(SerializationType::COMPACT, &bbs_jwk.to_public().unwrap(), decoded_issued_jwp.get_proof().unwrap()).unwrap();
println!("Compact Presented JWP: {}", compact_presented_jwp);
let decoded_presentation_jwp = JwpPresented::decode(compact_presented_jwp, SerializationType::COMPACT, &bbs_jwk.to_public().unwrap()).unwrap();
println!("DECODED PRESENTED JWP \n{:?}", decoded_presentation_jwp);
// let original = JptClaims::reconstruct_json_value(claims);
// println!("{:?}", original);
// let claims_map = jpt_claims.to_map();
// println!("{:?}", claims_map);
// let deserialized = JptClaims::from_map(claims_map);
// println!("{:?}", jpt_claims);
// println!("{:?}", deserialized);
}