UNPKG

credential-status

Version:
1 lines 7.33 kB
{"version":3,"file":"index.cjs","sources":["../src/index.ts"],"sourcesContent":["import { decodeJWT } from 'did-jwt'\nimport { DIDDocument } from 'did-resolver'\n\n/**\n * Represents the result of a status check.\n *\n * Implementations should populate the `revoked` boolean property, but they can return additional metadata that is\n * method specific.\n *\n * @alpha This API is still being developed and may be updated. Please follow progress or suggest improvements at\n * [https://github.com/uport-project/credential-status]\n */\nexport interface CredentialStatus {\n revoked?: boolean\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n [x: string]: any\n}\n\n/**\n * Represents a status method entry that could be embedded in a W3C Verifiable Credential.\n * Normally, only credentials that list a status method would need to be verified by it.\n *\n * ex:\n * ```json\n * credentialStatus: {\n * type: \"EthrStatusRegistry2019\",\n * id: \"rinkeby:0xregistryAddress\"\n * }\n * ```\n * See https://www.w3.org/TR/vc-data-model/#status\n *\n * @alpha This API is still being developed and may be updated. Please follow progress or suggest improvements at\n * [https://github.com/uport-project/credential-status]\n */\nexport interface StatusEntry {\n type: string\n id: string\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n [x: string]: any\n}\n\n/**\n * The interface expected for status resolvers.\n * `checkStatus` should be called with a raw credential and it should Promise a [[CredentialStatus]] result.\n * It is advisable that classes that implement this interface also provide a way to easily register the correct\n * Status method type.\n *\n * Example:\n * ```typescript\n * class CredentialStatusList2017 implements StatusResolver {\n * checkStatus: StatusMethod = async (credential: string) => {\n * // ...your implementation here\n * }\n * asStatusMethod = {\"CredentialStatusList2017\" : this.checkStatus}\n * }\n * ```\n *\n * @alpha This API is still being developed and may be updated. Please follow progress or suggest improvements at\n * [https://github.com/uport-project/credential-status]\n */\nexport interface StatusResolver {\n checkStatus: StatusMethod\n}\n\n/**\n * The Verifiable Credential or Presentation to be verified in either JSON/JSON-LD or JWT format.\n *\n * @alpha This API is still being developed and may be updated. Please follow progress or suggest improvements at\n * [https://github.com/uport-project/credential-status]\n */\nexport type CredentialJwtOrJSON = string | { credentialStatus?: StatusEntry }\n\n/**\n * The method signature expected to be implemented by credential status resolvers.\n *\n * @param credential The credential whose status will be verified\n * @param didDoc The DID document of the issuer.\n *\n * @return a Promise resolving to a `CredentialStatus` object or rejecting with a reason.\n *\n * @alpha This API is still being developed and may be updated. Please follow progress or suggest improvements at\n * [https://github.com/uport-project/credential-status]\n */\nexport type StatusMethod = (credential: CredentialJwtOrJSON, didDoc: DIDDocument) => Promise<CredentialStatus>\n\n/**\n * [draft] An implementation of a StatusMethod that can aggregate multiple other methods.\n * It calls the appropriate method based on the `credentialStatus.type` specified in the credential.\n *\n * @alpha This API is still being developed and may be updated. Please follow progress or suggest improvements at\n * [https://github.com/uport-project/credential-status]\n */\nexport class Status implements StatusResolver {\n private registry: Record<string, StatusMethod>\n\n /**\n * All the expected StatusMethods should be registered during construction.\n * Example:\n * ```typescript\n * const status = new Status({\n * ...new EthrStatusRegistry(config).asStatusMethod, //using convenience method\n * \"CredentialStatusList2017\": new CredentialStatusList2017().checkStatus, //referencing a checkStatus\n * implementation\n * \"CustomStatusChecker\": customStatusCheckerMethod //directly referencing an independent\n * method\n * })\n * ```\n */\n constructor(registry: Record<string, StatusMethod> = {}) {\n this.registry = registry\n }\n\n async checkStatus(credential: CredentialJwtOrJSON, didDoc: DIDDocument): Promise<CredentialStatus> {\n let statusEntry: StatusEntry | undefined = undefined\n\n if (typeof credential === 'string') {\n try {\n const decoded = decodeJWT(credential)\n statusEntry =\n decoded?.payload?.vc?.credentialStatus || // JWT Verifiable Credential payload\n decoded?.payload?.vp?.credentialStatus || // JWT Verifiable Presentation payload\n decoded?.payload?.credentialStatus // legacy JWT payload\n } catch (e1: unknown) {\n // not a JWT credential or presentation\n try {\n const decoded = JSON.parse(credential)\n statusEntry = decoded?.credentialStatus\n } catch (e2: unknown) {\n // not a JSON either.\n }\n }\n } else {\n statusEntry = credential.credentialStatus\n }\n\n if (!statusEntry) {\n return {\n revoked: false,\n message: 'credentialStatus property was not set on the original credential',\n }\n } else if (typeof statusEntry !== 'object' || !statusEntry?.type) {\n throw new Error('bad_request: credentialStatus entry is not formatted correctly. Validity can not be determined.')\n }\n\n const method = this.registry[statusEntry.type]\n\n if (!method) {\n throw new Error(\n `unknown_method: credentialStatus method ${statusEntry.type} unknown. Validity can not be determined.`,\n )\n } else {\n return method(credential, didDoc)\n }\n }\n}\n"],"names":["Status","constructor","registry","checkStatus","credential","didDoc","statusEntry","undefined","decoded","decodeJWT","payload","vc","credentialStatus","vp","e1","JSON","parse","e2","revoked","message","type","Error","method"],"mappings":";;AAuFA;;;;;;;;MAOaA;AAGX;;;;;;;;;;;;;AAaAC,EAAAA,YAAYC,WAAyC;SAf7CA;AAgBN,SAAKA,QAAL,GAAgBA,QAAhB;AACD;;AAEKC,EAAAA,WAAW,CAACC,UAAD,EAAkCC,MAAlC;AAAA;oBAgCA;;AA/Bf,UAAIC,WAAW,GAA4BC,SAA3C;;AAEA,UAAI,OAAOH,UAAP,KAAsB,QAA1B,EAAoC;AAClC,YAAI;AACF,gBAAMI,OAAO,GAAGC,gBAAS,CAACL,UAAD,CAAzB;AACAE,UAAAA,WAAW,GACTE,OAAO,EAAEE,OAAT,EAAkBC,EAAlB,EAAsBC,gBAAtB;AACAJ,UAAAA,OAAO,EAAEE,OAAT,EAAkBG,EAAlB,EAAsBD,gBADtB;AAEAJ,UAAAA,OAAO,EAAEE,OAAT,EAAkBE,gBAHpB,CAFE;AAMH,SAND,CAME,OAAOE,EAAP,EAAoB;AACpB;AACA,cAAI;AACF,kBAAMN,OAAO,GAAGO,IAAI,CAACC,KAAL,CAAWZ,UAAX,CAAhB;AACAE,YAAAA,WAAW,GAAGE,OAAO,EAAEI,gBAAvB;AACD,WAHD,CAGE,OAAOK,EAAP,EAAoB;AAErB;AACF;AACF,OAhBD,MAgBO;AACLX,QAAAA,WAAW,GAAGF,UAAU,CAACQ,gBAAzB;AACD;;AAED,UAAI,CAACN,WAAL,EAAkB;AAChB,+BAAO;AACLY,UAAAA,OAAO,EAAE,KADJ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