-
Notifications
You must be signed in to change notification settings - Fork 0
Add Directory support #6
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
b9eafae
Build Directory.fromURLs
rvlb 2800664
Add flow to create an SHCReader with a directory
rvlb 82b354e
Rename IssuerInterface -> Issuer
rvlb 1a92c52
Add error handling flows at Directory.fromURLs
rvlb 33c75f0
Add Directory tests
rvlb ab5a73b
Improve tests
rvlb 1ad9463
Merge branch 'main' into directories-shc
rvlb 7fc30b7
Update minimal node version to 20
rvlb 8fdb5da
Implement Directory.fromJSON
rvlb 38f2c3f
Update typing validations
rvlb 484898c
Fix directory error handling
rvlb 32fa0f9
Add error message checks in directory tests
rvlb a79b775
Ensure 100% coverage at directory.ts
rvlb 50acb7a
Add docs
rvlb 5e7c85e
Update fromURLs to use fromJSON
rvlb 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 hidden or 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 hidden or 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,118 @@ | ||
| import type { DirectoryJSON, Issuer, IssuerCrl, IssuerKey } from './types' | ||
|
|
||
| /** | ||
| * Directory is a lightweight representation of issuer metadata used by | ||
| * SMART Health Cards code paths. It contains a collection of issuer info | ||
| * objects including the issuer identifier, known JWK keys and optionally | ||
| * Certificate Revocation Lists (CRLs). | ||
| * | ||
| * @public | ||
| * @group SHC | ||
| * @category Lower-Level API | ||
| */ | ||
| export class Directory { | ||
| /** | ||
| * Create a new Directory instance from a list of issuer info objects. | ||
| * | ||
| * @param issuerInfo - Array of issuer entries (see {@link Issuer}) | ||
| */ | ||
| constructor(private issuerInfo: Issuer[]) {} | ||
|
|
||
| /** | ||
| * Return the internal issuer info array. | ||
| * | ||
| * @returns Array of issuer info objects | ||
| */ | ||
| getIssuerInfo(): Issuer[] { | ||
| return this.issuerInfo | ||
| } | ||
|
|
||
| /** | ||
| * Build a Directory from a parsed JSON object matching the published | ||
| * directory schema. | ||
| * | ||
| * This method is defensive: if `issuer.iss` is missing or not a string it | ||
| * will be coerced to an empty string; if `keys` or `crls` are not arrays | ||
| * they will be treated as empty arrays. | ||
| * | ||
| * @param directoryJson - The JSON object to convert into a Directory | ||
| * @returns A new {@link Directory} instance | ||
| * @example | ||
| * const directory = Directory.fromJSON(parsedJson) | ||
| */ | ||
| static fromJSON(directoryJson: DirectoryJSON): Directory { | ||
| const data: Issuer[] = directoryJson.issuerInfo.map(({ issuer, keys, crls }) => { | ||
| const iss = typeof issuer?.iss === 'string' ? issuer.iss : '' | ||
| const validKeys = Array.isArray(keys) ? keys : [] | ||
| const validCrls = Array.isArray(crls) ? crls : [] | ||
| return { | ||
| iss, | ||
| keys: validKeys, | ||
| crls: validCrls, | ||
| } | ||
| }) | ||
| return new Directory(data) | ||
| } | ||
|
|
||
| /** | ||
| * Create a Directory by fetching issuer metadata (JWKS) and CRLs from the | ||
| * provided issuer base URLs. | ||
| * | ||
| * For each issuer URL the method attempts to fetch `/.well-known/jwks.json` | ||
| * and will then attempt to fetch CRLs for each key at | ||
| * `/.well-known/crl/{kid}.json`. Failures to fetch a JWKS will skip that | ||
| * issuer; failures to fetch a CRL for an individual key will skip that key's | ||
| * CRL but keep the key. Errors are logged via `console.debug` and | ||
| * unexpected exceptions are caught and logged with `console.error`. | ||
| * | ||
| * @param issUrls - Array of issuer base URLs to fetch (e.g. `https://example.com/issuer`) | ||
| * @returns A {@link Directory} containing any successfully fetched issuer info | ||
| * @example | ||
| * const directory = await Directory.fromURLs(['https://example.com/issuer']) | ||
| */ | ||
| static async fromURLs(issUrls: string[]): Promise<Directory> { | ||
| const directoryJson: DirectoryJSON = { | ||
| issuerInfo: [], | ||
| } | ||
|
|
||
| try { | ||
| for (const issUrl of issUrls) { | ||
| const issuerInfo = { | ||
| issuer: { | ||
| iss: issUrl, | ||
| }, | ||
| keys: [] as IssuerKey[], | ||
| crls: [] as IssuerCrl[], | ||
| } | ||
|
|
||
| const jwksUrl = `${issUrl}/.well-known/jwks.json` | ||
| const jwksResponse = await fetch(jwksUrl) | ||
| if (!jwksResponse.ok) { | ||
| const errorMessage = `Failed to fetch jwks at ${jwksUrl} with status ${jwksResponse.status}, skipping issuer.` | ||
| console.debug(errorMessage) | ||
| continue | ||
| } | ||
|
|
||
| const { keys: issKeys } = await jwksResponse.json() | ||
| for (const key of issKeys) { | ||
| issuerInfo.keys.push(key) | ||
| const crlUrl = `${issUrl}/.well-known/crl/${key.kid}.json` | ||
| const crlResponse = await fetch(crlUrl) | ||
| if (!crlResponse.ok) { | ||
| const errorMessage = `Failed to fetch crl at ${crlUrl} with status ${crlResponse.status}, skipping key.` | ||
| console.debug(errorMessage) | ||
| continue | ||
| } | ||
| const crl = await crlResponse.json() | ||
| if (crl) issuerInfo.crls.push(crl) | ||
| } | ||
|
|
||
| directoryJson.issuerInfo.push(issuerInfo) | ||
| } | ||
| } catch (error) { | ||
| console.error('Error creating Directory:', error) | ||
| } | ||
|
|
||
| return Directory.fromJSON(directoryJson) | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.