-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve error handling. #172
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,214 @@ | ||
/*! | ||
* Copyright (c) 2020-2024 Digital Bazaar, Inc. All rights reserved. | ||
*/ | ||
import * as bedrock from '@bedrock/core'; | ||
import * as helpers from './helpers.js'; | ||
import {klona} from 'klona'; | ||
import {mockData} from './mock.data.js'; | ||
import {v4 as uuid} from 'uuid'; | ||
|
||
const badCredentials = [ | ||
{ | ||
title: 'empty credential', | ||
credential: {}, | ||
expect: { | ||
statusCode: 400, | ||
name: 'ValidationError', | ||
cause: { | ||
// FIXME non-BedrockError causes don't pass through | ||
// could add details.errors[] checks in this case | ||
//name: 'jsonld.ValidationError' | ||
} | ||
} | ||
}, | ||
{ | ||
title: 'unkonwn context', | ||
credential: { | ||
'@context': [ | ||
'https://www.w3.org/ns/credentials/v2', | ||
'bogus.example' | ||
], | ||
type: ['VerifiableCredential'], | ||
credentialSubject: { | ||
'ex:thing': true | ||
} | ||
}, | ||
expect: { | ||
statusCode: 400, | ||
name: 'DataError', | ||
detailsError: { | ||
name: 'jsonld.InvalidUrl' | ||
} | ||
} | ||
}, | ||
{ | ||
title: 'empty subject', | ||
credential: { | ||
'@context': ['https://www.w3.org/ns/credentials/v2'], | ||
type: ['VerifiableCredential'], | ||
credentialSubject: {} | ||
}, | ||
expect: { | ||
// FIXME Improve @digitalbazaar/vc error handling. | ||
// This test is a plain 'Error' with a message of | ||
// '"credentialSubject" must make a claim.'. | ||
statusCode: 400 | ||
} | ||
}, | ||
{ | ||
title: 'undefined terms', | ||
credential: { | ||
'@context': ['https://www.w3.org/ns/credentials/v2'], | ||
type: ['VerifiableCredential', 'UndefinedType'], | ||
credentialSubject: { | ||
undefinedTerm: 'notDefinedInContext' | ||
} | ||
}, | ||
expect: { | ||
statusCode: 400, | ||
name: 'DataError', | ||
message: 'Invalid credential.', | ||
detailsError: { | ||
name: 'jsonld.ValidationError' | ||
} | ||
} | ||
} | ||
]; | ||
|
||
describe('fail for bad credentials', () => { | ||
let suites; | ||
let capabilityAgent; | ||
let zcaps; | ||
let noStatusListIssuerId; | ||
let noStatusListIssuerRootZcap; | ||
beforeEach(async () => { | ||
// generate a proof set using all of these suites in each test | ||
suites = [{ | ||
name: 'Ed25519Signature2020', | ||
algorithm: 'Ed25519' | ||
}, { | ||
name: 'eddsa-rdfc-2022', | ||
algorithm: 'Ed25519' | ||
}, { | ||
name: 'ecdsa-rdfc-2019', | ||
algorithm: 'P-256' | ||
}, { | ||
name: 'ecdsa-sd-2023', | ||
algorithm: 'P-256', | ||
// require these options (do not allow client to override) | ||
options: { | ||
mandatoryPointers: ['/issuer'] | ||
} | ||
}, { | ||
name: 'ecdsa-xi-2023', | ||
algorithm: 'P-256' | ||
}, { | ||
name: 'bbs-2023', | ||
algorithm: 'Bls12381G2', | ||
// require these options (do not allow client to override) | ||
options: { | ||
mandatoryPointers: ['/issuer'] | ||
} | ||
}]; | ||
|
||
// generate a `did:web` DID for the issuer | ||
const {host} = bedrock.config.server; | ||
const localId = uuid(); | ||
const did = `did:web:${encodeURIComponent(host)}:did-web:${localId}`; | ||
|
||
// provision dependencies | ||
({capabilityAgent, zcaps} = await helpers.provisionDependencies({ | ||
did, cryptosuites: suites, status: false, zcaps: true | ||
})); | ||
|
||
// create issue options | ||
const issueOptions = { | ||
issuer: did, | ||
cryptosuites: suites.map(suite => { | ||
const {name, options, zcapReferenceIds} = suite; | ||
const cryptosuite = {name, zcapReferenceIds}; | ||
if(options) { | ||
cryptosuite.options = options; | ||
} | ||
return cryptosuite; | ||
}) | ||
}; | ||
|
||
// create `did:web` DID document for issuer | ||
const didDocument = { | ||
'@context': [ | ||
'https://www.w3.org/ns/did/v1', | ||
'https://w3id.org/security/suites/ed25519-2020/v1', | ||
'https://w3id.org/security/multikey/v1' | ||
], | ||
id: did, | ||
verificationMethod: [], | ||
assertionMethod: [] | ||
}; | ||
for(const {assertionMethodKey} of suites) { | ||
const description = await assertionMethodKey.getKeyDescription(); | ||
delete description['@context']; | ||
didDocument.verificationMethod.push(description); | ||
didDocument.assertionMethod.push(description.id); | ||
} | ||
// add DID doc to map with DID docs to be served | ||
mockData.didWebDocuments.set(localId, didDocument); | ||
|
||
// create issuer instance w/ no status list options | ||
const noStatusListIssuerConfig = await helpers.createIssuerConfig({ | ||
capabilityAgent, zcaps, issueOptions | ||
}); | ||
noStatusListIssuerId = noStatusListIssuerConfig.id; | ||
noStatusListIssuerRootZcap = | ||
`urn:zcap:root:${encodeURIComponent(noStatusListIssuerId)}`; | ||
}); | ||
for(const testCred of badCredentials) { | ||
// handle 'skip' and 'only' flags. | ||
let _it; | ||
if(testCred.skip) { | ||
_it = it.skip; | ||
} else if(testCred.only) { | ||
_it = it.only; | ||
} else { | ||
_it = it; | ||
} | ||
_it(`fails for ${testCred.title}`, async () => { | ||
const credential = klona(testCred.credential); | ||
let error; | ||
let result; | ||
try { | ||
const zcapClient = helpers.createZcapClient({capabilityAgent}); | ||
result = await zcapClient.write({ | ||
url: `${noStatusListIssuerId}/credentials/issue`, | ||
capability: noStatusListIssuerRootZcap, | ||
json: { | ||
credential, | ||
options: { | ||
extraInformation: 'abc' | ||
} | ||
} | ||
}); | ||
} catch(e) { | ||
error = e; | ||
} | ||
should.exist(error); | ||
should.not.exist(result); | ||
if(testCred.expect.statusCode) { | ||
error.status.should.equal(testCred.expect.statusCode); | ||
} | ||
if(testCred.expect.name) { | ||
error.data.name.should.equal(testCred.expect.name); | ||
} | ||
if(testCred.expect.message) { | ||
error.data.message.should.equal(testCred.expect.message); | ||
} | ||
if(testCred.expect?.cause?.name) { | ||
error.data.cause.name.should.equal(testCred.expect.cause.name); | ||
} | ||
if(testCred.expect?.detailsError?.name) { | ||
error.data.details.error.name.should.equal( | ||
testCred.expect.detailsError.name); | ||
} | ||
}); | ||
} | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This implementation needs to be updated (it was taken from a source where some bugs were discovered):
https://github.com/digitalbazaar/bedrock-vc-delivery/blob/main/lib/helpers.js#L151-L170
Once that's updated, I think this is ready to go.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aligned more with that code. jsonld.js doc loader is using
.details.cause
for not found errors, which also need stripping. Not sure if that's an issue in the other library as well.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's probably fine.