UNPKG

@microsoft/useragent-sdk

Version:

SDK for building decentralized identity wallets and enterprise agents.

155 lines (154 loc) 6.09 kB
/// <reference types="node" /> /// <reference types="pouchdb-core" /> import PublicKey from '../../../keys/PublicKey'; import IJwsGeneralJson, { JwsHeader } from './IJwsGeneralJson'; import { ProtectionFormat } from '../../../keyStore/ProtectionFormat'; import { IJwsSigningOptions } from '../IJoseOptions'; import JwsSignature from './JwsSignature'; import { ICryptoToken } from '../../ICryptoToken'; import IPayloadProtectionOptions from '../../IPayloadProtectionOptions'; /** * Class for containing JWS token operations. * This class hides the JOSE and crypto library dependencies to allow support for additional crypto algorithms. * Crypto calls always happen via CryptoFactory */ export default class JwsToken implements IJwsGeneralJson { /** * Payload (base64url encoded) */ payload: Buffer; /** * Signatures on content */ signatures: JwsSignature[]; /** * Get the request serialization format */ format: ProtectionFormat; private options; /** * Create an Jws token object * @param options Set of jws token options */ constructor(options?: IJwsSigningOptions); /** * Serialize a Jws token object from a token * @param format Optional specify the serialization format. If not specified, use default format. */ serialize(format?: ProtectionFormat): string; /** * Serialize a Jws token object from a token in General Json format * @param token JWS base object */ private static serializeJwsGeneralJson; /** * Serialize a Jws token object from a token in Flat Json format * @param token JWS base object */ private static serializeJwsFlatJson; /** * Serialize a Jws token object from a token in Compact format * @param token JWS base object */ private static serializeJwsCompact; /** * Deserialize a Jws token object */ static deserialize(token: string, options?: IJwsSigningOptions): JwsToken; /** * Try to parse the input token and set the properties of this JswToken * @param content Alledged IJwsGeneralJSon token * @returns true if valid token was parsed */ private setGeneralParts; /** * Try to parse the input token and set the properties of this JswToken * @param content Alledged IJwsFlatJson token * @returns true if valid token was parsed */ private setFlatParts; /** * Check if a valid token was found after decoding */ private isValidToken; /** * Get the keyStore to be used * @param newOptions Options passed in after the constructure * @param mandatory True if property needs to be defined */ private getKeyStore; /** * Get the CryptoFactory to be used * @param newOptions Options passed in after the constructure * @param mandatory True if property needs to be defined */ private getCryptoFactory; /** * Get the default protected header to be used from the options * @param newOptions Options passed in after the constructure * @param mandatory True if property needs to be defined */ getProtected(newOptions?: IJwsSigningOptions, mandatory?: boolean): JwsHeader; /** * Get the default header to be used from the options * @param newOptions Options passed in after the constructure * @param mandatory True if property needs to be defined */ getHeader(newOptions?: IJwsSigningOptions, mandatory?: boolean): JwsHeader; /** * Signs contents using the given private key in JWK format. * * @param signingKeyReference Reference to the signing key. * @param payload to sign. * @param format of the final signature. * @param options used for the signature. These options override the options provided in the constructor. * @returns Signed payload in compact JWS format. */ sign(signingKeyReference: string, payload: Buffer, format: ProtectionFormat, options?: IJwsSigningOptions): Promise<JwsToken>; /** * Verify the JWS signature. * * @param validationKeys Public JWK key to validate the signature. * @param options used for the signature. These options override the options provided in the constructor. * @returns True if signature validated. */ verify(validationKeys: PublicKey[], options?: IJwsSigningOptions): Promise<boolean>; /** * Gets the base64 URL decrypted payload. */ getPayload(): string; /** * Convert a @class ICryptoToken into a @class JwsToken * @param cryptoToken to convert * @param protectOptions options for the token */ static fromCryptoToken(cryptoToken: ICryptoToken, protectOptions: IPayloadProtectionOptions): JwsToken; /** * Convert a @class JwsToken into a @class ICryptoToken * @param protocolFormat format of the token * @param jwsToken to convert * @param options used for the signature. These options override the options provided in the constructor. */ static toCryptoToken(protocolFormat: ProtectionFormat, jwsToken: JwsToken, options: IPayloadProtectionOptions): ICryptoToken; /** * Convert a @class IPayloadProtectionOptions into a @class IJwsSigningOptions * @param protectOptions to convert */ static fromPayloadProtectionOptions(protectOptions: IPayloadProtectionOptions): IJwsSigningOptions; /** * Convert a @class IPayloadProtectionOptions into a @class IJwsSigningOptions * @param signingOptions to convert */ static toPayloadProtectionOptions(signingOptions: IJwsSigningOptions): IPayloadProtectionOptions; private validate; /** * Set the protected header * @param protectedHeader to set on the JwsToken object */ private setProtected; /** * Set the header for the signature * @param header to set on the JwsToken object */ private setHeader; }