-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cawg_identity): Define
CredentialHolder
trait (#821)
Includes `NaiveCredentialHolder` implementation to be used in test cases only. Split off from #644.
- Loading branch information
1 parent
2aa97ef
commit fc30157
Showing
11 changed files
with
233 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,51 @@ | ||
// Copyright 2024 Adobe. All rights reserved. | ||
// This file is licensed to you under the Apache License, | ||
// Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) | ||
// or the MIT license (http://opensource.org/licenses/MIT), | ||
// at your option. | ||
|
||
// Unless required by applicable law or agreed to in writing, | ||
// this software is distributed on an "AS IS" BASIS, WITHOUT | ||
// WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or | ||
// implied. See the LICENSE-MIT and LICENSE-APACHE files for the | ||
// specific language governing permissions and limitations under | ||
// each license. | ||
|
||
use async_trait::async_trait; | ||
|
||
use crate::{builder::IdentityBuilderError, SignerPayload}; | ||
|
||
/// An implementation of `CredentialHolder` is able to generate a signature over | ||
/// the [`SignerPayload`] data structure on behalf of a credential holder. | ||
/// | ||
/// Implementations of this trait will specialize based on the kind of | ||
/// credential as specified in [§8. Credentials, signatures, and validation | ||
/// methods] from the CAWG Identity Assertion specification. | ||
/// | ||
/// [§8. Credentials, signatures, and validation methods]: https://cawg.io/identity/1.1-draft/#_credentials_signatures_and_validation_methods | ||
#[cfg_attr(not(target_arch = "wasm32"), async_trait)] | ||
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] | ||
pub trait CredentialHolder { | ||
/// Returns the designated `sig_type` value for this kind of credential. | ||
fn sig_type(&self) -> &'static str; | ||
|
||
/// Returns the maximum expected size in bytes of the `signature` | ||
/// field for the identity assertion which will be subsequently | ||
/// returned by the [`sign`] function. Signing will fail if the | ||
/// subsequent signature is larger than this number of bytes. | ||
/// | ||
/// [`sign`]: Self::sign | ||
fn reserve_size(&self) -> usize; | ||
|
||
/// Signs the [`SignerPayload`] data structure on behalf of the credential | ||
/// holder. | ||
/// | ||
/// If successful, returns the exact binary content to be placed in | ||
/// the `signature` field for this identity assertion. | ||
/// | ||
/// The signature MUST NOT be larger than the size previously stated | ||
/// by the [`reserve_size`] function. | ||
/// | ||
/// [`reserve_size`]: Self::reserve_size | ||
async fn sign(&self, signer_payload: &SignerPayload) -> Result<Vec<u8>, IdentityBuilderError>; | ||
} |
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,44 @@ | ||
// Copyright 2025 Adobe. All rights reserved. | ||
// This file is licensed to you under the Apache License, | ||
// Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) | ||
// or the MIT license (http://opensource.org/licenses/MIT), | ||
// at your option. | ||
|
||
// Unless required by applicable law or agreed to in writing, | ||
// this software is distributed on an "AS IS" BASIS, WITHOUT | ||
// WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or | ||
// implied. See the LICENSE-MIT and LICENSE-APACHE files for the | ||
// specific language governing permissions and limitations under | ||
// each license. | ||
|
||
use std::fmt::Debug; | ||
|
||
use c2pa_crypto::raw_signature::RawSignerError; | ||
use thiserror::Error; | ||
|
||
/// Describes errors that can occur when building a CAWG identity assertion. | ||
#[derive(Debug, Error)] | ||
pub enum IdentityBuilderError { | ||
/// The box size provided for the signature is too small. | ||
#[error("the signature box is too small")] | ||
BoxSizeTooSmall, | ||
|
||
/// An error occurred while generating CBOR. | ||
#[error("error while generating CBOR ({0})")] | ||
CborGenerationError(String), | ||
|
||
/// An error occurred when generating the underlying raw signature. | ||
#[error(transparent)] | ||
RawSignerError(#[from] RawSignerError), | ||
|
||
/// An unexpected internal error occured while requesting the time stamp | ||
/// response. | ||
#[error("internal error ({0})")] | ||
InternalError(String), | ||
} | ||
|
||
impl<T: Debug> From<ciborium::ser::Error<T>> for IdentityBuilderError { | ||
fn from(err: ciborium::ser::Error<T>) -> Self { | ||
Self::CborGenerationError(err.to_string()) | ||
} | ||
} |
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,21 @@ | ||
// Copyright 2024 Adobe. All rights reserved. | ||
// This file is licensed to you under the Apache License, | ||
// Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) | ||
// or the MIT license (http://opensource.org/licenses/MIT), | ||
// at your option. | ||
|
||
// Unless required by applicable law or agreed to in writing, | ||
// this software is distributed on an "AS IS" BASIS, WITHOUT | ||
// WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or | ||
// implied. See the LICENSE-MIT and LICENSE-APACHE files for the | ||
// specific language governing permissions and limitations under | ||
// each license. | ||
|
||
//! This module contains the APIs you will use to build a | ||
//! C2PA Manifest that contains one or more CAWG identity assertions. | ||
pub(crate) mod credential_holder; | ||
pub use credential_holder::CredentialHolder; | ||
|
||
mod error; | ||
pub use error::IdentityBuilderError; |
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,25 @@ | ||
// Copyright 2025 Adobe. All rights reserved. | ||
// This file is licensed to you under the Apache License, | ||
// Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) | ||
// or the MIT license (http://opensource.org/licenses/MIT), | ||
// at your option. | ||
|
||
// Unless required by applicable law or agreed to in writing, | ||
// this software is distributed on an "AS IS" BASIS, WITHOUT | ||
// WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or | ||
// implied. See the LICENSE-MIT and LICENSE-APACHE files for the | ||
// specific language governing permissions and limitations under | ||
// each license. | ||
|
||
use crate::builder::IdentityBuilderError; | ||
|
||
#[test] | ||
fn impl_from_ciborium_err() { | ||
let ciborium_err: ciborium::ser::Error<String> = ciborium::ser::Error::Value("foo".to_string()); | ||
let builder_err: IdentityBuilderError = ciborium_err.into(); | ||
|
||
assert_eq!( | ||
builder_err.to_string(), | ||
"error while generating CBOR (Value(\"foo\"))" | ||
); | ||
} |
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,14 @@ | ||
// Copyright 2025 Adobe. All rights reserved. | ||
// This file is licensed to you under the Apache License, | ||
// Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) | ||
// or the MIT license (http://opensource.org/licenses/MIT), | ||
// at your option. | ||
|
||
// Unless required by applicable law or agreed to in writing, | ||
// this software is distributed on an "AS IS" BASIS, WITHOUT | ||
// WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or | ||
// implied. See the LICENSE-MIT and LICENSE-APACHE files for the | ||
// specific language governing permissions and limitations under | ||
// each license. | ||
|
||
mod error; |
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,16 @@ | ||
// Copyright 2024 Adobe. All rights reserved. | ||
// This file is licensed to you under the Apache License, | ||
// Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) | ||
// or the MIT license (http://opensource.org/licenses/MIT), | ||
// at your option. | ||
|
||
// Unless required by applicable law or agreed to in writing, | ||
// this software is distributed on an "AS IS" BASIS, WITHOUT | ||
// WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or | ||
// implied. See the LICENSE-MIT and LICENSE-APACHE files for the | ||
// specific language governing permissions and limitations under | ||
// each license. | ||
|
||
mod naive_credential_holder; | ||
#[allow(unused)] | ||
pub(crate) use naive_credential_holder::NaiveCredentialHolder; |
50 changes: 50 additions & 0 deletions
50
cawg_identity/src/tests/fixtures/naive_credential_holder.rs
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,50 @@ | ||
// Copyright 2024 Adobe. All rights reserved. | ||
// This file is licensed to you under the Apache License, | ||
// Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) | ||
// or the MIT license (http://opensource.org/licenses/MIT), | ||
// at your option. | ||
|
||
// Unless required by applicable law or agreed to in writing, | ||
// this software is distributed on an "AS IS" BASIS, WITHOUT | ||
// WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or | ||
// implied. See the LICENSE-MIT and LICENSE-APACHE files for the | ||
// specific language governing permissions and limitations under | ||
// each license. | ||
|
||
//! Naive implementation of credential-handling traits for | ||
//! proof-of-concept/testing purposes. | ||
//! | ||
//! The "signature" in this example is simply the CBOR encoding | ||
//! of the `signer_payload` struct. This is really intended to test | ||
//! the signature mechanism, not to be a meaningful signature itself. | ||
//! | ||
//! Not suitable for production use. | ||
use async_trait::async_trait; | ||
|
||
use crate::{ | ||
builder::{CredentialHolder, IdentityBuilderError}, | ||
SignerPayload, | ||
}; | ||
|
||
pub(crate) struct NaiveCredentialHolder {} | ||
|
||
#[cfg_attr(not(target_arch = "wasm32"), async_trait)] | ||
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] | ||
impl CredentialHolder for NaiveCredentialHolder { | ||
fn sig_type(&self) -> &'static str { | ||
"INVALID.identity.naive_credential" | ||
} | ||
|
||
fn reserve_size(&self) -> usize { | ||
1000 | ||
} | ||
|
||
async fn sign(&self, signer_payload: &SignerPayload) -> Result<Vec<u8>, IdentityBuilderError> { | ||
// Naive implementation simply serializes SignerPayload | ||
// in CBOR format and calls it a "signature." | ||
let mut result: Vec<u8> = vec![]; | ||
ciborium::into_writer(signer_payload, &mut result)?; | ||
Ok(result) | ||
} | ||
} |
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