@microsoft/useragent-sdk
Version:
SDK for building decentralized identity wallets and enterprise agents.
196 lines (195 loc) • 7.47 kB
TypeScript
/// <reference types="node" />
/// <reference types="pouchdb-core" />
import PublicKey from '../../../keys/PublicKey';
import IJweGeneralJson, { JweHeader } from './IJweGeneralJson';
import { ProtectionFormat } from '../../../keyStore/ProtectionFormat';
import { IJweEncryptionOptions } from "../IJoseOptions";
import JweRecipient from './JweRecipient';
import IPayloadProtectionOptions from '../../IPayloadProtectionOptions';
import { ICryptoToken } from '../../ICryptoToken';
/**
* Class for containing Jwe 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 JweToken implements IJweGeneralJson {
/**
* The protected header.
*/
protected: JweHeader;
/**
* The unprotected header.
*/
unprotected: JweHeader;
/**
* The initial vector.
*/
iv: Buffer;
/**
* The additional authenticated data.
*/
aad: Buffer;
/**
* The encrypted data.
*/
ciphertext: Buffer;
/**
* The authentication tag used by GCM.
*/
tag: Buffer;
/**
* Signatures on content
*/
recipients: JweRecipient[];
/**
* Get the request serialization format
*/
format: ProtectionFormat;
private options;
/**
* Create an Jwe token object
* @param options Set of Jwe token options
*/
constructor(options?: IJweEncryptionOptions);
/**
* Serialize a Jwe token object from a token
* @param format Optional specify the serialization format. If not specified, use default format.
*/
serialize(format?: ProtectionFormat): string;
/**
* Serialize a Jwe token object from a token in General Json format
* @param token Jwe base object
*/
private static serializeJweGeneralJson;
/**
* Serialize a Jwe token object from a token in Flat Json format
* @param token Jwe base object
*/
private static serializeJweFlatJson;
/**
* Serialize a Jwe token object from a token in Compact format
* @param token Jwe base object
*/
private static serializeJweCompact;
/**
* Deserialize a Jwe token object
*/
static deserialize(token: string, options?: IJweEncryptionOptions): JweToken;
/**
* Try to parse the input token and set the properties of this JswToken
* @param content Alledged IJweGeneralJSon 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 IJweFlatJson token
* @returns true if valid token was parsed
*/
private setFlatParts;
/**
* Check if a valid token was found after decoding
*/
private isValidToken;
/**
* Get the CryptoFactory to be used
* @param newOptions Options passed in after the constructure
* @param manadatory True if property needs to be defined
*/
private getCryptoFactory;
/**
* Get the key encryption key for testing
* @param newOptions Options passed in after the constructure
* @param manadatory True if property needs to be defined
*/
private getContentEncryptionKey;
/**
* Get the initial vector for testing
* @param newOptions Options passed in after the constructure
* @param manadatory True if property needs to be defined
*/
private getInitialVector;
/**
* Get the content encryption algorithm from the options
* @param newOptions Options passed in after the constructure
* @param manadatory True if property needs to be defined
*/
private getContentEncryptionAlgorithm;
/**
* Encrypt content using the given public keys in JWK format.
* The key type enforces the key encryption algorithm.
* The options can override certain algorithm choices.
*
* @param recipients List of recipients' public keys.
* @param payload to encrypt.
* @param format of the final serialization.
* @param options used for the signature. These options override the options provided in the constructor.
* @returns JweToken with encrypted payload.
*/
encrypt(recipients: PublicKey[], payload: string, format: ProtectionFormat, options?: IJweEncryptionOptions): Promise<JweToken>;
/**
* Decrypt the content.
*
* @param decryptionKeyReference Reference to the decryption key.
* @param options used for the signature. These options override the options provided in the constructor.
* @returns Signed payload in compact Jwe format.
*/
decrypt(decryptionKeyReference: string, options?: IJweEncryptionOptions): Promise<Buffer>;
private decryptContentEncryptionKey;
/**
* 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
*/
private getProtected;
/**
* 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
*/
getUnprotected(newOptions?: IJweEncryptionOptions, mandatory?: boolean): JweHeader;
/**
* 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?: IJweEncryptionOptions, mandatory?: boolean): JweHeader;
/**
* Convert a @class ICryptoToken into a @class JweToken
* @param cryptoToken to convert
* @param protectOptions options for the token
*/
static fromCryptoToken(cryptoToken: ICryptoToken, protectOptions: IPayloadProtectionOptions): JweToken;
/**
* Convert a @class JweToken into a @class ICryptoToken
* @param protocolFormat format of the token
* @param jweToken to convert
* @param options used for the encryption. These options override the options provided in the constructor.
*/
static toCryptoToken(protocolFormat: ProtectionFormat, jweToken: JweToken, options: IPayloadProtectionOptions): ICryptoToken;
/**
* Convert a @class IPayloadProtectionOptions into a @class IJweEncryptionOptions
* @param protectOptions to convert
*/
static fromPayloadProtectionOptions(protectOptions: IPayloadProtectionOptions): IJweEncryptionOptions;
/**
* Convert a @class IPayloadProtectionOptions into a @class IJweEncryptionOptions
* @param encryptionOptions to convert
*/
static toPayloadProtectionOptions(encryptionOptions: IJweEncryptionOptions): IPayloadProtectionOptions;
/**
* Set the header in the recipients object
* @param header to set on the JweToken recipients object
*/
private static setHeader;
/**
* Set the unprotected header
* @param unprotectedHeader to set on the JweToken object
*/
private static setUnprotected;
/**
* Set the protected header
* @param protectedHeader to set on the JweToken object
*/
private static setProtected;
}