Skip to content

Commit

Permalink
feat: parse id as Url
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-mader committed Dec 3, 2024
1 parent 53cd922 commit dc456a2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
2 changes: 1 addition & 1 deletion agent_api_rest/postman/ssi-agent.postman_collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@
"header": [],
"body": {
"mode": "raw",
"raw": "{\n \"offerId\":\"{{OFFER_ID}}\",\n \"credentialConfigurationId\": \"w3c_vc_credential\",\n \"credential\": {\n \"credentialSubject\": {\n \"id\": \"https://ecommerce.impierce.com/\",\n \"image\": \"https://static.wikia.nocookie.net/fictionalcompanies/images/c/c2/ACME_Corporation.png\",\n \"name\": \"VirtualVendors\",\n \"certificaat\": {\n \"type\": \"ACMECorpCredential\",\n \"certificeringsDatum\": \"2024-06-26\",\n \"geldigheidsPeriode\": \"1 jaar\",\n \"garanties\": [\n \"Het bedrijf is echt en bereikbaar.\",\n \"Voldoet aan de Thuiswinkel Algemene Voorwaarden.\",\n \"14 dagen bedenktijd.\",\n \"Veilige betaalmethoden.\",\n \"Duidelijke product/servicebeschrijvingen.\",\n \"Transparant bestelproces.\",\n \"Duidelijke prijzen.\",\n \"Veilige betaalomgeving.\",\n \"Veilige omgang met persoonlijke gegevens.\",\n \"Effectieve klachtenafhandeling en onafhankelijke geschillenbemiddeling.\"\n ]\n }\n }\n }\n}",
"raw": "{\n \"offerId\":\"{{OFFER_ID}}\",\n \"credentialConfigurationId\": \"w3c_vc_credential\",\n \"credential\": {\n \"id\": \"https://acme.example.org/1a2b3c4d5e6f\",\n \"credentialSubject\": {\n \"id\": \"https://ecommerce.impierce.com/\",\n \"image\": \"https://static.wikia.nocookie.net/fictionalcompanies/images/c/c2/ACME_Corporation.png\",\n \"name\": \"VirtualVendors\",\n \"certificaat\": {\n \"type\": \"ACMECorpCredential\",\n \"certificeringsDatum\": \"2024-06-26\",\n \"geldigheidsPeriode\": \"1 jaar\",\n \"garanties\": [\n \"Het bedrijf is echt en bereikbaar.\",\n \"Voldoet aan de Thuiswinkel Algemene Voorwaarden.\",\n \"14 dagen bedenktijd.\",\n \"Veilige betaalmethoden.\",\n \"Duidelijke product/servicebeschrijvingen.\",\n \"Transparant bestelproces.\",\n \"Duidelijke prijzen.\",\n \"Veilige betaalomgeving.\",\n \"Veilige omgang met persoonlijke gegevens.\",\n \"Effectieve klachtenafhandeling en onafhankelijke geschillenbemiddeling.\"\n ]\n }\n }\n }\n}",
"options": {
"raw": {
"language": "json"
Expand Down
30 changes: 22 additions & 8 deletions agent_issuance/src/credential/aggregate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@ impl Aggregate for Credential {
let credential_subject_json =
data.raw.get("credentialSubject").ok_or(MissingCredentialSubjectError)?;

let id = data.raw.get("id").map_or(Ok(None), |id| {
id.as_str()
.ok_or_else(|| {
InvalidVerifiableCredentialError("Invalid format: `id` must be a string".to_string())
})
.and_then(|id_str| {
Url::parse(id_str).map(Some).map_err(|_| {
InvalidVerifiableCredentialError(format!("Could not parse `id` as URL: `{id_str}`"))
})
})
})?;

// Loop through all the items in the `type` array in reverse until we find a match.
while let Some(credential_format) = credential_types.pop() {
match credential_format.as_str() {
Expand All @@ -119,10 +131,18 @@ impl Aggregate for Credential {
Err(_) => unreachable!("Couldn't parse issuer"),
};

let credential: W3CVerifiableCredential = W3CVerifiableCredentialBuilder::default()
let builder = W3CVerifiableCredentialBuilder::default()
.issuer(issuer)
.subject(subject)
.issuance_date(issuance_date.parse().expect("Could not parse issuance_date"))
.issuance_date(issuance_date.parse().expect("Could not parse issuance_date"));

let builder = if let Some(id) = id {
builder.id(id.into())
} else {
builder
};

let credential: W3CVerifiableCredential = builder
.build()
.map_err(|e| InvalidVerifiableCredentialError(e.to_string()))?;

Expand All @@ -149,12 +169,6 @@ impl Aggregate for Credential {
serde_json::from_value::<AchievementSubject>(credential_subject_json.clone())
.map_err(|e| InvalidVerifiableCredentialError(e.to_string()))?;

let id = data
.raw
.get("id")
.and_then(|id| id.as_str())
.and_then(|id| Url::parse(id).ok());

let builder = AchievementCredentialBuilder::default()
.context(vec![
"https://www.w3.org/2018/credentials/v1",
Expand Down

0 comments on commit dc456a2

Please sign in to comment.