Crates.io | sd-jwt-payload |
lib.rs | sd-jwt-payload |
version | 0.2.1 |
source | src |
created_at | 2023-12-11 11:07:54.931071 |
updated_at | 2024-03-12 10:03:04.019338 |
description | Rust implementation of the Selective Disclosure for JWTs (SD-JWT) |
homepage | https://www.iota.org |
repository | https://github.com/iotaledger/sd-jwt-payload |
max_upload_size | |
id | 1065014 |
size | 124,738 |
Rust implementation of the Selective Disclosure for JWTs (SD-JWT) version 07
This library supports
Sha-256
hash function is shipped by default, encoding/decoding with other hash functions is possible.
Include the library in your cargo.toml
.
[dependencies]
sd-jwt-payload = { version = "0.2.1" }
See sd_jwt.rs for a runnable example.
This library consists of the major structs:
SdObjectEncoder
: creates SD objects.
SdObjectDecoder
: decodes SD objects.
Disclosure
: used by the SdObjectEncoder
and SdObjectDecoder
to represent a disclosure.
SdJwt
: creates/parses SD-JWTs.
Hasher
: a trait to provide hash functions to the encoder/decoder.
Sha256Hasher
: implements Hasher
for the Sha-256
hash function.
Any JSON object can be encoded
let object = json!({
"given_name": "John",
"family_name": "Doe",
"address": {
"street_address": "123 Main St",
"region": "Anystate",
},
"phone": [
"+49 123456",
"+49 234567"
]
});
let mut encoder: SdObjectEncoder = object.try_into()?;
This creates a stateful encoder with Sha-256
hash function by default to create disclosure digests.
Note: SdObjectEncoder
is generic over Hasher
which allows custom encoding with other hash functions.
The encoder can encode any of the object's values or array elements, using the conceal
method. Suppose the value of street_address
should be selectively disclosed as well as the value of address
and the first phone
value.
let disclosure1 = encoder.conceal("/address/street_address"], None)?;
let disclosure2 = encoder.conceal("/address", None)?;
let disclosure3 = encoder.conceal("/phone/0", None)?;
"WyJHaGpUZVYwV2xlUHE1bUNrVUtPVTkzcXV4WURjTzIiLCAic3RyZWV0X2FkZHJlc3MiLCAiMTIzIE1haW4gU3QiXQ"
"WyJVVXVBelg5RDdFV1g0c0FRVVM5aURLYVp3cU13blUiLCAiYWRkcmVzcyIsIHsicmVnaW9uIjoiQW55c3RhdGUiLCJfc2QiOlsiaHdiX2d0eG01SnhVbzJmTTQySzc3Q194QTUxcmkwTXF0TVVLZmI0ZVByMCJdfV0"
"WyJHRDYzSTYwUFJjb3dvdXJUUmg4OG5aM1JNbW14YVMiLCAiKzQ5IDEyMzQ1NiJd"
Note: the conceal
method takes a JSON Pointer to determine the element to conceal inside the JSON object.
The encoder also supports adding decoys. For instance, the amount of phone numbers and the amount of claims need to be hidden.
encoder.add_decoys("/phone", 3).unwrap(); //Adds 3 decoys to the array `phone`.
encoder.add_decoys("", 6).unwrap(); // Adds 6 decoys to the top level object.
Add the hash function claim.
encoder.add_sd_alg_property(); // This adds "_sd_alg": "sha-256"
Now encoder.object()?
will return the encoded object.
{
"given_name": "John",
"family_name": "Doe",
"phone": [
{
"...": "eZVn0KkQm_T8x-x57VxYt-_MmNG91Sh34E-bZEnNfWY"
},
"+49 234567",
{
"...": "KAiJIx0tktQRXBxZSBVVld9298bZIp2WkpkDYDa3CWQ"
},
{
"...": "CBKARPh6sdTCJyliZ7pBOYzix7Z4Bb4yRh0EykHX2Uw"
},
{
"...": "oi1KgsYXgqBFXUXvbVaHSGYYaWhkB5RL55T90Gl_5s0"
}
],
"_sd": [
"Jj5jBeGEawY6vRvmHDg55EjeAIP8FVhWEV2FczhUXrY",
"8eqphBPJyCBgUJhNWNP7ci-Y79N615wpZQrxi5D4ju8",
"_hOU5puJjNzSBhK0bwh3h8_b6H6nN7vd_7I0uTp80Mo",
"G_tH70MrfCkVM0HhsH9REObIt1Ei19477y6CEsS0Zlo",
"zP56MeH0ryjzqh9Kadrb5C9Z2BE2FWg8nb3g0rR3LSA",
"dgfVW11ip9OOyVi8M4h1RjXK8akw7ICeMQkjUwSI6iU",
"Bx33mOyTF5-w8gRS5yL4YQ4dig44V3lmHxk1WRss_7U"
],
"_sd_alg": "sha-256"
}
Note: no JWT claims like exp
or iat
are added. If necessary, these need to be added and validated manually.
Since creating JWTs is outside the scope of this library, see sd_jwt.rs example where josekit
is used to create jwt
with the object above as the claim set.
Create SD-JWT
let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c3MiLCAiMTIzIE1haW4gU3QiXQ~WyJVVXVBelg5RDdFV1g0c0FRVVM5aURLYVp3cU13blUiLCAiYWRkcmVzcyIsIHsicmVnaW9uIjoiQW55c3RhdGUiLCJfc2QiOlsiaHdiX2d0eG01SnhVbzJmTTQySzc3Q194QTUxcmkwTXF0TVVLZmI0ZVByMCJdfV0~WyJHRDYzSTYwUFJjb3dvdXJUUmg4OG5aM1JNbW14YVMiLCAiKzQ5IDEyMzQ1NiJd~
Parse the SD-JWT string to extract the JWT and the disclosures in order to decode the claims and construct the disclosed values.
*Note: Validating the signature of the JWT and extracting the claim set is outside the scope of this library.
let sd_jwt: SdJwt = SdJwt::parse(sd_jwt_string)?;
let claims_set: // extract claims from `sd_jwt.jwt`.
let decoder = SdObjectDecoder::new();
let decoded_object = decoder.decode(claims_set, &sd_jwt.disclosures)?;
decoded_object
:
{
"given_name": "John",
"family_name": "Doe",
"phone": [
"+49 123456",
"+49 234567"
],
"address": {
"region": "Anystate",
"street_address": "123 Main St"
}
}
Note:
street_address
and address
are recursively decoded.
_sd_alg
property was removed.
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
git checkout -b feature/AmazingFeature
)git commit -m 'Add some AmazingFeature'
)git push origin feature/AmazingFeature
)Distributed under the Apache License. See LICENSE
for more information.