ssi_vc/v1/jwt/
mod.rs

1use serde::{de::DeserializeOwned, Serialize};
2use ssi_data_integrity::{CryptographicSuite, DataIntegrity};
3use ssi_jwt::{ClaimSet, RegisteredClaims};
4
5mod decode;
6mod encode;
7
8pub use decode::*;
9pub use encode::*;
10
11pub trait FromJwtClaims: Sized {
12    type Error;
13
14    fn from_jwt_claims(jwt: impl ClaimSet) -> Result<Self, Self::Error>;
15}
16
17pub trait ToJwtClaims {
18    type Error;
19
20    fn to_jwt_claims(&self) -> Result<RegisteredClaims, Self::Error>;
21}
22
23impl<S> FromJwtClaims for DataIntegrity<super::SpecializedJsonCredential, S>
24where
25    S: CryptographicSuite + TryFrom<ssi_data_integrity::Type>,
26    S::VerificationMethod: DeserializeOwned,
27    S::ProofOptions: DeserializeOwned,
28    S::Signature: DeserializeOwned,
29{
30    type Error = JwtVcDecodeError;
31
32    /// Decodes a verifiable credential from the claims of a JWT following the
33    /// standard [JWT-VC encoding].
34    ///
35    /// [JWT-VC encoding]: <https://www.w3.org/TR/vc-data-model/#jwt-encoding>
36    fn from_jwt_claims(jwt: impl ClaimSet) -> Result<Self, JwtVcDecodeError> {
37        decode_jwt_vc_claims(jwt)
38    }
39}
40
41impl<S: CryptographicSuite> ToJwtClaims for DataIntegrity<super::SpecializedJsonCredential, S>
42where
43    S::VerificationMethod: Serialize,
44    S::ProofOptions: Serialize,
45    S::Signature: Serialize,
46{
47    type Error = JwtVcEncodeError;
48
49    /// Encodes this verifiable credential into the claims of a JWT following
50    /// the standard [JWT-VC encoding].
51    ///
52    /// [JWT-VC encoding]: <https://www.w3.org/TR/vc-data-model/#jwt-encoding>
53    fn to_jwt_claims(&self) -> Result<RegisteredClaims, JwtVcEncodeError> {
54        encode_jwt_vc_claims(self)
55    }
56}
57
58impl ToJwtClaims for super::SpecializedJsonCredential {
59    type Error = JwtVcEncodeError;
60
61    /// Encodes this credential into the claims of a JWT following the standard
62    /// [JWT-VC encoding].
63    ///
64    /// [JWT-VC encoding]: <https://www.w3.org/TR/vc-data-model/#jwt-encoding>
65    fn to_jwt_claims(&self) -> Result<RegisteredClaims, JwtVcEncodeError> {
66        encode_jwt_vc_claims(self)
67    }
68}
69
70impl<C: DeserializeOwned, S> FromJwtClaims for DataIntegrity<super::JsonPresentation<C>, S>
71where
72    S: CryptographicSuite + TryFrom<ssi_data_integrity::Type>,
73    S::VerificationMethod: DeserializeOwned,
74    S::ProofOptions: DeserializeOwned,
75    S::Signature: DeserializeOwned,
76{
77    type Error = JwtVpDecodeError;
78
79    /// Decodes a verifiable presentation from the claims of a JWT following the
80    /// standard [JWT-VP encoding].
81    ///
82    /// [JWT-VP encoding]: <https://www.w3.org/TR/vc-data-model/#jwt-encoding>
83    fn from_jwt_claims(jwt: impl ClaimSet) -> Result<Self, JwtVpDecodeError> {
84        decode_jwt_vp_claims(jwt)
85    }
86}
87
88impl<C: Serialize, S: CryptographicSuite> ToJwtClaims
89    for DataIntegrity<super::JsonPresentation<C>, S>
90where
91    S::VerificationMethod: Serialize,
92    S::ProofOptions: Serialize,
93    S::Signature: Serialize,
94{
95    type Error = JwtVpEncodeError;
96
97    /// Encodes this verifiable presentation into the claims of a JWT following
98    /// the standard [JWT-VP encoding].
99    ///
100    /// [JWT-VP encoding]: <https://www.w3.org/TR/vc-data-model/#jwt-encoding>
101    fn to_jwt_claims(&self) -> Result<RegisteredClaims, JwtVpEncodeError> {
102        encode_jwt_vp_claims(self)
103    }
104}
105
106impl<C: Serialize> ToJwtClaims for super::JsonPresentation<C> {
107    type Error = JwtVpEncodeError;
108
109    /// Encodes this presentation into the claims of a JWT following the
110    /// standard [JWT-VP encoding].
111    ///
112    /// [JWT-VP encoding]: <https://www.w3.org/TR/vc-data-model/#jwt-encoding>
113    fn to_jwt_claims(&self) -> Result<RegisteredClaims, JwtVpEncodeError> {
114        encode_jwt_vp_claims(self)
115    }
116}