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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
use iref::{Iri, Uri};
use ssi_claims_core::{ClaimsValidity, DateTimeProvider, InvalidClaims, VerifiableClaims};
use ssi_data_integrity::{CryptographicSuite, DataIntegrity};
use static_iref::iri;
use xsd_types::DateTime;
use crate::{v1::syntax::VERIFIABLE_CREDENTIAL_TYPE, Identified, MaybeIdentified, Typed};
pub const VERIFIABLE_CREDENTIAL: &Iri =
iri!("https://www.w3.org/2018/credentials#VerifiableCredential");
/// Credential trait.
pub trait Credential {
/// Credential subject type.
type Subject;
/// Issuer type.
type Issuer: ?Sized + Identified;
/// Credential status type.
type Status: Identified + Typed;
/// Refresh service.
///
/// See: <https://www.w3.org/TR/vc-data-model//#refreshing>
type RefreshService: Identified + Typed;
/// Terms of Use type.
///
/// Terms of use can be utilized by an issuer or a holder to communicate the
/// terms under which a verifiable credential or verifiable presentation was
/// issued.
type TermsOfUse: MaybeIdentified + Typed;
/// Evidence type.
///
/// Can be included by an issuer to provide the verifier with additional
/// supporting information in a verifiable credential.
type Evidence: MaybeIdentified + Typed;
/// Credential Schemas (Zero-Knowledge Proofs).
///
/// See: <https://www.w3.org/TR/vc-data-model//#zero-knowledge-proofs>
type Schema: Identified + Typed;
/// Identifier.
fn id(&self) -> Option<&Uri> {
None
}
/// Types that are **not** `VerifiableCredential`.
///
/// Since the `VerifiableCredential` type is *required*, it is omitted from
/// the value returned by this function. If you need to iterate over
/// all the credential types, including `VerifiableCredential`, use the
/// [`Self::types`] method.
fn additional_types(&self) -> &[String] {
&[]
}
fn types(&self) -> CredentialTypes {
CredentialTypes::from_additional_types(self.additional_types())
}
/// Credential subject.
fn credential_subjects(&self) -> &[Self::Subject] {
&[]
}
/// Issuer.
///
/// This property is *required* for the credential to be *verifiable*.
fn issuer(&self) -> &Self::Issuer;
/// Issuance date.
///
/// This property is *required* for the credential to be *verifiable*.
fn issuance_date(&self) -> Option<DateTime>;
/// Expiration date.
fn expiration_date(&self) -> Option<DateTime> {
None
}
/// Credential status.
///
/// Used for discovery of information about the current status of a
/// verifiable credential, such as whether it is suspended or revoked.
fn credential_status(&self) -> &[Self::Status] {
&[]
}
/// Refresh services.
///
/// See: <https://www.w3.org/TR/vc-data-model//#refreshing>
fn refresh_services(&self) -> &[Self::RefreshService] {
&[]
}
/// Terms of Use.
///
/// Terms of use can be utilized by an issuer or a holder to communicate the
/// terms under which a verifiable credential or verifiable presentation was
/// issued.
fn terms_of_use(&self) -> &[Self::TermsOfUse] {
&[]
}
/// Evidence.
///
/// Can be included by an issuer to provide the verifier with additional
/// supporting information in a verifiable credential.
fn evidence(&self) -> &[Self::Evidence] {
&[]
}
/// Credential Schemas (Zero-Knowledge Proofs).
///
/// See: <https://www.w3.org/TR/vc-data-model//#zero-knowledge-proofs>
fn credential_schemas(&self) -> &[Self::Schema] {
&[]
}
/// Validates the credential.
///
/// Validation consists in verifying that the claims themselves are
/// consistent and valid with regard to the verification environment.
/// For instance, checking that a credential's expiration date is not in the
/// past, or the issue date not in the future.
///
/// Validation may fail even if the credential proof is successfully
/// verified.
fn validate_credential<E>(&self, env: &E) -> ClaimsValidity
where
E: DateTimeProvider,
{
let now = env.date_time();
let issuance_date = self
.issuance_date()
.ok_or(InvalidClaims::MissingIssuanceDate)?;
let valid_from = issuance_date.earliest().to_utc();
if valid_from > now {
// Credential is issued in the future!
return Err(InvalidClaims::Premature { now, valid_from });
}
if let Some(t) = self.expiration_date() {
let valid_until = t.latest().to_utc();
if now >= valid_until {
// Credential has expired.
return Err(InvalidClaims::Expired { now, valid_until });
}
}
Ok(())
}
}
pub trait VerifiableCredential: Credential + VerifiableClaims {}
impl<T: Credential + VerifiableClaims> VerifiableCredential for T {}
impl<T: Credential, S: CryptographicSuite> Credential for DataIntegrity<T, S> {
type Subject = T::Subject;
type Issuer = T::Issuer;
type Status = T::Status;
type RefreshService = T::RefreshService;
type TermsOfUse = T::TermsOfUse;
type Evidence = T::Evidence;
type Schema = T::Schema;
fn id(&self) -> Option<&Uri> {
T::id(&self.claims)
}
fn additional_types(&self) -> &[String] {
T::additional_types(&self.claims)
}
fn credential_subjects(&self) -> &[Self::Subject] {
T::credential_subjects(&self.claims)
}
fn issuer(&self) -> &Self::Issuer {
T::issuer(&self.claims)
}
fn issuance_date(&self) -> Option<DateTime> {
T::issuance_date(&self.claims)
}
fn expiration_date(&self) -> Option<DateTime> {
T::expiration_date(&self.claims)
}
fn credential_status(&self) -> &[Self::Status] {
T::credential_status(&self.claims)
}
fn refresh_services(&self) -> &[Self::RefreshService] {
T::refresh_services(&self.claims)
}
fn terms_of_use(&self) -> &[Self::TermsOfUse] {
T::terms_of_use(&self.claims)
}
fn evidence(&self) -> &[Self::Evidence] {
T::evidence(&self.claims)
}
fn credential_schemas(&self) -> &[Self::Schema] {
T::credential_schemas(&self.claims)
}
}
pub struct CredentialTypes<'a> {
base_type: bool,
additional_types: std::slice::Iter<'a, String>,
}
impl<'a> CredentialTypes<'a> {
pub fn from_additional_types(additional_types: &'a [String]) -> Self {
Self {
base_type: true,
additional_types: additional_types.iter(),
}
}
}
impl<'a> Iterator for CredentialTypes<'a> {
type Item = &'a str;
fn next(&mut self) -> Option<Self::Item> {
if self.base_type {
self.base_type = false;
Some(VERIFIABLE_CREDENTIAL_TYPE)
} else {
self.additional_types.next().map(String::as_str)
}
}
}