|
| 1 | +// |
| 2 | +// Copyright Inrupt Inc. |
| 3 | +// |
| 4 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +// of this software and associated documentation files (the "Software"), to deal in |
| 6 | +// the Software without restriction, including without limitation the rights to use, |
| 7 | +// copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
| 8 | +// Software, and to permit persons to whom the Software is furnished to do so, |
| 9 | +// subject to the following conditions: |
| 10 | +// |
| 11 | +// The above copyright notice and this permission notice shall be included in |
| 12 | +// all copies or substantial portions of the Software. |
| 13 | +// |
| 14 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 15 | +// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
| 16 | +// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 17 | +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 18 | +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 19 | +// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 20 | +// |
| 21 | + |
| 22 | +import { CONTEXT_VC_W3C } from "../gConsent/constants"; |
| 23 | + |
| 24 | +export const SUPPORTED_CONTEXTS = [ |
| 25 | + "https://schema.inrupt.com/credentials/v1.jsonld", |
| 26 | + "https://schema.inrupt.com/credentials/v2.jsonld", |
| 27 | +] as const; |
| 28 | + |
| 29 | +const SELF_HOSTED_URI_TEMPLATE = "https://{DOMAIN}/credentials/v1"; |
| 30 | +const instantiateSelfHosted = (domain: string) => |
| 31 | + SELF_HOSTED_URI_TEMPLATE.replace("{DOMAIN}", domain); |
| 32 | + |
| 33 | +// The type assertion is required because TS doesn't validate the const array size vs the index. |
| 34 | +export const DEFAULT_CONTEXT = SUPPORTED_CONTEXTS.at( |
| 35 | + -1, |
| 36 | +) as (typeof SUPPORTED_CONTEXTS)[number]; |
| 37 | +const WELL_KNOWN_CONFIG = "/.well-known/vc-configuration"; |
| 38 | + |
| 39 | +export type AccessProvider = { |
| 40 | + context: (typeof SUPPORTED_CONTEXTS)[number] | string; |
| 41 | +}; |
| 42 | + |
| 43 | +const providerCache: Record<string, AccessProvider> = {}; |
| 44 | + |
| 45 | +/** |
| 46 | + * This is an internal function to avoid having to mock fetch on all tests. |
| 47 | + * @hidden |
| 48 | + */ |
| 49 | +export function cacheProvider(url: string, provider: AccessProvider) { |
| 50 | + providerCache[url] = provider; |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * This is an internal function to avoid having to mock fetch on all tests. |
| 55 | + * @hidden |
| 56 | + */ |
| 57 | +export function clearProviderCache() { |
| 58 | + Object.keys(providerCache).forEach((url) => { |
| 59 | + delete providerCache[url]; |
| 60 | + }); |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * This internal function negotiates the most recent context supported by both the provider and the client. |
| 65 | + * It also caches the result in memory so that the VC provider configuration is only fetched once. |
| 66 | + * FIXME: use proper caching for eventual eviction. |
| 67 | + */ |
| 68 | +export async function getIssuerContext( |
| 69 | + issuer: URL, |
| 70 | +): Promise<(typeof SUPPORTED_CONTEXTS)[number] | undefined | string> { |
| 71 | + if (providerCache[issuer.href] !== undefined) { |
| 72 | + return providerCache[issuer.href].context; |
| 73 | + } |
| 74 | + const configUrl = new URL(WELL_KNOWN_CONFIG, issuer); |
| 75 | + const response = await fetch(configUrl); |
| 76 | + try { |
| 77 | + const config = await response.json(); |
| 78 | + const contexts = config["@context"] as Array<string>; |
| 79 | + let providerCtx = contexts.find((ctx) => |
| 80 | + // Typescript is too strict validating consts. |
| 81 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 82 | + SUPPORTED_CONTEXTS.includes(ctx as any), |
| 83 | + ); |
| 84 | + const selfHosted = instantiateSelfHosted(issuer.hostname); |
| 85 | + // If no canonical context is being used, check for self-hosted (applicable to ESS 2.2). |
| 86 | + if (providerCtx === undefined && contexts.indexOf(selfHosted) !== -1) { |
| 87 | + // If the well-known config uses a self-hosted context, canonical v1 should be used. |
| 88 | + [providerCtx] = SUPPORTED_CONTEXTS; |
| 89 | + } |
| 90 | + if (providerCtx !== undefined) { |
| 91 | + providerCache[issuer.href] = { context: providerCtx }; |
| 92 | + } |
| 93 | + return providerCtx; |
| 94 | + } catch (e) { |
| 95 | + // We don't want this issue to be swallowed silently. |
| 96 | + // eslint-disable-next-line no-console |
| 97 | + console.error(e); |
| 98 | + } |
| 99 | + return undefined; |
| 100 | +} |
| 101 | + |
| 102 | +export const buildProviderContext = async ( |
| 103 | + issuer: URL, |
| 104 | +): Promise<Array<string>> => [ |
| 105 | + CONTEXT_VC_W3C, |
| 106 | + // If the issuer uses a context we don't support, default to the latest supported. |
| 107 | + (await getIssuerContext(issuer)) ?? DEFAULT_CONTEXT, |
| 108 | +]; |
0 commit comments