Skip to content

Commit

Permalink
Merge pull request #17 from smart-on-fhir/directory-filter-bad-issuers
Browse files Browse the repository at this point in the history
omit issuer that fail validation from Directory
  • Loading branch information
ljoy913 authored Jun 14, 2022
2 parents 3c59664 + 064151e commit f4d3ce9
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# CHANGE LOG

## v1.1.1
- Added option to omit malformed issuers from a Directory instead of failing the entire Directory (this is now the default)

## v1.1.0
- Add revocation support
- Add Directory class
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "smart-health-card-decoder",
"version": "1.0.0",
"version": "1.1.1",
"description": "API for decoding a QR encoded SMART Health Card",
"main": "esm/index.js",
"types": "esm/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ async function createFromUrl(url: string, context: Context): Promise<[IDirectory
return [undefDirectory, context];
}

if (!(await validate(downloaded as IDirectory, context))) {
if ( (await validate(downloaded as IDirectory, context)) === false && context.options.validation?.directory?.ommitBadIssuers === false ) {
return [undefDirectory, context];
}

Expand Down
17 changes: 14 additions & 3 deletions src/issuer.info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,19 @@ export async function validate(issuerInfos: IssuerInfo[], context: Context): Pro
return Promise.resolve(false);
}

const badIssuers: IssuerInfo[] = [];
for (const ii of issuerInfos) {
await validateSingle(ii, context);
const success = await validateSingle(ii, context);
if (!success && context.options.validation?.directory?.ommitBadIssuers !== false) {
badIssuers.push(ii);
log.error(`Issuer "${ii.issuer.name || ii.issuer.iss}" failed validation and was omitted from the Directory`);
}
}

if (badIssuers.length) {
badIssuers.forEach(ii => {
issuerInfos.splice(issuerInfos.indexOf(ii), 1);
})
}

if (context.options.validation?.directory?.allowDuplicates !== true) {
Expand Down Expand Up @@ -53,9 +64,9 @@ async function validateSingle(issuerInfo: IssuerInfo, context: Context): Promise
await key.validate.keys(issuerInfo.keys, context);

// rewrite the error labels adding the iss so the error output can be traced
if(context.errors?.length ?? 0 > (errorCount ?? 0) ) {
if (context.errors?.length ?? 0 > (errorCount ?? 0)) {
for (let index = (errorCount ?? 0); index < (context.errors?.length ?? 0); index++) {
context.errors![index].label = `KEY:${issuerInfo.issuer.iss}`;
context.errors![index].label = `KEY:${issuerInfo.issuer.iss}`;
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ export interface DirectoryOptions {
filterBadKeys?: boolean
},
directory?: {
allowDuplicates?: boolean
allowDuplicates?: boolean,
ommitBadIssuers?: boolean
}
}

Expand Down

0 comments on commit f4d3ce9

Please sign in to comment.