@microsoft/useragent-sdk
Version:
SDK for building decentralized identity wallets and enterprise agents.
164 lines • 8.42 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
const CommitProtector_1 = require("../../../src/hubSession/crypto/CommitProtector");
const SubtleCryptoExtension_1 = require("../../../src/crypto/plugin/SubtleCryptoExtension");
const src_1 = require("../../../src");
const CryptoFactory_1 = require("../../../src/crypto/plugin/CryptoFactory");
const ProtectionStrategy_1 = require("../../../src/crypto/strategies/ProtectionStrategy");
const JoseProtocol_1 = require("../../../src/crypto/protocols/jose/JoseProtocol");
const HubInterface_1 = require("../../../src/hubInterfaces/HubInterface");
const JoseConstants_1 = require("../../../src/crypto/protocols/jose/JoseConstants");
const typescript_map_1 = require("typescript-map");
const KeyContainer_1 = require("../../../src/crypto/keys/KeyContainer");
describe('CommitProtector', () => {
const did = 'did:ion:test';
const payloadProtection = new JoseProtocol_1.default();
const payloadData = 'payload';
const signingKeyReference = 'signingKey';
const encryptionKeyReference = 'encryptionKey';
const commitFields = {
interface: src_1.HubInterfaceType.Collections,
context: 'schema.org',
type: 'MusicPlaylist',
operation: HubInterface_1.Operation.Create,
committed_at: '2019-01-01',
commit_strategy: src_1.CommitStrategyType.Basic,
iss: 'did:example:sub.id',
sub: 'did:example:sub.id',
payload: payloadData,
object_id: undefined
};
const keyStore = new src_1.KeyStoreInMemory();
let signingKey;
let cryptoFactory;
beforeAll(async () => {
const seedReference = 'seed';
await keyStore.save(seedReference, new KeyContainer_1.default(new src_1.OctKey('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')));
const subtle = src_1.SubtleCryptoNode.getSubtleCrypto();
cryptoFactory = new CryptoFactory_1.default(keyStore, subtle);
let alg = { name: 'ECDSA', namedCurve: 'P-256K', hash: { name: 'SHA-256' } };
const generate = new SubtleCryptoExtension_1.default(cryptoFactory);
// Generate signing key
signingKey = await generate.generatePairwiseKey(alg, seedReference, 'did:personaId', 'did:peerId');
signingKey.alg = 'ES256K';
signingKey.defaultSignAlgorithm = 'ES256K';
await keyStore.save(signingKeyReference, new KeyContainer_1.default(signingKey));
// Generate encryption key
alg = { name: 'RSA-OAEP', hash: 'SHA-256', modulusLength: 2048, publicExponent: new Uint8Array([0x01, 0x00, 0x01]) };
const encryptionKey = await generate.generatePairwiseKey(alg, seedReference, 'did:personaId', 'did:peerId');
await keyStore.save(encryptionKeyReference, new KeyContainer_1.default(encryptionKey));
});
it('should sign and encrypt the commit', async () => {
const options = {
cryptoFactory: cryptoFactory,
options: new typescript_map_1.TSMap(),
payloadProtection: new JoseProtocol_1.default()
};
const commitProtectOptions = {
did: did,
signingKeyReference: signingKeyReference,
recipientsPublicKeys: [(await keyStore.get(encryptionKeyReference, true)).getKey()],
payloadProtection: payloadProtection,
payloadProtectionOptions: options,
hubProtectionStrategy: undefined,
};
commitFields.payload = { data: 'data' };
const commit = new src_1.Commit(commitFields);
const commitProtector = new CommitProtector_1.default(commitProtectOptions);
const protectedCommit = await commitProtector.protect(commit);
expect(protectedCommit.get(JoseConstants_1.default.tokenCiphertext)).toBeDefined();
expect(protectedCommit.get(JoseConstants_1.default.tokenSignatures)).toBeUndefined();
commitFields.payload = payloadData;
});
it('should throw with bad optiona', async () => {
const protectionStrategy = new ProtectionStrategy_1.default();
// Missing signing key
const options = {
cryptoFactory: cryptoFactory,
options: new typescript_map_1.TSMap(),
payloadProtection: new JoseProtocol_1.default()
};
let commitProtectOptions = {
did: did,
signingKeyReference: undefined,
recipientsPublicKeys: undefined,
payloadProtection: payloadProtection,
payloadProtectionOptions: options,
hubProtectionStrategy: protectionStrategy
};
let throwed = false;
let commitProtector = new CommitProtector_1.default(commitProtectOptions);
let commit = new src_1.Commit(commitFields);
try {
await commitProtector.protect(commit);
}
catch (err) {
throwed = true;
expect(err.message).toEqual('The signing key reference is missing from the options');
}
expect(throwed).toBeTruthy();
// Missing encryption key
throwed = false;
protectionStrategy.payloadSigningStrategy.enabled = false;
commitProtector = new CommitProtector_1.default(commitProtectOptions);
commit = new src_1.Commit(commitFields);
try {
await commitProtector.protect(commit);
}
catch (err) {
throwed = true;
expect(err.message).toEqual('The encryption public keys are missing from the options');
}
expect(throwed).toBeTruthy();
});
it('should sign the commit', async () => {
const protectionStrategy = new ProtectionStrategy_1.default();
protectionStrategy.PayloadEncryptionStrategy.enabled = false;
const options = {
cryptoFactory: cryptoFactory,
options: new typescript_map_1.TSMap(),
payloadProtection: new JoseProtocol_1.default()
};
const commitProtectOptions = {
did: did,
signingKeyReference: signingKeyReference,
recipientsPublicKeys: undefined,
payloadProtection: payloadProtection,
payloadProtectionOptions: options,
hubProtectionStrategy: protectionStrategy
};
const commit = new src_1.Commit(commitFields);
const commitProtector = new CommitProtector_1.default(commitProtectOptions);
const protectedCommit = await commitProtector.protect(commit);
expect(protectedCommit.get(JoseConstants_1.default.tokenPayload)).toEqual(Buffer.from(payloadData));
expect(protectedCommit.get(JoseConstants_1.default.tokenSignatures)).toBeDefined();
});
it('should encrypt the commit', async () => {
const protectionStrategy = new ProtectionStrategy_1.default();
protectionStrategy.payloadSigningStrategy.enabled = false;
const options = {
cryptoFactory: cryptoFactory,
options: new typescript_map_1.TSMap(),
payloadProtection: new JoseProtocol_1.default()
};
const commitProtectOptions = {
did: did,
signingKeyReference: undefined,
recipientsPublicKeys: [(await keyStore.get(encryptionKeyReference, true)).getKey()],
payloadProtection: payloadProtection,
payloadProtectionOptions: options,
hubProtectionStrategy: protectionStrategy,
};
commitFields.payload = { data: 'data' };
const commit = new src_1.Commit(commitFields);
const commitProtector = new CommitProtector_1.default(commitProtectOptions);
const protectedCommit = await commitProtector.protect(commit);
expect(protectedCommit.get(JoseConstants_1.default.tokenCiphertext)).toBeDefined();
expect(protectedCommit.get(JoseConstants_1.default.tokenSignatures)).toBeUndefined();
});
});
//# sourceMappingURL=CommitProtector.spec.js.map