-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
630 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright 2020-2024 IOTA Stiftung | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use crate::{ | ||
common::{ArrayString, RecordStringAny}, | ||
error::{Result, WasmResult}, | ||
}; | ||
use identity_iota::sd_jwt_payload::SdObjectDecoder; | ||
use serde_json::{Map, Value}; | ||
use wasm_bindgen::prelude::*; | ||
|
||
/// Substitutes digests in an SD-JWT object by their corresponding plaintext values provided by disclosures. | ||
#[wasm_bindgen(js_name = SdObjectDecoder, inspectable)] | ||
pub struct WasmSdObjectDecoder(pub(crate) SdObjectDecoder); | ||
|
||
#[wasm_bindgen(js_class = SdObjectDecoder)] | ||
impl WasmSdObjectDecoder { | ||
/// Creates a new `SdObjectDecoder` with `sha-256` hasher. | ||
#[wasm_bindgen(constructor)] | ||
pub fn new() -> WasmSdObjectDecoder { | ||
Self(SdObjectDecoder::new_with_sha256()) | ||
} | ||
|
||
/// Decodes an SD-JWT `object` containing by Substituting the digests with their corresponding | ||
/// plaintext values provided by `disclosures`. | ||
/// | ||
/// ## Notes | ||
/// * Claims like `exp` or `iat` are not validated in the process of decoding. | ||
/// * `_sd_alg` property will be removed if present. | ||
#[wasm_bindgen] | ||
pub fn decode(&self, object: RecordStringAny, disclosures: ArrayString) -> Result<RecordStringAny> { | ||
let object: Map<String, Value> = object.into_serde().wasm_result()?; | ||
let disclosures: Vec<String> = disclosures.into_serde().wasm_result()?; | ||
let decoded = self.0.decode(&object, &disclosures).wasm_result()?; | ||
Ok( | ||
JsValue::from_serde(&decoded) | ||
.wasm_result()? | ||
.unchecked_into::<RecordStringAny>(), | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright 2020-2024 IOTA Stiftung | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use crate::error::Result; | ||
use crate::error::WasmResult; | ||
use identity_iota::sd_jwt_payload::Disclosure; | ||
use wasm_bindgen::prelude::*; | ||
use wasm_bindgen::JsValue; | ||
|
||
/// Represents an elements constructing a disclosure. | ||
/// Object properties and array elements disclosures are supported. | ||
/// | ||
/// See: https://www.ietf.org/archive/id/draft-ietf-oauth-selective-disclosure-jwt-07.html#name-disclosures | ||
#[wasm_bindgen(js_name = Disclosure, inspectable)] | ||
pub struct WasmDisclosure(pub(crate) Disclosure); | ||
|
||
#[wasm_bindgen(js_class = Disclosure)] | ||
impl WasmDisclosure { | ||
#[wasm_bindgen(constructor)] | ||
pub fn new(salt: String, claim_name: Option<String>, claim_value: JsValue) -> Result<WasmDisclosure> { | ||
Ok(Self(Disclosure::new( | ||
salt, | ||
claim_name, | ||
claim_value.into_serde().wasm_result()?, | ||
))) | ||
} | ||
|
||
/// Parses a Base64 encoded disclosure into a `Disclosure`. | ||
/// | ||
/// ## Error | ||
/// | ||
/// Returns an `InvalidDisclosure` if input is not a valid disclosure. | ||
#[wasm_bindgen] | ||
pub fn parse(disclosure: String) -> Result<WasmDisclosure> { | ||
Ok(WasmDisclosure(Disclosure::parse(disclosure).wasm_result()?)) | ||
} | ||
|
||
/// Returns a copy of the base64url-encoded string. | ||
#[wasm_bindgen(js_name = disclosure)] | ||
pub fn disclosure(&self) -> String { | ||
self.0.disclosure.clone() | ||
} | ||
|
||
/// Returns a copy of the base64url-encoded string. | ||
#[wasm_bindgen(js_name = toString)] | ||
pub fn to_string(&self) -> String { | ||
self.0.disclosure.clone() | ||
} | ||
|
||
/// Returns a copy of the salt value. | ||
#[wasm_bindgen(js_name = salt)] | ||
pub fn salt(&self) -> String { | ||
self.0.salt.clone() | ||
} | ||
|
||
/// Returns a copy of the claim name, optional for array elements. | ||
#[wasm_bindgen(js_name = claimName)] | ||
pub fn claim_name(&self) -> Option<String> { | ||
self.0.claim_name.clone() | ||
} | ||
|
||
/// Returns a copy of the claim Value which can be of any type. | ||
#[wasm_bindgen(js_name = claimValue)] | ||
pub fn claim_value(&self) -> Result<JsValue> { | ||
JsValue::from_serde(&self.0.claim_value.clone()).wasm_result() | ||
} | ||
} | ||
impl_wasm_json!(WasmDisclosure, Disclosure); |
Oops, something went wrong.