1
0
mirror of https://github.com/bitwarden/jslib synced 2026-01-06 10:33:15 +00:00

Run prettier in feature branch

This commit is contained in:
Thomas Rittson
2021-12-17 06:09:55 +10:00
parent 44b0d51336
commit 1acfa54f00
593 changed files with 47237 additions and 42401 deletions

View File

@@ -1,73 +1,73 @@
import { FirefoxCsvImporter as Importer } from 'jslib-common/importers/firefoxCsvImporter';
import { FirefoxCsvImporter as Importer } from "jslib-common/importers/firefoxCsvImporter";
import { CipherView } from 'jslib-common/models/view/cipherView';
import { LoginUriView } from 'jslib-common/models/view/loginUriView';
import { LoginView } from 'jslib-common/models/view/loginView';
import { CipherView } from "jslib-common/models/view/cipherView";
import { LoginUriView } from "jslib-common/models/view/loginUriView";
import { LoginView } from "jslib-common/models/view/loginView";
import { data as firefoxAccountsData } from './testData/firefoxCsv/firefoxAccountsData.csv';
import { data as simplePasswordData } from './testData/firefoxCsv/simplePasswordData.csv';
import { data as firefoxAccountsData } from "./testData/firefoxCsv/firefoxAccountsData.csv";
import { data as simplePasswordData } from "./testData/firefoxCsv/simplePasswordData.csv";
const CipherData = [
{
title: 'should parse password',
csv: simplePasswordData,
expected: Object.assign(new CipherView(), {
id: null,
organizationId: null,
folderId: null,
name: 'example.com',
login: Object.assign(new LoginView(), {
username: 'foo',
password: 'bar',
uris: [
Object.assign(new LoginUriView(), {
uri: 'https://example.com',
}),
],
}),
notes: null,
type: 1,
}),
},
{
title: 'should skip "chrome://FirefoxAccounts"',
csv: firefoxAccountsData,
expected: Object.assign(new CipherView(), {
id: null,
organizationId: null,
folderId: null,
name: 'example.com',
login: Object.assign(new LoginView(), {
username: 'foo',
password: 'bar',
uris: [
Object.assign(new LoginUriView(), {
uri: 'https://example.com',
}),
],
}),
notes: null,
type: 1,
}),
},
{
title: "should parse password",
csv: simplePasswordData,
expected: Object.assign(new CipherView(), {
id: null,
organizationId: null,
folderId: null,
name: "example.com",
login: Object.assign(new LoginView(), {
username: "foo",
password: "bar",
uris: [
Object.assign(new LoginUriView(), {
uri: "https://example.com",
}),
],
}),
notes: null,
type: 1,
}),
},
{
title: 'should skip "chrome://FirefoxAccounts"',
csv: firefoxAccountsData,
expected: Object.assign(new CipherView(), {
id: null,
organizationId: null,
folderId: null,
name: "example.com",
login: Object.assign(new LoginView(), {
username: "foo",
password: "bar",
uris: [
Object.assign(new LoginUriView(), {
uri: "https://example.com",
}),
],
}),
notes: null,
type: 1,
}),
},
];
describe('Firefox CSV Importer', () => {
CipherData.forEach(data => {
it(data.title, async () => {
const importer = new Importer();
const result = await importer.parse(data.csv);
expect(result != null).toBe(true);
expect(result.ciphers.length).toBeGreaterThan(0);
describe("Firefox CSV Importer", () => {
CipherData.forEach((data) => {
it(data.title, async () => {
const importer = new Importer();
const result = await importer.parse(data.csv);
expect(result != null).toBe(true);
expect(result.ciphers.length).toBeGreaterThan(0);
const cipher = result.ciphers.shift();
let property: keyof typeof data.expected;
for (property in data.expected) {
if (data.expected.hasOwnProperty(property)) {
expect(cipher.hasOwnProperty(property)).toBe(true);
expect(cipher[property]).toEqual(data.expected[property]);
}
}
});
const cipher = result.ciphers.shift();
let property: keyof typeof data.expected;
for (property in data.expected) {
if (data.expected.hasOwnProperty(property)) {
expect(cipher.hasOwnProperty(property)).toBe(true);
expect(cipher[property]).toEqual(data.expected[property]);
}
}
});
});
});

View File

@@ -1,80 +1,77 @@
import { FSecureFskImporter as Importer } from 'jslib-common/importers/fsecureFskImporter';
import { FSecureFskImporter as Importer } from "jslib-common/importers/fsecureFskImporter";
const TestDataWithStyleSetToWebsite: string =
JSON.stringify({
data: {
'8d58b5cf252dd06fbd98f5289e918ab1': {
color: '#00baff',
reatedDate: 1609302913,
creditCvv: '',
creditExpiry: '',
creditNumber: '',
favorite: 0,
modifiedDate: 1609302913,
notes: 'note',
password: 'word',
passwordList: [],
passwordModifiedDate: 1609302913,
rev: 1,
service: 'My first pass',
style: 'website',
type: 1,
url: 'https://bitwarden.com',
username: 'pass',
},
},
});
const TestDataWithStyleSetToGlobe: string =
JSON.stringify({
data: {
'8d58b5cf252dd06fbd98f5289e918ab1': {
color: '#00baff',
reatedDate: 1609302913,
creditCvv: '',
creditExpiry: '',
creditNumber: '',
favorite: 0,
modifiedDate: 1609302913,
notes: 'note',
password: 'word',
passwordList: [],
passwordModifiedDate: 1609302913,
rev: 1,
service: 'My first pass',
style: 'globe',
type: 1,
url: 'https://bitwarden.com',
username: 'pass',
},
},
});
describe('FSecure FSK Importer', () => {
it('should parse data with style set to website', async () => {
const importer = new Importer();
const result = await importer.parse(TestDataWithStyleSetToWebsite);
expect(result != null).toBe(true);
const cipher = result.ciphers.shift();
expect(cipher.login.username).toEqual('pass');
expect(cipher.login.password).toEqual('word');
expect(cipher.login.uris.length).toEqual(1);
const uriView = cipher.login.uris.shift();
expect(uriView.uri).toEqual('https://bitwarden.com');
});
it('should parse data with style set to globe', async () => {
const importer = new Importer();
const result = await importer.parse(TestDataWithStyleSetToGlobe);
expect(result != null).toBe(true);
const cipher = result.ciphers.shift();
expect(cipher.login.username).toEqual('pass');
expect(cipher.login.password).toEqual('word');
expect(cipher.login.uris.length).toEqual(1);
const uriView = cipher.login.uris.shift();
expect(uriView.uri).toEqual('https://bitwarden.com');
});
const TestDataWithStyleSetToWebsite: string = JSON.stringify({
data: {
"8d58b5cf252dd06fbd98f5289e918ab1": {
color: "#00baff",
reatedDate: 1609302913,
creditCvv: "",
creditExpiry: "",
creditNumber: "",
favorite: 0,
modifiedDate: 1609302913,
notes: "note",
password: "word",
passwordList: [],
passwordModifiedDate: 1609302913,
rev: 1,
service: "My first pass",
style: "website",
type: 1,
url: "https://bitwarden.com",
username: "pass",
},
},
});
const TestDataWithStyleSetToGlobe: string = JSON.stringify({
data: {
"8d58b5cf252dd06fbd98f5289e918ab1": {
color: "#00baff",
reatedDate: 1609302913,
creditCvv: "",
creditExpiry: "",
creditNumber: "",
favorite: 0,
modifiedDate: 1609302913,
notes: "note",
password: "word",
passwordList: [],
passwordModifiedDate: 1609302913,
rev: 1,
service: "My first pass",
style: "globe",
type: 1,
url: "https://bitwarden.com",
username: "pass",
},
},
});
describe("FSecure FSK Importer", () => {
it("should parse data with style set to website", async () => {
const importer = new Importer();
const result = await importer.parse(TestDataWithStyleSetToWebsite);
expect(result != null).toBe(true);
const cipher = result.ciphers.shift();
expect(cipher.login.username).toEqual("pass");
expect(cipher.login.password).toEqual("word");
expect(cipher.login.uris.length).toEqual(1);
const uriView = cipher.login.uris.shift();
expect(uriView.uri).toEqual("https://bitwarden.com");
});
it("should parse data with style set to globe", async () => {
const importer = new Importer();
const result = await importer.parse(TestDataWithStyleSetToGlobe);
expect(result != null).toBe(true);
const cipher = result.ciphers.shift();
expect(cipher.login.username).toEqual("pass");
expect(cipher.login.password).toEqual("word");
expect(cipher.login.uris.length).toEqual(1);
const uriView = cipher.login.uris.shift();
expect(uriView.uri).toEqual("https://bitwarden.com");
});
});

View File

@@ -1,4 +1,4 @@
import { KeePass2XmlImporter as Importer } from 'jslib-common/importers/keepass2XmlImporter';
import { KeePass2XmlImporter as Importer } from "jslib-common/importers/keepass2XmlImporter";
const TestData: string = `<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<KeePassFile>
@@ -180,10 +180,10 @@ line2</Value>
</Root>
</KeePassFile>`;
describe('KeePass2 Xml Importer', () => {
it('should parse XML data', async () => {
const importer = new Importer();
const result = await importer.parse(TestData);
expect(result != null).toBe(true);
});
describe("KeePass2 Xml Importer", () => {
it("should parse XML data", async () => {
const importer = new Importer();
const result = await importer.parse(TestData);
expect(result != null).toBe(true);
});
});

View File

@@ -1,33 +1,33 @@
import { LastPassCsvImporter as Importer } from 'jslib-common/importers/lastpassCsvImporter';
import { LastPassCsvImporter as Importer } from "jslib-common/importers/lastpassCsvImporter";
import { ImportResult } from 'jslib-common/models/domain/importResult';
import { CipherView } from 'jslib-common/models/view/cipherView';
import { FieldView } from 'jslib-common/models/view/fieldView';
import { ImportResult } from "jslib-common/models/domain/importResult";
import { CipherView } from "jslib-common/models/view/cipherView";
import { FieldView } from "jslib-common/models/view/fieldView";
import { CipherType } from 'jslib-common/enums/cipherType';
import { FieldType } from 'jslib-common/enums/fieldType';
import { CipherType } from "jslib-common/enums/cipherType";
import { FieldType } from "jslib-common/enums/fieldType";
function baseExcept(result: ImportResult) {
expect(result).not.toBeNull();
expect(result.success).toBe(true);
expect(result.ciphers.length).toBe(1);
expect(result).not.toBeNull();
expect(result.success).toBe(true);
expect(result.ciphers.length).toBe(1);
}
function expectLogin(cipher: CipherView) {
expect(cipher.type).toBe(CipherType.Login);
expect(cipher.type).toBe(CipherType.Login);
expect(cipher.name).toBe('example.com');
expect(cipher.notes).toBe('super secure notes');
expect(cipher.login.uri).toBe('http://example.com');
expect(cipher.login.username).toBe('someUser');
expect(cipher.login.password).toBe('myPassword');
expect(cipher.login.totp).toBe('Y64VEVMBTSXCYIWRSHRNDZW62MPGVU2G');
expect(cipher.name).toBe("example.com");
expect(cipher.notes).toBe("super secure notes");
expect(cipher.login.uri).toBe("http://example.com");
expect(cipher.login.username).toBe("someUser");
expect(cipher.login.password).toBe("myPassword");
expect(cipher.login.totp).toBe("Y64VEVMBTSXCYIWRSHRNDZW62MPGVU2G");
}
const CipherData = [
{
title: 'should parse expiration date',
csv: `url,username,password,extra,name,grouping,fav
{
title: "should parse expiration date",
csv: `url,username,password,extra,name,grouping,fav
http://sn,,,"NoteType:Credit Card
Name on Card:John Doe
Type:
@@ -37,32 +37,32 @@ Start Date:October,2017
Expiration Date:June,2020
Notes:some text
",Credit-card,,0`,
expected: Object.assign(new CipherView(), {
id: null,
organizationId: null,
folderId: null,
name: 'Credit-card',
notes: 'some text\n',
type: 3,
card: {
cardholderName: 'John Doe',
number: '1234567812345678',
code: '123',
expYear: '2020',
expMonth: '6',
},
fields: [
Object.assign(new FieldView(), {
name: 'Start Date',
value: 'October,2017',
type: FieldType.Text,
}),
],
expected: Object.assign(new CipherView(), {
id: null,
organizationId: null,
folderId: null,
name: "Credit-card",
notes: "some text\n",
type: 3,
card: {
cardholderName: "John Doe",
number: "1234567812345678",
code: "123",
expYear: "2020",
expMonth: "6",
},
fields: [
Object.assign(new FieldView(), {
name: "Start Date",
value: "October,2017",
type: FieldType.Text,
}),
},
{
title: 'should parse blank card note',
csv: `url,username,password,extra,name,grouping,fav
],
}),
},
{
title: "should parse blank card note",
csv: `url,username,password,extra,name,grouping,fav
http://sn,,,"NoteType:Credit Card
Name on Card:
Type:
@@ -71,28 +71,28 @@ Security Code:
Start Date:,
Expiration Date:,
Notes:",empty,,0`,
expected: Object.assign(new CipherView(), {
id: null,
organizationId: null,
folderId: null,
name: 'empty',
notes: null,
type: 3,
card: {
expMonth: undefined,
},
fields: [
Object.assign(new FieldView(), {
name: 'Start Date',
value: ',',
type: FieldType.Text,
}),
],
expected: Object.assign(new CipherView(), {
id: null,
organizationId: null,
folderId: null,
name: "empty",
notes: null,
type: 3,
card: {
expMonth: undefined,
},
fields: [
Object.assign(new FieldView(), {
name: "Start Date",
value: ",",
type: FieldType.Text,
}),
},
{
title: 'should parse card expiration date w/ no exp year',
csv: `url,username,password,extra,name,grouping,fav
],
}),
},
{
title: "should parse card expiration date w/ no exp year",
csv: `url,username,password,extra,name,grouping,fav
http://sn,,,"NoteType:Credit Card
Name on Card:John Doe
Type:Visa
@@ -101,36 +101,36 @@ Security Code:321
Start Date:,
Expiration Date:January,
Notes:",noyear,,0`,
expected: Object.assign(new CipherView(), {
id: null,
organizationId: null,
folderId: null,
name: 'noyear',
notes: null,
type: 3,
card: {
cardholderName: 'John Doe',
number: '1234567887654321',
code: '321',
expMonth: '1',
},
fields: [
Object.assign(new FieldView(), {
name: 'Type',
value: 'Visa',
type: FieldType.Text,
}),
Object.assign(new FieldView(), {
name: 'Start Date',
value: ',',
type: FieldType.Text,
}),
],
expected: Object.assign(new CipherView(), {
id: null,
organizationId: null,
folderId: null,
name: "noyear",
notes: null,
type: 3,
card: {
cardholderName: "John Doe",
number: "1234567887654321",
code: "321",
expMonth: "1",
},
fields: [
Object.assign(new FieldView(), {
name: "Type",
value: "Visa",
type: FieldType.Text,
}),
},
{
title: 'should parse card expiration date w/ no month',
csv: `url,username,password,extra,name,grouping,fav
Object.assign(new FieldView(), {
name: "Start Date",
value: ",",
type: FieldType.Text,
}),
],
}),
},
{
title: "should parse card expiration date w/ no month",
csv: `url,username,password,extra,name,grouping,fav
http://sn,,,"NoteType:Credit Card
Name on Card:John Doe
Type:Mastercard
@@ -139,64 +139,64 @@ Security Code:987
Start Date:,
Expiration Date:,2020
Notes:",nomonth,,0`,
expected: Object.assign(new CipherView(), {
id: null,
organizationId: null,
folderId: null,
name: 'nomonth',
notes: null,
type: 3,
card: {
cardholderName: 'John Doe',
number: '8765432112345678',
code: '987',
expYear: '2020',
expMonth: undefined,
},
fields: [
Object.assign(new FieldView(), {
name: 'Type',
value: 'Mastercard',
type: FieldType.Text,
}),
Object.assign(new FieldView(), {
name: 'Start Date',
value: ',',
type: FieldType.Text,
}),
],
expected: Object.assign(new CipherView(), {
id: null,
organizationId: null,
folderId: null,
name: "nomonth",
notes: null,
type: 3,
card: {
cardholderName: "John Doe",
number: "8765432112345678",
code: "987",
expYear: "2020",
expMonth: undefined,
},
fields: [
Object.assign(new FieldView(), {
name: "Type",
value: "Mastercard",
type: FieldType.Text,
}),
},
Object.assign(new FieldView(), {
name: "Start Date",
value: ",",
type: FieldType.Text,
}),
],
}),
},
];
describe('Lastpass CSV Importer', () => {
CipherData.forEach(data => {
it(data.title, async () => {
const importer = new Importer();
const result = await importer.parse(data.csv);
expect(result != null).toBe(true);
expect(result.ciphers.length).toBeGreaterThan(0);
describe("Lastpass CSV Importer", () => {
CipherData.forEach((data) => {
it(data.title, async () => {
const importer = new Importer();
const result = await importer.parse(data.csv);
expect(result != null).toBe(true);
expect(result.ciphers.length).toBeGreaterThan(0);
const cipher = result.ciphers.shift();
let property: keyof typeof data.expected;
for (property in data.expected) {
if (data.expected.hasOwnProperty(property)) {
expect(cipher.hasOwnProperty(property)).toBe(true);
expect(cipher[property]).toEqual(data.expected[property]);
}
}
});
const cipher = result.ciphers.shift();
let property: keyof typeof data.expected;
for (property in data.expected) {
if (data.expected.hasOwnProperty(property)) {
expect(cipher.hasOwnProperty(property)).toBe(true);
expect(cipher[property]).toEqual(data.expected[property]);
}
}
});
});
it('should parse login with totp', async () => {
const input = `url,username,password,totp,extra,name,grouping,fav
it("should parse login with totp", async () => {
const input = `url,username,password,totp,extra,name,grouping,fav
http://example.com,someUser,myPassword,Y64VEVMBTSXCYIWRSHRNDZW62MPGVU2G,super secure notes,example.com,,0`;
const importer = new Importer();
const result = await importer.parse(input);
baseExcept(result);
const importer = new Importer();
const result = await importer.parse(input);
baseExcept(result);
const cipher = result.ciphers[0];
expectLogin(cipher);
});
});
const cipher = result.ciphers[0];
expectLogin(cipher);
});
});

View File

@@ -1,181 +1,182 @@
import { NordPassCsvImporter as Importer } from 'jslib-common/importers/nordpassCsvImporter';
import { NordPassCsvImporter as Importer } from "jslib-common/importers/nordpassCsvImporter";
import { CipherType } from 'jslib-common/enums/cipherType';
import { SecureNoteType } from 'jslib-common/enums/secureNoteType';
import { CipherView } from 'jslib-common/models/view/cipherView';
import { IdentityView } from 'jslib-common/models/view/identityView';
import { CipherType } from "jslib-common/enums/cipherType";
import { SecureNoteType } from "jslib-common/enums/secureNoteType";
import { CipherView } from "jslib-common/models/view/cipherView";
import { IdentityView } from "jslib-common/models/view/identityView";
import { data as creditCardData } from './testData/nordpassCsv/nordpass.card.csv';
import { data as identityData } from './testData/nordpassCsv/nordpass.identity.csv';
import { data as loginData } from './testData/nordpassCsv/nordpass.login.csv';
import { data as secureNoteData } from './testData/nordpassCsv/nordpass.secureNote.csv';
import { data as creditCardData } from "./testData/nordpassCsv/nordpass.card.csv";
import { data as identityData } from "./testData/nordpassCsv/nordpass.identity.csv";
import { data as loginData } from "./testData/nordpassCsv/nordpass.login.csv";
import { data as secureNoteData } from "./testData/nordpassCsv/nordpass.secureNote.csv";
const namesTestData = [
{
title: 'Given #fullName should set firstName',
fullName: 'MyFirstName',
expected: Object.assign(new IdentityView(), {
firstName: 'MyFirstName',
middleName: null,
lastName: null,
}),
},
{
title: 'Given #fullName should set first- and lastName',
fullName: 'MyFirstName MyLastName',
expected: Object.assign(new IdentityView(), {
firstName: 'MyFirstName',
middleName: null,
lastName: 'MyLastName',
}),
},
{
title: 'Given #fullName should set first-, middle and lastName',
fullName: 'MyFirstName MyMiddleName MyLastName',
expected: Object.assign(new IdentityView(), {
firstName: 'MyFirstName',
middleName: 'MyMiddleName',
lastName: 'MyLastName',
}),
},
{
title: 'Given #fullName should set first-, middle and lastName with Jr',
fullName: 'MyFirstName MyMiddleName MyLastName Jr',
expected: Object.assign(new IdentityView(), {
firstName: 'MyFirstName',
middleName: 'MyMiddleName',
lastName: 'MyLastName Jr',
}),
},
{
title: 'Given #fullName should set first-, middle and lastName with Jr and III',
fullName: 'MyFirstName MyMiddleName MyLastName Jr III',
expected: Object.assign(new IdentityView(), {
firstName: 'MyFirstName',
middleName: 'MyMiddleName',
lastName: 'MyLastName Jr III',
}),
},
{
title: "Given #fullName should set firstName",
fullName: "MyFirstName",
expected: Object.assign(new IdentityView(), {
firstName: "MyFirstName",
middleName: null,
lastName: null,
}),
},
{
title: "Given #fullName should set first- and lastName",
fullName: "MyFirstName MyLastName",
expected: Object.assign(new IdentityView(), {
firstName: "MyFirstName",
middleName: null,
lastName: "MyLastName",
}),
},
{
title: "Given #fullName should set first-, middle and lastName",
fullName: "MyFirstName MyMiddleName MyLastName",
expected: Object.assign(new IdentityView(), {
firstName: "MyFirstName",
middleName: "MyMiddleName",
lastName: "MyLastName",
}),
},
{
title: "Given #fullName should set first-, middle and lastName with Jr",
fullName: "MyFirstName MyMiddleName MyLastName Jr",
expected: Object.assign(new IdentityView(), {
firstName: "MyFirstName",
middleName: "MyMiddleName",
lastName: "MyLastName Jr",
}),
},
{
title: "Given #fullName should set first-, middle and lastName with Jr and III",
fullName: "MyFirstName MyMiddleName MyLastName Jr III",
expected: Object.assign(new IdentityView(), {
firstName: "MyFirstName",
middleName: "MyMiddleName",
lastName: "MyLastName Jr III",
}),
},
];
function expectLogin(cipher: CipherView) {
expect(cipher.type).toBe(CipherType.Login);
expect(cipher.type).toBe(CipherType.Login);
expect(cipher.name).toBe('SomeVaultItemName');
expect(cipher.notes).toBe('Some note for the VaultItem');
expect(cipher.login.uri).toBe('https://example.com');
expect(cipher.login.username).toBe('hello@bitwarden.com');
expect(cipher.login.password).toBe('someStrongPassword');
expect(cipher.name).toBe("SomeVaultItemName");
expect(cipher.notes).toBe("Some note for the VaultItem");
expect(cipher.login.uri).toBe("https://example.com");
expect(cipher.login.username).toBe("hello@bitwarden.com");
expect(cipher.login.password).toBe("someStrongPassword");
}
function expectCreditCard(cipher: CipherView) {
expect(cipher.type).toBe(CipherType.Card);
expect(cipher.type).toBe(CipherType.Card);
expect(cipher.name).toBe('SomeVisa');
expect(cipher.card.brand).toBe('Visa');
expect(cipher.card.cardholderName).toBe('SomeHolder');
expect(cipher.card.number).toBe('4024007103939509');
expect(cipher.card.code).toBe('123');
expect(cipher.card.expMonth).toBe('1');
expect(cipher.card.expYear).toBe('22');
expect(cipher.name).toBe("SomeVisa");
expect(cipher.card.brand).toBe("Visa");
expect(cipher.card.cardholderName).toBe("SomeHolder");
expect(cipher.card.number).toBe("4024007103939509");
expect(cipher.card.code).toBe("123");
expect(cipher.card.expMonth).toBe("1");
expect(cipher.card.expYear).toBe("22");
}
function expectIdentity(cipher: CipherView) {
expect(cipher.type).toBe(CipherType.Identity);
expect(cipher.type).toBe(CipherType.Identity);
expect(cipher.name).toBe('SomeTitle');
expect(cipher.identity.fullName).toBe('MyFirstName MyMiddleName MyLastName');
expect(cipher.identity.firstName).toBe('MyFirstName');
expect(cipher.identity.middleName).toBe('MyMiddleName');
expect(cipher.identity.lastName).toBe('MyLastName');
expect(cipher.identity.email).toBe('hello@bitwarden.com');
expect(cipher.identity.phone).toBe('123456789');
expect(cipher.name).toBe("SomeTitle");
expect(cipher.identity.fullName).toBe("MyFirstName MyMiddleName MyLastName");
expect(cipher.identity.firstName).toBe("MyFirstName");
expect(cipher.identity.middleName).toBe("MyMiddleName");
expect(cipher.identity.lastName).toBe("MyLastName");
expect(cipher.identity.email).toBe("hello@bitwarden.com");
expect(cipher.identity.phone).toBe("123456789");
expect(cipher.identity.address1).toBe('Test street 123');
expect(cipher.identity.address2).toBe('additional addressinfo');
expect(cipher.identity.postalCode).toBe('123456');
expect(cipher.identity.city).toBe('Cologne');
expect(cipher.identity.state).toBe('North-Rhine-Westphalia');
expect(cipher.identity.country).toBe('GERMANY');
expect(cipher.notes).toBe('SomeNoteToMyIdentity');
expect(cipher.identity.address1).toBe("Test street 123");
expect(cipher.identity.address2).toBe("additional addressinfo");
expect(cipher.identity.postalCode).toBe("123456");
expect(cipher.identity.city).toBe("Cologne");
expect(cipher.identity.state).toBe("North-Rhine-Westphalia");
expect(cipher.identity.country).toBe("GERMANY");
expect(cipher.notes).toBe("SomeNoteToMyIdentity");
}
function expectSecureNote(cipher: CipherView) {
expect(cipher.type).toBe(CipherType.SecureNote);
expect(cipher.type).toBe(CipherType.SecureNote);
expect(cipher.name).toBe('MySuperSecureNoteTitle');
expect(cipher.secureNote.type).toBe(SecureNoteType.Generic);
expect(cipher.notes).toBe('MySuperSecureNote');
expect(cipher.name).toBe("MySuperSecureNoteTitle");
expect(cipher.secureNote.type).toBe(SecureNoteType.Generic);
expect(cipher.notes).toBe("MySuperSecureNote");
}
describe('NordPass CSV Importer', () => {
let importer: Importer;
beforeEach(() => {
importer = new Importer();
describe("NordPass CSV Importer", () => {
let importer: Importer;
beforeEach(() => {
importer = new Importer();
});
it("should parse login records", async () => {
const result = await importer.parse(loginData);
expect(result).not.toBeNull();
expect(result.success).toBe(true);
expect(result.ciphers.length).toBe(1);
const cipher = result.ciphers[0];
expectLogin(cipher);
});
it("should parse credit card records", async () => {
const result = await importer.parse(creditCardData);
expect(result).not.toBeNull();
expect(result.success).toBe(true);
expect(result.ciphers.length).toBe(1);
const cipher = result.ciphers[0];
expectCreditCard(cipher);
});
it("should parse identity records", async () => {
const result = await importer.parse(
identityData.replace("#fullName", "MyFirstName MyMiddleName MyLastName")
);
expect(result).not.toBeNull();
expect(result.success).toBe(true);
expect(result.ciphers.length).toBe(1);
const cipher = result.ciphers[0];
expectIdentity(cipher);
});
namesTestData.forEach((data) => {
it(data.title.replace("#fullName", data.fullName), async () => {
const result = await importer.parse(identityData.replace("#fullName", data.fullName));
expect(result).not.toBeNull();
expect(result.success).toBe(true);
expect(result.ciphers.length).toBe(1);
const cipher = result.ciphers[0];
expect(cipher.identity.firstName).toBe(data.expected.firstName);
expect(cipher.identity.middleName).toBe(data.expected.middleName);
expect(cipher.identity.lastName).toBe(data.expected.lastName);
});
});
it('should parse login records', async () => {
const result = await importer.parse(loginData);
it("should parse secureNote records", async () => {
const result = await importer.parse(secureNoteData);
expect(result).not.toBeNull();
expect(result.success).toBe(true);
expect(result.ciphers.length).toBe(1);
const cipher = result.ciphers[0];
expectLogin(cipher);
});
expect(result).not.toBeNull();
expect(result.success).toBe(true);
expect(result.ciphers.length).toBe(1);
const cipher = result.ciphers[0];
expectSecureNote(cipher);
});
it('should parse credit card records', async () => {
const result = await importer.parse(creditCardData);
it("should parse an item and create a folder", async () => {
const result = await importer.parse(secureNoteData);
expect(result).not.toBeNull();
expect(result.success).toBe(true);
expect(result.ciphers.length).toBe(1);
const cipher = result.ciphers[0];
expectCreditCard(cipher);
});
it('should parse identity records', async () => {
const result = await importer.parse(identityData.replace('#fullName', 'MyFirstName MyMiddleName MyLastName'));
expect(result).not.toBeNull();
expect(result.success).toBe(true);
expect(result.ciphers.length).toBe(1);
const cipher = result.ciphers[0];
expectIdentity(cipher);
});
namesTestData.forEach(data => {
it(data.title.replace('#fullName', data.fullName), async () => {
const result = await importer.parse(identityData.replace('#fullName', data.fullName));
expect(result).not.toBeNull();
expect(result.success).toBe(true);
expect(result.ciphers.length).toBe(1);
const cipher = result.ciphers[0];
expect(cipher.identity.firstName).toBe(data.expected.firstName);
expect(cipher.identity.middleName).toBe(data.expected.middleName);
expect(cipher.identity.lastName).toBe(data.expected.lastName);
});
});
it('should parse secureNote records', async () => {
const result = await importer.parse(secureNoteData);
expect(result).not.toBeNull();
expect(result.success).toBe(true);
expect(result.ciphers.length).toBe(1);
const cipher = result.ciphers[0];
expectSecureNote(cipher);
});
it('should parse an item and create a folder', async () => {
const result = await importer.parse(secureNoteData);
expect(result).not.toBeNull();
expect(result.success).toBe(true);
expect(result.folders.length).toBe(1);
const folder = result.folders[0];
expect(folder.name).toBe('notesFolder');
});
expect(result).not.toBeNull();
expect(result.success).toBe(true);
expect(result.folders.length).toBe(1);
const folder = result.folders[0];
expect(folder.name).toBe("notesFolder");
});
});

View File

@@ -1,513 +1,514 @@
import { FieldType } from 'jslib-common/enums/fieldType';
import { OnePassword1PifImporter as Importer } from 'jslib-common/importers/onepasswordImporters/onepassword1PifImporter';
import { FieldType } from "jslib-common/enums/fieldType";
import { OnePassword1PifImporter as Importer } from "jslib-common/importers/onepasswordImporters/onepassword1PifImporter";
const TestData: string = '***aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee***\n' +
JSON.stringify({
uuid: 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
updatedAt: 1486071244,
securityLevel: 'SL5',
contentsHash: 'aaaaaaaa',
title: 'Imported Entry',
location: 'https://www.google.com',
secureContents: {
fields: [
{
value: 'user@test.net',
id: 'email-input',
name: 'email',
type: 'T',
designation: 'username',
},
{
value: 'myservicepassword',
id: 'password-input',
name: 'password',
type: 'P',
designation: 'password',
},
],
sections: [
{
fields: [
{
k: 'concealed',
n: 'AAAAAAAAAAAABBBBBBBBBBBCCCCCCCCC',
v: 'console-password-123',
t: 'console password',
},
],
title: 'Admin Console',
name: 'admin_console',
},
],
passwordHistory: [
{
value: 'old-password',
time: 1447791421,
},
],
const TestData: string =
"***aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee***\n" +
JSON.stringify({
uuid: "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
updatedAt: 1486071244,
securityLevel: "SL5",
contentsHash: "aaaaaaaa",
title: "Imported Entry",
location: "https://www.google.com",
secureContents: {
fields: [
{
value: "user@test.net",
id: "email-input",
name: "email",
type: "T",
designation: "username",
},
URLs: [
{
value: "myservicepassword",
id: "password-input",
name: "password",
type: "P",
designation: "password",
},
],
sections: [
{
fields: [
{
label: 'website',
url: 'https://www.google.com',
k: "concealed",
n: "AAAAAAAAAAAABBBBBBBBBBBCCCCCCCCC",
v: "console-password-123",
t: "console password",
},
],
txTimestamp: 1508941334,
createdAt: 1390426636,
typeName: 'webforms.WebForm',
});
],
title: "Admin Console",
name: "admin_console",
},
],
passwordHistory: [
{
value: "old-password",
time: 1447791421,
},
],
},
URLs: [
{
label: "website",
url: "https://www.google.com",
},
],
txTimestamp: 1508941334,
createdAt: 1390426636,
typeName: "webforms.WebForm",
});
const WindowsOpVaultTestData = JSON.stringify({
category: '001',
created: 1544823719,
hmac: 'NtyBmTTPOb88HV3JUKPx1xl/vcMhac9kvCfe/NtszY0=',
k: '**REMOVED LONG LINE FOR LINTER** -Kyle',
tx: 1553395669,
updated: 1553395669,
uuid: '528AB076FB5F4FBF960884B8E01619AC',
overview: {
title: 'Google',
URLs: [
{
u: 'google.com',
},
],
url: 'google.com',
ps: 26,
ainfo: 'googluser',
},
details: {
passwordHistory: [
{
value: 'oldpass1',
time: 1553394449,
},
{
value: 'oldpass2',
time: 1553394457,
},
{
value: 'oldpass3',
time: 1553394458,
},
{
value: 'oldpass4',
time: 1553394459,
},
{
value: 'oldpass5',
time: 1553394460,
},
{
value: 'oldpass6',
time: 1553394461,
},
],
category: "001",
created: 1544823719,
hmac: "NtyBmTTPOb88HV3JUKPx1xl/vcMhac9kvCfe/NtszY0=",
k: "**REMOVED LONG LINE FOR LINTER** -Kyle",
tx: 1553395669,
updated: 1553395669,
uuid: "528AB076FB5F4FBF960884B8E01619AC",
overview: {
title: "Google",
URLs: [
{
u: "google.com",
},
],
url: "google.com",
ps: 26,
ainfo: "googluser",
},
details: {
passwordHistory: [
{
value: "oldpass1",
time: 1553394449,
},
{
value: "oldpass2",
time: 1553394457,
},
{
value: "oldpass3",
time: 1553394458,
},
{
value: "oldpass4",
time: 1553394459,
},
{
value: "oldpass5",
time: 1553394460,
},
{
value: "oldpass6",
time: 1553394461,
},
],
fields: [
{
type: "T",
id: "username",
name: "username",
value: "googluser",
designation: "username",
},
{
type: "P",
id: "password",
name: "password",
value: "12345678901",
designation: "password",
},
],
notesPlain: "This is a note\r\n\r\nline1\r\nline2",
sections: [
{
title: "test",
name: "1214FD88CD30405D9EED14BEB4D61B60",
fields: [
{
type: 'T',
id: 'username',
name: 'username',
value: 'googluser',
designation: 'username',
},
{
type: 'P',
id: 'password',
name: 'password',
value: '12345678901',
designation: 'password',
},
{
k: "string",
n: "6CC3BD77482D4559A4B8BB2D360F821B",
v: "fgfg",
t: "fgggf",
},
{
k: "concealed",
n: "5CFE7BCAA1DF4578BBF7EB508959BFF3",
v: "dfgdfgfdg",
t: "pwfield",
},
],
notesPlain: 'This is a note\r\n\r\nline1\r\nline2',
sections: [
{
title: 'test',
name: '1214FD88CD30405D9EED14BEB4D61B60',
fields: [
{
k: 'string',
n: '6CC3BD77482D4559A4B8BB2D360F821B',
v: 'fgfg',
t: 'fgggf',
},
{
k: 'concealed',
n: '5CFE7BCAA1DF4578BBF7EB508959BFF3',
v: 'dfgdfgfdg',
t: 'pwfield',
},
],
},
],
},
},
],
},
});
const IdentityTestData = JSON.stringify({
uuid: 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA',
updatedAt: 1553365894,
securityLevel: 'SL5',
contentsHash: 'eeeeeeee',
title: 'Test Identity',
secureContents: {
lastname: 'Fritzenberger',
zip: '223344',
birthdate_dd: '11',
homephone: '+49 333 222 111',
company: 'Web Inc.',
firstname: 'Frank',
birthdate_mm: '3',
country: 'de',
sex: 'male',
sections: [
{
fields: [
{
k: 'string',
inputTraits: {
autocapitalization: 'Words',
},
n: 'firstname',
v: 'Frank',
a: {
guarded: 'yes',
},
t: 'first name',
},
{
k: 'string',
inputTraits: {
autocapitalization: 'Words',
},
n: 'initial',
v: 'MD',
a: {
guarded: 'yes',
},
t: 'initial',
},
{
k: 'string',
inputTraits: {
autocapitalization: 'Words',
},
n: 'lastname',
v: 'Fritzenberger',
a: {
guarded: 'yes',
},
t: 'last name',
},
{
k: 'menu',
v: 'male',
n: 'sex',
a: {
guarded: 'yes',
},
t: 'sex',
},
{
k: 'date',
v: 1552305660,
n: 'birthdate',
a: {
guarded: 'yes',
},
t: 'birth date',
},
{
k: 'string',
inputTraits: {
autocapitalization: 'Words',
},
n: 'occupation',
v: 'Engineer',
a: {
guarded: 'yes',
},
t: 'occupation',
},
{
k: 'string',
inputTraits: {
autocapitalization: 'Words',
},
n: 'company',
v: 'Web Inc.',
a: {
guarded: 'yes',
},
t: 'company',
},
{
k: 'string',
inputTraits: {
autocapitalization: 'Words',
},
n: 'department',
v: 'IT',
a: {
guarded: 'yes',
},
t: 'department',
},
{
k: 'string',
inputTraits: {
autocapitalization: 'Words',
},
n: 'jobtitle',
v: 'Developer',
a: {
guarded: 'yes',
},
t: 'job title',
},
],
title: 'Identification',
name: 'name',
uuid: "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
updatedAt: 1553365894,
securityLevel: "SL5",
contentsHash: "eeeeeeee",
title: "Test Identity",
secureContents: {
lastname: "Fritzenberger",
zip: "223344",
birthdate_dd: "11",
homephone: "+49 333 222 111",
company: "Web Inc.",
firstname: "Frank",
birthdate_mm: "3",
country: "de",
sex: "male",
sections: [
{
fields: [
{
k: "string",
inputTraits: {
autocapitalization: "Words",
},
{
fields: [
{
k: 'address',
inputTraits: {
autocapitalization: 'Sentences',
},
n: 'address',
v: {
street: 'Mainstreet 1',
city: 'Berlin',
country: 'de',
zip: '223344',
},
a: {
guarded: 'yes',
},
t: 'address',
},
{
k: 'phone',
v: '+49 001 222 333 44',
n: 'defphone',
a: {
guarded: 'yes',
},
t: 'default phone',
},
{
k: 'phone',
v: '+49 333 222 111',
n: 'homephone',
a: {
guarded: 'yes',
},
t: 'home',
},
{
k: 'phone',
n: 'cellphone',
a: {
guarded: 'yes',
},
t: 'mobile',
},
{
k: 'phone',
n: 'busphone',
a: {
guarded: 'yes',
},
t: 'business',
},
],
title: 'Address',
name: 'address',
n: "firstname",
v: "Frank",
a: {
guarded: "yes",
},
{
fields: [
{
k: 'string',
n: 'username',
a: {
guarded: 'yes',
},
t: 'username',
},
{
k: 'string',
n: 'reminderq',
t: 'reminder question',
},
{
k: 'string',
n: 'remindera',
t: 'reminder answer',
},
{
k: 'string',
inputTraits: {
keyboard: 'EmailAddress',
},
n: 'email',
v: 'test@web.de',
a: {
guarded: 'yes',
},
t: 'email',
},
{
k: 'string',
n: 'website',
inputTraits: {
keyboard: 'URL',
},
t: 'website',
},
{
k: 'string',
n: 'icq',
t: 'ICQ',
},
{
k: 'string',
n: 'skype',
t: 'skype',
},
{
k: 'string',
n: 'aim',
t: 'AOL/AIM',
},
{
k: 'string',
n: 'yahoo',
t: 'Yahoo',
},
{
k: 'string',
n: 'msn',
t: 'MSN',
},
{
k: 'string',
n: 'forumsig',
t: 'forum signature',
},
],
title: 'Internet Details',
name: 'internet',
t: "first name",
},
{
k: "string",
inputTraits: {
autocapitalization: "Words",
},
{
title: 'Related Items',
name: 'linked items',
n: "initial",
v: "MD",
a: {
guarded: "yes",
},
t: "initial",
},
{
k: "string",
inputTraits: {
autocapitalization: "Words",
},
n: "lastname",
v: "Fritzenberger",
a: {
guarded: "yes",
},
t: "last name",
},
{
k: "menu",
v: "male",
n: "sex",
a: {
guarded: "yes",
},
t: "sex",
},
{
k: "date",
v: 1552305660,
n: "birthdate",
a: {
guarded: "yes",
},
t: "birth date",
},
{
k: "string",
inputTraits: {
autocapitalization: "Words",
},
n: "occupation",
v: "Engineer",
a: {
guarded: "yes",
},
t: "occupation",
},
{
k: "string",
inputTraits: {
autocapitalization: "Words",
},
n: "company",
v: "Web Inc.",
a: {
guarded: "yes",
},
t: "company",
},
{
k: "string",
inputTraits: {
autocapitalization: "Words",
},
n: "department",
v: "IT",
a: {
guarded: "yes",
},
t: "department",
},
{
k: "string",
inputTraits: {
autocapitalization: "Words",
},
n: "jobtitle",
v: "Developer",
a: {
guarded: "yes",
},
t: "job title",
},
],
initial: 'MD',
address1: 'Mainstreet 1',
city: 'Berlin',
jobtitle: 'Developer',
occupation: 'Engineer',
department: 'IT',
email: 'test@web.de',
birthdate_yy: '2019',
homephone_local: '+49 333 222 111',
defphone_local: '+49 001 222 333 44',
defphone: '+49 001 222 333 44',
},
txTimestamp: 1553365894,
createdAt: 1553364679,
typeName: 'identities.Identity',
title: "Identification",
name: "name",
},
{
fields: [
{
k: "address",
inputTraits: {
autocapitalization: "Sentences",
},
n: "address",
v: {
street: "Mainstreet 1",
city: "Berlin",
country: "de",
zip: "223344",
},
a: {
guarded: "yes",
},
t: "address",
},
{
k: "phone",
v: "+49 001 222 333 44",
n: "defphone",
a: {
guarded: "yes",
},
t: "default phone",
},
{
k: "phone",
v: "+49 333 222 111",
n: "homephone",
a: {
guarded: "yes",
},
t: "home",
},
{
k: "phone",
n: "cellphone",
a: {
guarded: "yes",
},
t: "mobile",
},
{
k: "phone",
n: "busphone",
a: {
guarded: "yes",
},
t: "business",
},
],
title: "Address",
name: "address",
},
{
fields: [
{
k: "string",
n: "username",
a: {
guarded: "yes",
},
t: "username",
},
{
k: "string",
n: "reminderq",
t: "reminder question",
},
{
k: "string",
n: "remindera",
t: "reminder answer",
},
{
k: "string",
inputTraits: {
keyboard: "EmailAddress",
},
n: "email",
v: "test@web.de",
a: {
guarded: "yes",
},
t: "email",
},
{
k: "string",
n: "website",
inputTraits: {
keyboard: "URL",
},
t: "website",
},
{
k: "string",
n: "icq",
t: "ICQ",
},
{
k: "string",
n: "skype",
t: "skype",
},
{
k: "string",
n: "aim",
t: "AOL/AIM",
},
{
k: "string",
n: "yahoo",
t: "Yahoo",
},
{
k: "string",
n: "msn",
t: "MSN",
},
{
k: "string",
n: "forumsig",
t: "forum signature",
},
],
title: "Internet Details",
name: "internet",
},
{
title: "Related Items",
name: "linked items",
},
],
initial: "MD",
address1: "Mainstreet 1",
city: "Berlin",
jobtitle: "Developer",
occupation: "Engineer",
department: "IT",
email: "test@web.de",
birthdate_yy: "2019",
homephone_local: "+49 333 222 111",
defphone_local: "+49 001 222 333 44",
defphone: "+49 001 222 333 44",
},
txTimestamp: 1553365894,
createdAt: 1553364679,
typeName: "identities.Identity",
});
describe('1Password 1Pif Importer', () => {
it('should parse data', async () => {
const importer = new Importer();
const result = await importer.parse(TestData);
expect(result != null).toBe(true);
describe("1Password 1Pif Importer", () => {
it("should parse data", async () => {
const importer = new Importer();
const result = await importer.parse(TestData);
expect(result != null).toBe(true);
const cipher = result.ciphers.shift();
expect(cipher.login.username).toEqual('user@test.net');
expect(cipher.login.password).toEqual('myservicepassword');
expect(cipher.login.uris.length).toEqual(1);
const uriView = cipher.login.uris.shift();
expect(uriView.uri).toEqual('https://www.google.com');
});
const cipher = result.ciphers.shift();
expect(cipher.login.username).toEqual("user@test.net");
expect(cipher.login.password).toEqual("myservicepassword");
expect(cipher.login.uris.length).toEqual(1);
const uriView = cipher.login.uris.shift();
expect(uriView.uri).toEqual("https://www.google.com");
});
it('should create concealed field as "hidden" type', async () => {
const importer = new Importer();
const result = await importer.parse(TestData);
expect(result != null).toBe(true);
it('should create concealed field as "hidden" type', async () => {
const importer = new Importer();
const result = await importer.parse(TestData);
expect(result != null).toBe(true);
const ciphers = result.ciphers;
expect(ciphers.length).toEqual(1);
const ciphers = result.ciphers;
expect(ciphers.length).toEqual(1);
const cipher = ciphers.shift();
const fields = cipher.fields;
expect(fields.length).toEqual(1);
const cipher = ciphers.shift();
const fields = cipher.fields;
expect(fields.length).toEqual(1);
const field = fields.shift();
expect(field.name).toEqual('console password');
expect(field.value).toEqual('console-password-123');
expect(field.type).toEqual(FieldType.Hidden);
});
const field = fields.shift();
expect(field.name).toEqual("console password");
expect(field.value).toEqual("console-password-123");
expect(field.type).toEqual(FieldType.Hidden);
});
it('should create identity records', async () => {
const importer = new Importer();
const result = await importer.parse(IdentityTestData);
expect(result != null).toBe(true);
const cipher = result.ciphers.shift();
expect(cipher.name).toEqual('Test Identity');
it("should create identity records", async () => {
const importer = new Importer();
const result = await importer.parse(IdentityTestData);
expect(result != null).toBe(true);
const cipher = result.ciphers.shift();
expect(cipher.name).toEqual("Test Identity");
const identity = cipher.identity;
expect(identity.firstName).toEqual('Frank');
expect(identity.middleName).toEqual('MD');
expect(identity.lastName).toEqual('Fritzenberger');
expect(identity.company).toEqual('Web Inc.');
expect(identity.address1).toEqual('Mainstreet 1');
expect(identity.country).toEqual('DE');
expect(identity.city).toEqual('Berlin');
expect(identity.postalCode).toEqual('223344');
expect(identity.phone).toEqual('+49 001 222 333 44');
expect(identity.email).toEqual('test@web.de');
const identity = cipher.identity;
expect(identity.firstName).toEqual("Frank");
expect(identity.middleName).toEqual("MD");
expect(identity.lastName).toEqual("Fritzenberger");
expect(identity.company).toEqual("Web Inc.");
expect(identity.address1).toEqual("Mainstreet 1");
expect(identity.country).toEqual("DE");
expect(identity.city).toEqual("Berlin");
expect(identity.postalCode).toEqual("223344");
expect(identity.phone).toEqual("+49 001 222 333 44");
expect(identity.email).toEqual("test@web.de");
// remaining fields as custom fields
expect(cipher.fields.length).toEqual(6);
});
// remaining fields as custom fields
expect(cipher.fields.length).toEqual(6);
});
it('should create password history', async () => {
const importer = new Importer();
const result = await importer.parse(TestData);
const cipher = result.ciphers.shift();
it("should create password history", async () => {
const importer = new Importer();
const result = await importer.parse(TestData);
const cipher = result.ciphers.shift();
expect(cipher.passwordHistory.length).toEqual(1);
const ph = cipher.passwordHistory.shift();
expect(ph.password).toEqual('old-password');
expect(ph.lastUsedDate.toISOString()).toEqual('2015-11-17T20:17:01.000Z');
});
expect(cipher.passwordHistory.length).toEqual(1);
const ph = cipher.passwordHistory.shift();
expect(ph.password).toEqual("old-password");
expect(ph.lastUsedDate.toISOString()).toEqual("2015-11-17T20:17:01.000Z");
});
it('should create password history from windows opvault 1pif format', async () => {
const importer = new Importer();
const result = await importer.parse(WindowsOpVaultTestData);
const cipher = result.ciphers.shift();
it("should create password history from windows opvault 1pif format", async () => {
const importer = new Importer();
const result = await importer.parse(WindowsOpVaultTestData);
const cipher = result.ciphers.shift();
expect(cipher.passwordHistory.length).toEqual(5);
let ph = cipher.passwordHistory.shift();
expect(ph.password).toEqual('oldpass6');
expect(ph.lastUsedDate.toISOString()).toEqual('2019-03-24T02:27:41.000Z');
ph = cipher.passwordHistory.shift();
expect(ph.password).toEqual('oldpass5');
expect(ph.lastUsedDate.toISOString()).toEqual('2019-03-24T02:27:40.000Z');
ph = cipher.passwordHistory.shift();
expect(ph.password).toEqual('oldpass4');
expect(ph.lastUsedDate.toISOString()).toEqual('2019-03-24T02:27:39.000Z');
ph = cipher.passwordHistory.shift();
expect(ph.password).toEqual('oldpass3');
expect(ph.lastUsedDate.toISOString()).toEqual('2019-03-24T02:27:38.000Z');
ph = cipher.passwordHistory.shift();
expect(ph.password).toEqual('oldpass2');
expect(ph.lastUsedDate.toISOString()).toEqual('2019-03-24T02:27:37.000Z');
});
expect(cipher.passwordHistory.length).toEqual(5);
let ph = cipher.passwordHistory.shift();
expect(ph.password).toEqual("oldpass6");
expect(ph.lastUsedDate.toISOString()).toEqual("2019-03-24T02:27:41.000Z");
ph = cipher.passwordHistory.shift();
expect(ph.password).toEqual("oldpass5");
expect(ph.lastUsedDate.toISOString()).toEqual("2019-03-24T02:27:40.000Z");
ph = cipher.passwordHistory.shift();
expect(ph.password).toEqual("oldpass4");
expect(ph.lastUsedDate.toISOString()).toEqual("2019-03-24T02:27:39.000Z");
ph = cipher.passwordHistory.shift();
expect(ph.password).toEqual("oldpass3");
expect(ph.lastUsedDate.toISOString()).toEqual("2019-03-24T02:27:38.000Z");
ph = cipher.passwordHistory.shift();
expect(ph.password).toEqual("oldpass2");
expect(ph.lastUsedDate.toISOString()).toEqual("2019-03-24T02:27:37.000Z");
});
});

View File

@@ -1,71 +1,75 @@
import { OnePasswordMacCsvImporter as Importer } from 'jslib-common/importers/onepasswordImporters/onepasswordMacCsvImporter';
import { OnePasswordMacCsvImporter as Importer } from "jslib-common/importers/onepasswordImporters/onepasswordMacCsvImporter";
import { CipherType } from 'jslib-common/enums/cipherType';
import { CipherView } from 'jslib-common/models/view/cipherView';
import { CipherType } from "jslib-common/enums/cipherType";
import { CipherView } from "jslib-common/models/view/cipherView";
import { data as creditCardData } from './testData/onePasswordCsv/creditCard.mac.csv';
import { data as identityData } from './testData/onePasswordCsv/identity.mac.csv';
import { data as multiTypeData } from './testData/onePasswordCsv/multipleItems.mac.csv';
import { data as creditCardData } from "./testData/onePasswordCsv/creditCard.mac.csv";
import { data as identityData } from "./testData/onePasswordCsv/identity.mac.csv";
import { data as multiTypeData } from "./testData/onePasswordCsv/multipleItems.mac.csv";
function expectIdentity(cipher: CipherView) {
expect(cipher.type).toBe(CipherType.Identity);
expect(cipher.type).toBe(CipherType.Identity);
expect(cipher.identity).toEqual(jasmine.objectContaining({
firstName: 'first name',
middleName: 'mi',
lastName: 'last name',
username: 'userNam3',
company: 'bitwarden',
phone: '8005555555',
email: 'email@bitwarden.com',
}));
expect(cipher.identity).toEqual(
jasmine.objectContaining({
firstName: "first name",
middleName: "mi",
lastName: "last name",
username: "userNam3",
company: "bitwarden",
phone: "8005555555",
email: "email@bitwarden.com",
})
);
expect(cipher.notes).toContain('address\ncity state zip\nUnited States');
expect(cipher.notes).toContain("address\ncity state zip\nUnited States");
}
function expectCreditCard(cipher: CipherView) {
expect(cipher.type).toBe(CipherType.Card);
expect(cipher.type).toBe(CipherType.Card);
expect(cipher.card).toEqual(jasmine.objectContaining({
number: '4111111111111111',
code: '111',
cardholderName: 'test',
expMonth: '1',
expYear: '2030',
}));
expect(cipher.card).toEqual(
jasmine.objectContaining({
number: "4111111111111111",
code: "111",
cardholderName: "test",
expMonth: "1",
expYear: "2030",
})
);
}
describe('1Password mac CSV Importer', () => {
it('should parse identity records', async () => {
const importer = new Importer();
const result = await importer.parse(identityData);
describe("1Password mac CSV Importer", () => {
it("should parse identity records", async () => {
const importer = new Importer();
const result = await importer.parse(identityData);
expect(result).not.toBeNull();
expect(result.success).toBe(true);
expect(result.ciphers.length).toBe(1);
const cipher = result.ciphers[0];
expectIdentity(cipher);
});
expect(result).not.toBeNull();
expect(result.success).toBe(true);
expect(result.ciphers.length).toBe(1);
const cipher = result.ciphers[0];
expectIdentity(cipher);
});
it('should parse credit card records', async () => {
const importer = new Importer();
const result = await importer.parse(creditCardData);
it("should parse credit card records", async () => {
const importer = new Importer();
const result = await importer.parse(creditCardData);
expect(result).not.toBeNull();
expect(result.success).toBe(true);
expect(result.ciphers.length).toBe(1);
const cipher = result.ciphers[0];
expectCreditCard(cipher);
});
expect(result).not.toBeNull();
expect(result.success).toBe(true);
expect(result.ciphers.length).toBe(1);
const cipher = result.ciphers[0];
expectCreditCard(cipher);
});
it('should parse csv\'s with multiple record type', async () => {
const importer = new Importer();
const result = await importer.parse(multiTypeData);
it("should parse csv's with multiple record type", async () => {
const importer = new Importer();
const result = await importer.parse(multiTypeData);
expect(result).not.toBeNull();
expect(result.success).toBe(true);
expect(result.ciphers.length).toBe(4);
expectIdentity(result.ciphers[1]);
expectCreditCard(result.ciphers[2]);
});
expect(result).not.toBeNull();
expect(result.success).toBe(true);
expect(result.ciphers.length).toBe(4);
expectIdentity(result.ciphers[1]);
expectCreditCard(result.ciphers[2]);
});
});

View File

@@ -1,82 +1,88 @@
import { OnePasswordWinCsvImporter as Importer } from 'jslib-common/importers/onepasswordImporters/onepasswordWinCsvImporter';
import { OnePasswordWinCsvImporter as Importer } from "jslib-common/importers/onepasswordImporters/onepasswordWinCsvImporter";
import { CipherType } from 'jslib-common/enums/cipherType';
import { FieldType } from 'jslib-common/enums/fieldType';
import { CipherView } from 'jslib-common/models/view/cipherView';
import { FieldView } from 'jslib-common/models/view/fieldView';
import { CipherType } from "jslib-common/enums/cipherType";
import { FieldType } from "jslib-common/enums/fieldType";
import { CipherView } from "jslib-common/models/view/cipherView";
import { FieldView } from "jslib-common/models/view/fieldView";
import { data as creditCardData } from './testData/onePasswordCsv/creditCard.windows.csv';
import { data as identityData } from './testData/onePasswordCsv/identity.windows.csv';
import { data as multiTypeData } from './testData/onePasswordCsv/multipleItems.windows.csv';
import { data as creditCardData } from "./testData/onePasswordCsv/creditCard.windows.csv";
import { data as identityData } from "./testData/onePasswordCsv/identity.windows.csv";
import { data as multiTypeData } from "./testData/onePasswordCsv/multipleItems.windows.csv";
function expectIdentity(cipher: CipherView) {
expect(cipher.type).toBe(CipherType.Identity);
expect(cipher.type).toBe(CipherType.Identity);
expect(cipher.identity).toEqual(jasmine.objectContaining({
firstName: 'first name',
middleName: 'mi',
lastName: 'last name',
username: 'userNam3',
company: 'bitwarden',
phone: '8005555555',
email: 'email@bitwarden.com',
}));
expect(cipher.identity).toEqual(
jasmine.objectContaining({
firstName: "first name",
middleName: "mi",
lastName: "last name",
username: "userNam3",
company: "bitwarden",
phone: "8005555555",
email: "email@bitwarden.com",
})
);
expect(cipher.fields).toEqual(jasmine.arrayContaining([
Object.assign(new FieldView(), {
type: FieldType.Text,
name: 'address',
value: 'address city state zip us',
}),
]));
expect(cipher.fields).toEqual(
jasmine.arrayContaining([
Object.assign(new FieldView(), {
type: FieldType.Text,
name: "address",
value: "address city state zip us",
}),
])
);
}
function expectCreditCard(cipher: CipherView) {
expect(cipher.type).toBe(CipherType.Card);
expect(cipher.type).toBe(CipherType.Card);
expect(cipher.card).toEqual(jasmine.objectContaining({
number: '4111111111111111',
code: '111',
cardholderName: 'test',
expMonth: '1',
expYear: '1970',
}));
expect(cipher.card).toEqual(
jasmine.objectContaining({
number: "4111111111111111",
code: "111",
cardholderName: "test",
expMonth: "1",
expYear: "1970",
})
);
}
describe('1Password windows CSV Importer', () => {
let importer: Importer;
beforeEach(() => {
importer = new Importer();
});
describe("1Password windows CSV Importer", () => {
let importer: Importer;
beforeEach(() => {
importer = new Importer();
});
it('should parse identity records', async () => {
const result = await importer.parse(identityData);
it("should parse identity records", async () => {
const result = await importer.parse(identityData);
expect(result).not.toBeNull();
expect(result.success).toBe(true);
expect(result.ciphers.length).toBe(1);
const cipher = result.ciphers[0];
expectIdentity(cipher);
});
expect(result).not.toBeNull();
expect(result.success).toBe(true);
expect(result.ciphers.length).toBe(1);
const cipher = result.ciphers[0];
expectIdentity(cipher);
});
it('should parse credit card records', async () => {
const result = await importer.parse(creditCardData);
it("should parse credit card records", async () => {
const result = await importer.parse(creditCardData);
expect(result).not.toBeNull();
expect(result.success).toBe(true);
expect(result.ciphers.length).toBe(1);
const cipher = result.ciphers[0];
expectCreditCard(cipher);
});
expect(result).not.toBeNull();
expect(result.success).toBe(true);
expect(result.ciphers.length).toBe(1);
const cipher = result.ciphers[0];
expectCreditCard(cipher);
});
it('should parse csv\'s with multiple record types', async () => {
const result = await importer.parse(multiTypeData);
it("should parse csv's with multiple record types", async () => {
const result = await importer.parse(multiTypeData);
expect(result).not.toBeNull();
expect(result.success).toBe(true);
expect(result.ciphers.length).toBe(4);
expect(result).not.toBeNull();
expect(result.success).toBe(true);
expect(result.ciphers.length).toBe(4);
expectIdentity(result.ciphers[1]);
expectCreditCard(result.ciphers[2]);
});
expectIdentity(result.ciphers[1]);
expectCreditCard(result.ciphers[2]);
});
});

View File

@@ -1,3 +1,3 @@
export const data = `name,url,username,password,note,cardholdername,cardnumber,cvc,expirydate,zipcode,folder,full_name,phone_number,email,address1,address2,city,country,state
notesFolder,,,,,,,,,,,,,,,,,,
MySuperSecureNoteTitle,,,,MySuperSecureNote,,,,,,notesFolder,,,,,,,,`;
MySuperSecureNoteTitle,,,,MySuperSecureNote,,,,,,notesFolder,,,,,,,,`;

View File

@@ -1,2 +1,2 @@
export const data = `"UUID","TITLE","SCOPE","AUTOSUBMIT","1: CARDHOLDER NAME","2: NUMBER","3: VERIFICATION NUMBER","4: EXPIRY DATE","SECTION 2: SECTION_PZET7LEKRQXZUINIEGH5ABA2UY","SECTION_PZET7LEKRQXZUINIEGH5ABA2UY 1: LABEL"
"sd26pt226etnsijbl3kqzi5bmm","test card","Default","Default","test","4111111111111111","111","1/3/1970 12:23 AM","section","field (phone)"`;
export const data = `"UUID","TITLE","SCOPE","AUTOSUBMIT","1: CARDHOLDER NAME","2: NUMBER","3: VERIFICATION NUMBER","4: EXPIRY DATE","SECTION 2: SECTION_PZET7LEKRQXZUINIEGH5ABA2UY","SECTION_PZET7LEKRQXZUINIEGH5ABA2UY 1: LABEL"
"sd26pt226etnsijbl3kqzi5bmm","test card","Default","Default","test","4111111111111111","111","1/3/1970 12:23 AM","section","field (phone)"`;

View File

@@ -1,2 +1,2 @@
export const data = `"UUID","TITLE","SCOPE","AUTOSUBMIT","TAGS","NOTES","SECTION 1: NAME","NAME 1: FIRST NAME","NAME 2: INITIAL","NAME 3: LAST NAME","NAME 4: BIRTH DATE","NAME 5: OCCUPATION","NAME 6: COMPANY","NAME 7: DEPARTMENT","NAME 8: JOB TITLE","SECTION 2: ADDRESS","ADDRESS 1: ADDRESS","ADDRESS 2: DEFAULT PHONE","SECTION 3: INTERNET","INTERNET 1: USERNAME","INTERNET 2: EMAIL","SECTION 4: MFJQKMWEOYDZDFH4YMR7WLJKIY","MFJQKMWEOYDZDFH4YMR7WLJKIY 1: SECTION FIELD","MFJQKMWEOYDZDFH4YMR7WLJKIY 2: SECTION FIELD"
"6v56y5z4tejwg37jsettta7d7m","Identity Item","Default","Default","Starter Kit","Its you! 🖐 Select Edit to fill in more details, like your address and contact information.","Identification","first name","mi","last name","12/2/2020 4:01 AM","occupation","bitwarden","department","job title","Address","address city state zip us","8005555555","Internet Details","userNam3","email@bitwarden.com","💡 Did you know?","1Password can fill names and addresses into webpages:","https://support.1password.com/credit-card-address-filling/"`;
export const data = `"UUID","TITLE","SCOPE","AUTOSUBMIT","TAGS","NOTES","SECTION 1: NAME","NAME 1: FIRST NAME","NAME 2: INITIAL","NAME 3: LAST NAME","NAME 4: BIRTH DATE","NAME 5: OCCUPATION","NAME 6: COMPANY","NAME 7: DEPARTMENT","NAME 8: JOB TITLE","SECTION 2: ADDRESS","ADDRESS 1: ADDRESS","ADDRESS 2: DEFAULT PHONE","SECTION 3: INTERNET","INTERNET 1: USERNAME","INTERNET 2: EMAIL","SECTION 4: MFJQKMWEOYDZDFH4YMR7WLJKIY","MFJQKMWEOYDZDFH4YMR7WLJKIY 1: SECTION FIELD","MFJQKMWEOYDZDFH4YMR7WLJKIY 2: SECTION FIELD"
"6v56y5z4tejwg37jsettta7d7m","Identity Item","Default","Default","Starter Kit","Its you! 🖐 Select Edit to fill in more details, like your address and contact information.","Identification","first name","mi","last name","12/2/2020 4:01 AM","occupation","bitwarden","department","job title","Address","address city state zip us","8005555555","Internet Details","userNam3","email@bitwarden.com","💡 Did you know?","1Password can fill names and addresses into webpages:","https://support.1password.com/credit-card-address-filling/"`;

View File

@@ -1,5 +1,5 @@
export const data = `"UUID","TITLE","USERNAME","PASSWORD","URL","URLS","EMAIL","MASTER-PASSWORD","ACCOUNT-KEY","SCOPE","AUTOSUBMIT","TAGS","NOTES","SECTION 1: WXHDKEQREE3TH6QRFCPFPSD3AE","WXHDKEQREE3TH6QRFCPFPSD3AE 1: SECRET KEY","SECTION 1: NAME","NAME 1: FIRST NAME","NAME 2: INITIAL","NAME 3: LAST NAME","NAME 4: BIRTH DATE","NAME 5: OCCUPATION","NAME 6: COMPANY","NAME 7: DEPARTMENT","NAME 8: JOB TITLE","SECTION 2: ADDRESS","ADDRESS 1: ADDRESS","ADDRESS 2: DEFAULT PHONE","SECTION 3: INTERNET","INTERNET 1: USERNAME","INTERNET 2: EMAIL","SECTION 4: MFJQKMWEOYDZDFH4YMR7WLJKIY","MFJQKMWEOYDZDFH4YMR7WLJKIY 1: SECTION FIELD","MFJQKMWEOYDZDFH4YMR7WLJKIY 2: SECTION FIELD","1: CARDHOLDER NAME","2: NUMBER","3: VERIFICATION NUMBER","4: EXPIRY DATE","SECTION 2: SECTION_PZET7LEKRQXZUINIEGH5ABA2UY","SECTION_PZET7LEKRQXZUINIEGH5ABA2UY 1: LABEL","SECTION 1: 4PQVXPR4BMOPGC3DBMTP5U4OFY","4PQVXPR4BMOPGC3DBMTP5U4OFY 1: SECTION FIELD","4PQVXPR4BMOPGC3DBMTP5U4OFY 2: SECTION FIELD","SECTION 2: M2NTUZZBFOFTPAYXVXE6EMZ5JU","M2NTUZZBFOFTPAYXVXE6EMZ5JU 1: SECTION FIELD","M2NTUZZBFOFTPAYXVXE6EMZ5JU 2: SECTION FIELD","SECTION 3: WC3KPAWH6ZAEQB2ARJB6WYZ3DQ","WC3KPAWH6ZAEQB2ARJB6WYZ3DQ 1: SECTION FIELD","WC3KPAWH6ZAEQB2ARJB6WYZ3DQ 2: SECTION FIELD","WC3KPAWH6ZAEQB2ARJB6WYZ3DQ 3: SECTION FIELD","SECTION 4: TOHUYJEJEMGMI6GEQAZ2LJODFE","TOHUYJEJEMGMI6GEQAZ2LJODFE 1: SECTION FIELD","TOHUYJEJEMGMI6GEQAZ2LJODFE 2: SECTION FIELD","SECTION 5: O26UWJJTXRAANG3ONYYOUUJHDM","O26UWJJTXRAANG3ONYYOUUJHDM 1: SECTION FIELD","O26UWJJTXRAANG3ONYYOUUJHDM 2: WATCH VIDEOS","O26UWJJTXRAANG3ONYYOUUJHDM 3: GET SUPPORT","O26UWJJTXRAANG3ONYYOUUJHDM 4: READ THE BLOG","O26UWJJTXRAANG3ONYYOUUJHDM 5: CONTACT US"
"xjq32axcswefpcxu2mtxxqnufa","1Password Account","email@bitwarden.com","the account's password","https://my.1password.com","https://my.1password.com","email@bitwarden.com","the account's password","A3-76TR2N-NJG3TZ-9NXFX-WT8GF-6YQC9-R2659","Default","Default","Starter Kit","You can use this login to sign in to your account on 1password.com.","🔑 Secret Key","the account's secret key","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""
"6v56y5z4tejwg37jsettta7d7m","Identity Item","","","","","","","","Default","Default","Starter Kit","Its you! 🖐 Select Edit to fill in more details, like your address and contact information.","","","Identification","first name","mi","last name","12/2/2020 4:01 AM","occupation","bitwarden","department","job title","Address","address city state zip us","8005555555","Internet Details","userNam3","email@bitwarden.com","💡 Did you know?","1Password can fill names and addresses into webpages:","https://support.1password.com/credit-card-address-filling/","","","","","","","","","","","","","","","","","","","","","","","","",""
"sd26pt226etnsijbl3kqzi5bmm","test card","","","","","","","","Default","Default","","","","","","","","","","","","","","","","","","","","","","","test","4111111111111111","111","1/3/1970 12:23 AM","section","field (phone)","","","","","","","","","","","","","","","","","","",""
"oml2sgit3yk7737kxdis65o4xq","🎉 Welcome to 1Password!","","","","","","","","Default","Default","Starter Kit","Follow these steps to get started.","","","","","","","","","","","","","","","","","","","","","","","","","","","1⃣ Get the apps","https://1password.com/downloads","Install 1Password everywhere you need your passwords.","2⃣ Get 1Password in your browser","https://1password.com/downloads/#browsers","Install 1Password in your browser to save and fill passwords.","3⃣ Save your first password","1. Sign in to your favorite website.","2. 1Password will ask to save your username and password.","3. Click Save Login.","4⃣ Fill passwords and more","https://support.1password.com/explore/extension/","Save and fill passwords, credit cards, and addresses.","📚 Learn 1Password","Check out our videos and articles:","https://youtube.com/1PasswordVideos","https://support.1password.com/","https://blog.1password.com/","https://support.1password.com/contact-us/"`;
export const data = `"UUID","TITLE","USERNAME","PASSWORD","URL","URLS","EMAIL","MASTER-PASSWORD","ACCOUNT-KEY","SCOPE","AUTOSUBMIT","TAGS","NOTES","SECTION 1: WXHDKEQREE3TH6QRFCPFPSD3AE","WXHDKEQREE3TH6QRFCPFPSD3AE 1: SECRET KEY","SECTION 1: NAME","NAME 1: FIRST NAME","NAME 2: INITIAL","NAME 3: LAST NAME","NAME 4: BIRTH DATE","NAME 5: OCCUPATION","NAME 6: COMPANY","NAME 7: DEPARTMENT","NAME 8: JOB TITLE","SECTION 2: ADDRESS","ADDRESS 1: ADDRESS","ADDRESS 2: DEFAULT PHONE","SECTION 3: INTERNET","INTERNET 1: USERNAME","INTERNET 2: EMAIL","SECTION 4: MFJQKMWEOYDZDFH4YMR7WLJKIY","MFJQKMWEOYDZDFH4YMR7WLJKIY 1: SECTION FIELD","MFJQKMWEOYDZDFH4YMR7WLJKIY 2: SECTION FIELD","1: CARDHOLDER NAME","2: NUMBER","3: VERIFICATION NUMBER","4: EXPIRY DATE","SECTION 2: SECTION_PZET7LEKRQXZUINIEGH5ABA2UY","SECTION_PZET7LEKRQXZUINIEGH5ABA2UY 1: LABEL","SECTION 1: 4PQVXPR4BMOPGC3DBMTP5U4OFY","4PQVXPR4BMOPGC3DBMTP5U4OFY 1: SECTION FIELD","4PQVXPR4BMOPGC3DBMTP5U4OFY 2: SECTION FIELD","SECTION 2: M2NTUZZBFOFTPAYXVXE6EMZ5JU","M2NTUZZBFOFTPAYXVXE6EMZ5JU 1: SECTION FIELD","M2NTUZZBFOFTPAYXVXE6EMZ5JU 2: SECTION FIELD","SECTION 3: WC3KPAWH6ZAEQB2ARJB6WYZ3DQ","WC3KPAWH6ZAEQB2ARJB6WYZ3DQ 1: SECTION FIELD","WC3KPAWH6ZAEQB2ARJB6WYZ3DQ 2: SECTION FIELD","WC3KPAWH6ZAEQB2ARJB6WYZ3DQ 3: SECTION FIELD","SECTION 4: TOHUYJEJEMGMI6GEQAZ2LJODFE","TOHUYJEJEMGMI6GEQAZ2LJODFE 1: SECTION FIELD","TOHUYJEJEMGMI6GEQAZ2LJODFE 2: SECTION FIELD","SECTION 5: O26UWJJTXRAANG3ONYYOUUJHDM","O26UWJJTXRAANG3ONYYOUUJHDM 1: SECTION FIELD","O26UWJJTXRAANG3ONYYOUUJHDM 2: WATCH VIDEOS","O26UWJJTXRAANG3ONYYOUUJHDM 3: GET SUPPORT","O26UWJJTXRAANG3ONYYOUUJHDM 4: READ THE BLOG","O26UWJJTXRAANG3ONYYOUUJHDM 5: CONTACT US"
"xjq32axcswefpcxu2mtxxqnufa","1Password Account","email@bitwarden.com","the account's password","https://my.1password.com","https://my.1password.com","email@bitwarden.com","the account's password","A3-76TR2N-NJG3TZ-9NXFX-WT8GF-6YQC9-R2659","Default","Default","Starter Kit","You can use this login to sign in to your account on 1password.com.","🔑 Secret Key","the account's secret key","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""
"6v56y5z4tejwg37jsettta7d7m","Identity Item","","","","","","","","Default","Default","Starter Kit","Its you! 🖐 Select Edit to fill in more details, like your address and contact information.","","","Identification","first name","mi","last name","12/2/2020 4:01 AM","occupation","bitwarden","department","job title","Address","address city state zip us","8005555555","Internet Details","userNam3","email@bitwarden.com","💡 Did you know?","1Password can fill names and addresses into webpages:","https://support.1password.com/credit-card-address-filling/","","","","","","","","","","","","","","","","","","","","","","","","",""
"sd26pt226etnsijbl3kqzi5bmm","test card","","","","","","","","Default","Default","","","","","","","","","","","","","","","","","","","","","","","test","4111111111111111","111","1/3/1970 12:23 AM","section","field (phone)","","","","","","","","","","","","","","","","","","",""
"oml2sgit3yk7737kxdis65o4xq","🎉 Welcome to 1Password!","","","","","","","","Default","Default","Starter Kit","Follow these steps to get started.","","","","","","","","","","","","","","","","","","","","","","","","","","","1⃣ Get the apps","https://1password.com/downloads","Install 1Password everywhere you need your passwords.","2⃣ Get 1Password in your browser","https://1password.com/downloads/#browsers","Install 1Password in your browser to save and fill passwords.","3⃣ Save your first password","1. Sign in to your favorite website.","2. 1Password will ask to save your username and password.","3. Click Save Login.","4⃣ Fill passwords and more","https://support.1password.com/explore/extension/","Save and fill passwords, credit cards, and addresses.","📚 Learn 1Password","Check out our videos and articles:","https://youtube.com/1PasswordVideos","https://support.1password.com/","https://blog.1password.com/","https://support.1password.com/contact-us/"`;

View File

@@ -59,4 +59,4 @@ export const data = `
<label_id>3</label_id>
</card>
</database>
`;
`;

View File

@@ -1,141 +1,127 @@
import { sequentialize } from 'jslib-common/misc/sequentialize';
import { sequentialize } from "jslib-common/misc/sequentialize";
describe('sequentialize decorator', () => {
it('should call the function once', async () => {
const foo = new Foo();
const promises = [];
for (let i = 0; i < 10; i++) {
promises.push(foo.bar(1));
}
await Promise.all(promises);
describe("sequentialize decorator", () => {
it("should call the function once", async () => {
const foo = new Foo();
const promises = [];
for (let i = 0; i < 10; i++) {
promises.push(foo.bar(1));
}
await Promise.all(promises);
expect(foo.calls).toBe(1);
});
expect(foo.calls).toBe(1);
});
it('should call the function once for each instance of the object', async () => {
const foo = new Foo();
const foo2 = new Foo();
const promises = [];
for (let i = 0; i < 10; i++) {
promises.push(foo.bar(1));
promises.push(foo2.bar(1));
}
await Promise.all(promises);
it("should call the function once for each instance of the object", async () => {
const foo = new Foo();
const foo2 = new Foo();
const promises = [];
for (let i = 0; i < 10; i++) {
promises.push(foo.bar(1));
promises.push(foo2.bar(1));
}
await Promise.all(promises);
expect(foo.calls).toBe(1);
expect(foo2.calls).toBe(1);
});
expect(foo.calls).toBe(1);
expect(foo2.calls).toBe(1);
});
it('should call the function once with key function', async () => {
const foo = new Foo();
const promises = [];
for (let i = 0; i < 10; i++) {
promises.push(foo.baz(1));
}
await Promise.all(promises);
it("should call the function once with key function", async () => {
const foo = new Foo();
const promises = [];
for (let i = 0; i < 10; i++) {
promises.push(foo.baz(1));
}
await Promise.all(promises);
expect(foo.calls).toBe(1);
});
expect(foo.calls).toBe(1);
});
it('should call the function again when already resolved', async () => {
const foo = new Foo();
await foo.bar(1);
expect(foo.calls).toBe(1);
await foo.bar(1);
expect(foo.calls).toBe(2);
});
it("should call the function again when already resolved", async () => {
const foo = new Foo();
await foo.bar(1);
expect(foo.calls).toBe(1);
await foo.bar(1);
expect(foo.calls).toBe(2);
});
it('should call the function again when already resolved with a key function', async () => {
const foo = new Foo();
await foo.baz(1);
expect(foo.calls).toBe(1);
await foo.baz(1);
expect(foo.calls).toBe(2);
});
it("should call the function again when already resolved with a key function", async () => {
const foo = new Foo();
await foo.baz(1);
expect(foo.calls).toBe(1);
await foo.baz(1);
expect(foo.calls).toBe(2);
});
it('should call the function for each argument', async () => {
const foo = new Foo();
await Promise.all([
foo.bar(1),
foo.bar(1),
foo.bar(2),
foo.bar(2),
foo.bar(3),
foo.bar(3),
]);
expect(foo.calls).toBe(3);
});
it("should call the function for each argument", async () => {
const foo = new Foo();
await Promise.all([foo.bar(1), foo.bar(1), foo.bar(2), foo.bar(2), foo.bar(3), foo.bar(3)]);
expect(foo.calls).toBe(3);
});
it('should call the function for each argument with key function', async () => {
const foo = new Foo();
await Promise.all([
foo.baz(1),
foo.baz(1),
foo.baz(2),
foo.baz(2),
foo.baz(3),
foo.baz(3),
]);
expect(foo.calls).toBe(3);
});
it("should call the function for each argument with key function", async () => {
const foo = new Foo();
await Promise.all([foo.baz(1), foo.baz(1), foo.baz(2), foo.baz(2), foo.baz(3), foo.baz(3)]);
expect(foo.calls).toBe(3);
});
it('should return correct result for each call', async () => {
const foo = new Foo();
const allRes: number[] = [];
it("should return correct result for each call", async () => {
const foo = new Foo();
const allRes: number[] = [];
await Promise.all([
foo.bar(1).then(res => allRes.push(res)),
foo.bar(1).then(res => allRes.push(res)),
foo.bar(2).then(res => allRes.push(res)),
foo.bar(2).then(res => allRes.push(res)),
foo.bar(3).then(res => allRes.push(res)),
foo.bar(3).then(res => allRes.push(res)),
]);
expect(foo.calls).toBe(3);
expect(allRes.length).toBe(6);
allRes.sort();
expect(allRes).toEqual([2, 2, 4, 4, 6, 6]);
});
await Promise.all([
foo.bar(1).then((res) => allRes.push(res)),
foo.bar(1).then((res) => allRes.push(res)),
foo.bar(2).then((res) => allRes.push(res)),
foo.bar(2).then((res) => allRes.push(res)),
foo.bar(3).then((res) => allRes.push(res)),
foo.bar(3).then((res) => allRes.push(res)),
]);
expect(foo.calls).toBe(3);
expect(allRes.length).toBe(6);
allRes.sort();
expect(allRes).toEqual([2, 2, 4, 4, 6, 6]);
});
it('should return correct result for each call with key function', async () => {
const foo = new Foo();
const allRes: number[] = [];
it("should return correct result for each call with key function", async () => {
const foo = new Foo();
const allRes: number[] = [];
await Promise.all([
foo.baz(1).then(res => allRes.push(res)),
foo.baz(1).then(res => allRes.push(res)),
foo.baz(2).then(res => allRes.push(res)),
foo.baz(2).then(res => allRes.push(res)),
foo.baz(3).then(res => allRes.push(res)),
foo.baz(3).then(res => allRes.push(res)),
]);
expect(foo.calls).toBe(3);
expect(allRes.length).toBe(6);
allRes.sort();
expect(allRes).toEqual([3, 3, 6, 6, 9, 9]);
});
await Promise.all([
foo.baz(1).then((res) => allRes.push(res)),
foo.baz(1).then((res) => allRes.push(res)),
foo.baz(2).then((res) => allRes.push(res)),
foo.baz(2).then((res) => allRes.push(res)),
foo.baz(3).then((res) => allRes.push(res)),
foo.baz(3).then((res) => allRes.push(res)),
]);
expect(foo.calls).toBe(3);
expect(allRes.length).toBe(6);
allRes.sort();
expect(allRes).toEqual([3, 3, 6, 6, 9, 9]);
});
});
class Foo {
calls = 0;
calls = 0;
@sequentialize(args => 'bar' + args[0])
bar(a: number): Promise<number> {
this.calls++;
return new Promise(res => {
setTimeout(() => {
res(a * 2);
}, Math.random() * 100);
});
}
@sequentialize((args) => "bar" + args[0])
bar(a: number): Promise<number> {
this.calls++;
return new Promise((res) => {
setTimeout(() => {
res(a * 2);
}, Math.random() * 100);
});
}
@sequentialize(args => 'baz' + args[0])
baz(a: number): Promise<number> {
this.calls++;
return new Promise(res => {
setTimeout(() => {
res(a * 3);
}, Math.random() * 100);
});
}
@sequentialize((args) => "baz" + args[0])
baz(a: number): Promise<number> {
this.calls++;
return new Promise((res) => {
setTimeout(() => {
res(a * 3);
}, Math.random() * 100);
});
}
}

View File

@@ -1,110 +1,110 @@
import { sequentialize } from 'jslib-common/misc/sequentialize';
import { throttle } from 'jslib-common/misc/throttle';
import { sequentialize } from "jslib-common/misc/sequentialize";
import { throttle } from "jslib-common/misc/throttle";
describe('throttle decorator', () => {
it('should call the function once at a time', async () => {
const foo = new Foo();
const promises = [];
for (let i = 0; i < 10; i++) {
promises.push(foo.bar(1));
}
await Promise.all(promises);
describe("throttle decorator", () => {
it("should call the function once at a time", async () => {
const foo = new Foo();
const promises = [];
for (let i = 0; i < 10; i++) {
promises.push(foo.bar(1));
}
await Promise.all(promises);
expect(foo.calls).toBe(10);
});
expect(foo.calls).toBe(10);
});
it('should call the function once at a time for each object', async () => {
const foo = new Foo();
const foo2 = new Foo();
const promises = [];
for (let i = 0; i < 10; i++) {
promises.push(foo.bar(1));
promises.push(foo2.bar(1));
}
await Promise.all(promises);
it("should call the function once at a time for each object", async () => {
const foo = new Foo();
const foo2 = new Foo();
const promises = [];
for (let i = 0; i < 10; i++) {
promises.push(foo.bar(1));
promises.push(foo2.bar(1));
}
await Promise.all(promises);
expect(foo.calls).toBe(10);
expect(foo2.calls).toBe(10);
});
expect(foo.calls).toBe(10);
expect(foo2.calls).toBe(10);
});
it('should call the function limit at a time', async () => {
const foo = new Foo();
const promises = [];
for (let i = 0; i < 10; i++) {
promises.push(foo.baz(1));
}
await Promise.all(promises);
it("should call the function limit at a time", async () => {
const foo = new Foo();
const promises = [];
for (let i = 0; i < 10; i++) {
promises.push(foo.baz(1));
}
await Promise.all(promises);
expect(foo.calls).toBe(10);
});
expect(foo.calls).toBe(10);
});
it('should call the function limit at a time for each object', async () => {
const foo = new Foo();
const foo2 = new Foo();
const promises = [];
for (let i = 0; i < 10; i++) {
promises.push(foo.baz(1));
promises.push(foo2.baz(1));
}
await Promise.all(promises);
it("should call the function limit at a time for each object", async () => {
const foo = new Foo();
const foo2 = new Foo();
const promises = [];
for (let i = 0; i < 10; i++) {
promises.push(foo.baz(1));
promises.push(foo2.baz(1));
}
await Promise.all(promises);
expect(foo.calls).toBe(10);
expect(foo2.calls).toBe(10);
});
expect(foo.calls).toBe(10);
expect(foo2.calls).toBe(10);
});
it('should work together with sequentialize', async () => {
const foo = new Foo();
const promises = [];
for (let i = 0; i < 10; i++) {
promises.push(foo.qux(Math.floor(i / 2) * 2));
}
await Promise.all(promises);
it("should work together with sequentialize", async () => {
const foo = new Foo();
const promises = [];
for (let i = 0; i < 10; i++) {
promises.push(foo.qux(Math.floor(i / 2) * 2));
}
await Promise.all(promises);
expect(foo.calls).toBe(5);
});
expect(foo.calls).toBe(5);
});
});
class Foo {
calls = 0;
inflight = 0;
calls = 0;
inflight = 0;
@throttle(1, () => 'bar')
bar(a: number) {
this.calls++;
this.inflight++;
return new Promise(res => {
setTimeout(() => {
expect(this.inflight).toBe(1);
this.inflight--;
res(a * 2);
}, Math.random() * 10);
});
}
@throttle(1, () => "bar")
bar(a: number) {
this.calls++;
this.inflight++;
return new Promise((res) => {
setTimeout(() => {
expect(this.inflight).toBe(1);
this.inflight--;
res(a * 2);
}, Math.random() * 10);
});
}
@throttle(5, () => 'baz')
baz(a: number) {
this.calls++;
this.inflight++;
return new Promise(res => {
setTimeout(() => {
expect(this.inflight).toBeLessThanOrEqual(5);
this.inflight--;
res(a * 3);
}, Math.random() * 10);
});
}
@throttle(5, () => "baz")
baz(a: number) {
this.calls++;
this.inflight++;
return new Promise((res) => {
setTimeout(() => {
expect(this.inflight).toBeLessThanOrEqual(5);
this.inflight--;
res(a * 3);
}, Math.random() * 10);
});
}
@sequentialize(args => 'qux' + args[0])
@throttle(1, () => 'qux')
qux(a: number) {
this.calls++;
this.inflight++;
return new Promise(res => {
setTimeout(() => {
expect(this.inflight).toBe(1);
this.inflight--;
res(a * 3);
}, Math.random() * 10);
});
}
@sequentialize((args) => "qux" + args[0])
@throttle(1, () => "qux")
qux(a: number) {
this.calls++;
this.inflight++;
return new Promise((res) => {
setTimeout(() => {
expect(this.inflight).toBe(1);
this.inflight--;
res(a * 3);
}, Math.random() * 10);
});
}
}

View File

@@ -1,71 +1,73 @@
import { Utils } from 'jslib-common/misc/utils';
import { Utils } from "jslib-common/misc/utils";
describe('Utils Service', () => {
describe('getDomain', () => {
it('should fail for invalid urls', () => {
expect(Utils.getDomain(null)).toBeNull();
expect(Utils.getDomain(undefined)).toBeNull();
expect(Utils.getDomain(' ')).toBeNull();
expect(Utils.getDomain('https://bit!:"_&ward.com')).toBeNull();
expect(Utils.getDomain('bitwarden')).toBeNull();
});
it('should fail for data urls', () => {
expect(Utils.getDomain('data:image/jpeg;base64,AAA')).toBeNull();
});
it('should handle urls without protocol', () => {
expect(Utils.getDomain('bitwarden.com')).toBe('bitwarden.com');
expect(Utils.getDomain('wrong://bitwarden.com')).toBe('bitwarden.com');
});
it('should handle valid urls', () => {
expect(Utils.getDomain('https://bitwarden')).toBe('bitwarden');
expect(Utils.getDomain('https://bitwarden.com')).toBe('bitwarden.com');
expect(Utils.getDomain('http://bitwarden.com')).toBe('bitwarden.com');
expect(Utils.getDomain('http://vault.bitwarden.com')).toBe('bitwarden.com');
expect(Utils.getDomain('https://user:password@bitwarden.com:8080/password/sites?and&query#hash'))
.toBe('bitwarden.com');
expect(Utils.getDomain('https://bitwarden.unknown')).toBe('bitwarden.unknown');
});
it('should support localhost and IP', () => {
expect(Utils.getDomain('https://localhost')).toBe('localhost');
expect(Utils.getDomain('https://192.168.1.1')).toBe('192.168.1.1');
});
it('should reject invalid hostnames', () => {
expect(Utils.getDomain('https://mywebsite.com$.mywebsite.com')).toBeNull();
expect(Utils.getDomain('https://mywebsite.com!.mywebsite.com')).toBeNull();
});
describe("Utils Service", () => {
describe("getDomain", () => {
it("should fail for invalid urls", () => {
expect(Utils.getDomain(null)).toBeNull();
expect(Utils.getDomain(undefined)).toBeNull();
expect(Utils.getDomain(" ")).toBeNull();
expect(Utils.getDomain('https://bit!:"_&ward.com')).toBeNull();
expect(Utils.getDomain("bitwarden")).toBeNull();
});
describe('getHostname', () => {
it('should fail for invalid urls', () => {
expect(Utils.getHostname(null)).toBeNull();
expect(Utils.getHostname(undefined)).toBeNull();
expect(Utils.getHostname(' ')).toBeNull();
expect(Utils.getHostname('https://bit!:"_&ward.com')).toBeNull();
expect(Utils.getHostname('bitwarden')).toBeNull();
});
it('should handle valid urls', () => {
expect(Utils.getHostname('bitwarden.com')).toBe('bitwarden.com');
expect(Utils.getHostname('https://bitwarden.com')).toBe('bitwarden.com');
expect(Utils.getHostname('http://bitwarden.com')).toBe('bitwarden.com');
expect(Utils.getHostname('http://vault.bitwarden.com')).toBe('vault.bitwarden.com');
});
it('should support localhost and IP', () => {
expect(Utils.getHostname('https://localhost')).toBe('localhost');
expect(Utils.getHostname('https://192.168.1.1')).toBe('192.168.1.1');
});
it("should fail for data urls", () => {
expect(Utils.getDomain("data:image/jpeg;base64,AAA")).toBeNull();
});
describe('newGuid', () => {
it('should create a valid guid', () => {
const validGuid = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
expect(Utils.newGuid()).toMatch(validGuid);
});
it("should handle urls without protocol", () => {
expect(Utils.getDomain("bitwarden.com")).toBe("bitwarden.com");
expect(Utils.getDomain("wrong://bitwarden.com")).toBe("bitwarden.com");
});
it("should handle valid urls", () => {
expect(Utils.getDomain("https://bitwarden")).toBe("bitwarden");
expect(Utils.getDomain("https://bitwarden.com")).toBe("bitwarden.com");
expect(Utils.getDomain("http://bitwarden.com")).toBe("bitwarden.com");
expect(Utils.getDomain("http://vault.bitwarden.com")).toBe("bitwarden.com");
expect(
Utils.getDomain("https://user:password@bitwarden.com:8080/password/sites?and&query#hash")
).toBe("bitwarden.com");
expect(Utils.getDomain("https://bitwarden.unknown")).toBe("bitwarden.unknown");
});
it("should support localhost and IP", () => {
expect(Utils.getDomain("https://localhost")).toBe("localhost");
expect(Utils.getDomain("https://192.168.1.1")).toBe("192.168.1.1");
});
it("should reject invalid hostnames", () => {
expect(Utils.getDomain("https://mywebsite.com$.mywebsite.com")).toBeNull();
expect(Utils.getDomain("https://mywebsite.com!.mywebsite.com")).toBeNull();
});
});
describe("getHostname", () => {
it("should fail for invalid urls", () => {
expect(Utils.getHostname(null)).toBeNull();
expect(Utils.getHostname(undefined)).toBeNull();
expect(Utils.getHostname(" ")).toBeNull();
expect(Utils.getHostname('https://bit!:"_&ward.com')).toBeNull();
expect(Utils.getHostname("bitwarden")).toBeNull();
});
it("should handle valid urls", () => {
expect(Utils.getHostname("bitwarden.com")).toBe("bitwarden.com");
expect(Utils.getHostname("https://bitwarden.com")).toBe("bitwarden.com");
expect(Utils.getHostname("http://bitwarden.com")).toBe("bitwarden.com");
expect(Utils.getHostname("http://vault.bitwarden.com")).toBe("vault.bitwarden.com");
});
it("should support localhost and IP", () => {
expect(Utils.getHostname("https://localhost")).toBe("localhost");
expect(Utils.getHostname("https://192.168.1.1")).toBe("192.168.1.1");
});
});
describe("newGuid", () => {
it("should create a valid guid", () => {
const validGuid =
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
expect(Utils.newGuid()).toMatch(validGuid);
});
});
});

View File

@@ -1,487 +1,544 @@
import { Arg, Substitute, SubstituteOf } from '@fluffy-spoon/substitute';
import { Arg, Substitute, SubstituteOf } from "@fluffy-spoon/substitute";
import { ApiService } from 'jslib-common/abstractions/api.service';
import { AppIdService } from 'jslib-common/abstractions/appId.service';
import { CryptoService } from 'jslib-common/abstractions/crypto.service';
import { CryptoFunctionService } from 'jslib-common/abstractions/cryptoFunction.service';
import { EnvironmentService } from 'jslib-common/abstractions/environment.service';
import { I18nService } from 'jslib-common/abstractions/i18n.service';
import { KeyConnectorService } from 'jslib-common/abstractions/keyConnector.service';
import { LogService } from 'jslib-common/abstractions/log.service';
import { MessagingService } from 'jslib-common/abstractions/messaging.service';
import { PlatformUtilsService } from 'jslib-common/abstractions/platformUtils.service';
import { StateService } from 'jslib-common/abstractions/state.service';
import { TokenService } from 'jslib-common/abstractions/token.service';
import { VaultTimeoutService } from 'jslib-common/abstractions/vaultTimeout.service';
import { ApiService } from "jslib-common/abstractions/api.service";
import { AppIdService } from "jslib-common/abstractions/appId.service";
import { CryptoService } from "jslib-common/abstractions/crypto.service";
import { CryptoFunctionService } from "jslib-common/abstractions/cryptoFunction.service";
import { EnvironmentService } from "jslib-common/abstractions/environment.service";
import { I18nService } from "jslib-common/abstractions/i18n.service";
import { KeyConnectorService } from "jslib-common/abstractions/keyConnector.service";
import { LogService } from "jslib-common/abstractions/log.service";
import { MessagingService } from "jslib-common/abstractions/messaging.service";
import { PlatformUtilsService } from "jslib-common/abstractions/platformUtils.service";
import { StateService } from "jslib-common/abstractions/state.service";
import { TokenService } from "jslib-common/abstractions/token.service";
import { VaultTimeoutService } from "jslib-common/abstractions/vaultTimeout.service";
import { AuthService } from 'jslib-common/services/auth.service';
import { AuthService } from "jslib-common/services/auth.service";
import { Utils } from 'jslib-common/misc/utils';
import { Utils } from "jslib-common/misc/utils";
import { AccountProfile, AccountTokens } from 'jslib-common/models/domain/account';
import { AuthResult } from 'jslib-common/models/domain/authResult';
import { EncString } from 'jslib-common/models/domain/encString';
import { SymmetricCryptoKey } from 'jslib-common/models/domain/symmetricCryptoKey';
import { AccountProfile, AccountTokens } from "jslib-common/models/domain/account";
import { AuthResult } from "jslib-common/models/domain/authResult";
import { EncString } from "jslib-common/models/domain/encString";
import { SymmetricCryptoKey } from "jslib-common/models/domain/symmetricCryptoKey";
import { IdentityTokenResponse } from 'jslib-common/models/response/identityTokenResponse';
import { IdentityTokenResponse } from "jslib-common/models/response/identityTokenResponse";
import { TwoFactorService } from 'jslib-common/abstractions/twoFactor.service';
import { HashPurpose } from 'jslib-common/enums/hashPurpose';
import { TwoFactorProviderType } from 'jslib-common/enums/twoFactorProviderType';
import { SsoTokenRequest } from 'jslib-common/models/request/identityToken/ssoTokenRequest';
import { ApiTokenRequest } from 'jslib-common/models/request/identityToken/apiTokenRequest';
import { PasswordTokenRequest } from 'jslib-common/models/request/identityToken/passwordTokenRequest';
import { TwoFactorService } from "jslib-common/abstractions/twoFactor.service";
import { HashPurpose } from "jslib-common/enums/hashPurpose";
import { TwoFactorProviderType } from "jslib-common/enums/twoFactorProviderType";
import { SsoTokenRequest } from "jslib-common/models/request/identityToken/ssoTokenRequest";
import { ApiTokenRequest } from "jslib-common/models/request/identityToken/apiTokenRequest";
import { PasswordTokenRequest } from "jslib-common/models/request/identityToken/passwordTokenRequest";
describe('Cipher Service', () => {
let cryptoService: SubstituteOf<CryptoService>;
let apiService: SubstituteOf<ApiService>;
let tokenService: SubstituteOf<TokenService>;
let appIdService: SubstituteOf<AppIdService>;
let platformUtilsService: SubstituteOf<PlatformUtilsService>;
let messagingService: SubstituteOf<MessagingService>;
let logService: SubstituteOf<LogService>;
let cryptoFunctionService: SubstituteOf<CryptoFunctionService>;
let environmentService: SubstituteOf<EnvironmentService>;
let keyConnectorService: SubstituteOf<KeyConnectorService>;
let stateService: SubstituteOf<StateService>;
let twoFactorService: SubstituteOf<TwoFactorService>;
const setCryptoKeys = true;
describe("Cipher Service", () => {
let cryptoService: SubstituteOf<CryptoService>;
let apiService: SubstituteOf<ApiService>;
let tokenService: SubstituteOf<TokenService>;
let appIdService: SubstituteOf<AppIdService>;
let platformUtilsService: SubstituteOf<PlatformUtilsService>;
let messagingService: SubstituteOf<MessagingService>;
let logService: SubstituteOf<LogService>;
let cryptoFunctionService: SubstituteOf<CryptoFunctionService>;
let environmentService: SubstituteOf<EnvironmentService>;
let keyConnectorService: SubstituteOf<KeyConnectorService>;
let stateService: SubstituteOf<StateService>;
let twoFactorService: SubstituteOf<TwoFactorService>;
const setCryptoKeys = true;
const email = 'hello@world.com';
const masterPassword = 'password';
const hashedPassword = 'HASHED_PASSWORD';
const localHashedPassword = 'LOCAL_HASHED_PASSWORD';
const preloginKey = new SymmetricCryptoKey(Utils.fromB64ToArray('N2KWjlLpfi5uHjv+YcfUKIpZ1l+W+6HRensmIqD+BFYBf6N/dvFpJfWwYnVBdgFCK2tJTAIMLhqzIQQEUmGFgg=='));
const deviceId = Utils.newGuid();
const accessToken = 'ACCESS_TOKEN';
const refreshToken = 'REFRESH_TOKEN';
const encKey = 'ENC_KEY';
const privateKey = 'PRIVATE_KEY';
const keyConnectorUrl = 'KEY_CONNECTOR_URL';
const kdf = 0;
const kdfIterations = 10000;
const userId = Utils.newGuid();
const email = "hello@world.com";
const masterPassword = "password";
const hashedPassword = "HASHED_PASSWORD";
const localHashedPassword = "LOCAL_HASHED_PASSWORD";
const preloginKey = new SymmetricCryptoKey(
Utils.fromB64ToArray(
"N2KWjlLpfi5uHjv+YcfUKIpZ1l+W+6HRensmIqD+BFYBf6N/dvFpJfWwYnVBdgFCK2tJTAIMLhqzIQQEUmGFgg=="
)
);
const deviceId = Utils.newGuid();
const accessToken = "ACCESS_TOKEN";
const refreshToken = "REFRESH_TOKEN";
const encKey = "ENC_KEY";
const privateKey = "PRIVATE_KEY";
const keyConnectorUrl = "KEY_CONNECTOR_URL";
const kdf = 0;
const kdfIterations = 10000;
const userId = Utils.newGuid();
const decodedToken = {
sub: userId,
email: email,
premium: false,
};
const decodedToken = {
sub: userId,
email: email,
premium: false,
};
const ssoCode = 'SSO_CODE';
const ssoCodeVerifier = 'SSO_CODE_VERIFIER';
const ssoRedirectUrl = 'SSO_REDIRECT_URL';
const ssoOrgId = 'SSO_ORG_ID';
const ssoCode = "SSO_CODE";
const ssoCodeVerifier = "SSO_CODE_VERIFIER";
const ssoRedirectUrl = "SSO_REDIRECT_URL";
const ssoOrgId = "SSO_ORG_ID";
const twoFactorProviderType = TwoFactorProviderType.Authenticator;
const twoFactorToken = 'TWO_FACTOR_TOKEN';
const twoFactorRemember = true;
const twoFactorProviderType = TwoFactorProviderType.Authenticator;
const twoFactorToken = "TWO_FACTOR_TOKEN";
const twoFactorRemember = true;
const apiClientId = 'API_CLIENT_ID';
const apiClientSecret = 'API_CLIENT_SECRET';
const apiClientId = "API_CLIENT_ID";
const apiClientSecret = "API_CLIENT_SECRET";
let authService: AuthService;
let authService: AuthService;
beforeEach(() => {
cryptoService = Substitute.for<CryptoService>();
apiService = Substitute.for<ApiService>();
tokenService = Substitute.for<TokenService>();
appIdService = Substitute.for<AppIdService>();
platformUtilsService = Substitute.for<PlatformUtilsService>();
messagingService = Substitute.for<MessagingService>();
logService = Substitute.for<LogService>();
cryptoFunctionService = Substitute.for<CryptoFunctionService>();
environmentService = Substitute.for<EnvironmentService>();
stateService = Substitute.for<StateService>();
keyConnectorService = Substitute.for<KeyConnectorService>();
twoFactorService = Substitute.for<TwoFactorService>();
beforeEach(() => {
cryptoService = Substitute.for<CryptoService>();
apiService = Substitute.for<ApiService>();
tokenService = Substitute.for<TokenService>();
appIdService = Substitute.for<AppIdService>();
platformUtilsService = Substitute.for<PlatformUtilsService>();
messagingService = Substitute.for<MessagingService>();
logService = Substitute.for<LogService>();
cryptoFunctionService = Substitute.for<CryptoFunctionService>();
environmentService = Substitute.for<EnvironmentService>();
stateService = Substitute.for<StateService>();
keyConnectorService = Substitute.for<KeyConnectorService>();
twoFactorService = Substitute.for<TwoFactorService>();
authService = new AuthService(cryptoService, apiService, tokenService, appIdService,
platformUtilsService, messagingService, logService, cryptoFunctionService,
keyConnectorService, environmentService, stateService, twoFactorService, setCryptoKeys);
authService = new AuthService(
cryptoService,
apiService,
tokenService,
appIdService,
platformUtilsService,
messagingService,
logService,
cryptoFunctionService,
keyConnectorService,
environmentService,
stateService,
twoFactorService,
setCryptoKeys
);
});
function logInSetup() {
// Arrange for logIn and logInComplete
cryptoService.makeKey(masterPassword, email, Arg.any(), Arg.any()).resolves(preloginKey);
cryptoService.hashPassword(masterPassword, Arg.any()).resolves(hashedPassword);
cryptoService
.hashPassword(masterPassword, Arg.any(), HashPurpose.LocalAuthorization)
.resolves(localHashedPassword);
}
function commonSetup() {
// For logInHelper, i.e. always required
appIdService.getAppId().resolves(deviceId);
tokenService.decodeToken(accessToken).resolves(decodedToken);
}
function commonSuccessAssertions() {
stateService.received(1).addAccount({
profile: {
...new AccountProfile(),
...{
userId: userId,
email: email,
apiKeyClientId: null,
apiKeyClientSecret: null,
hasPremiumPersonally: false,
kdfIterations: kdfIterations,
kdfType: kdf,
},
},
tokens: {
...new AccountTokens(),
...{
accessToken: accessToken,
refreshToken: refreshToken,
},
},
});
stateService.received(1).setBiometricLocked(false);
messagingService.received(1).send("loggedIn");
}
function logInSetup() {
// Arrange for logIn and logInComplete
cryptoService.makeKey(masterPassword, email, Arg.any(), Arg.any()).resolves(preloginKey);
cryptoService.hashPassword(masterPassword, Arg.any()).resolves(hashedPassword);
cryptoService.hashPassword(masterPassword, Arg.any(), HashPurpose.LocalAuthorization).resolves(localHashedPassword);
}
function newTokenResponse() {
const tokenResponse = new IdentityTokenResponse({});
(tokenResponse as any).twoFactorProviders2 = false;
(tokenResponse as any).siteKey = undefined;
tokenResponse.resetMasterPassword = false;
tokenResponse.forcePasswordReset = false;
tokenResponse.accessToken = accessToken;
tokenResponse.refreshToken = refreshToken;
tokenResponse.kdf = kdf;
tokenResponse.kdfIterations = kdfIterations;
tokenResponse.key = encKey;
tokenResponse.privateKey = privateKey;
return tokenResponse;
}
function commonSetup() {
// For logInHelper, i.e. always required
appIdService.getAppId().resolves(deviceId);
function newAuthResponse() {
const expected = new AuthResult();
expected.forcePasswordReset = false;
expected.resetMasterPassword = false;
expected.twoFactor = false;
expected.twoFactorProviders = null;
expected.captchaSiteKey = undefined;
return expected;
}
tokenService.decodeToken(accessToken).resolves(decodedToken);
}
it("logIn: works in the most simple case (no 2FA, no captcha, no password reset, no KC)", async () => {
logInSetup();
commonSetup();
const tokenResponse = newTokenResponse();
function commonSuccessAssertions() {
stateService.received(1).addAccount({
profile: {
...new AccountProfile(),
...{
userId: userId,
email: email,
apiKeyClientId: null,
apiKeyClientSecret: null,
hasPremiumPersonally: false,
kdfIterations: kdfIterations,
kdfType: kdf,
},
},
tokens: {
...new AccountTokens(),
...{
accessToken: accessToken,
refreshToken: refreshToken,
},
},
});
stateService.received(1).setBiometricLocked(false);
messagingService.received(1).send('loggedIn');
}
tokenService.getTwoFactorToken(email).resolves(null);
apiService.postIdentityToken(Arg.any()).resolves(tokenResponse);
function newTokenResponse() {
const tokenResponse = new IdentityTokenResponse({});
(tokenResponse as any).twoFactorProviders2 = false;
(tokenResponse as any).siteKey = undefined;
tokenResponse.resetMasterPassword = false;
tokenResponse.forcePasswordReset = false;
tokenResponse.accessToken = accessToken;
tokenResponse.refreshToken = refreshToken;
tokenResponse.kdf = kdf;
tokenResponse.kdfIterations = kdfIterations;
tokenResponse.key = encKey;
tokenResponse.privateKey = privateKey;
return tokenResponse;
}
const expected = newAuthResponse();
function newAuthResponse() {
const expected = new AuthResult();
expected.forcePasswordReset = false;
expected.resetMasterPassword = false;
expected.twoFactor = false;
expected.twoFactorProviders = null;
expected.captchaSiteKey = undefined;
return expected;
}
// Act
const result = await authService.logIn(email, masterPassword);
it('logIn: works in the most simple case (no 2FA, no captcha, no password reset, no KC)', async () => {
logInSetup();
commonSetup();
const tokenResponse = newTokenResponse();
// Assert
// Api call:
apiService.received(1).postIdentityToken(
Arg.is((actual) => {
const passwordTokenRequest = actual as PasswordTokenRequest;
return (
passwordTokenRequest.email === email &&
passwordTokenRequest.masterPasswordHash === hashedPassword &&
actual.device.identifier === deviceId &&
actual.provider == null &&
actual.token == null &&
actual.captchaResponse == null
);
})
);
tokenService.getTwoFactorToken(email).resolves(null);
apiService.postIdentityToken(Arg.any()).resolves(tokenResponse);
// Sets local environment:
commonSuccessAssertions();
cryptoService.received(1).setKey(preloginKey);
cryptoService.received(1).setKeyHash(localHashedPassword);
cryptoService.received(1).setEncKey(encKey);
cryptoService.received(1).setEncPrivateKey(privateKey);
const expected = newAuthResponse();
// Negative tests
apiService.didNotReceive().postAccountKeys(Arg.any()); // Did not generate new private key pair
keyConnectorService.didNotReceive().getAndSetKey(Arg.any()); // Did not fetch Key Connector key
apiService.didNotReceive().postUserKeyToKeyConnector(Arg.any(), Arg.any()); // Did not send key to KC
tokenService.didNotReceive().setTwoFactorToken(Arg.any(), Arg.any()); // Did not save 2FA token
// Act
const result = await authService.logIn(email, masterPassword);
// Return result:
expect(result).toEqual(expected);
});
// Assert
// Api call:
apiService.received(1).postIdentityToken(Arg.is(actual => {
const passwordTokenRequest = actual as PasswordTokenRequest;
return passwordTokenRequest.email === email &&
passwordTokenRequest.masterPasswordHash === hashedPassword &&
actual.device.identifier === deviceId &&
actual.provider == null &&
actual.token == null &&
actual.captchaResponse == null
}));
it("logIn: bails out if captchaSiteKey is true", async () => {
const siteKey = "CAPTCHA_SITE_KEY";
// Sets local environment:
commonSuccessAssertions();
cryptoService.received(1).setKey(preloginKey);
cryptoService.received(1).setKeyHash(localHashedPassword);
cryptoService.received(1).setEncKey(encKey);
cryptoService.received(1).setEncPrivateKey(privateKey);
logInSetup();
commonSetup();
const tokenResponse = newTokenResponse();
(tokenResponse as any).siteKey = siteKey;
// Negative tests
apiService.didNotReceive().postAccountKeys(Arg.any()); // Did not generate new private key pair
keyConnectorService.didNotReceive().getAndSetKey(Arg.any()); // Did not fetch Key Connector key
apiService.didNotReceive().postUserKeyToKeyConnector(Arg.any(), Arg.any()); // Did not send key to KC
tokenService.didNotReceive().setTwoFactorToken(Arg.any(), Arg.any()); // Did not save 2FA token
tokenService.getTwoFactorToken(email).resolves(null);
apiService.postIdentityToken(Arg.any()).resolves(tokenResponse);
// Return result:
expect(result).toEqual(expected);
});
const expected = new AuthResult();
expected.captchaSiteKey = siteKey;
it('logIn: bails out if captchaSiteKey is true', async () => {
const siteKey = 'CAPTCHA_SITE_KEY';
// Act
const result = await authService.logIn(email, masterPassword);
logInSetup();
commonSetup();
const tokenResponse = newTokenResponse();
(tokenResponse as any).siteKey = siteKey;
// Assertions
stateService.didNotReceive().addAccount(Arg.any());
messagingService.didNotReceive().send(Arg.any());
expect(result).toEqual(expected);
});
tokenService.getTwoFactorToken(email).resolves(null);
apiService.postIdentityToken(Arg.any()).resolves(tokenResponse);
it("logIn: does not set crypto keys if setCryptoKeys is false", async () => {
logInSetup();
commonSetup();
const tokenResponse = newTokenResponse();
const expected = new AuthResult();
expected.captchaSiteKey = siteKey;
tokenService.getTwoFactorToken(email).resolves(null);
apiService.postIdentityToken(Arg.any()).resolves(tokenResponse);
// Act
const result = await authService.logIn(email, masterPassword);
// Re-init authService with setCryptoKeys = false
authService = new AuthService(
cryptoService,
apiService,
tokenService,
appIdService,
platformUtilsService,
messagingService,
logService,
cryptoFunctionService,
keyConnectorService,
environmentService,
stateService,
twoFactorService,
false
);
// Assertions
stateService.didNotReceive().addAccount(Arg.any());
messagingService.didNotReceive().send(Arg.any());
expect(result).toEqual(expected);
});
// Act
const result = await authService.logIn(email, masterPassword);
it('logIn: does not set crypto keys if setCryptoKeys is false', async () => {
logInSetup();
commonSetup();
const tokenResponse = newTokenResponse();
// Assertions
commonSuccessAssertions();
cryptoService.didNotReceive().setKey(Arg.any());
cryptoService.didNotReceive().setKeyHash(Arg.any());
cryptoService.didNotReceive().setEncKey(Arg.any());
cryptoService.didNotReceive().setEncPrivateKey(Arg.any());
});
tokenService.getTwoFactorToken(email).resolves(null);
apiService.postIdentityToken(Arg.any()).resolves(tokenResponse);
it("logIn: makes new KeyPair for an old account", async () => {
logInSetup();
commonSetup();
const tokenResponse = newTokenResponse();
tokenResponse.privateKey = null;
// Re-init authService with setCryptoKeys = false
authService = new AuthService(cryptoService, apiService, tokenService, appIdService, platformUtilsService,
messagingService, logService, cryptoFunctionService, keyConnectorService, environmentService, stateService,
twoFactorService, false);
tokenService.getTwoFactorToken(email).resolves(null);
apiService.postIdentityToken(Arg.any()).resolves(tokenResponse);
// Act
const result = await authService.logIn(email, masterPassword);
const result = await authService.logIn(email, masterPassword);
// Assertions
commonSuccessAssertions();
cryptoService.didNotReceive().setKey(Arg.any());
cryptoService.didNotReceive().setKeyHash(Arg.any());
cryptoService.didNotReceive().setEncKey(Arg.any());
cryptoService.didNotReceive().setEncPrivateKey(Arg.any());
});
commonSuccessAssertions();
apiService.received(1).postAccountKeys(Arg.any());
});
it('logIn: makes new KeyPair for an old account', async () => {
logInSetup();
commonSetup();
const tokenResponse = newTokenResponse();
tokenResponse.privateKey = null;
// 2FA
tokenService.getTwoFactorToken(email).resolves(null);
apiService.postIdentityToken(Arg.any()).resolves(tokenResponse);
it("logIn: bails out if 2FA is required", async () => {
const twoFactorProviders = new Map<number, null>([[1, null]]);
const result = await authService.logIn(email, masterPassword);
logInSetup();
commonSetup();
const tokenResponse = newTokenResponse();
(tokenResponse as any).twoFactorProviders2 = twoFactorProviders;
commonSuccessAssertions();
apiService.received(1).postAccountKeys(Arg.any());
});
tokenService.getTwoFactorToken(email).resolves(null);
apiService.postIdentityToken(Arg.any()).resolves(tokenResponse);
// 2FA
const expected = new AuthResult();
expected.twoFactor = true;
expected.twoFactorProviders = twoFactorProviders;
expected.captchaSiteKey = undefined;
it('logIn: bails out if 2FA is required', async () => {
const twoFactorProviders = new Map<number, null>([
[1, null],
]);
const result = await authService.logIn(email, masterPassword);
logInSetup();
commonSetup();
const tokenResponse = newTokenResponse();
(tokenResponse as any).twoFactorProviders2 = twoFactorProviders;
stateService.didNotReceive().addAccount(Arg.any());
messagingService.didNotReceive().send(Arg.any());
tokenService.getTwoFactorToken(email).resolves(null);
apiService.postIdentityToken(Arg.any()).resolves(tokenResponse);
expect(result).toEqual(expected);
});
const expected = new AuthResult();
expected.twoFactor = true;
expected.twoFactorProviders = twoFactorProviders;
expected.captchaSiteKey = undefined;
it("logInTwoFactor: sends 2FA token to server when using Master Password", async () => {
commonSetup();
const result = await authService.logIn(email, masterPassword);
authService.email = email;
authService.masterPasswordHash = hashedPassword;
authService.localMasterPasswordHash = localHashedPassword;
stateService.didNotReceive().addAccount(Arg.any());
messagingService.didNotReceive().send(Arg.any());
await authService.logInTwoFactor(twoFactorProviderType, twoFactorToken, twoFactorRemember);
expect(result).toEqual(expected);
});
apiService.received(1).postIdentityToken(
Arg.is((actual) => {
const passwordTokenRequest = actual as PasswordTokenRequest;
return (
passwordTokenRequest.email === email &&
passwordTokenRequest.masterPasswordHash === hashedPassword &&
actual.device.identifier === deviceId &&
actual.provider === twoFactorProviderType &&
actual.token === twoFactorToken &&
actual.remember === twoFactorRemember &&
actual.captchaResponse == null
);
})
);
});
it('logInTwoFactor: sends 2FA token to server when using Master Password', async () => {
commonSetup();
// SSO
authService.email = email;
authService.masterPasswordHash = hashedPassword;
authService.localMasterPasswordHash = localHashedPassword;
it("logInSso: user can log in with Sso", async () => {
commonSetup();
const tokenResponse = newTokenResponse();
await authService.logInTwoFactor(twoFactorProviderType, twoFactorToken, twoFactorRemember);
tokenService.getTwoFactorToken(null).resolves(null);
apiService.postIdentityToken(Arg.any()).resolves(tokenResponse);
apiService.received(1).postIdentityToken(Arg.is(actual => {
const passwordTokenRequest = actual as PasswordTokenRequest;
return passwordTokenRequest.email === email &&
passwordTokenRequest.masterPasswordHash === hashedPassword &&
actual.device.identifier === deviceId &&
actual.provider === twoFactorProviderType &&
actual.token === twoFactorToken &&
actual.remember === twoFactorRemember &&
actual.captchaResponse == null
}));
});
const result = await authService.logInSso(ssoCode, ssoCodeVerifier, ssoRedirectUrl, ssoOrgId);
// SSO
// Assert
// Api call:
apiService.received(1).postIdentityToken(
Arg.is((actual) => {
const ssoTokenRequest = actual as SsoTokenRequest;
return (
ssoTokenRequest.code === ssoCode &&
ssoTokenRequest.codeVerifier === ssoCodeVerifier &&
ssoTokenRequest.redirectUri === ssoRedirectUrl &&
actual.device.identifier === deviceId &&
actual.provider == null &&
actual.token == null &&
actual.captchaResponse == null
);
})
);
it('logInSso: user can log in with Sso', async () => {
commonSetup();
const tokenResponse = newTokenResponse();
// Sets local environment:
commonSuccessAssertions();
cryptoService.received(1).setEncPrivateKey(privateKey);
cryptoService.received(1).setEncKey(encKey);
tokenService.getTwoFactorToken(null).resolves(null);
apiService.postIdentityToken(Arg.any()).resolves(tokenResponse);
// Negative tests
// No keys are returned because SSO (even if we get Key Connector we do that later)
cryptoService.didNotReceive().setKey(preloginKey);
cryptoService.didNotReceive().setKeyHash(localHashedPassword);
apiService.didNotReceive().postAccountKeys(Arg.any()); // Did not generate new private key pair
keyConnectorService.didNotReceive().getAndSetKey(Arg.any()); // Did not fetch Key Connector key
apiService.didNotReceive().postUserKeyToKeyConnector(Arg.any(), Arg.any()); // Did not send key to KC
tokenService.didNotReceive().setTwoFactorToken(Arg.any(), Arg.any()); // Did not save 2FA token
const result = await authService.logInSso(ssoCode, ssoCodeVerifier, ssoRedirectUrl, ssoOrgId);
// Return result:
const expected = newAuthResponse();
expect(result).toEqual(expected);
});
// Assert
// Api call:
apiService.received(1).postIdentityToken(Arg.is(actual => {
const ssoTokenRequest = actual as SsoTokenRequest;
return ssoTokenRequest.code === ssoCode &&
ssoTokenRequest.codeVerifier === ssoCodeVerifier &&
ssoTokenRequest.redirectUri === ssoRedirectUrl &&
actual.device.identifier === deviceId &&
actual.provider == null &&
actual.token == null &&
actual.captchaResponse == null
}));
it("logInSso: do not set keys for new SSO user flow", async () => {
commonSetup();
const tokenResponse = newTokenResponse();
tokenResponse.key = null;
// Sets local environment:
commonSuccessAssertions();
cryptoService.received(1).setEncPrivateKey(privateKey);
cryptoService.received(1).setEncKey(encKey);
tokenService.getTwoFactorToken(null).resolves(null);
apiService.postIdentityToken(Arg.any()).resolves(tokenResponse);
// Negative tests
// No keys are returned because SSO (even if we get Key Connector we do that later)
cryptoService.didNotReceive().setKey(preloginKey);
cryptoService.didNotReceive().setKeyHash(localHashedPassword);
apiService.didNotReceive().postAccountKeys(Arg.any()); // Did not generate new private key pair
keyConnectorService.didNotReceive().getAndSetKey(Arg.any()); // Did not fetch Key Connector key
apiService.didNotReceive().postUserKeyToKeyConnector(Arg.any(), Arg.any()); // Did not send key to KC
tokenService.didNotReceive().setTwoFactorToken(Arg.any(), Arg.any()); // Did not save 2FA token
const result = await authService.logInSso(ssoCode, ssoCodeVerifier, ssoRedirectUrl, ssoOrgId);
// Return result:
const expected = newAuthResponse();
expect(result).toEqual(expected);
});
// Assert
cryptoService.didNotReceive().setEncPrivateKey(privateKey);
cryptoService.didNotReceive().setEncKey(encKey);
});
it('logInSso: do not set keys for new SSO user flow', async () => {
commonSetup();
const tokenResponse = newTokenResponse();
tokenResponse.key = null;
it("logInSso: gets and sets KeyConnector key for enrolled user", async () => {
commonSetup();
const tokenResponse = newTokenResponse();
tokenResponse.keyConnectorUrl = keyConnectorUrl;
tokenService.getTwoFactorToken(null).resolves(null);
apiService.postIdentityToken(Arg.any()).resolves(tokenResponse);
apiService.postIdentityToken(Arg.any()).resolves(tokenResponse);
const result = await authService.logInSso(ssoCode, ssoCodeVerifier, ssoRedirectUrl, ssoOrgId);
const result = await authService.logInSso(ssoCode, ssoCodeVerifier, ssoRedirectUrl, ssoOrgId);
// Assert
cryptoService.didNotReceive().setEncPrivateKey(privateKey);
cryptoService.didNotReceive().setEncKey(encKey);
});
commonSuccessAssertions();
keyConnectorService.received(1).getAndSetKey(keyConnectorUrl);
});
it('logInSso: gets and sets KeyConnector key for enrolled user', async () => {
commonSetup();
const tokenResponse = newTokenResponse();
tokenResponse.keyConnectorUrl = keyConnectorUrl;
it("logInSso: new SSO user with Key Connector posts key to the server", async () => {
const realEncKey: [SymmetricCryptoKey, EncString] = [
new SymmetricCryptoKey(Utils.fromB64ToArray("T/tvXd/wvROlTu69qimTod1l7bnYltYOdx1es+xiiEI=")),
new EncString(
"2.j/dbf/fUThfQtOPLU5rbBQ==|u82vHRHBkNY4E44hNmv0BIJQx961Tgh7RJ/p2pGytnZckFn8jwu72GG6HMNhvG4+GGwoIEd4GgP+oOFBhqIHXh5niH4wT2kuuyeA22+0VZM=|r1q0wYATeXB4ejUY6CORagAtqwUT246sY+wp46Kj6pY="
),
];
const pubKey =
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApqH10xpM+VyAfjLj1cwLm8ydOx/qZq4UcUji/lJ1+p0In1vs1BPeVa5MjaiCO8zrALfSckUfviFv74p7Eo0e4XwbJljnh5E2FdMLUL3oUjKiqCFZ9obdGzzGh7ImY1rnjAOEXjme/kbfWTeva54cv7SYBSoAK3IuNxJeQh1cICohMI/7VUCwlQgHITZ7dK0oQ/gIb/E7wX4TG5x0g6zjYjhWUjWjBaxkUodgF4d7KLd/d7OhM0EQSWA1oaR8nxEaAuAkB4AMNqQhzfZWNzdGJVYdr1j5cizVnDWPoTCk30kHc/Pffuo1hw7156XxLOiM46K/cgKEk9lHTf9s8bSouQIDAQAB";
const privKey = new EncString(
"2.NGK1ka8L+XG32nDAc4u36A==|XbdTogpmevDG2s55ZpJhwVizILBq+oXwIDFmZHXjCVOO2f6Iz1WiVvPvMfp/gqOIVZN770KUYpSiWvwgbuEWr9fKbqaqPUTs188rqMs8C3JWLgRpNeWvc2VNskl+A89lkb2BCHFsOXyxK/IaK85ORpY/PXobWlA1ese7DKqv4iEMjJvMKqPi5xTxLJ1uJGEwqNwOZN021I4RQxDgByYQCmjC2iCIR6rMuSwLRR2a9jwY+CLa/XslxIFkTe8dVzO5o2KXiYwz0UoN82tYxCHz9/8EQfaFI/CPFbE/OQtjN8mfF72i0JtRuYBH9y6yfPFUzYnZpZ7dHSYnF33+pUOALHR1WgJXaHz3VSRQR1yAuoF5ZARtWHn4buw0OXM9tnqcosCQ/BlP9ExN6httJGO6kxZpsbZs+DSCOzAWWxkaVwexE1QGZ+OrbJh8d0lzzlTW5QUukQL5y5cNoaBsF+U2qdb8a16/kxvPr56T9uhUoAfdV6mWyuhc/8Rl7sSMsrVZQ/YINrmjEDtkgFrcgdSxGhTnRJ5lIFDNS5ljyotcj6J8luVvC5gV58vO+AqhN9xTP3f88+Fn0EE5edX7WDqXQdxCGYtjrbGiKuSWXBe/b8NHdYP9t/snfMlR21OAun3Rw3yS/GRvZNPozdtGVPAgMTfy62rCbED6HS1EjNRaYzoL52Ges4uZVhFYxcmFFH4Ol8k8txVYwFihmt8caJHYGmK6m2ryfOkRKaWf0map5BevJYPrmd0WHtcAGmavuPXUYUoeXq14+Fo63lkq6z+YIBLTxTQChglia8sb7qp2Kc2NCs43DDRlmVCXmesedpj5HwrrywC5mGkl9D/Awp7NKcpO3n8kIcupeiRWI6v/Y+uBBebIEpVTBJhKC2klF3azUJRSm6/5i5YRIXQJ4KkzYSQyKeQIOQzCtNwifTtk/NBuPmnXAGS8SNaUToyr1SGCDvPYHtSC91pHYQ4gYKbR5la38xndj/d1id5xmN8fZBvn2G07p0VEPPSjKnzp1vvi3dr6m65acAUoGWVZGhkyy03wrUd8Jd2KqTByeVOTZ9jDTFzXdt00nJQaCJpN5gjaNuT77ESsg+Bot9j0clXvvOQvx1lQPe4EN12TXgCoUpgkEGKqy2cf5sbc5PJm4eYFKjJ8KobCJgpLMWhfmOpK1uB02BcZ9BQJLfAqb1IsZ65w0Z5MwuDI50eUFGUHccfdrgmS/Gf/BvK4nJOQFMeaOOEGEFp2TG4DU71Ft0uVO+9l58I3rkkaedejMu+fC0tTdK8GS8Dc4ASi2wqjrOkDXBoHAU3hSfdWxEXbm3k9CrI+a8UftYqguuOxg23288YSblc8V3ca+FNhOS1VkgteE+HGVLidzGoE+7dX2xqV2piW9ihEgLR3hoZU9Rl6G37oGKtTFn0HuWoK+idJpmobfiFqqdSObzQvFUcvqV5Rxa+R90AwUv9MqCNoAlYlQg7Hn6/l4zKqEXrmQsNr7QccVDTqlbBZpnk/PmS0OJ55RJ7Ow9tRGdbb6ePfq4XjMTx0knZDBdgPYZ0n9XSShR8RrN+nLA4yNoWkOeYoVIHEauHxVh+aDMOch1EhIViz4HW/CcSSd32dE3+NSfz8Uq8v/v1Bt2dkszf90cfxqRdJGV0=|u4eqyjxRrhrQf10L+quvC6rY5gRqVtZwg6YSLWgYv9Q="
);
apiService.postIdentityToken(Arg.any()).resolves(tokenResponse);
commonSetup();
const result = await authService.logInSso(ssoCode, ssoCodeVerifier, ssoRedirectUrl, ssoOrgId);
const tokenResponse = newTokenResponse();
tokenResponse.keyConnectorUrl = keyConnectorUrl;
tokenResponse.key = null;
commonSuccessAssertions();
keyConnectorService.received(1).getAndSetKey(keyConnectorUrl);
});
cryptoFunctionService
.randomBytes(Arg.any())
.resolves(Utils.fromB64ToArray("bNr5Ykzpv9lXJF26Cyz8iGpeGjs9si6MYkiC5iZzy4H7fWnnevSvBvLL"));
cryptoService.makeKey(Arg.any(), Arg.any(), kdf, kdfIterations).resolves(preloginKey);
cryptoService.makeEncKey(preloginKey).resolves(realEncKey);
cryptoService.makeKeyPair().resolves([pubKey, privKey]);
it('logInSso: new SSO user with Key Connector posts key to the server', async () => {
const realEncKey: [SymmetricCryptoKey, EncString] = [
new SymmetricCryptoKey(Utils.fromB64ToArray('T/tvXd/wvROlTu69qimTod1l7bnYltYOdx1es+xiiEI=')),
new EncString('2.j/dbf/fUThfQtOPLU5rbBQ==|u82vHRHBkNY4E44hNmv0BIJQx961Tgh7RJ/p2pGytnZckFn8jwu72GG6HMNhvG4+GGwoIEd4GgP+oOFBhqIHXh5niH4wT2kuuyeA22+0VZM=|r1q0wYATeXB4ejUY6CORagAtqwUT246sY+wp46Kj6pY='),
];
const pubKey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApqH10xpM+VyAfjLj1cwLm8ydOx/qZq4UcUji/lJ1+p0In1vs1BPeVa5MjaiCO8zrALfSckUfviFv74p7Eo0e4XwbJljnh5E2FdMLUL3oUjKiqCFZ9obdGzzGh7ImY1rnjAOEXjme/kbfWTeva54cv7SYBSoAK3IuNxJeQh1cICohMI/7VUCwlQgHITZ7dK0oQ/gIb/E7wX4TG5x0g6zjYjhWUjWjBaxkUodgF4d7KLd/d7OhM0EQSWA1oaR8nxEaAuAkB4AMNqQhzfZWNzdGJVYdr1j5cizVnDWPoTCk30kHc/Pffuo1hw7156XxLOiM46K/cgKEk9lHTf9s8bSouQIDAQAB';
const privKey = new EncString('2.NGK1ka8L+XG32nDAc4u36A==|XbdTogpmevDG2s55ZpJhwVizILBq+oXwIDFmZHXjCVOO2f6Iz1WiVvPvMfp/gqOIVZN770KUYpSiWvwgbuEWr9fKbqaqPUTs188rqMs8C3JWLgRpNeWvc2VNskl+A89lkb2BCHFsOXyxK/IaK85ORpY/PXobWlA1ese7DKqv4iEMjJvMKqPi5xTxLJ1uJGEwqNwOZN021I4RQxDgByYQCmjC2iCIR6rMuSwLRR2a9jwY+CLa/XslxIFkTe8dVzO5o2KXiYwz0UoN82tYxCHz9/8EQfaFI/CPFbE/OQtjN8mfF72i0JtRuYBH9y6yfPFUzYnZpZ7dHSYnF33+pUOALHR1WgJXaHz3VSRQR1yAuoF5ZARtWHn4buw0OXM9tnqcosCQ/BlP9ExN6httJGO6kxZpsbZs+DSCOzAWWxkaVwexE1QGZ+OrbJh8d0lzzlTW5QUukQL5y5cNoaBsF+U2qdb8a16/kxvPr56T9uhUoAfdV6mWyuhc/8Rl7sSMsrVZQ/YINrmjEDtkgFrcgdSxGhTnRJ5lIFDNS5ljyotcj6J8luVvC5gV58vO+AqhN9xTP3f88+Fn0EE5edX7WDqXQdxCGYtjrbGiKuSWXBe/b8NHdYP9t/snfMlR21OAun3Rw3yS/GRvZNPozdtGVPAgMTfy62rCbED6HS1EjNRaYzoL52Ges4uZVhFYxcmFFH4Ol8k8txVYwFihmt8caJHYGmK6m2ryfOkRKaWf0map5BevJYPrmd0WHtcAGmavuPXUYUoeXq14+Fo63lkq6z+YIBLTxTQChglia8sb7qp2Kc2NCs43DDRlmVCXmesedpj5HwrrywC5mGkl9D/Awp7NKcpO3n8kIcupeiRWI6v/Y+uBBebIEpVTBJhKC2klF3azUJRSm6/5i5YRIXQJ4KkzYSQyKeQIOQzCtNwifTtk/NBuPmnXAGS8SNaUToyr1SGCDvPYHtSC91pHYQ4gYKbR5la38xndj/d1id5xmN8fZBvn2G07p0VEPPSjKnzp1vvi3dr6m65acAUoGWVZGhkyy03wrUd8Jd2KqTByeVOTZ9jDTFzXdt00nJQaCJpN5gjaNuT77ESsg+Bot9j0clXvvOQvx1lQPe4EN12TXgCoUpgkEGKqy2cf5sbc5PJm4eYFKjJ8KobCJgpLMWhfmOpK1uB02BcZ9BQJLfAqb1IsZ65w0Z5MwuDI50eUFGUHccfdrgmS/Gf/BvK4nJOQFMeaOOEGEFp2TG4DU71Ft0uVO+9l58I3rkkaedejMu+fC0tTdK8GS8Dc4ASi2wqjrOkDXBoHAU3hSfdWxEXbm3k9CrI+a8UftYqguuOxg23288YSblc8V3ca+FNhOS1VkgteE+HGVLidzGoE+7dX2xqV2piW9ihEgLR3hoZU9Rl6G37oGKtTFn0HuWoK+idJpmobfiFqqdSObzQvFUcvqV5Rxa+R90AwUv9MqCNoAlYlQg7Hn6/l4zKqEXrmQsNr7QccVDTqlbBZpnk/PmS0OJ55RJ7Ow9tRGdbb6ePfq4XjMTx0knZDBdgPYZ0n9XSShR8RrN+nLA4yNoWkOeYoVIHEauHxVh+aDMOch1EhIViz4HW/CcSSd32dE3+NSfz8Uq8v/v1Bt2dkszf90cfxqRdJGV0=|u4eqyjxRrhrQf10L+quvC6rY5gRqVtZwg6YSLWgYv9Q=');
apiService.postIdentityToken(Arg.any()).resolves(tokenResponse);
commonSetup();
const result = await authService.logInSso(ssoCode, ssoCodeVerifier, ssoRedirectUrl, ssoOrgId);
const tokenResponse = newTokenResponse();
tokenResponse.keyConnectorUrl = keyConnectorUrl;
tokenResponse.key = null;
cryptoFunctionService.randomBytes(Arg.any()).resolves(Utils.fromB64ToArray('bNr5Ykzpv9lXJF26Cyz8iGpeGjs9si6MYkiC5iZzy4H7fWnnevSvBvLL'));
cryptoService.makeKey(Arg.any(), Arg.any(), kdf, kdfIterations).resolves(preloginKey);
cryptoService.makeEncKey(preloginKey).resolves(realEncKey);
cryptoService.makeKeyPair().resolves([pubKey, privKey]);
apiService.postIdentityToken(Arg.any()).resolves(tokenResponse);
const result = await authService.logInSso(ssoCode, ssoCodeVerifier, ssoRedirectUrl, ssoOrgId);
commonSuccessAssertions();
cryptoService.received(1).setKey(preloginKey);
cryptoService.received(1).setEncKey(Arg.any());
apiService.received(1).postUserKeyToKeyConnector(keyConnectorUrl, Arg.any());
apiService.received(1).postSetKeyConnectorKey(Arg.is(r =>
commonSuccessAssertions();
cryptoService.received(1).setKey(preloginKey);
cryptoService.received(1).setEncKey(Arg.any());
apiService.received(1).postUserKeyToKeyConnector(keyConnectorUrl, Arg.any());
apiService
.received(1)
.postSetKeyConnectorKey(
Arg.is(
(r) =>
r.kdf === kdf &&
r.kdfIterations === kdfIterations &&
r.key === realEncKey[1].encryptedString &&
r.orgIdentifier === ssoOrgId &&
r.keys.encryptedPrivateKey === privKey.encryptedString &&
r.keys.publicKey === pubKey));
r.keys.publicKey === pubKey
)
);
});
// API
it("logInApi: user can login with api key", async () => {
commonSetup();
tokenService.getTwoFactorToken(Arg.any()).resolves(null);
const tokenResponse = newTokenResponse();
apiService.postIdentityToken(Arg.any()).resolves(tokenResponse);
const result = await authService.logInApiKey(apiClientId, apiClientSecret);
apiService.received(1).postIdentityToken(
Arg.is((actual) => {
const apiTokenRequest = actual as ApiTokenRequest;
return (
apiTokenRequest.clientId === apiClientId &&
apiTokenRequest.clientSecret === apiClientSecret &&
actual.device.identifier === deviceId &&
actual.provider == null &&
actual.token == null &&
actual.captchaResponse == null
);
})
);
// Sets local environment:
stateService.received(1).addAccount({
profile: {
...new AccountProfile(),
...{
userId: userId,
email: email,
apiKeyClientId: apiClientId,
apiKeyClientSecret: apiClientSecret,
hasPremiumPersonally: false,
kdfIterations: kdfIterations,
kdfType: kdf,
},
},
tokens: {
...new AccountTokens(),
...{
accessToken: accessToken,
refreshToken: refreshToken,
},
},
});
stateService.received(1).setBiometricLocked(false);
messagingService.received(1).send("loggedIn");
// API
cryptoService.received(1).setEncKey(encKey);
cryptoService.received(1).setEncPrivateKey(privateKey);
it('logInApi: user can login with api key', async () => {
commonSetup();
tokenService.getTwoFactorToken(Arg.any()).resolves(null);
const tokenResponse = newTokenResponse();
apiService.postIdentityToken(Arg.any()).resolves(tokenResponse);
// Negative tests
apiService.didNotReceive().postAccountKeys(Arg.any()); // Did not generate new private key pair
keyConnectorService.didNotReceive().getAndSetKey(Arg.any()); // Did not fetch Key Connector key
apiService.didNotReceive().postUserKeyToKeyConnector(Arg.any(), Arg.any()); // Did not send key to KC
tokenService.didNotReceive().setTwoFactorToken(Arg.any(), Arg.any()); // Did not save 2FA token
const result = await authService.logInApiKey(apiClientId, apiClientSecret);
apiService.received(1).postIdentityToken(Arg.is(actual => {
const apiTokenRequest = actual as ApiTokenRequest;
return apiTokenRequest.clientId === apiClientId &&
apiTokenRequest.clientSecret === apiClientSecret &&
actual.device.identifier === deviceId &&
actual.provider == null &&
actual.token == null &&
actual.captchaResponse == null
}));
// Sets local environment:
stateService.received(1).addAccount({
profile: {
...new AccountProfile(),
...{
userId: userId,
email: email,
apiKeyClientId: apiClientId,
apiKeyClientSecret: apiClientSecret,
hasPremiumPersonally: false,
kdfIterations: kdfIterations,
kdfType: kdf,
},
},
tokens: {
...new AccountTokens(),
...{
accessToken: accessToken,
refreshToken: refreshToken,
},
},
});
stateService.received(1).setBiometricLocked(false);
messagingService.received(1).send('loggedIn');
cryptoService.received(1).setEncKey(encKey);
cryptoService.received(1).setEncPrivateKey(privateKey);
// Negative tests
apiService.didNotReceive().postAccountKeys(Arg.any()); // Did not generate new private key pair
keyConnectorService.didNotReceive().getAndSetKey(Arg.any()); // Did not fetch Key Connector key
apiService.didNotReceive().postUserKeyToKeyConnector(Arg.any(), Arg.any()); // Did not send key to KC
tokenService.didNotReceive().setTwoFactorToken(Arg.any(), Arg.any()); // Did not save 2FA token
// Return result:
const expected = newAuthResponse();
expect(result).toEqual(expected);
});
// Return result:
const expected = newAuthResponse();
expect(result).toEqual(expected);
});
});

View File

@@ -1,61 +1,71 @@
import { Arg, Substitute, SubstituteOf } from '@fluffy-spoon/substitute';
import { Arg, Substitute, SubstituteOf } from "@fluffy-spoon/substitute";
import { ApiService } from 'jslib-common/abstractions/api.service';
import { CryptoService } from 'jslib-common/abstractions/crypto.service';
import { FileUploadService } from 'jslib-common/abstractions/fileUpload.service';
import { I18nService } from 'jslib-common/abstractions/i18n.service';
import { LogService } from 'jslib-common/abstractions/log.service';
import { SearchService } from 'jslib-common/abstractions/search.service';
import { SettingsService } from 'jslib-common/abstractions/settings.service';
import { StateService } from 'jslib-common/abstractions/state.service';
import { ApiService } from "jslib-common/abstractions/api.service";
import { CryptoService } from "jslib-common/abstractions/crypto.service";
import { FileUploadService } from "jslib-common/abstractions/fileUpload.service";
import { I18nService } from "jslib-common/abstractions/i18n.service";
import { LogService } from "jslib-common/abstractions/log.service";
import { SearchService } from "jslib-common/abstractions/search.service";
import { SettingsService } from "jslib-common/abstractions/settings.service";
import { StateService } from "jslib-common/abstractions/state.service";
import { Utils } from 'jslib-common/misc/utils';
import { Cipher } from 'jslib-common/models/domain/cipher';
import { EncArrayBuffer } from 'jslib-common/models/domain/encArrayBuffer';
import { EncString } from 'jslib-common/models/domain/encString';
import { SymmetricCryptoKey } from 'jslib-common/models/domain/symmetricCryptoKey';
import { Utils } from "jslib-common/misc/utils";
import { Cipher } from "jslib-common/models/domain/cipher";
import { EncArrayBuffer } from "jslib-common/models/domain/encArrayBuffer";
import { EncString } from "jslib-common/models/domain/encString";
import { SymmetricCryptoKey } from "jslib-common/models/domain/symmetricCryptoKey";
import { CipherService } from 'jslib-common/services/cipher.service';
import { CipherService } from "jslib-common/services/cipher.service";
const ENCRYPTED_TEXT = 'This data has been encrypted';
const ENCRYPTED_TEXT = "This data has been encrypted";
const ENCRYPTED_BYTES = new EncArrayBuffer(Utils.fromUtf8ToArray(ENCRYPTED_TEXT).buffer);
describe('Cipher Service', () => {
let cryptoService: SubstituteOf<CryptoService>;
let stateService: SubstituteOf<StateService>;
let settingsService: SubstituteOf<SettingsService>;
let apiService: SubstituteOf<ApiService>;
let fileUploadService: SubstituteOf<FileUploadService>;
let i18nService: SubstituteOf<I18nService>;
let searchService: SubstituteOf<SearchService>;
let logService: SubstituteOf<LogService>;
describe("Cipher Service", () => {
let cryptoService: SubstituteOf<CryptoService>;
let stateService: SubstituteOf<StateService>;
let settingsService: SubstituteOf<SettingsService>;
let apiService: SubstituteOf<ApiService>;
let fileUploadService: SubstituteOf<FileUploadService>;
let i18nService: SubstituteOf<I18nService>;
let searchService: SubstituteOf<SearchService>;
let logService: SubstituteOf<LogService>;
let cipherService: CipherService;
let cipherService: CipherService;
beforeEach(() => {
cryptoService = Substitute.for<CryptoService>();
stateService = Substitute.for<StateService>();
settingsService = Substitute.for<SettingsService>();
apiService = Substitute.for<ApiService>();
fileUploadService = Substitute.for<FileUploadService>();
i18nService = Substitute.for<I18nService>();
searchService = Substitute.for<SearchService>();
logService = Substitute.for<LogService>();
beforeEach(() => {
cryptoService = Substitute.for<CryptoService>();
stateService = Substitute.for<StateService>();
settingsService = Substitute.for<SettingsService>();
apiService = Substitute.for<ApiService>();
fileUploadService = Substitute.for<FileUploadService>();
i18nService = Substitute.for<I18nService>();
searchService = Substitute.for<SearchService>();
logService = Substitute.for<LogService>();
cryptoService.encryptToBytes(Arg.any(), Arg.any()).resolves(ENCRYPTED_BYTES);
cryptoService.encrypt(Arg.any(), Arg.any()).resolves(new EncString(ENCRYPTED_TEXT));
cryptoService.encryptToBytes(Arg.any(), Arg.any()).resolves(ENCRYPTED_BYTES);
cryptoService.encrypt(Arg.any(), Arg.any()).resolves(new EncString(ENCRYPTED_TEXT));
cipherService = new CipherService(cryptoService, settingsService, apiService, fileUploadService,
i18nService, () => searchService, logService, stateService);
});
cipherService = new CipherService(
cryptoService,
settingsService,
apiService,
fileUploadService,
i18nService,
() => searchService,
logService,
stateService
);
});
it('attachments upload encrypted file contents', async () => {
const fileName = 'filename';
const fileData = new Uint8Array(10).buffer;
cryptoService.getOrgKey(Arg.any()).resolves(new SymmetricCryptoKey(new Uint8Array(32).buffer));
it("attachments upload encrypted file contents", async () => {
const fileName = "filename";
const fileData = new Uint8Array(10).buffer;
cryptoService.getOrgKey(Arg.any()).resolves(new SymmetricCryptoKey(new Uint8Array(32).buffer));
await cipherService.saveAttachmentRawWithServer(new Cipher(), fileName, fileData);
await cipherService.saveAttachmentRawWithServer(new Cipher(), fileName, fileData);
fileUploadService.received(1).uploadCipherAttachment(Arg.any(), Arg.any(), new EncString(ENCRYPTED_TEXT), ENCRYPTED_BYTES);
});
fileUploadService
.received(1)
.uploadCipherAttachment(Arg.any(), Arg.any(), new EncString(ENCRYPTED_TEXT), ENCRYPTED_BYTES);
});
});

View File

@@ -1,4 +1,4 @@
import { ConsoleLogService } from 'jslib-common/services/consoleLog.service';
import { ConsoleLogService } from "jslib-common/services/consoleLog.service";
const originalConsole = console;
let caughtMessage: any;
@@ -6,90 +6,97 @@ let caughtMessage: any;
declare var console: any;
export function interceptConsole(interceptions: any): object {
console = {
// tslint:disable-next-line
log: function () {
interceptions.log = arguments;
},
// tslint:disable-next-line
warn: function () {
interceptions.warn = arguments;
},
// tslint:disable-next-line
error: function () {
interceptions.error = arguments;
},
};
return interceptions;
console = {
// tslint:disable-next-line
log: function () {
interceptions.log = arguments;
},
// tslint:disable-next-line
warn: function () {
interceptions.warn = arguments;
},
// tslint:disable-next-line
error: function () {
interceptions.error = arguments;
},
};
return interceptions;
}
export function restoreConsole() {
console = originalConsole;
console = originalConsole;
}
describe('ConsoleLogService', () => {
let logService: ConsoleLogService;
beforeEach(() => {
caughtMessage = {};
interceptConsole(caughtMessage);
logService = new ConsoleLogService(true);
describe("ConsoleLogService", () => {
let logService: ConsoleLogService;
beforeEach(() => {
caughtMessage = {};
interceptConsole(caughtMessage);
logService = new ConsoleLogService(true);
});
afterAll(() => {
restoreConsole();
});
it("filters messages below the set threshold", () => {
logService = new ConsoleLogService(true, (level) => true);
logService.debug("debug");
logService.info("info");
logService.warning("warning");
logService.error("error");
expect(caughtMessage).toEqual({});
});
it("only writes debug messages in dev mode", () => {
logService = new ConsoleLogService(false);
logService.debug("debug message");
expect(caughtMessage.log).toBeUndefined();
});
it("writes debug/info messages to console.log", () => {
logService.debug("this is a debug message");
expect(caughtMessage).toEqual({
log: jasmine.arrayWithExactContents(["this is a debug message"]),
});
afterAll(() => {
restoreConsole();
logService.info("this is an info message");
expect(caughtMessage).toEqual({
log: jasmine.arrayWithExactContents(["this is an info message"]),
});
it('filters messages below the set threshold', () => {
logService = new ConsoleLogService(true, level => true);
logService.debug('debug');
logService.info('info');
logService.warning('warning');
logService.error('error');
expect(caughtMessage).toEqual({});
});
it("writes warning messages to console.warn", () => {
logService.warning("this is a warning message");
expect(caughtMessage).toEqual({
warn: jasmine.arrayWithExactContents(["this is a warning message"]),
});
it('only writes debug messages in dev mode', () => {
logService = new ConsoleLogService(false);
logService.debug('debug message');
expect(caughtMessage.log).toBeUndefined();
});
it("writes error messages to console.error", () => {
logService.error("this is an error message");
expect(caughtMessage).toEqual({
error: jasmine.arrayWithExactContents(["this is an error message"]),
});
});
it("times with output to info", async () => {
logService.time();
await new Promise((r) => setTimeout(r, 250));
const duration = logService.timeEnd();
expect(duration[0]).toBe(0);
expect(duration[1]).toBeGreaterThan(0);
expect(duration[1]).toBeLessThan(500 * 10e6);
it('writes debug/info messages to console.log', () => {
logService.debug('this is a debug message');
expect(caughtMessage).toEqual({ log: jasmine.arrayWithExactContents(['this is a debug message']) });
expect(caughtMessage).toEqual(jasmine.arrayContaining([]));
expect(caughtMessage.log.length).toBe(1);
expect(caughtMessage.log[0]).toEqual(jasmine.stringMatching(/^default: \d+\.?\d*ms$/));
});
logService.info('this is an info message');
expect(caughtMessage).toEqual({ log: jasmine.arrayWithExactContents(['this is an info message']) });
});
it('writes warning messages to console.warn', () => {
logService.warning('this is a warning message');
expect(caughtMessage).toEqual({ warn: jasmine.arrayWithExactContents(['this is a warning message']) });
});
it('writes error messages to console.error', () => {
logService.error('this is an error message');
expect(caughtMessage).toEqual({ error: jasmine.arrayWithExactContents(['this is an error message']) });
});
it("filters time output", async () => {
logService = new ConsoleLogService(true, (level) => true);
logService.time();
logService.timeEnd();
it('times with output to info', async () => {
logService.time();
await new Promise(r => setTimeout(r, 250));
const duration = logService.timeEnd();
expect(duration[0]).toBe(0);
expect(duration[1]).toBeGreaterThan(0);
expect(duration[1]).toBeLessThan(500 * 10e6);
expect(caughtMessage).toEqual(jasmine.arrayContaining([]));
expect(caughtMessage.log.length).toBe(1);
expect(caughtMessage.log[0]).toEqual(jasmine.stringMatching(/^default: \d+\.?\d*ms$/));
});
it('filters time output', async () => {
logService = new ConsoleLogService(true, level => true);
logService.time();
logService.timeEnd();
expect(caughtMessage).toEqual({});
});
expect(caughtMessage).toEqual({});
});
});

View File

@@ -1,123 +1,135 @@
import { Substitute, SubstituteOf } from '@fluffy-spoon/substitute';
import { Substitute, SubstituteOf } from "@fluffy-spoon/substitute";
import { ApiService } from 'jslib-common/abstractions/api.service';
import { CipherService } from 'jslib-common/abstractions/cipher.service';
import { CryptoService } from 'jslib-common/abstractions/crypto.service';
import { FolderService } from 'jslib-common/abstractions/folder.service';
import { ApiService } from "jslib-common/abstractions/api.service";
import { CipherService } from "jslib-common/abstractions/cipher.service";
import { CryptoService } from "jslib-common/abstractions/crypto.service";
import { FolderService } from "jslib-common/abstractions/folder.service";
import { ExportService } from 'jslib-common/services/export.service';
import { ExportService } from "jslib-common/services/export.service";
import { Cipher } from 'jslib-common/models/domain/cipher';
import { EncString } from 'jslib-common/models/domain/encString';
import { Login } from 'jslib-common/models/domain/login';
import { CipherWithIds as CipherExport } from 'jslib-common/models/export/cipherWithIds';
import { Cipher } from "jslib-common/models/domain/cipher";
import { EncString } from "jslib-common/models/domain/encString";
import { Login } from "jslib-common/models/domain/login";
import { CipherWithIds as CipherExport } from "jslib-common/models/export/cipherWithIds";
import { CipherType } from 'jslib-common/enums/cipherType';
import { CipherView } from 'jslib-common/models/view/cipherView';
import { LoginView } from 'jslib-common/models/view/loginView';
import { CipherType } from "jslib-common/enums/cipherType";
import { CipherView } from "jslib-common/models/view/cipherView";
import { LoginView } from "jslib-common/models/view/loginView";
import { BuildTestObject, GetUniqueString } from '../../utils';
import { BuildTestObject, GetUniqueString } from "../../utils";
const UserCipherViews = [
generateCipherView(false),
generateCipherView(false),
generateCipherView(true),
generateCipherView(false),
generateCipherView(false),
generateCipherView(true),
];
const UserCipherDomains = [
generateCipherDomain(false),
generateCipherDomain(false),
generateCipherDomain(true),
generateCipherDomain(false),
generateCipherDomain(false),
generateCipherDomain(true),
];
function generateCipherView(deleted: boolean) {
return BuildTestObject({
id: GetUniqueString('id'),
notes: GetUniqueString('notes'),
type: CipherType.Login,
login: BuildTestObject<LoginView>({
username: GetUniqueString('username'),
password: GetUniqueString('password'),
}, LoginView),
collectionIds: null,
deletedDate: deleted ? new Date() : null,
}, CipherView);
return BuildTestObject(
{
id: GetUniqueString("id"),
notes: GetUniqueString("notes"),
type: CipherType.Login,
login: BuildTestObject<LoginView>(
{
username: GetUniqueString("username"),
password: GetUniqueString("password"),
},
LoginView
),
collectionIds: null,
deletedDate: deleted ? new Date() : null,
},
CipherView
);
}
function generateCipherDomain(deleted: boolean) {
return BuildTestObject({
id: GetUniqueString('id'),
notes: new EncString(GetUniqueString('notes')),
type: CipherType.Login,
login: BuildTestObject<Login>({
username: new EncString(GetUniqueString('username')),
password: new EncString(GetUniqueString('password')),
}, Login),
collectionIds: null,
deletedDate: deleted ? new Date() : null,
}, Cipher);
return BuildTestObject(
{
id: GetUniqueString("id"),
notes: new EncString(GetUniqueString("notes")),
type: CipherType.Login,
login: BuildTestObject<Login>(
{
username: new EncString(GetUniqueString("username")),
password: new EncString(GetUniqueString("password")),
},
Login
),
collectionIds: null,
deletedDate: deleted ? new Date() : null,
},
Cipher
);
}
function expectEqualCiphers(ciphers: CipherView[] | Cipher[], jsonResult: string) {
const actual = JSON.stringify(JSON.parse(jsonResult).items);
const items: CipherExport[] = [];
ciphers.forEach((c: CipherView | Cipher) => {
const item = new CipherExport();
item.build(c);
items.push(item);
});
const actual = JSON.stringify(JSON.parse(jsonResult).items);
const items: CipherExport[] = [];
ciphers.forEach((c: CipherView | Cipher) => {
const item = new CipherExport();
item.build(c);
items.push(item);
});
expect(actual).toEqual(JSON.stringify(items));
expect(actual).toEqual(JSON.stringify(items));
}
describe('ExportService', () => {
let exportService: ExportService;
let apiService: SubstituteOf<ApiService>;
let cipherService: SubstituteOf<CipherService>;
let folderService: SubstituteOf<FolderService>;
let cryptoService: SubstituteOf<CryptoService>;
describe("ExportService", () => {
let exportService: ExportService;
let apiService: SubstituteOf<ApiService>;
let cipherService: SubstituteOf<CipherService>;
let folderService: SubstituteOf<FolderService>;
let cryptoService: SubstituteOf<CryptoService>;
beforeEach(() => {
apiService = Substitute.for<ApiService>();
cipherService = Substitute.for<CipherService>();
folderService = Substitute.for<FolderService>();
cryptoService = Substitute.for<CryptoService>();
beforeEach(() => {
apiService = Substitute.for<ApiService>();
cipherService = Substitute.for<CipherService>();
folderService = Substitute.for<FolderService>();
cryptoService = Substitute.for<CryptoService>();
folderService.getAllDecrypted().resolves([]);
folderService.getAll().resolves([]);
folderService.getAllDecrypted().resolves([]);
folderService.getAll().resolves([]);
exportService = new ExportService(folderService, cipherService, apiService, cryptoService);
});
exportService = new ExportService(folderService, cipherService, apiService, cryptoService);
});
it('exports unecrypted user ciphers', async () => {
cipherService.getAllDecrypted().resolves(UserCipherViews.slice(0, 1));
it("exports unecrypted user ciphers", async () => {
cipherService.getAllDecrypted().resolves(UserCipherViews.slice(0, 1));
const actual = await exportService.getExport('json');
const actual = await exportService.getExport("json");
expectEqualCiphers(UserCipherViews.slice(0, 1), actual);
});
expectEqualCiphers(UserCipherViews.slice(0, 1), actual);
});
it('exports encrypted json user ciphers', async () => {
cipherService.getAll().resolves(UserCipherDomains.slice(0, 1));
it("exports encrypted json user ciphers", async () => {
cipherService.getAll().resolves(UserCipherDomains.slice(0, 1));
const actual = await exportService.getExport('encrypted_json');
const actual = await exportService.getExport("encrypted_json");
expectEqualCiphers(UserCipherDomains.slice(0, 1), actual);
});
expectEqualCiphers(UserCipherDomains.slice(0, 1), actual);
});
it('does not unecrypted export trashed user items', async () => {
cipherService.getAllDecrypted().resolves(UserCipherViews);
it("does not unecrypted export trashed user items", async () => {
cipherService.getAllDecrypted().resolves(UserCipherViews);
const actual = await exportService.getExport('json');
const actual = await exportService.getExport("json");
expectEqualCiphers(UserCipherViews.slice(0, 2), actual);
});
expectEqualCiphers(UserCipherViews.slice(0, 2), actual);
});
it('does not encrypted export trashed user items', async () => {
cipherService.getAll().resolves(UserCipherDomains);
it("does not encrypted export trashed user items", async () => {
cipherService.getAll().resolves(UserCipherDomains);
const actual = await exportService.getExport('encrypted_json');
const actual = await exportService.getExport("encrypted_json");
expectEqualCiphers(UserCipherDomains.slice(0, 2), actual);
});
expectEqualCiphers(UserCipherDomains.slice(0, 2), actual);
});
});

View File

@@ -1,9 +1,9 @@
import { ElectronLogService } from 'jslib-electron/services/electronLog.service';
import { ElectronLogService } from "jslib-electron/services/electronLog.service";
describe('ElectronLogService', () => {
it('sets dev based on electron method', () => {
process.env.ELECTRON_IS_DEV = '1';
const logService = new ElectronLogService();
expect(logService).toEqual(jasmine.objectContaining({ isDev: true }) as any);
});
describe("ElectronLogService", () => {
it("sets dev based on electron method", () => {
process.env.ELECTRON_IS_DEV = "1";
const logService = new ElectronLogService();
expect(logService).toEqual(jasmine.objectContaining({ isDev: true }) as any);
});
});

View File

@@ -1,27 +1,27 @@
import { cleanUserAgent } from 'jslib-electron/utils';
import { cleanUserAgent } from "jslib-electron/utils";
const expectedUserAgent = `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${process.versions.chrome} Safari/537.36`;
describe('cleanUserAgent', () => {
it('cleans mac agent', () => {
const initialMacAgent = `Mozilla/5.0 (Macintosh; Intel Mac OS X 11_6_0) AppleWebKit/537.36 (KHTML, like Gecko) Bitwarden/${process.version} Chrome/${process.versions.chrome} Electron/${process.versions.electron} Safari/537.36`;
expect(cleanUserAgent(initialMacAgent)).toEqual(expectedUserAgent);
});
describe("cleanUserAgent", () => {
it("cleans mac agent", () => {
const initialMacAgent = `Mozilla/5.0 (Macintosh; Intel Mac OS X 11_6_0) AppleWebKit/537.36 (KHTML, like Gecko) Bitwarden/${process.version} Chrome/${process.versions.chrome} Electron/${process.versions.electron} Safari/537.36`;
expect(cleanUserAgent(initialMacAgent)).toEqual(expectedUserAgent);
});
it('cleans windows agent', () => {
const initialWindowsAgent = `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Bitwarden/${process.version} Chrome/${process.versions.chrome} Electron/${process.versions.electron} Safari/537.36`;
expect(cleanUserAgent(initialWindowsAgent)).toEqual(expectedUserAgent);
});
it("cleans windows agent", () => {
const initialWindowsAgent = `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Bitwarden/${process.version} Chrome/${process.versions.chrome} Electron/${process.versions.electron} Safari/537.36`;
expect(cleanUserAgent(initialWindowsAgent)).toEqual(expectedUserAgent);
});
it('cleans linux agent', () => {
const initialWindowsAgent = `Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Bitwarden/${process.version} Chrome/${process.versions.chrome} Electron/${process.versions.electron} Safari/537.36`;
expect(cleanUserAgent(initialWindowsAgent)).toEqual(expectedUserAgent);
});
it("cleans linux agent", () => {
const initialWindowsAgent = `Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Bitwarden/${process.version} Chrome/${process.versions.chrome} Electron/${process.versions.electron} Safari/537.36`;
expect(cleanUserAgent(initialWindowsAgent)).toEqual(expectedUserAgent);
});
it('does not change version numbers', () => {
const expected = `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36`;
const initialAgent = `Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Bitwarden/1.28.3 Chrome/87.0.4280.141 Electron/11.4.5 Safari/537.36`;
it("does not change version numbers", () => {
const expected = `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36`;
const initialAgent = `Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Bitwarden/1.28.3 Chrome/87.0.4280.141 Electron/11.4.5 Safari/537.36`;
expect(cleanUserAgent(initialAgent)).toEqual(expected);
});
expect(cleanUserAgent(initialAgent)).toEqual(expected);
});
});

View File

@@ -1,9 +1,9 @@
// tslint:disable-next-line
const TSConsoleReporter = require('jasmine-ts-console-reporter');
const TSConsoleReporter = require("jasmine-ts-console-reporter");
jasmine.getEnv().clearReporters(); // Clear default console reporter
jasmine.getEnv().addReporter(new TSConsoleReporter());
// Polyfills
// tslint:disable-next-line
const jsdom: any = require('jsdom');
const jsdom: any = require("jsdom");
(global as any).DOMParser = new jsdom.JSDOM().window.DOMParser;

View File

@@ -1,41 +1,42 @@
import { ConsoleLogService } from 'jslib-node/cli/services/consoleLog.service';
import { interceptConsole, restoreConsole } from '../../common/services/consoleLog.service.spec';
import { ConsoleLogService } from "jslib-node/cli/services/consoleLog.service";
import { interceptConsole, restoreConsole } from "../../common/services/consoleLog.service.spec";
const originalConsole = console;
let caughtMessage: any = {};
describe('CLI Console log service', () => {
let logService: ConsoleLogService;
beforeEach(() => {
caughtMessage = {};
interceptConsole(caughtMessage);
logService = new ConsoleLogService(true);
describe("CLI Console log service", () => {
let logService: ConsoleLogService;
beforeEach(() => {
caughtMessage = {};
interceptConsole(caughtMessage);
logService = new ConsoleLogService(true);
});
afterAll(() => {
restoreConsole();
});
it("should redirect all console to error if BW_RESPONSE env is true", () => {
process.env.BW_RESPONSE = "true";
logService.debug("this is a debug message");
expect(caughtMessage).toEqual({
error: jasmine.arrayWithExactContents(["this is a debug message"]),
});
});
afterAll(() => {
restoreConsole();
});
it('should redirect all console to error if BW_RESPONSE env is true', () => {
process.env.BW_RESPONSE = 'true';
logService.debug('this is a debug message');
expect(caughtMessage).toEqual({ error: jasmine.arrayWithExactContents(['this is a debug message']) });
});
it('should not redirect console to error if BW_RESPONSE != true', () => {
process.env.BW_RESPONSE = 'false';
logService.debug('debug');
logService.info('info');
logService.warning('warning');
logService.error('error');
expect(caughtMessage).toEqual({
log: jasmine.arrayWithExactContents(['info']),
warn: jasmine.arrayWithExactContents(['warning']),
error: jasmine.arrayWithExactContents(['error']),
});
it("should not redirect console to error if BW_RESPONSE != true", () => {
process.env.BW_RESPONSE = "false";
logService.debug("debug");
logService.info("info");
logService.warning("warning");
logService.error("error");
expect(caughtMessage).toEqual({
log: jasmine.arrayWithExactContents(["info"]),
warn: jasmine.arrayWithExactContents(["warning"]),
error: jasmine.arrayWithExactContents(["error"]),
});
});
});

View File

@@ -1,424 +1,519 @@
import { NodeCryptoFunctionService } from 'jslib-node/services/nodeCryptoFunction.service';
import { NodeCryptoFunctionService } from "jslib-node/services/nodeCryptoFunction.service";
import { Utils } from 'jslib-common/misc/utils';
import { SymmetricCryptoKey } from 'jslib-common/models/domain/symmetricCryptoKey';
import { Utils } from "jslib-common/misc/utils";
import { SymmetricCryptoKey } from "jslib-common/models/domain/symmetricCryptoKey";
const RsaPublicKey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAl0Vawl/toXzkEvB82FEtqHP' +
'4xlU2ab/v0crqIfXfIoWF/XXdHGIdrZeilnRXPPJT1B9dTsasttEZNnua/0Rek/cjNDHtzT52irfoZYS7X6HNIfOi54Q+egP' +
'RQ1H7iNHVZz3K8Db9GCSKPeC8MbW6gVCzb15esCe1gGzg6wkMuWYDFYPoh/oBqcIqrGah7firqB1nDedzEjw32heP2DAffVN' +
'084iTDjiWrJNUxBJ2pDD5Z9dT3MzQ2s09ew1yMWK2z37rT3YerC7OgEDmo3WYo3xL3qYJznu3EO2nmrYjiRa40wKSjxsTlUc' +
'xDF+F0uMW8oR9EMUHgepdepfAtLsSAQIDAQAB';
const RsaPrivateKey = 'MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCXRVrCX+2hfOQS8Hz' +
'YUS2oc/jGVTZpv+/Ryuoh9d8ihYX9dd0cYh2tl6KWdFc88lPUH11Oxqy20Rk2e5r/RF6T9yM0Me3NPnaKt+hlhLtfoc0h86L' +
'nhD56A9FDUfuI0dVnPcrwNv0YJIo94LwxtbqBULNvXl6wJ7WAbODrCQy5ZgMVg+iH+gGpwiqsZqHt+KuoHWcN53MSPDfaF4/' +
'YMB99U3TziJMOOJask1TEEnakMPln11PczNDazT17DXIxYrbPfutPdh6sLs6AQOajdZijfEvepgnOe7cQ7aeatiOJFrjTApK' +
'PGxOVRzEMX4XS4xbyhH0QxQeB6l16l8C0uxIBAgMBAAECggEASaWfeVDA3cVzOPFSpvJm20OTE+R6uGOU+7vh36TX/POq92q' +
'Buwbd0h0oMD32FxsXywd2IxtBDUSiFM9699qufTVuM0Q3tZw6lHDTOVG08+tPdr8qSbMtw7PGFxN79fHLBxejjO4IrM9lapj' +
'WpxEF+11x7r+wM+0xRZQ8sNFYG46aPfIaty4BGbL0I2DQ2y8I57iBCAy69eht59NLMm27fRWGJIWCuBIjlpfzET1j2HLXUIh' +
'5bTBNzqaN039WH49HczGE3mQKVEJZc/efk3HaVd0a1Sjzyn0QY+N1jtZN3jTRbuDWA1AknkX1LX/0tUhuS3/7C3ejHxjw4Dk' +
'1ZLo5/QKBgQDIWvqFn0+IKRSu6Ua2hDsufIHHUNLelbfLUMmFthxabcUn4zlvIscJO00Tq/ezopSRRvbGiqnxjv/mYxucvOU' +
'BeZtlus0Q9RTACBtw9TGoNTmQbEunJ2FOSlqbQxkBBAjgGEppRPt30iGj/VjAhCATq2MYOa/X4dVR51BqQAFIEwKBgQDBSIf' +
'TFKC/hDk6FKZlgwvupWYJyU9RkyfstPErZFmzoKhPkQ3YORo2oeAYmVUbS9I2iIYpYpYQJHX8jMuCbCz4ONxTCuSIXYQYUcU' +
'q4PglCKp31xBAE6TN8SvhfME9/MvuDssnQinAHuF0GDAhF646T3LLS1not6Vszv7brwSoGwKBgQC88v/8cGfi80ssQZeMnVv' +
'q1UTXIeQcQnoY5lGHJl3K8mbS3TnXE6c9j417Fdz+rj8KWzBzwWXQB5pSPflWcdZO886Xu/mVGmy9RWgLuVFhXwCwsVEPjNX' +
'5ramRb0/vY0yzenUCninBsIxFSbIfrPtLUYCc4hpxr+sr2Mg/y6jpvQKBgBezMRRs3xkcuXepuI2R+BCXL1/b02IJTUf1F+1' +
'eLLGd7YV0H+J3fgNc7gGWK51hOrF9JBZHBGeOUPlaukmPwiPdtQZpu4QNE3l37VlIpKTF30E6mb+BqR+nht3rUjarnMXgAoE' +
'Z18y6/KIjpSMpqC92Nnk/EBM9EYe6Cf4eA9ApAoGAeqEUg46UTlJySkBKURGpIs3v1kkf5I0X8DnOhwb+HPxNaiEdmO7ckm8' +
'+tPVgppLcG0+tMdLjigFQiDUQk2y3WjyxP5ZvXu7U96jaJRI8PFMoE06WeVYcdIzrID2HvqH+w0UQJFrLJ/0Mn4stFAEzXKZ' +
'BokBGnjFnTnKcs7nv/O8=';
const RsaPublicKey =
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAl0Vawl/toXzkEvB82FEtqHP" +
"4xlU2ab/v0crqIfXfIoWF/XXdHGIdrZeilnRXPPJT1B9dTsasttEZNnua/0Rek/cjNDHtzT52irfoZYS7X6HNIfOi54Q+egP" +
"RQ1H7iNHVZz3K8Db9GCSKPeC8MbW6gVCzb15esCe1gGzg6wkMuWYDFYPoh/oBqcIqrGah7firqB1nDedzEjw32heP2DAffVN" +
"084iTDjiWrJNUxBJ2pDD5Z9dT3MzQ2s09ew1yMWK2z37rT3YerC7OgEDmo3WYo3xL3qYJznu3EO2nmrYjiRa40wKSjxsTlUc" +
"xDF+F0uMW8oR9EMUHgepdepfAtLsSAQIDAQAB";
const RsaPrivateKey =
"MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCXRVrCX+2hfOQS8Hz" +
"YUS2oc/jGVTZpv+/Ryuoh9d8ihYX9dd0cYh2tl6KWdFc88lPUH11Oxqy20Rk2e5r/RF6T9yM0Me3NPnaKt+hlhLtfoc0h86L" +
"nhD56A9FDUfuI0dVnPcrwNv0YJIo94LwxtbqBULNvXl6wJ7WAbODrCQy5ZgMVg+iH+gGpwiqsZqHt+KuoHWcN53MSPDfaF4/" +
"YMB99U3TziJMOOJask1TEEnakMPln11PczNDazT17DXIxYrbPfutPdh6sLs6AQOajdZijfEvepgnOe7cQ7aeatiOJFrjTApK" +
"PGxOVRzEMX4XS4xbyhH0QxQeB6l16l8C0uxIBAgMBAAECggEASaWfeVDA3cVzOPFSpvJm20OTE+R6uGOU+7vh36TX/POq92q" +
"Buwbd0h0oMD32FxsXywd2IxtBDUSiFM9699qufTVuM0Q3tZw6lHDTOVG08+tPdr8qSbMtw7PGFxN79fHLBxejjO4IrM9lapj" +
"WpxEF+11x7r+wM+0xRZQ8sNFYG46aPfIaty4BGbL0I2DQ2y8I57iBCAy69eht59NLMm27fRWGJIWCuBIjlpfzET1j2HLXUIh" +
"5bTBNzqaN039WH49HczGE3mQKVEJZc/efk3HaVd0a1Sjzyn0QY+N1jtZN3jTRbuDWA1AknkX1LX/0tUhuS3/7C3ejHxjw4Dk" +
"1ZLo5/QKBgQDIWvqFn0+IKRSu6Ua2hDsufIHHUNLelbfLUMmFthxabcUn4zlvIscJO00Tq/ezopSRRvbGiqnxjv/mYxucvOU" +
"BeZtlus0Q9RTACBtw9TGoNTmQbEunJ2FOSlqbQxkBBAjgGEppRPt30iGj/VjAhCATq2MYOa/X4dVR51BqQAFIEwKBgQDBSIf" +
"TFKC/hDk6FKZlgwvupWYJyU9RkyfstPErZFmzoKhPkQ3YORo2oeAYmVUbS9I2iIYpYpYQJHX8jMuCbCz4ONxTCuSIXYQYUcU" +
"q4PglCKp31xBAE6TN8SvhfME9/MvuDssnQinAHuF0GDAhF646T3LLS1not6Vszv7brwSoGwKBgQC88v/8cGfi80ssQZeMnVv" +
"q1UTXIeQcQnoY5lGHJl3K8mbS3TnXE6c9j417Fdz+rj8KWzBzwWXQB5pSPflWcdZO886Xu/mVGmy9RWgLuVFhXwCwsVEPjNX" +
"5ramRb0/vY0yzenUCninBsIxFSbIfrPtLUYCc4hpxr+sr2Mg/y6jpvQKBgBezMRRs3xkcuXepuI2R+BCXL1/b02IJTUf1F+1" +
"eLLGd7YV0H+J3fgNc7gGWK51hOrF9JBZHBGeOUPlaukmPwiPdtQZpu4QNE3l37VlIpKTF30E6mb+BqR+nht3rUjarnMXgAoE" +
"Z18y6/KIjpSMpqC92Nnk/EBM9EYe6Cf4eA9ApAoGAeqEUg46UTlJySkBKURGpIs3v1kkf5I0X8DnOhwb+HPxNaiEdmO7ckm8" +
"+tPVgppLcG0+tMdLjigFQiDUQk2y3WjyxP5ZvXu7U96jaJRI8PFMoE06WeVYcdIzrID2HvqH+w0UQJFrLJ/0Mn4stFAEzXKZ" +
"BokBGnjFnTnKcs7nv/O8=";
const Sha1Mac = '4d4c223f95dc577b665ec4ccbcb680b80a397038';
const Sha256Mac = '6be3caa84922e12aaaaa2f16c40d44433bb081ef323db584eb616333ab4e874f';
const Sha512Mac = '21910e341fa12106ca35758a2285374509326c9fbe0bd64e7b99c898f841dc948c58ce66d3504d8883c' +
'5ea7817a0b7c5d4d9b00364ccd214669131fc17fe4aca';
const Sha1Mac = "4d4c223f95dc577b665ec4ccbcb680b80a397038";
const Sha256Mac = "6be3caa84922e12aaaaa2f16c40d44433bb081ef323db584eb616333ab4e874f";
const Sha512Mac =
"21910e341fa12106ca35758a2285374509326c9fbe0bd64e7b99c898f841dc948c58ce66d3504d8883c" +
"5ea7817a0b7c5d4d9b00364ccd214669131fc17fe4aca";
describe('NodeCrypto Function Service', () => {
describe('pbkdf2', () => {
const regular256Key = 'pj9prw/OHPleXI6bRdmlaD+saJS4awrMiQsQiDjeu2I=';
const utf8256Key = 'yqvoFXgMRmHR3QPYr5pyR4uVuoHkltv9aHUP63p8n7I=';
const unicode256Key = 'ZdeOata6xoRpB4DLp8zHhXz5kLmkWtX5pd+TdRH8w8w=';
describe("NodeCrypto Function Service", () => {
describe("pbkdf2", () => {
const regular256Key = "pj9prw/OHPleXI6bRdmlaD+saJS4awrMiQsQiDjeu2I=";
const utf8256Key = "yqvoFXgMRmHR3QPYr5pyR4uVuoHkltv9aHUP63p8n7I=";
const unicode256Key = "ZdeOata6xoRpB4DLp8zHhXz5kLmkWtX5pd+TdRH8w8w=";
const regular512Key = 'liTi/Ke8LPU1Qv+Vl7NGEVt/XMbsBVJ2kQxtVG/Z1/JFHFKQW3ZkI81qVlwTiCpb+cFXzs+57' +
'eyhhx5wfKo5Cg==';
const utf8512Key = 'df0KdvIBeCzD/kyXptwQohaqUa4e7IyFUyhFQjXCANu5T+scq55hCcE4dG4T/MhAk2exw8j7ixRN' +
'zXANiVZpnw==';
const unicode512Key = 'FE+AnUJaxv8jh+zUDtZz4mjjcYk0/PZDZm+SLJe3XtxtnpdqqpblX6JjuMZt/dYYNMOrb2+mD' +
'L3FiQDTROh1lg==';
const regular512Key =
"liTi/Ke8LPU1Qv+Vl7NGEVt/XMbsBVJ2kQxtVG/Z1/JFHFKQW3ZkI81qVlwTiCpb+cFXzs+57" +
"eyhhx5wfKo5Cg==";
const utf8512Key =
"df0KdvIBeCzD/kyXptwQohaqUa4e7IyFUyhFQjXCANu5T+scq55hCcE4dG4T/MhAk2exw8j7ixRN" +
"zXANiVZpnw==";
const unicode512Key =
"FE+AnUJaxv8jh+zUDtZz4mjjcYk0/PZDZm+SLJe3XtxtnpdqqpblX6JjuMZt/dYYNMOrb2+mD" +
"L3FiQDTROh1lg==";
testPbkdf2('sha256', regular256Key, utf8256Key, unicode256Key);
testPbkdf2('sha512', regular512Key, utf8512Key, unicode512Key);
testPbkdf2("sha256", regular256Key, utf8256Key, unicode256Key);
testPbkdf2("sha512", regular512Key, utf8512Key, unicode512Key);
});
describe("hkdf", () => {
const regular256Key = "qBUmEYtwTwwGPuw/z6bs/qYXXYNUlocFlyAuuANI8Pw=";
const utf8256Key = "6DfJwW1R3txgiZKkIFTvVAb7qVlG7lKcmJGJoxR2GBU=";
const unicode256Key = "gejGI82xthA+nKtKmIh82kjw+ttHr+ODsUoGdu5sf0A=";
const regular512Key = "xe5cIG6ZfwGmb1FvsOedM0XKOm21myZkjL/eDeKIqqM=";
const utf8512Key = "XQMVBnxVEhlvjSFDQc77j5GDE9aorvbS0vKnjhRg0LY=";
const unicode512Key = "148GImrTbrjaGAe/iWEpclINM8Ehhko+9lB14+52lqc=";
testHkdf("sha256", regular256Key, utf8256Key, unicode256Key);
testHkdf("sha512", regular512Key, utf8512Key, unicode512Key);
});
describe("hkdfExpand", () => {
const prk16Byte = "criAmKtfzxanbgea5/kelQ==";
const prk32Byte = "F5h4KdYQnIVH4rKH0P9CZb1GrR4n16/sJrS0PsQEn0Y=";
const prk64Byte =
"ssBK0mRG17VHdtsgt8yo4v25CRNpauH+0r2fwY/E9rLyaFBAOMbIeTry+" +
"gUJ28p8y+hFh3EI9pcrEWaNvFYonQ==";
testHkdfExpand("sha256", prk32Byte, 32, "BnIqJlfnHm0e/2iB/15cbHyR19ARPIcWRp4oNS22CD8=");
testHkdfExpand(
"sha256",
prk32Byte,
64,
"BnIqJlfnHm0e/2iB/15cbHyR19ARPIcWRp4oNS22CD9BV+" +
"/queOZenPNkDhmlVyL2WZ3OSU5+7ISNF5NhNfvZA=="
);
testHkdfExpand("sha512", prk64Byte, 32, "uLWbMWodSBms5uGJ5WTRTesyW+MD7nlpCZvagvIRXlk=");
testHkdfExpand(
"sha512",
prk64Byte,
64,
"uLWbMWodSBms5uGJ5WTRTesyW+MD7nlpCZvagvIRXlkY5Pv0sB+" +
"MqvaopmkC6sD/j89zDwTV9Ib2fpucUydO8w=="
);
it("should fail with prk too small", async () => {
const cryptoFunctionService = new NodeCryptoFunctionService();
const f = cryptoFunctionService.hkdfExpand(
Utils.fromB64ToArray(prk16Byte),
"info",
32,
"sha256"
);
await expectAsync(f).toBeRejectedWith(new Error("prk is too small."));
});
describe('hkdf', () => {
const regular256Key = 'qBUmEYtwTwwGPuw/z6bs/qYXXYNUlocFlyAuuANI8Pw=';
const utf8256Key = '6DfJwW1R3txgiZKkIFTvVAb7qVlG7lKcmJGJoxR2GBU=';
const unicode256Key = 'gejGI82xthA+nKtKmIh82kjw+ttHr+ODsUoGdu5sf0A=';
it("should fail with outputByteSize is too large", async () => {
const cryptoFunctionService = new NodeCryptoFunctionService();
const f = cryptoFunctionService.hkdfExpand(
Utils.fromB64ToArray(prk32Byte),
"info",
8161,
"sha256"
);
await expectAsync(f).toBeRejectedWith(new Error("outputByteSize is too large."));
});
});
const regular512Key = 'xe5cIG6ZfwGmb1FvsOedM0XKOm21myZkjL/eDeKIqqM=';
const utf8512Key = 'XQMVBnxVEhlvjSFDQc77j5GDE9aorvbS0vKnjhRg0LY=';
const unicode512Key = '148GImrTbrjaGAe/iWEpclINM8Ehhko+9lB14+52lqc=';
describe("hash", () => {
const regular1Hash = "2a241604fb921fad12bf877282457268e1dccb70";
const utf81Hash = "85672798dc5831e96d6c48655d3d39365a9c88b6";
const unicode1Hash = "39c975935054a3efc805a9709b60763a823a6ad4";
testHkdf('sha256', regular256Key, utf8256Key, unicode256Key);
testHkdf('sha512', regular512Key, utf8512Key, unicode512Key);
const regular256Hash = "2b8e96031d352a8655d733d7a930b5ffbea69dc25cf65c7bca7dd946278908b2";
const utf8256Hash = "25fe8440f5b01ed113b0a0e38e721b126d2f3f77a67518c4a04fcde4e33eeb9d";
const unicode256Hash = "adc1c0c2afd6e92cefdf703f9b6eb2c38e0d6d1a040c83f8505c561fea58852e";
const regular512Hash =
"c15cf11d43bde333647e3f559ec4193bb2edeaa0e8b902772f514cdf3f785a3f49a6e02a4b87b3" +
"b47523271ad45b7e0aebb5cdcc1bc54815d256eb5dcb80da9d";
const utf8512Hash =
"035c31a877a291af09ed2d3a1a293e69c3e079ea2cecc00211f35e6bce10474ca3ad6e30b59e26118" +
"37463f20969c5bc95282965a051a88f8cdf2e166549fcdd";
const unicode512Hash =
"2b16a5561af8ad6fe414cc103fc8036492e1fc6d9aabe1b655497054f760fe0e34c5d100ac773d" +
"9f3030438284f22dbfa20cb2e9b019f2c98dfe38ce1ef41bae";
const regularMd5 = "5eceffa53a5fd58c44134211e2c5f522";
const utf8Md5 = "3abc9433c09551b939c80aa0aa3174e1";
const unicodeMd5 = "85ae134072c8d81257933f7045ba17ca";
testHash("sha1", regular1Hash, utf81Hash, unicode1Hash);
testHash("sha256", regular256Hash, utf8256Hash, unicode256Hash);
testHash("sha512", regular512Hash, utf8512Hash, unicode512Hash);
testHash("md5", regularMd5, utf8Md5, unicodeMd5);
});
describe("hmac", () => {
testHmac("sha1", Sha1Mac);
testHmac("sha256", Sha256Mac);
testHmac("sha512", Sha512Mac);
});
describe("compare", () => {
testCompare(false);
});
describe("hmacFast", () => {
testHmac("sha1", Sha1Mac, true);
testHmac("sha256", Sha256Mac, true);
testHmac("sha512", Sha512Mac, true);
});
describe("compareFast", () => {
testCompare(true);
});
describe("aesEncrypt", () => {
it("should successfully encrypt data", async () => {
const nodeCryptoFunctionService = new NodeCryptoFunctionService();
const iv = makeStaticByteArray(16);
const key = makeStaticByteArray(32);
const data = Utils.fromUtf8ToArray("EncryptMe!");
const encValue = await nodeCryptoFunctionService.aesEncrypt(
data.buffer,
iv.buffer,
key.buffer
);
expect(Utils.fromBufferToB64(encValue)).toBe("ByUF8vhyX4ddU9gcooznwA==");
});
describe('hkdfExpand', () => {
const prk16Byte = 'criAmKtfzxanbgea5/kelQ==';
const prk32Byte = 'F5h4KdYQnIVH4rKH0P9CZb1GrR4n16/sJrS0PsQEn0Y=';
const prk64Byte = 'ssBK0mRG17VHdtsgt8yo4v25CRNpauH+0r2fwY/E9rLyaFBAOMbIeTry+' +
'gUJ28p8y+hFh3EI9pcrEWaNvFYonQ==';
it("should successfully encrypt and then decrypt data", async () => {
const nodeCryptoFunctionService = new NodeCryptoFunctionService();
const iv = makeStaticByteArray(16);
const key = makeStaticByteArray(32);
const value = "EncryptMe!";
const data = Utils.fromUtf8ToArray(value);
const encValue = await nodeCryptoFunctionService.aesEncrypt(
data.buffer,
iv.buffer,
key.buffer
);
const decValue = await nodeCryptoFunctionService.aesDecrypt(encValue, iv.buffer, key.buffer);
expect(Utils.fromBufferToUtf8(decValue)).toBe(value);
});
});
testHkdfExpand('sha256', prk32Byte, 32, 'BnIqJlfnHm0e/2iB/15cbHyR19ARPIcWRp4oNS22CD8=');
testHkdfExpand('sha256', prk32Byte, 64, 'BnIqJlfnHm0e/2iB/15cbHyR19ARPIcWRp4oNS22CD9BV+' +
'/queOZenPNkDhmlVyL2WZ3OSU5+7ISNF5NhNfvZA==');
testHkdfExpand('sha512', prk64Byte, 32, 'uLWbMWodSBms5uGJ5WTRTesyW+MD7nlpCZvagvIRXlk=');
testHkdfExpand('sha512', prk64Byte, 64, 'uLWbMWodSBms5uGJ5WTRTesyW+MD7nlpCZvagvIRXlkY5Pv0sB+' +
'MqvaopmkC6sD/j89zDwTV9Ib2fpucUydO8w==');
describe("aesDecryptFast", () => {
it("should successfully decrypt data", async () => {
const nodeCryptoFunctionService = new NodeCryptoFunctionService();
const iv = Utils.fromBufferToB64(makeStaticByteArray(16).buffer);
const symKey = new SymmetricCryptoKey(makeStaticByteArray(32).buffer);
const data = "ByUF8vhyX4ddU9gcooznwA==";
const params = nodeCryptoFunctionService.aesDecryptFastParameters(data, iv, null, symKey);
const decValue = await nodeCryptoFunctionService.aesDecryptFast(params);
expect(decValue).toBe("EncryptMe!");
});
});
it('should fail with prk too small', async () => {
const cryptoFunctionService = new NodeCryptoFunctionService();
const f = cryptoFunctionService.hkdfExpand(Utils.fromB64ToArray(prk16Byte), 'info', 32, 'sha256');
await expectAsync(f).toBeRejectedWith(new Error('prk is too small.'));
});
describe("aesDecrypt", () => {
it("should successfully decrypt data", async () => {
const nodeCryptoFunctionService = new NodeCryptoFunctionService();
const iv = makeStaticByteArray(16);
const key = makeStaticByteArray(32);
const data = Utils.fromB64ToArray("ByUF8vhyX4ddU9gcooznwA==");
const decValue = await nodeCryptoFunctionService.aesDecrypt(
data.buffer,
iv.buffer,
key.buffer
);
expect(Utils.fromBufferToUtf8(decValue)).toBe("EncryptMe!");
});
});
it('should fail with outputByteSize is too large', async () => {
const cryptoFunctionService = new NodeCryptoFunctionService();
const f = cryptoFunctionService.hkdfExpand(Utils.fromB64ToArray(prk32Byte), 'info', 8161, 'sha256');
await expectAsync(f).toBeRejectedWith(new Error('outputByteSize is too large.'));
});
describe("rsaEncrypt", () => {
it("should successfully encrypt and then decrypt data", async () => {
const nodeCryptoFunctionService = new NodeCryptoFunctionService();
const pubKey = Utils.fromB64ToArray(RsaPublicKey);
const privKey = Utils.fromB64ToArray(RsaPrivateKey);
const value = "EncryptMe!";
const data = Utils.fromUtf8ToArray(value);
const encValue = await nodeCryptoFunctionService.rsaEncrypt(
data.buffer,
pubKey.buffer,
"sha1"
);
const decValue = await nodeCryptoFunctionService.rsaDecrypt(encValue, privKey.buffer, "sha1");
expect(Utils.fromBufferToUtf8(decValue)).toBe(value);
});
});
describe("rsaDecrypt", () => {
it("should successfully decrypt data", async () => {
const nodeCryptoFunctionService = new NodeCryptoFunctionService();
const privKey = Utils.fromB64ToArray(RsaPrivateKey);
const data = Utils.fromB64ToArray(
"A1/p8BQzN9UrbdYxUY2Va5+kPLyfZXF9JsZrjeEXcaclsnHurdxVAJcnbEqYMP3UXV" +
"4YAS/mpf+Rxe6/X0WS1boQdA0MAHSgx95hIlAraZYpiMLLiJRKeo2u8YivCdTM9V5vuAEJwf9Tof/qFsFci3sApdbATkorCT" +
"zFOIEPF2S1zgperEP23M01mr4dWVdYN18B32YF67xdJHMbFhp5dkQwv9CmscoWq7OE5HIfOb+JAh7BEZb+CmKhM3yWJvoR/D" +
"/5jcercUtK2o+XrzNrL4UQ7yLZcFz6Bfwb/j6ICYvqd/YJwXNE6dwlL57OfwJyCdw2rRYf0/qI00t9u8Iitw=="
);
const decValue = await nodeCryptoFunctionService.rsaDecrypt(
data.buffer,
privKey.buffer,
"sha1"
);
expect(Utils.fromBufferToUtf8(decValue)).toBe("EncryptMe!");
});
});
describe("rsaExtractPublicKey", () => {
it("should successfully extract key", async () => {
const nodeCryptoFunctionService = new NodeCryptoFunctionService();
const privKey = Utils.fromB64ToArray(RsaPrivateKey);
const publicKey = await nodeCryptoFunctionService.rsaExtractPublicKey(privKey.buffer);
expect(Utils.fromBufferToB64(publicKey)).toBe(RsaPublicKey);
});
});
describe("rsaGenerateKeyPair", () => {
testRsaGenerateKeyPair(1024);
testRsaGenerateKeyPair(2048);
// Generating 4096 bit keys is really slow with Forge lib.
// Maybe move to something else if we ever want to generate keys of this size.
// testRsaGenerateKeyPair(4096);
});
describe("randomBytes", () => {
it("should make a value of the correct length", async () => {
const nodeCryptoFunctionService = new NodeCryptoFunctionService();
const randomData = await nodeCryptoFunctionService.randomBytes(16);
expect(randomData.byteLength).toBe(16);
});
describe('hash', () => {
const regular1Hash = '2a241604fb921fad12bf877282457268e1dccb70';
const utf81Hash = '85672798dc5831e96d6c48655d3d39365a9c88b6';
const unicode1Hash = '39c975935054a3efc805a9709b60763a823a6ad4';
const regular256Hash = '2b8e96031d352a8655d733d7a930b5ffbea69dc25cf65c7bca7dd946278908b2';
const utf8256Hash = '25fe8440f5b01ed113b0a0e38e721b126d2f3f77a67518c4a04fcde4e33eeb9d';
const unicode256Hash = 'adc1c0c2afd6e92cefdf703f9b6eb2c38e0d6d1a040c83f8505c561fea58852e';
const regular512Hash = 'c15cf11d43bde333647e3f559ec4193bb2edeaa0e8b902772f514cdf3f785a3f49a6e02a4b87b3' +
'b47523271ad45b7e0aebb5cdcc1bc54815d256eb5dcb80da9d';
const utf8512Hash = '035c31a877a291af09ed2d3a1a293e69c3e079ea2cecc00211f35e6bce10474ca3ad6e30b59e26118' +
'37463f20969c5bc95282965a051a88f8cdf2e166549fcdd';
const unicode512Hash = '2b16a5561af8ad6fe414cc103fc8036492e1fc6d9aabe1b655497054f760fe0e34c5d100ac773d' +
'9f3030438284f22dbfa20cb2e9b019f2c98dfe38ce1ef41bae';
const regularMd5 = '5eceffa53a5fd58c44134211e2c5f522';
const utf8Md5 = '3abc9433c09551b939c80aa0aa3174e1';
const unicodeMd5 = '85ae134072c8d81257933f7045ba17ca';
testHash('sha1', regular1Hash, utf81Hash, unicode1Hash);
testHash('sha256', regular256Hash, utf8256Hash, unicode256Hash);
testHash('sha512', regular512Hash, utf8512Hash, unicode512Hash);
testHash('md5', regularMd5, utf8Md5, unicodeMd5);
});
describe('hmac', () => {
testHmac('sha1', Sha1Mac);
testHmac('sha256', Sha256Mac);
testHmac('sha512', Sha512Mac);
});
describe('compare', () => {
testCompare(false);
});
describe('hmacFast', () => {
testHmac('sha1', Sha1Mac, true);
testHmac('sha256', Sha256Mac, true);
testHmac('sha512', Sha512Mac, true);
});
describe('compareFast', () => {
testCompare(true);
});
describe('aesEncrypt', () => {
it('should successfully encrypt data', async () => {
const nodeCryptoFunctionService = new NodeCryptoFunctionService();
const iv = makeStaticByteArray(16);
const key = makeStaticByteArray(32);
const data = Utils.fromUtf8ToArray('EncryptMe!');
const encValue = await nodeCryptoFunctionService.aesEncrypt(data.buffer, iv.buffer, key.buffer);
expect(Utils.fromBufferToB64(encValue)).toBe('ByUF8vhyX4ddU9gcooznwA==');
});
it('should successfully encrypt and then decrypt data', async () => {
const nodeCryptoFunctionService = new NodeCryptoFunctionService();
const iv = makeStaticByteArray(16);
const key = makeStaticByteArray(32);
const value = 'EncryptMe!';
const data = Utils.fromUtf8ToArray(value);
const encValue = await nodeCryptoFunctionService.aesEncrypt(data.buffer, iv.buffer, key.buffer);
const decValue = await nodeCryptoFunctionService.aesDecrypt(encValue, iv.buffer, key.buffer);
expect(Utils.fromBufferToUtf8(decValue)).toBe(value);
});
});
describe('aesDecryptFast', () => {
it('should successfully decrypt data', async () => {
const nodeCryptoFunctionService = new NodeCryptoFunctionService();
const iv = Utils.fromBufferToB64(makeStaticByteArray(16).buffer);
const symKey = new SymmetricCryptoKey(makeStaticByteArray(32).buffer);
const data = 'ByUF8vhyX4ddU9gcooznwA==';
const params = nodeCryptoFunctionService.aesDecryptFastParameters(data, iv, null, symKey);
const decValue = await nodeCryptoFunctionService.aesDecryptFast(params);
expect(decValue).toBe('EncryptMe!');
});
});
describe('aesDecrypt', () => {
it('should successfully decrypt data', async () => {
const nodeCryptoFunctionService = new NodeCryptoFunctionService();
const iv = makeStaticByteArray(16);
const key = makeStaticByteArray(32);
const data = Utils.fromB64ToArray('ByUF8vhyX4ddU9gcooznwA==');
const decValue = await nodeCryptoFunctionService.aesDecrypt(data.buffer, iv.buffer, key.buffer);
expect(Utils.fromBufferToUtf8(decValue)).toBe('EncryptMe!');
});
});
describe('rsaEncrypt', () => {
it('should successfully encrypt and then decrypt data', async () => {
const nodeCryptoFunctionService = new NodeCryptoFunctionService();
const pubKey = Utils.fromB64ToArray(RsaPublicKey);
const privKey = Utils.fromB64ToArray(RsaPrivateKey);
const value = 'EncryptMe!';
const data = Utils.fromUtf8ToArray(value);
const encValue = await nodeCryptoFunctionService.rsaEncrypt(data.buffer, pubKey.buffer, 'sha1');
const decValue = await nodeCryptoFunctionService.rsaDecrypt(encValue, privKey.buffer, 'sha1');
expect(Utils.fromBufferToUtf8(decValue)).toBe(value);
});
});
describe('rsaDecrypt', () => {
it('should successfully decrypt data', async () => {
const nodeCryptoFunctionService = new NodeCryptoFunctionService();
const privKey = Utils.fromB64ToArray(RsaPrivateKey);
const data = Utils.fromB64ToArray('A1/p8BQzN9UrbdYxUY2Va5+kPLyfZXF9JsZrjeEXcaclsnHurdxVAJcnbEqYMP3UXV' +
'4YAS/mpf+Rxe6/X0WS1boQdA0MAHSgx95hIlAraZYpiMLLiJRKeo2u8YivCdTM9V5vuAEJwf9Tof/qFsFci3sApdbATkorCT' +
'zFOIEPF2S1zgperEP23M01mr4dWVdYN18B32YF67xdJHMbFhp5dkQwv9CmscoWq7OE5HIfOb+JAh7BEZb+CmKhM3yWJvoR/D' +
'/5jcercUtK2o+XrzNrL4UQ7yLZcFz6Bfwb/j6ICYvqd/YJwXNE6dwlL57OfwJyCdw2rRYf0/qI00t9u8Iitw==');
const decValue = await nodeCryptoFunctionService.rsaDecrypt(data.buffer, privKey.buffer, 'sha1');
expect(Utils.fromBufferToUtf8(decValue)).toBe('EncryptMe!');
});
});
describe('rsaExtractPublicKey', () => {
it('should successfully extract key', async () => {
const nodeCryptoFunctionService = new NodeCryptoFunctionService();
const privKey = Utils.fromB64ToArray(RsaPrivateKey);
const publicKey = await nodeCryptoFunctionService.rsaExtractPublicKey(privKey.buffer);
expect(Utils.fromBufferToB64(publicKey)).toBe(RsaPublicKey);
});
});
describe('rsaGenerateKeyPair', () => {
testRsaGenerateKeyPair(1024);
testRsaGenerateKeyPair(2048);
// Generating 4096 bit keys is really slow with Forge lib.
// Maybe move to something else if we ever want to generate keys of this size.
// testRsaGenerateKeyPair(4096);
});
describe('randomBytes', () => {
it('should make a value of the correct length', async () => {
const nodeCryptoFunctionService = new NodeCryptoFunctionService();
const randomData = await nodeCryptoFunctionService.randomBytes(16);
expect(randomData.byteLength).toBe(16);
});
it('should not make the same value twice', async () => {
const nodeCryptoFunctionService = new NodeCryptoFunctionService();
const randomData = await nodeCryptoFunctionService.randomBytes(16);
const randomData2 = await nodeCryptoFunctionService.randomBytes(16);
expect(randomData.byteLength === randomData2.byteLength && randomData !== randomData2).toBeTruthy();
});
it("should not make the same value twice", async () => {
const nodeCryptoFunctionService = new NodeCryptoFunctionService();
const randomData = await nodeCryptoFunctionService.randomBytes(16);
const randomData2 = await nodeCryptoFunctionService.randomBytes(16);
expect(
randomData.byteLength === randomData2.byteLength && randomData !== randomData2
).toBeTruthy();
});
});
});
function testPbkdf2(algorithm: 'sha256' | 'sha512', regularKey: string, utf8Key: string, unicodeKey: string) {
const regularEmail = 'user@example.com';
const utf8Email = 'üser@example.com';
function testPbkdf2(
algorithm: "sha256" | "sha512",
regularKey: string,
utf8Key: string,
unicodeKey: string
) {
const regularEmail = "user@example.com";
const utf8Email = "üser@example.com";
const regularPassword = 'password';
const utf8Password = 'pǻssword';
const unicodePassword = '😀password🙏';
const regularPassword = "password";
const utf8Password = "pǻssword";
const unicodePassword = "😀password🙏";
it('should create valid ' + algorithm + ' key from regular input', async () => {
const cryptoFunctionService = new NodeCryptoFunctionService();
const key = await cryptoFunctionService.pbkdf2(regularPassword, regularEmail, algorithm, 5000);
expect(Utils.fromBufferToB64(key)).toBe(regularKey);
});
it("should create valid " + algorithm + " key from regular input", async () => {
const cryptoFunctionService = new NodeCryptoFunctionService();
const key = await cryptoFunctionService.pbkdf2(regularPassword, regularEmail, algorithm, 5000);
expect(Utils.fromBufferToB64(key)).toBe(regularKey);
});
it('should create valid ' + algorithm + ' key from utf8 input', async () => {
const cryptoFunctionService = new NodeCryptoFunctionService();
const key = await cryptoFunctionService.pbkdf2(utf8Password, utf8Email, algorithm, 5000);
expect(Utils.fromBufferToB64(key)).toBe(utf8Key);
});
it("should create valid " + algorithm + " key from utf8 input", async () => {
const cryptoFunctionService = new NodeCryptoFunctionService();
const key = await cryptoFunctionService.pbkdf2(utf8Password, utf8Email, algorithm, 5000);
expect(Utils.fromBufferToB64(key)).toBe(utf8Key);
});
it('should create valid ' + algorithm + ' key from unicode input', async () => {
const cryptoFunctionService = new NodeCryptoFunctionService();
const key = await cryptoFunctionService.pbkdf2(unicodePassword, regularEmail, algorithm, 5000);
expect(Utils.fromBufferToB64(key)).toBe(unicodeKey);
});
it("should create valid " + algorithm + " key from unicode input", async () => {
const cryptoFunctionService = new NodeCryptoFunctionService();
const key = await cryptoFunctionService.pbkdf2(unicodePassword, regularEmail, algorithm, 5000);
expect(Utils.fromBufferToB64(key)).toBe(unicodeKey);
});
it('should create valid ' + algorithm + ' key from array buffer input', async () => {
const cryptoFunctionService = new NodeCryptoFunctionService();
const key = await cryptoFunctionService.pbkdf2(Utils.fromUtf8ToArray(regularPassword).buffer,
Utils.fromUtf8ToArray(regularEmail).buffer, algorithm, 5000);
expect(Utils.fromBufferToB64(key)).toBe(regularKey);
});
it("should create valid " + algorithm + " key from array buffer input", async () => {
const cryptoFunctionService = new NodeCryptoFunctionService();
const key = await cryptoFunctionService.pbkdf2(
Utils.fromUtf8ToArray(regularPassword).buffer,
Utils.fromUtf8ToArray(regularEmail).buffer,
algorithm,
5000
);
expect(Utils.fromBufferToB64(key)).toBe(regularKey);
});
}
function testHkdf(algorithm: 'sha256' | 'sha512', regularKey: string, utf8Key: string, unicodeKey: string) {
const ikm = Utils.fromB64ToArray('criAmKtfzxanbgea5/kelQ==');
function testHkdf(
algorithm: "sha256" | "sha512",
regularKey: string,
utf8Key: string,
unicodeKey: string
) {
const ikm = Utils.fromB64ToArray("criAmKtfzxanbgea5/kelQ==");
const regularSalt = 'salt';
const utf8Salt = 'üser_salt';
const unicodeSalt = '😀salt🙏';
const regularSalt = "salt";
const utf8Salt = "üser_salt";
const unicodeSalt = "😀salt🙏";
const regularInfo = 'info';
const utf8Info = 'üser_info';
const unicodeInfo = '😀info🙏';
const regularInfo = "info";
const utf8Info = "üser_info";
const unicodeInfo = "😀info🙏";
it('should create valid ' + algorithm + ' key from regular input', async () => {
const cryptoFunctionService = new NodeCryptoFunctionService();
const key = await cryptoFunctionService.hkdf(ikm, regularSalt, regularInfo, 32, algorithm);
expect(Utils.fromBufferToB64(key)).toBe(regularKey);
});
it("should create valid " + algorithm + " key from regular input", async () => {
const cryptoFunctionService = new NodeCryptoFunctionService();
const key = await cryptoFunctionService.hkdf(ikm, regularSalt, regularInfo, 32, algorithm);
expect(Utils.fromBufferToB64(key)).toBe(regularKey);
});
it('should create valid ' + algorithm + ' key from utf8 input', async () => {
const cryptoFunctionService = new NodeCryptoFunctionService();
const key = await cryptoFunctionService.hkdf(ikm, utf8Salt, utf8Info, 32, algorithm);
expect(Utils.fromBufferToB64(key)).toBe(utf8Key);
});
it("should create valid " + algorithm + " key from utf8 input", async () => {
const cryptoFunctionService = new NodeCryptoFunctionService();
const key = await cryptoFunctionService.hkdf(ikm, utf8Salt, utf8Info, 32, algorithm);
expect(Utils.fromBufferToB64(key)).toBe(utf8Key);
});
it('should create valid ' + algorithm + ' key from unicode input', async () => {
const cryptoFunctionService = new NodeCryptoFunctionService();
const key = await cryptoFunctionService.hkdf(ikm, unicodeSalt, unicodeInfo, 32, algorithm);
expect(Utils.fromBufferToB64(key)).toBe(unicodeKey);
});
it("should create valid " + algorithm + " key from unicode input", async () => {
const cryptoFunctionService = new NodeCryptoFunctionService();
const key = await cryptoFunctionService.hkdf(ikm, unicodeSalt, unicodeInfo, 32, algorithm);
expect(Utils.fromBufferToB64(key)).toBe(unicodeKey);
});
it('should create valid ' + algorithm + ' key from array buffer input', async () => {
const cryptoFunctionService = new NodeCryptoFunctionService();
const key = await cryptoFunctionService.hkdf(ikm, Utils.fromUtf8ToArray(regularSalt).buffer,
Utils.fromUtf8ToArray(regularInfo).buffer, 32, algorithm);
expect(Utils.fromBufferToB64(key)).toBe(regularKey);
});
it("should create valid " + algorithm + " key from array buffer input", async () => {
const cryptoFunctionService = new NodeCryptoFunctionService();
const key = await cryptoFunctionService.hkdf(
ikm,
Utils.fromUtf8ToArray(regularSalt).buffer,
Utils.fromUtf8ToArray(regularInfo).buffer,
32,
algorithm
);
expect(Utils.fromBufferToB64(key)).toBe(regularKey);
});
}
function testHkdfExpand(algorithm: 'sha256' | 'sha512', b64prk: string, outputByteSize: number,
b64ExpectedOkm: string) {
const info = 'info';
function testHkdfExpand(
algorithm: "sha256" | "sha512",
b64prk: string,
outputByteSize: number,
b64ExpectedOkm: string
) {
const info = "info";
it('should create valid ' + algorithm + ' ' + outputByteSize + ' byte okm', async () => {
const cryptoFunctionService = new NodeCryptoFunctionService();
const okm = await cryptoFunctionService.hkdfExpand(Utils.fromB64ToArray(b64prk), info, outputByteSize,
algorithm);
expect(Utils.fromBufferToB64(okm)).toBe(b64ExpectedOkm);
});
it("should create valid " + algorithm + " " + outputByteSize + " byte okm", async () => {
const cryptoFunctionService = new NodeCryptoFunctionService();
const okm = await cryptoFunctionService.hkdfExpand(
Utils.fromB64ToArray(b64prk),
info,
outputByteSize,
algorithm
);
expect(Utils.fromBufferToB64(okm)).toBe(b64ExpectedOkm);
});
}
function testHash(algorithm: 'sha1' | 'sha256' | 'sha512' | 'md5', regularHash: string,
utf8Hash: string, unicodeHash: string) {
const regularValue = 'HashMe!!';
const utf8Value = 'HǻshMe!!';
const unicodeValue = '😀HashMe!!!🙏';
function testHash(
algorithm: "sha1" | "sha256" | "sha512" | "md5",
regularHash: string,
utf8Hash: string,
unicodeHash: string
) {
const regularValue = "HashMe!!";
const utf8Value = "HǻshMe!!";
const unicodeValue = "😀HashMe!!!🙏";
it('should create valid ' + algorithm + ' hash from regular input', async () => {
const cryptoFunctionService = new NodeCryptoFunctionService();
const hash = await cryptoFunctionService.hash(regularValue, algorithm);
expect(Utils.fromBufferToHex(hash)).toBe(regularHash);
});
it("should create valid " + algorithm + " hash from regular input", async () => {
const cryptoFunctionService = new NodeCryptoFunctionService();
const hash = await cryptoFunctionService.hash(regularValue, algorithm);
expect(Utils.fromBufferToHex(hash)).toBe(regularHash);
});
it('should create valid ' + algorithm + ' hash from utf8 input', async () => {
const cryptoFunctionService = new NodeCryptoFunctionService();
const hash = await cryptoFunctionService.hash(utf8Value, algorithm);
expect(Utils.fromBufferToHex(hash)).toBe(utf8Hash);
});
it("should create valid " + algorithm + " hash from utf8 input", async () => {
const cryptoFunctionService = new NodeCryptoFunctionService();
const hash = await cryptoFunctionService.hash(utf8Value, algorithm);
expect(Utils.fromBufferToHex(hash)).toBe(utf8Hash);
});
it('should create valid ' + algorithm + ' hash from unicode input', async () => {
const cryptoFunctionService = new NodeCryptoFunctionService();
const hash = await cryptoFunctionService.hash(unicodeValue, algorithm);
expect(Utils.fromBufferToHex(hash)).toBe(unicodeHash);
});
it("should create valid " + algorithm + " hash from unicode input", async () => {
const cryptoFunctionService = new NodeCryptoFunctionService();
const hash = await cryptoFunctionService.hash(unicodeValue, algorithm);
expect(Utils.fromBufferToHex(hash)).toBe(unicodeHash);
});
it('should create valid ' + algorithm + ' hash from array buffer input', async () => {
const cryptoFunctionService = new NodeCryptoFunctionService();
const hash = await cryptoFunctionService.hash(Utils.fromUtf8ToArray(regularValue).buffer, algorithm);
expect(Utils.fromBufferToHex(hash)).toBe(regularHash);
});
it("should create valid " + algorithm + " hash from array buffer input", async () => {
const cryptoFunctionService = new NodeCryptoFunctionService();
const hash = await cryptoFunctionService.hash(
Utils.fromUtf8ToArray(regularValue).buffer,
algorithm
);
expect(Utils.fromBufferToHex(hash)).toBe(regularHash);
});
}
function testHmac(algorithm: 'sha1' | 'sha256' | 'sha512', mac: string, fast = false) {
it('should create valid ' + algorithm + ' hmac', async () => {
const cryptoFunctionService = new NodeCryptoFunctionService();
const value = Utils.fromUtf8ToArray('SignMe!!').buffer;
const key = Utils.fromUtf8ToArray('secretkey').buffer;
let computedMac: ArrayBuffer = null;
if (fast) {
computedMac = await cryptoFunctionService.hmacFast(value, key, algorithm);
} else {
computedMac = await cryptoFunctionService.hmac(value, key, algorithm);
}
expect(Utils.fromBufferToHex(computedMac)).toBe(mac);
});
function testHmac(algorithm: "sha1" | "sha256" | "sha512", mac: string, fast = false) {
it("should create valid " + algorithm + " hmac", async () => {
const cryptoFunctionService = new NodeCryptoFunctionService();
const value = Utils.fromUtf8ToArray("SignMe!!").buffer;
const key = Utils.fromUtf8ToArray("secretkey").buffer;
let computedMac: ArrayBuffer = null;
if (fast) {
computedMac = await cryptoFunctionService.hmacFast(value, key, algorithm);
} else {
computedMac = await cryptoFunctionService.hmac(value, key, algorithm);
}
expect(Utils.fromBufferToHex(computedMac)).toBe(mac);
});
}
function testCompare(fast = false) {
it('should successfully compare two of the same values', async () => {
const cryptoFunctionService = new NodeCryptoFunctionService();
const a = new Uint8Array(2);
a[0] = 1;
a[1] = 2;
const equal = fast ? await cryptoFunctionService.compareFast(a.buffer, a.buffer) :
await cryptoFunctionService.compare(a.buffer, a.buffer);
expect(equal).toBe(true);
});
it("should successfully compare two of the same values", async () => {
const cryptoFunctionService = new NodeCryptoFunctionService();
const a = new Uint8Array(2);
a[0] = 1;
a[1] = 2;
const equal = fast
? await cryptoFunctionService.compareFast(a.buffer, a.buffer)
: await cryptoFunctionService.compare(a.buffer, a.buffer);
expect(equal).toBe(true);
});
it('should successfully compare two different values of the same length', async () => {
const cryptoFunctionService = new NodeCryptoFunctionService();
const a = new Uint8Array(2);
a[0] = 1;
a[1] = 2;
const b = new Uint8Array(2);
b[0] = 3;
b[1] = 4;
const equal = fast ? await cryptoFunctionService.compareFast(a.buffer, b.buffer) :
await cryptoFunctionService.compare(a.buffer, b.buffer);
expect(equal).toBe(false);
});
it("should successfully compare two different values of the same length", async () => {
const cryptoFunctionService = new NodeCryptoFunctionService();
const a = new Uint8Array(2);
a[0] = 1;
a[1] = 2;
const b = new Uint8Array(2);
b[0] = 3;
b[1] = 4;
const equal = fast
? await cryptoFunctionService.compareFast(a.buffer, b.buffer)
: await cryptoFunctionService.compare(a.buffer, b.buffer);
expect(equal).toBe(false);
});
it('should successfully compare two different values of different lengths', async () => {
const cryptoFunctionService = new NodeCryptoFunctionService();
const a = new Uint8Array(2);
a[0] = 1;
a[1] = 2;
const b = new Uint8Array(2);
b[0] = 3;
const equal = fast ? await cryptoFunctionService.compareFast(a.buffer, b.buffer) :
await cryptoFunctionService.compare(a.buffer, b.buffer);
expect(equal).toBe(false);
});
it("should successfully compare two different values of different lengths", async () => {
const cryptoFunctionService = new NodeCryptoFunctionService();
const a = new Uint8Array(2);
a[0] = 1;
a[1] = 2;
const b = new Uint8Array(2);
b[0] = 3;
const equal = fast
? await cryptoFunctionService.compareFast(a.buffer, b.buffer)
: await cryptoFunctionService.compare(a.buffer, b.buffer);
expect(equal).toBe(false);
});
}
function testRsaGenerateKeyPair(length: 1024 | 2048 | 4096) {
it('should successfully generate a ' + length + ' bit key pair', async () => {
const cryptoFunctionService = new NodeCryptoFunctionService();
const keyPair = await cryptoFunctionService.rsaGenerateKeyPair(length);
expect(keyPair[0] == null || keyPair[1] == null).toBe(false);
const publicKey = await cryptoFunctionService.rsaExtractPublicKey(keyPair[1]);
expect(Utils.fromBufferToB64(keyPair[0])).toBe(Utils.fromBufferToB64(publicKey));
}, 30000);
it(
"should successfully generate a " + length + " bit key pair",
async () => {
const cryptoFunctionService = new NodeCryptoFunctionService();
const keyPair = await cryptoFunctionService.rsaGenerateKeyPair(length);
expect(keyPair[0] == null || keyPair[1] == null).toBe(false);
const publicKey = await cryptoFunctionService.rsaExtractPublicKey(keyPair[1]);
expect(Utils.fromBufferToB64(keyPair[0])).toBe(Utils.fromBufferToB64(publicKey));
},
30000
);
}
function makeStaticByteArray(length: number) {
const arr = new Uint8Array(length);
for (let i = 0; i < length; i++) {
arr[i] = i;
}
return arr;
const arr = new Uint8Array(length);
for (let i = 0; i < length; i++) {
arr[i] = i;
}
return arr;
}

View File

@@ -1,13 +1,7 @@
{
"spec_dir": "dist/spec",
"spec_files": [
"common/**/*[sS]pec.js",
"node/**/*[sS]pec.js",
"electron/**/*[sS]pec.js"
],
"helpers": [
"helpers.js"
],
"spec_files": ["common/**/*[sS]pec.js", "node/**/*[sS]pec.js", "electron/**/*[sS]pec.js"],
"helpers": ["helpers.js"],
"stopSpecOnExpectationFailure": false,
"random": true
}

View File

@@ -1,98 +1,98 @@
module.exports = (config) => {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '../../',
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: "../../",
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine', 'detectBrowsers'],
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ["jasmine", "detectBrowsers"],
// list of files / patterns to load in the browser
files: [
{ pattern: 'spec/utils.ts', watched: false },
{ pattern: 'spec/common/**/*.ts', watched: false },
{ pattern: 'spec/web/**/*.ts', watched: false },
// list of files / patterns to load in the browser
files: [
{ pattern: "spec/utils.ts", watched: false },
{ pattern: "spec/common/**/*.ts", watched: false },
{ pattern: "spec/web/**/*.ts", watched: false },
],
// list of files to exclude
exclude: [],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
"spec/**/*.ts": "webpack",
},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ["progress", "kjhtml"],
// web server port
port: 9876,
// enable / disable colors in the output (reporters and logs)
colors: true,
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity,
client: {
clearContext: false, // leave Jasmine Spec Runner output visible in browser
},
webpack: {
resolve: {
extensions: [".js", ".ts", ".tsx"],
},
module: {
rules: [
{
test: /\.tsx?$/,
loader: "ts-loader",
options: {
compiler: "ttypescript",
},
},
],
// list of files to exclude
exclude: [
],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
'spec/**/*.ts': 'webpack'
},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress', 'kjhtml'],
// web server port
port: 9876,
// enable / disable colors in the output (reporters and logs)
},
stats: {
colors: true,
modules: true,
reasons: true,
errorDetails: true,
},
devtool: "inline-source-map",
},
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
detectBrowsers: {
usePhantomJS: false,
postDetection: (availableBrowsers) => {
const result = availableBrowsers;
function removeBrowser(browser) {
if (availableBrowsers.length > 1 && availableBrowsers.indexOf(browser) > -1) {
result.splice(result.indexOf(browser), 1);
}
}
// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity,
removeBrowser("IE");
removeBrowser("Opera");
removeBrowser("SafariTechPreview");
client: {
clearContext: false // leave Jasmine Spec Runner output visible in browser
},
var githubAction =
process.env.GITHUB_WORKFLOW != null && process.env.GITHUB_WORKFLOW !== "";
// if (githubAction) {
removeBrowser("Firefox");
removeBrowser("Safari");
// }
webpack: {
resolve: {
extensions: ['.js', '.ts', '.tsx'],
},
module: {
rules: [
{
test: /\.tsx?$/,
loader: 'ts-loader',
options: {
compiler: 'ttypescript'
},
},
],
},
stats: {
colors: true,
modules: true,
reasons: true,
errorDetails: true,
},
devtool: 'inline-source-map',
},
detectBrowsers: {
usePhantomJS: false,
postDetection: (availableBrowsers) => {
const result = availableBrowsers;
function removeBrowser(browser) {
if (availableBrowsers.length > 1 && availableBrowsers.indexOf(browser) > -1) {
result.splice(result.indexOf(browser), 1);
}
}
removeBrowser('IE');
removeBrowser('Opera');
removeBrowser('SafariTechPreview');
var githubAction = process.env.GITHUB_WORKFLOW != null && process.env.GITHUB_WORKFLOW !== '';
// if (githubAction) {
removeBrowser('Firefox');
removeBrowser('Safari');
// }
return result;
}
},
})
}
return result;
},
},
});
};

View File

@@ -1,16 +1,19 @@
function newGuid() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
// tslint:disable:no-bitwise
const r = Math.random() * 16 | 0;
const v = c === 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
// tslint:disable:no-bitwise
const r = (Math.random() * 16) | 0;
const v = c === "x" ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
}
export function GetUniqueString(prefix: string = '') {
return prefix + '_' + newGuid();
export function GetUniqueString(prefix: string = "") {
return prefix + "_" + newGuid();
}
export function BuildTestObject<T, K extends keyof T = keyof T>(def: Partial<Pick<T, K>> | T, constructor?: (new () => T)): T {
return Object.assign(constructor === null ? {} : new constructor(), def) as T;
export function BuildTestObject<T, K extends keyof T = keyof T>(
def: Partial<Pick<T, K>> | T,
constructor?: new () => T
): T {
return Object.assign(constructor === null ? {} : new constructor(), def) as T;
}

View File

@@ -1,484 +1,567 @@
import Substitute from '@fluffy-spoon/substitute';
import Substitute from "@fluffy-spoon/substitute";
import { PlatformUtilsService } from 'jslib-common/abstractions/platformUtils.service';
import { PlatformUtilsService } from "jslib-common/abstractions/platformUtils.service";
import { WebCryptoFunctionService } from 'jslib-common/services/webCryptoFunction.service';
import { WebCryptoFunctionService } from "jslib-common/services/webCryptoFunction.service";
import { Utils } from 'jslib-common/misc/utils';
import { SymmetricCryptoKey } from 'jslib-common/models/domain/symmetricCryptoKey';
import { Utils } from "jslib-common/misc/utils";
import { SymmetricCryptoKey } from "jslib-common/models/domain/symmetricCryptoKey";
const RsaPublicKey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAl0Vawl/toXzkEvB82FEtqHP' +
'4xlU2ab/v0crqIfXfIoWF/XXdHGIdrZeilnRXPPJT1B9dTsasttEZNnua/0Rek/cjNDHtzT52irfoZYS7X6HNIfOi54Q+egP' +
'RQ1H7iNHVZz3K8Db9GCSKPeC8MbW6gVCzb15esCe1gGzg6wkMuWYDFYPoh/oBqcIqrGah7firqB1nDedzEjw32heP2DAffVN' +
'084iTDjiWrJNUxBJ2pDD5Z9dT3MzQ2s09ew1yMWK2z37rT3YerC7OgEDmo3WYo3xL3qYJznu3EO2nmrYjiRa40wKSjxsTlUc' +
'xDF+F0uMW8oR9EMUHgepdepfAtLsSAQIDAQAB';
const RsaPrivateKey = 'MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCXRVrCX+2hfOQS8Hz' +
'YUS2oc/jGVTZpv+/Ryuoh9d8ihYX9dd0cYh2tl6KWdFc88lPUH11Oxqy20Rk2e5r/RF6T9yM0Me3NPnaKt+hlhLtfoc0h86L' +
'nhD56A9FDUfuI0dVnPcrwNv0YJIo94LwxtbqBULNvXl6wJ7WAbODrCQy5ZgMVg+iH+gGpwiqsZqHt+KuoHWcN53MSPDfaF4/' +
'YMB99U3TziJMOOJask1TEEnakMPln11PczNDazT17DXIxYrbPfutPdh6sLs6AQOajdZijfEvepgnOe7cQ7aeatiOJFrjTApK' +
'PGxOVRzEMX4XS4xbyhH0QxQeB6l16l8C0uxIBAgMBAAECggEASaWfeVDA3cVzOPFSpvJm20OTE+R6uGOU+7vh36TX/POq92q' +
'Buwbd0h0oMD32FxsXywd2IxtBDUSiFM9699qufTVuM0Q3tZw6lHDTOVG08+tPdr8qSbMtw7PGFxN79fHLBxejjO4IrM9lapj' +
'WpxEF+11x7r+wM+0xRZQ8sNFYG46aPfIaty4BGbL0I2DQ2y8I57iBCAy69eht59NLMm27fRWGJIWCuBIjlpfzET1j2HLXUIh' +
'5bTBNzqaN039WH49HczGE3mQKVEJZc/efk3HaVd0a1Sjzyn0QY+N1jtZN3jTRbuDWA1AknkX1LX/0tUhuS3/7C3ejHxjw4Dk' +
'1ZLo5/QKBgQDIWvqFn0+IKRSu6Ua2hDsufIHHUNLelbfLUMmFthxabcUn4zlvIscJO00Tq/ezopSRRvbGiqnxjv/mYxucvOU' +
'BeZtlus0Q9RTACBtw9TGoNTmQbEunJ2FOSlqbQxkBBAjgGEppRPt30iGj/VjAhCATq2MYOa/X4dVR51BqQAFIEwKBgQDBSIf' +
'TFKC/hDk6FKZlgwvupWYJyU9RkyfstPErZFmzoKhPkQ3YORo2oeAYmVUbS9I2iIYpYpYQJHX8jMuCbCz4ONxTCuSIXYQYUcU' +
'q4PglCKp31xBAE6TN8SvhfME9/MvuDssnQinAHuF0GDAhF646T3LLS1not6Vszv7brwSoGwKBgQC88v/8cGfi80ssQZeMnVv' +
'q1UTXIeQcQnoY5lGHJl3K8mbS3TnXE6c9j417Fdz+rj8KWzBzwWXQB5pSPflWcdZO886Xu/mVGmy9RWgLuVFhXwCwsVEPjNX' +
'5ramRb0/vY0yzenUCninBsIxFSbIfrPtLUYCc4hpxr+sr2Mg/y6jpvQKBgBezMRRs3xkcuXepuI2R+BCXL1/b02IJTUf1F+1' +
'eLLGd7YV0H+J3fgNc7gGWK51hOrF9JBZHBGeOUPlaukmPwiPdtQZpu4QNE3l37VlIpKTF30E6mb+BqR+nht3rUjarnMXgAoE' +
'Z18y6/KIjpSMpqC92Nnk/EBM9EYe6Cf4eA9ApAoGAeqEUg46UTlJySkBKURGpIs3v1kkf5I0X8DnOhwb+HPxNaiEdmO7ckm8' +
'+tPVgppLcG0+tMdLjigFQiDUQk2y3WjyxP5ZvXu7U96jaJRI8PFMoE06WeVYcdIzrID2HvqH+w0UQJFrLJ/0Mn4stFAEzXKZ' +
'BokBGnjFnTnKcs7nv/O8=';
const RsaPublicKey =
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAl0Vawl/toXzkEvB82FEtqHP" +
"4xlU2ab/v0crqIfXfIoWF/XXdHGIdrZeilnRXPPJT1B9dTsasttEZNnua/0Rek/cjNDHtzT52irfoZYS7X6HNIfOi54Q+egP" +
"RQ1H7iNHVZz3K8Db9GCSKPeC8MbW6gVCzb15esCe1gGzg6wkMuWYDFYPoh/oBqcIqrGah7firqB1nDedzEjw32heP2DAffVN" +
"084iTDjiWrJNUxBJ2pDD5Z9dT3MzQ2s09ew1yMWK2z37rT3YerC7OgEDmo3WYo3xL3qYJznu3EO2nmrYjiRa40wKSjxsTlUc" +
"xDF+F0uMW8oR9EMUHgepdepfAtLsSAQIDAQAB";
const RsaPrivateKey =
"MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCXRVrCX+2hfOQS8Hz" +
"YUS2oc/jGVTZpv+/Ryuoh9d8ihYX9dd0cYh2tl6KWdFc88lPUH11Oxqy20Rk2e5r/RF6T9yM0Me3NPnaKt+hlhLtfoc0h86L" +
"nhD56A9FDUfuI0dVnPcrwNv0YJIo94LwxtbqBULNvXl6wJ7WAbODrCQy5ZgMVg+iH+gGpwiqsZqHt+KuoHWcN53MSPDfaF4/" +
"YMB99U3TziJMOOJask1TEEnakMPln11PczNDazT17DXIxYrbPfutPdh6sLs6AQOajdZijfEvepgnOe7cQ7aeatiOJFrjTApK" +
"PGxOVRzEMX4XS4xbyhH0QxQeB6l16l8C0uxIBAgMBAAECggEASaWfeVDA3cVzOPFSpvJm20OTE+R6uGOU+7vh36TX/POq92q" +
"Buwbd0h0oMD32FxsXywd2IxtBDUSiFM9699qufTVuM0Q3tZw6lHDTOVG08+tPdr8qSbMtw7PGFxN79fHLBxejjO4IrM9lapj" +
"WpxEF+11x7r+wM+0xRZQ8sNFYG46aPfIaty4BGbL0I2DQ2y8I57iBCAy69eht59NLMm27fRWGJIWCuBIjlpfzET1j2HLXUIh" +
"5bTBNzqaN039WH49HczGE3mQKVEJZc/efk3HaVd0a1Sjzyn0QY+N1jtZN3jTRbuDWA1AknkX1LX/0tUhuS3/7C3ejHxjw4Dk" +
"1ZLo5/QKBgQDIWvqFn0+IKRSu6Ua2hDsufIHHUNLelbfLUMmFthxabcUn4zlvIscJO00Tq/ezopSRRvbGiqnxjv/mYxucvOU" +
"BeZtlus0Q9RTACBtw9TGoNTmQbEunJ2FOSlqbQxkBBAjgGEppRPt30iGj/VjAhCATq2MYOa/X4dVR51BqQAFIEwKBgQDBSIf" +
"TFKC/hDk6FKZlgwvupWYJyU9RkyfstPErZFmzoKhPkQ3YORo2oeAYmVUbS9I2iIYpYpYQJHX8jMuCbCz4ONxTCuSIXYQYUcU" +
"q4PglCKp31xBAE6TN8SvhfME9/MvuDssnQinAHuF0GDAhF646T3LLS1not6Vszv7brwSoGwKBgQC88v/8cGfi80ssQZeMnVv" +
"q1UTXIeQcQnoY5lGHJl3K8mbS3TnXE6c9j417Fdz+rj8KWzBzwWXQB5pSPflWcdZO886Xu/mVGmy9RWgLuVFhXwCwsVEPjNX" +
"5ramRb0/vY0yzenUCninBsIxFSbIfrPtLUYCc4hpxr+sr2Mg/y6jpvQKBgBezMRRs3xkcuXepuI2R+BCXL1/b02IJTUf1F+1" +
"eLLGd7YV0H+J3fgNc7gGWK51hOrF9JBZHBGeOUPlaukmPwiPdtQZpu4QNE3l37VlIpKTF30E6mb+BqR+nht3rUjarnMXgAoE" +
"Z18y6/KIjpSMpqC92Nnk/EBM9EYe6Cf4eA9ApAoGAeqEUg46UTlJySkBKURGpIs3v1kkf5I0X8DnOhwb+HPxNaiEdmO7ckm8" +
"+tPVgppLcG0+tMdLjigFQiDUQk2y3WjyxP5ZvXu7U96jaJRI8PFMoE06WeVYcdIzrID2HvqH+w0UQJFrLJ/0Mn4stFAEzXKZ" +
"BokBGnjFnTnKcs7nv/O8=";
const Sha1Mac = '4d4c223f95dc577b665ec4ccbcb680b80a397038';
const Sha256Mac = '6be3caa84922e12aaaaa2f16c40d44433bb081ef323db584eb616333ab4e874f';
const Sha512Mac = '21910e341fa12106ca35758a2285374509326c9fbe0bd64e7b99c898f841dc948c58ce66d3504d8883c' +
'5ea7817a0b7c5d4d9b00364ccd214669131fc17fe4aca';
const Sha1Mac = "4d4c223f95dc577b665ec4ccbcb680b80a397038";
const Sha256Mac = "6be3caa84922e12aaaaa2f16c40d44433bb081ef323db584eb616333ab4e874f";
const Sha512Mac =
"21910e341fa12106ca35758a2285374509326c9fbe0bd64e7b99c898f841dc948c58ce66d3504d8883c" +
"5ea7817a0b7c5d4d9b00364ccd214669131fc17fe4aca";
describe('WebCrypto Function Service', () => {
describe('pbkdf2', () => {
const regular256Key = 'pj9prw/OHPleXI6bRdmlaD+saJS4awrMiQsQiDjeu2I=';
const utf8256Key = 'yqvoFXgMRmHR3QPYr5pyR4uVuoHkltv9aHUP63p8n7I=';
const unicode256Key = 'ZdeOata6xoRpB4DLp8zHhXz5kLmkWtX5pd+TdRH8w8w=';
describe("WebCrypto Function Service", () => {
describe("pbkdf2", () => {
const regular256Key = "pj9prw/OHPleXI6bRdmlaD+saJS4awrMiQsQiDjeu2I=";
const utf8256Key = "yqvoFXgMRmHR3QPYr5pyR4uVuoHkltv9aHUP63p8n7I=";
const unicode256Key = "ZdeOata6xoRpB4DLp8zHhXz5kLmkWtX5pd+TdRH8w8w=";
const regular512Key = 'liTi/Ke8LPU1Qv+Vl7NGEVt/XMbsBVJ2kQxtVG/Z1/JFHFKQW3ZkI81qVlwTiCpb+cFXzs+57' +
'eyhhx5wfKo5Cg==';
const utf8512Key = 'df0KdvIBeCzD/kyXptwQohaqUa4e7IyFUyhFQjXCANu5T+scq55hCcE4dG4T/MhAk2exw8j7ixRN' +
'zXANiVZpnw==';
const unicode512Key = 'FE+AnUJaxv8jh+zUDtZz4mjjcYk0/PZDZm+SLJe3XtxtnpdqqpblX6JjuMZt/dYYNMOrb2+mD' +
'L3FiQDTROh1lg==';
const regular512Key =
"liTi/Ke8LPU1Qv+Vl7NGEVt/XMbsBVJ2kQxtVG/Z1/JFHFKQW3ZkI81qVlwTiCpb+cFXzs+57" +
"eyhhx5wfKo5Cg==";
const utf8512Key =
"df0KdvIBeCzD/kyXptwQohaqUa4e7IyFUyhFQjXCANu5T+scq55hCcE4dG4T/MhAk2exw8j7ixRN" +
"zXANiVZpnw==";
const unicode512Key =
"FE+AnUJaxv8jh+zUDtZz4mjjcYk0/PZDZm+SLJe3XtxtnpdqqpblX6JjuMZt/dYYNMOrb2+mD" +
"L3FiQDTROh1lg==";
testPbkdf2('sha256', regular256Key, utf8256Key, unicode256Key);
testPbkdf2('sha512', regular512Key, utf8512Key, unicode512Key);
testPbkdf2("sha256", regular256Key, utf8256Key, unicode256Key);
testPbkdf2("sha512", regular512Key, utf8512Key, unicode512Key);
});
describe("hkdf", () => {
const regular256Key = "qBUmEYtwTwwGPuw/z6bs/qYXXYNUlocFlyAuuANI8Pw=";
const utf8256Key = "6DfJwW1R3txgiZKkIFTvVAb7qVlG7lKcmJGJoxR2GBU=";
const unicode256Key = "gejGI82xthA+nKtKmIh82kjw+ttHr+ODsUoGdu5sf0A=";
const regular512Key = "xe5cIG6ZfwGmb1FvsOedM0XKOm21myZkjL/eDeKIqqM=";
const utf8512Key = "XQMVBnxVEhlvjSFDQc77j5GDE9aorvbS0vKnjhRg0LY=";
const unicode512Key = "148GImrTbrjaGAe/iWEpclINM8Ehhko+9lB14+52lqc=";
testHkdf("sha256", regular256Key, utf8256Key, unicode256Key);
testHkdf("sha512", regular512Key, utf8512Key, unicode512Key);
});
describe("hkdfExpand", () => {
const prk16Byte = "criAmKtfzxanbgea5/kelQ==";
const prk32Byte = "F5h4KdYQnIVH4rKH0P9CZb1GrR4n16/sJrS0PsQEn0Y=";
const prk64Byte =
"ssBK0mRG17VHdtsgt8yo4v25CRNpauH+0r2fwY/E9rLyaFBAOMbIeTry+" +
"gUJ28p8y+hFh3EI9pcrEWaNvFYonQ==";
testHkdfExpand("sha256", prk32Byte, 32, "BnIqJlfnHm0e/2iB/15cbHyR19ARPIcWRp4oNS22CD8=");
testHkdfExpand(
"sha256",
prk32Byte,
64,
"BnIqJlfnHm0e/2iB/15cbHyR19ARPIcWRp4oNS22CD9BV+" +
"/queOZenPNkDhmlVyL2WZ3OSU5+7ISNF5NhNfvZA=="
);
testHkdfExpand("sha512", prk64Byte, 32, "uLWbMWodSBms5uGJ5WTRTesyW+MD7nlpCZvagvIRXlk=");
testHkdfExpand(
"sha512",
prk64Byte,
64,
"uLWbMWodSBms5uGJ5WTRTesyW+MD7nlpCZvagvIRXlkY5Pv0sB+" +
"MqvaopmkC6sD/j89zDwTV9Ib2fpucUydO8w=="
);
it("should fail with prk too small", async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const f = cryptoFunctionService.hkdfExpand(
Utils.fromB64ToArray(prk16Byte),
"info",
32,
"sha256"
);
await expectAsync(f).toBeRejectedWith(new Error("prk is too small."));
});
describe('hkdf', () => {
const regular256Key = 'qBUmEYtwTwwGPuw/z6bs/qYXXYNUlocFlyAuuANI8Pw=';
const utf8256Key = '6DfJwW1R3txgiZKkIFTvVAb7qVlG7lKcmJGJoxR2GBU=';
const unicode256Key = 'gejGI82xthA+nKtKmIh82kjw+ttHr+ODsUoGdu5sf0A=';
it("should fail with outputByteSize is too large", async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const f = cryptoFunctionService.hkdfExpand(
Utils.fromB64ToArray(prk32Byte),
"info",
8161,
"sha256"
);
await expectAsync(f).toBeRejectedWith(new Error("outputByteSize is too large."));
});
});
const regular512Key = 'xe5cIG6ZfwGmb1FvsOedM0XKOm21myZkjL/eDeKIqqM=';
const utf8512Key = 'XQMVBnxVEhlvjSFDQc77j5GDE9aorvbS0vKnjhRg0LY=';
const unicode512Key = '148GImrTbrjaGAe/iWEpclINM8Ehhko+9lB14+52lqc=';
describe("hash", () => {
const regular1Hash = "2a241604fb921fad12bf877282457268e1dccb70";
const utf81Hash = "85672798dc5831e96d6c48655d3d39365a9c88b6";
const unicode1Hash = "39c975935054a3efc805a9709b60763a823a6ad4";
testHkdf('sha256', regular256Key, utf8256Key, unicode256Key);
testHkdf('sha512', regular512Key, utf8512Key, unicode512Key);
const regular256Hash = "2b8e96031d352a8655d733d7a930b5ffbea69dc25cf65c7bca7dd946278908b2";
const utf8256Hash = "25fe8440f5b01ed113b0a0e38e721b126d2f3f77a67518c4a04fcde4e33eeb9d";
const unicode256Hash = "adc1c0c2afd6e92cefdf703f9b6eb2c38e0d6d1a040c83f8505c561fea58852e";
const regular512Hash =
"c15cf11d43bde333647e3f559ec4193bb2edeaa0e8b902772f514cdf3f785a3f49a6e02a4b87b3" +
"b47523271ad45b7e0aebb5cdcc1bc54815d256eb5dcb80da9d";
const utf8512Hash =
"035c31a877a291af09ed2d3a1a293e69c3e079ea2cecc00211f35e6bce10474ca3ad6e30b59e26118" +
"37463f20969c5bc95282965a051a88f8cdf2e166549fcdd";
const unicode512Hash =
"2b16a5561af8ad6fe414cc103fc8036492e1fc6d9aabe1b655497054f760fe0e34c5d100ac773d" +
"9f3030438284f22dbfa20cb2e9b019f2c98dfe38ce1ef41bae";
const regularMd5 = "5eceffa53a5fd58c44134211e2c5f522";
const utf8Md5 = "3abc9433c09551b939c80aa0aa3174e1";
const unicodeMd5 = "85ae134072c8d81257933f7045ba17ca";
testHash("sha1", regular1Hash, utf81Hash, unicode1Hash);
testHash("sha256", regular256Hash, utf8256Hash, unicode256Hash);
testHash("sha512", regular512Hash, utf8512Hash, unicode512Hash);
testHash("md5", regularMd5, utf8Md5, unicodeMd5);
});
describe("hmac", () => {
testHmac("sha1", Sha1Mac);
testHmac("sha256", Sha256Mac);
testHmac("sha512", Sha512Mac);
});
describe("compare", () => {
it("should successfully compare two of the same values", async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const a = new Uint8Array(2);
a[0] = 1;
a[1] = 2;
const equal = await cryptoFunctionService.compare(a.buffer, a.buffer);
expect(equal).toBe(true);
});
describe('hkdfExpand', () => {
const prk16Byte = 'criAmKtfzxanbgea5/kelQ==';
const prk32Byte = 'F5h4KdYQnIVH4rKH0P9CZb1GrR4n16/sJrS0PsQEn0Y=';
const prk64Byte = 'ssBK0mRG17VHdtsgt8yo4v25CRNpauH+0r2fwY/E9rLyaFBAOMbIeTry+' +
'gUJ28p8y+hFh3EI9pcrEWaNvFYonQ==';
testHkdfExpand('sha256', prk32Byte, 32, 'BnIqJlfnHm0e/2iB/15cbHyR19ARPIcWRp4oNS22CD8=');
testHkdfExpand('sha256', prk32Byte, 64, 'BnIqJlfnHm0e/2iB/15cbHyR19ARPIcWRp4oNS22CD9BV+' +
'/queOZenPNkDhmlVyL2WZ3OSU5+7ISNF5NhNfvZA==');
testHkdfExpand('sha512', prk64Byte, 32, 'uLWbMWodSBms5uGJ5WTRTesyW+MD7nlpCZvagvIRXlk=');
testHkdfExpand('sha512', prk64Byte, 64, 'uLWbMWodSBms5uGJ5WTRTesyW+MD7nlpCZvagvIRXlkY5Pv0sB+' +
'MqvaopmkC6sD/j89zDwTV9Ib2fpucUydO8w==');
it('should fail with prk too small', async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const f = cryptoFunctionService.hkdfExpand(Utils.fromB64ToArray(prk16Byte), 'info', 32, 'sha256');
await expectAsync(f).toBeRejectedWith(new Error('prk is too small.'));
});
it('should fail with outputByteSize is too large', async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const f = cryptoFunctionService.hkdfExpand(Utils.fromB64ToArray(prk32Byte), 'info', 8161, 'sha256');
await expectAsync(f).toBeRejectedWith(new Error('outputByteSize is too large.'));
});
it("should successfully compare two different values of the same length", async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const a = new Uint8Array(2);
a[0] = 1;
a[1] = 2;
const b = new Uint8Array(2);
b[0] = 3;
b[1] = 4;
const equal = await cryptoFunctionService.compare(a.buffer, b.buffer);
expect(equal).toBe(false);
});
describe('hash', () => {
const regular1Hash = '2a241604fb921fad12bf877282457268e1dccb70';
const utf81Hash = '85672798dc5831e96d6c48655d3d39365a9c88b6';
const unicode1Hash = '39c975935054a3efc805a9709b60763a823a6ad4';
it("should successfully compare two different values of different lengths", async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const a = new Uint8Array(2);
a[0] = 1;
a[1] = 2;
const b = new Uint8Array(2);
b[0] = 3;
const equal = await cryptoFunctionService.compare(a.buffer, b.buffer);
expect(equal).toBe(false);
});
});
const regular256Hash = '2b8e96031d352a8655d733d7a930b5ffbea69dc25cf65c7bca7dd946278908b2';
const utf8256Hash = '25fe8440f5b01ed113b0a0e38e721b126d2f3f77a67518c4a04fcde4e33eeb9d';
const unicode256Hash = 'adc1c0c2afd6e92cefdf703f9b6eb2c38e0d6d1a040c83f8505c561fea58852e';
describe("hmacFast", () => {
testHmacFast("sha1", Sha1Mac);
testHmacFast("sha256", Sha256Mac);
testHmacFast("sha512", Sha512Mac);
});
const regular512Hash = 'c15cf11d43bde333647e3f559ec4193bb2edeaa0e8b902772f514cdf3f785a3f49a6e02a4b87b3' +
'b47523271ad45b7e0aebb5cdcc1bc54815d256eb5dcb80da9d';
const utf8512Hash = '035c31a877a291af09ed2d3a1a293e69c3e079ea2cecc00211f35e6bce10474ca3ad6e30b59e26118' +
'37463f20969c5bc95282965a051a88f8cdf2e166549fcdd';
const unicode512Hash = '2b16a5561af8ad6fe414cc103fc8036492e1fc6d9aabe1b655497054f760fe0e34c5d100ac773d' +
'9f3030438284f22dbfa20cb2e9b019f2c98dfe38ce1ef41bae';
const regularMd5 = '5eceffa53a5fd58c44134211e2c5f522';
const utf8Md5 = '3abc9433c09551b939c80aa0aa3174e1';
const unicodeMd5 = '85ae134072c8d81257933f7045ba17ca';
testHash('sha1', regular1Hash, utf81Hash, unicode1Hash);
testHash('sha256', regular256Hash, utf8256Hash, unicode256Hash);
testHash('sha512', regular512Hash, utf8512Hash, unicode512Hash);
testHash('md5', regularMd5, utf8Md5, unicodeMd5);
describe("compareFast", () => {
it("should successfully compare two of the same values", async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const a = new Uint8Array(2);
a[0] = 1;
a[1] = 2;
const aByteString = Utils.fromBufferToByteString(a.buffer);
const equal = await cryptoFunctionService.compareFast(aByteString, aByteString);
expect(equal).toBe(true);
});
describe('hmac', () => {
testHmac('sha1', Sha1Mac);
testHmac('sha256', Sha256Mac);
testHmac('sha512', Sha512Mac);
it("should successfully compare two different values of the same length", async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const a = new Uint8Array(2);
a[0] = 1;
a[1] = 2;
const aByteString = Utils.fromBufferToByteString(a.buffer);
const b = new Uint8Array(2);
b[0] = 3;
b[1] = 4;
const bByteString = Utils.fromBufferToByteString(b.buffer);
const equal = await cryptoFunctionService.compareFast(aByteString, bByteString);
expect(equal).toBe(false);
});
describe('compare', () => {
it('should successfully compare two of the same values', async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const a = new Uint8Array(2);
a[0] = 1;
a[1] = 2;
const equal = await cryptoFunctionService.compare(a.buffer, a.buffer);
expect(equal).toBe(true);
});
it("should successfully compare two different values of different lengths", async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const a = new Uint8Array(2);
a[0] = 1;
a[1] = 2;
const aByteString = Utils.fromBufferToByteString(a.buffer);
const b = new Uint8Array(2);
b[0] = 3;
const bByteString = Utils.fromBufferToByteString(b.buffer);
const equal = await cryptoFunctionService.compareFast(aByteString, bByteString);
expect(equal).toBe(false);
});
});
it('should successfully compare two different values of the same length', async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const a = new Uint8Array(2);
a[0] = 1;
a[1] = 2;
const b = new Uint8Array(2);
b[0] = 3;
b[1] = 4;
const equal = await cryptoFunctionService.compare(a.buffer, b.buffer);
expect(equal).toBe(false);
});
it('should successfully compare two different values of different lengths', async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const a = new Uint8Array(2);
a[0] = 1;
a[1] = 2;
const b = new Uint8Array(2);
b[0] = 3;
const equal = await cryptoFunctionService.compare(a.buffer, b.buffer);
expect(equal).toBe(false);
});
describe("aesEncrypt", () => {
it("should successfully encrypt data", async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const iv = makeStaticByteArray(16);
const key = makeStaticByteArray(32);
const data = Utils.fromUtf8ToArray("EncryptMe!");
const encValue = await cryptoFunctionService.aesEncrypt(data.buffer, iv.buffer, key.buffer);
expect(Utils.fromBufferToB64(encValue)).toBe("ByUF8vhyX4ddU9gcooznwA==");
});
describe('hmacFast', () => {
testHmacFast('sha1', Sha1Mac);
testHmacFast('sha256', Sha256Mac);
testHmacFast('sha512', Sha512Mac);
it("should successfully encrypt and then decrypt data fast", async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const iv = makeStaticByteArray(16);
const key = makeStaticByteArray(32);
const value = "EncryptMe!";
const data = Utils.fromUtf8ToArray(value);
const encValue = await cryptoFunctionService.aesEncrypt(data.buffer, iv.buffer, key.buffer);
const encData = Utils.fromBufferToB64(encValue);
const b64Iv = Utils.fromBufferToB64(iv.buffer);
const symKey = new SymmetricCryptoKey(key.buffer);
const params = cryptoFunctionService.aesDecryptFastParameters(encData, b64Iv, null, symKey);
const decValue = await cryptoFunctionService.aesDecryptFast(params);
expect(decValue).toBe(value);
});
describe('compareFast', () => {
it('should successfully compare two of the same values', async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const a = new Uint8Array(2);
a[0] = 1;
a[1] = 2;
const aByteString = Utils.fromBufferToByteString(a.buffer);
const equal = await cryptoFunctionService.compareFast(aByteString, aByteString);
expect(equal).toBe(true);
});
it("should successfully encrypt and then decrypt data", async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const iv = makeStaticByteArray(16);
const key = makeStaticByteArray(32);
const value = "EncryptMe!";
const data = Utils.fromUtf8ToArray(value);
const encValue = await cryptoFunctionService.aesEncrypt(data.buffer, iv.buffer, key.buffer);
const decValue = await cryptoFunctionService.aesDecrypt(encValue, iv.buffer, key.buffer);
expect(Utils.fromBufferToUtf8(decValue)).toBe(value);
});
});
it('should successfully compare two different values of the same length', async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const a = new Uint8Array(2);
a[0] = 1;
a[1] = 2;
const aByteString = Utils.fromBufferToByteString(a.buffer);
const b = new Uint8Array(2);
b[0] = 3;
b[1] = 4;
const bByteString = Utils.fromBufferToByteString(b.buffer);
const equal = await cryptoFunctionService.compareFast(aByteString, bByteString);
expect(equal).toBe(false);
});
describe("aesDecryptFast", () => {
it("should successfully decrypt data", async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const iv = Utils.fromBufferToB64(makeStaticByteArray(16).buffer);
const symKey = new SymmetricCryptoKey(makeStaticByteArray(32).buffer);
const data = "ByUF8vhyX4ddU9gcooznwA==";
const params = cryptoFunctionService.aesDecryptFastParameters(data, iv, null, symKey);
const decValue = await cryptoFunctionService.aesDecryptFast(params);
expect(decValue).toBe("EncryptMe!");
});
});
it('should successfully compare two different values of different lengths', async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const a = new Uint8Array(2);
a[0] = 1;
a[1] = 2;
const aByteString = Utils.fromBufferToByteString(a.buffer);
const b = new Uint8Array(2);
b[0] = 3;
const bByteString = Utils.fromBufferToByteString(b.buffer);
const equal = await cryptoFunctionService.compareFast(aByteString, bByteString);
expect(equal).toBe(false);
});
describe("aesDecrypt", () => {
it("should successfully decrypt data", async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const iv = makeStaticByteArray(16);
const key = makeStaticByteArray(32);
const data = Utils.fromB64ToArray("ByUF8vhyX4ddU9gcooznwA==");
const decValue = await cryptoFunctionService.aesDecrypt(data.buffer, iv.buffer, key.buffer);
expect(Utils.fromBufferToUtf8(decValue)).toBe("EncryptMe!");
});
});
describe("rsaEncrypt", () => {
it("should successfully encrypt and then decrypt data", async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const pubKey = Utils.fromB64ToArray(RsaPublicKey);
const privKey = Utils.fromB64ToArray(RsaPrivateKey);
const value = "EncryptMe!";
const data = Utils.fromUtf8ToArray(value);
const encValue = await cryptoFunctionService.rsaEncrypt(data.buffer, pubKey.buffer, "sha1");
const decValue = await cryptoFunctionService.rsaDecrypt(encValue, privKey.buffer, "sha1");
expect(Utils.fromBufferToUtf8(decValue)).toBe(value);
});
});
describe("rsaDecrypt", () => {
it("should successfully decrypt data", async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const privKey = Utils.fromB64ToArray(RsaPrivateKey);
const data = Utils.fromB64ToArray(
"A1/p8BQzN9UrbdYxUY2Va5+kPLyfZXF9JsZrjeEXcaclsnHurdxVAJcnbEqYMP3UXV" +
"4YAS/mpf+Rxe6/X0WS1boQdA0MAHSgx95hIlAraZYpiMLLiJRKeo2u8YivCdTM9V5vuAEJwf9Tof/qFsFci3sApdbATkorCT" +
"zFOIEPF2S1zgperEP23M01mr4dWVdYN18B32YF67xdJHMbFhp5dkQwv9CmscoWq7OE5HIfOb+JAh7BEZb+CmKhM3yWJvoR/D" +
"/5jcercUtK2o+XrzNrL4UQ7yLZcFz6Bfwb/j6ICYvqd/YJwXNE6dwlL57OfwJyCdw2rRYf0/qI00t9u8Iitw=="
);
const decValue = await cryptoFunctionService.rsaDecrypt(data.buffer, privKey.buffer, "sha1");
expect(Utils.fromBufferToUtf8(decValue)).toBe("EncryptMe!");
});
});
describe("rsaExtractPublicKey", () => {
it("should successfully extract key", async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const privKey = Utils.fromB64ToArray(RsaPrivateKey);
const publicKey = await cryptoFunctionService.rsaExtractPublicKey(privKey.buffer);
expect(Utils.fromBufferToB64(publicKey)).toBe(RsaPublicKey);
});
});
describe("rsaGenerateKeyPair", () => {
testRsaGenerateKeyPair(1024);
testRsaGenerateKeyPair(2048);
// Generating 4096 bit keys can be slow. Commenting it out to save CI.
// testRsaGenerateKeyPair(4096);
});
describe("randomBytes", () => {
it("should make a value of the correct length", async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const randomData = await cryptoFunctionService.randomBytes(16);
expect(randomData.byteLength).toBe(16);
});
describe('aesEncrypt', () => {
it('should successfully encrypt data', async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const iv = makeStaticByteArray(16);
const key = makeStaticByteArray(32);
const data = Utils.fromUtf8ToArray('EncryptMe!');
const encValue = await cryptoFunctionService.aesEncrypt(data.buffer, iv.buffer, key.buffer);
expect(Utils.fromBufferToB64(encValue)).toBe('ByUF8vhyX4ddU9gcooznwA==');
});
it('should successfully encrypt and then decrypt data fast', async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const iv = makeStaticByteArray(16);
const key = makeStaticByteArray(32);
const value = 'EncryptMe!';
const data = Utils.fromUtf8ToArray(value);
const encValue = await cryptoFunctionService.aesEncrypt(data.buffer, iv.buffer, key.buffer);
const encData = Utils.fromBufferToB64(encValue);
const b64Iv = Utils.fromBufferToB64(iv.buffer);
const symKey = new SymmetricCryptoKey(key.buffer);
const params = cryptoFunctionService.aesDecryptFastParameters(encData, b64Iv, null, symKey);
const decValue = await cryptoFunctionService.aesDecryptFast(params);
expect(decValue).toBe(value);
});
it('should successfully encrypt and then decrypt data', async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const iv = makeStaticByteArray(16);
const key = makeStaticByteArray(32);
const value = 'EncryptMe!';
const data = Utils.fromUtf8ToArray(value);
const encValue = await cryptoFunctionService.aesEncrypt(data.buffer, iv.buffer, key.buffer);
const decValue = await cryptoFunctionService.aesDecrypt(encValue, iv.buffer, key.buffer);
expect(Utils.fromBufferToUtf8(decValue)).toBe(value);
});
});
describe('aesDecryptFast', () => {
it('should successfully decrypt data', async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const iv = Utils.fromBufferToB64(makeStaticByteArray(16).buffer);
const symKey = new SymmetricCryptoKey(makeStaticByteArray(32).buffer);
const data = 'ByUF8vhyX4ddU9gcooznwA==';
const params = cryptoFunctionService.aesDecryptFastParameters(data, iv, null, symKey);
const decValue = await cryptoFunctionService.aesDecryptFast(params);
expect(decValue).toBe('EncryptMe!');
});
});
describe('aesDecrypt', () => {
it('should successfully decrypt data', async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const iv = makeStaticByteArray(16);
const key = makeStaticByteArray(32);
const data = Utils.fromB64ToArray('ByUF8vhyX4ddU9gcooznwA==');
const decValue = await cryptoFunctionService.aesDecrypt(data.buffer, iv.buffer, key.buffer);
expect(Utils.fromBufferToUtf8(decValue)).toBe('EncryptMe!');
});
});
describe('rsaEncrypt', () => {
it('should successfully encrypt and then decrypt data', async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const pubKey = Utils.fromB64ToArray(RsaPublicKey);
const privKey = Utils.fromB64ToArray(RsaPrivateKey);
const value = 'EncryptMe!';
const data = Utils.fromUtf8ToArray(value);
const encValue = await cryptoFunctionService.rsaEncrypt(data.buffer, pubKey.buffer, 'sha1');
const decValue = await cryptoFunctionService.rsaDecrypt(encValue, privKey.buffer, 'sha1');
expect(Utils.fromBufferToUtf8(decValue)).toBe(value);
});
});
describe('rsaDecrypt', () => {
it('should successfully decrypt data', async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const privKey = Utils.fromB64ToArray(RsaPrivateKey);
const data = Utils.fromB64ToArray('A1/p8BQzN9UrbdYxUY2Va5+kPLyfZXF9JsZrjeEXcaclsnHurdxVAJcnbEqYMP3UXV' +
'4YAS/mpf+Rxe6/X0WS1boQdA0MAHSgx95hIlAraZYpiMLLiJRKeo2u8YivCdTM9V5vuAEJwf9Tof/qFsFci3sApdbATkorCT' +
'zFOIEPF2S1zgperEP23M01mr4dWVdYN18B32YF67xdJHMbFhp5dkQwv9CmscoWq7OE5HIfOb+JAh7BEZb+CmKhM3yWJvoR/D' +
'/5jcercUtK2o+XrzNrL4UQ7yLZcFz6Bfwb/j6ICYvqd/YJwXNE6dwlL57OfwJyCdw2rRYf0/qI00t9u8Iitw==');
const decValue = await cryptoFunctionService.rsaDecrypt(data.buffer, privKey.buffer, 'sha1');
expect(Utils.fromBufferToUtf8(decValue)).toBe('EncryptMe!');
});
});
describe('rsaExtractPublicKey', () => {
it('should successfully extract key', async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const privKey = Utils.fromB64ToArray(RsaPrivateKey);
const publicKey = await cryptoFunctionService.rsaExtractPublicKey(privKey.buffer);
expect(Utils.fromBufferToB64(publicKey)).toBe(RsaPublicKey);
});
});
describe('rsaGenerateKeyPair', () => {
testRsaGenerateKeyPair(1024);
testRsaGenerateKeyPair(2048);
// Generating 4096 bit keys can be slow. Commenting it out to save CI.
// testRsaGenerateKeyPair(4096);
});
describe('randomBytes', () => {
it('should make a value of the correct length', async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const randomData = await cryptoFunctionService.randomBytes(16);
expect(randomData.byteLength).toBe(16);
});
it('should not make the same value twice', async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const randomData = await cryptoFunctionService.randomBytes(16);
const randomData2 = await cryptoFunctionService.randomBytes(16);
expect(randomData.byteLength === randomData2.byteLength && randomData !== randomData2).toBeTruthy();
});
it("should not make the same value twice", async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const randomData = await cryptoFunctionService.randomBytes(16);
const randomData2 = await cryptoFunctionService.randomBytes(16);
expect(
randomData.byteLength === randomData2.byteLength && randomData !== randomData2
).toBeTruthy();
});
});
});
function testPbkdf2(algorithm: 'sha256' | 'sha512', regularKey: string,
utf8Key: string, unicodeKey: string) {
const regularEmail = 'user@example.com';
const utf8Email = 'üser@example.com';
function testPbkdf2(
algorithm: "sha256" | "sha512",
regularKey: string,
utf8Key: string,
unicodeKey: string
) {
const regularEmail = "user@example.com";
const utf8Email = "üser@example.com";
const regularPassword = 'password';
const utf8Password = 'pǻssword';
const unicodePassword = '😀password🙏';
const regularPassword = "password";
const utf8Password = "pǻssword";
const unicodePassword = "😀password🙏";
it('should create valid ' + algorithm + ' key from regular input', async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const key = await cryptoFunctionService.pbkdf2(regularPassword, regularEmail, algorithm, 5000);
expect(Utils.fromBufferToB64(key)).toBe(regularKey);
});
it("should create valid " + algorithm + " key from regular input", async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const key = await cryptoFunctionService.pbkdf2(regularPassword, regularEmail, algorithm, 5000);
expect(Utils.fromBufferToB64(key)).toBe(regularKey);
});
it('should create valid ' + algorithm + ' key from utf8 input', async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const key = await cryptoFunctionService.pbkdf2(utf8Password, utf8Email, algorithm, 5000);
expect(Utils.fromBufferToB64(key)).toBe(utf8Key);
});
it("should create valid " + algorithm + " key from utf8 input", async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const key = await cryptoFunctionService.pbkdf2(utf8Password, utf8Email, algorithm, 5000);
expect(Utils.fromBufferToB64(key)).toBe(utf8Key);
});
it('should create valid ' + algorithm + ' key from unicode input', async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const key = await cryptoFunctionService.pbkdf2(unicodePassword, regularEmail, algorithm, 5000);
expect(Utils.fromBufferToB64(key)).toBe(unicodeKey);
});
it("should create valid " + algorithm + " key from unicode input", async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const key = await cryptoFunctionService.pbkdf2(unicodePassword, regularEmail, algorithm, 5000);
expect(Utils.fromBufferToB64(key)).toBe(unicodeKey);
});
it('should create valid ' + algorithm + ' key from array buffer input', async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const key = await cryptoFunctionService.pbkdf2(Utils.fromUtf8ToArray(regularPassword).buffer,
Utils.fromUtf8ToArray(regularEmail).buffer, algorithm, 5000);
expect(Utils.fromBufferToB64(key)).toBe(regularKey);
});
it("should create valid " + algorithm + " key from array buffer input", async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const key = await cryptoFunctionService.pbkdf2(
Utils.fromUtf8ToArray(regularPassword).buffer,
Utils.fromUtf8ToArray(regularEmail).buffer,
algorithm,
5000
);
expect(Utils.fromBufferToB64(key)).toBe(regularKey);
});
}
function testHkdf(algorithm: 'sha256' | 'sha512', regularKey: string, utf8Key: string, unicodeKey: string) {
const ikm = Utils.fromB64ToArray('criAmKtfzxanbgea5/kelQ==');
function testHkdf(
algorithm: "sha256" | "sha512",
regularKey: string,
utf8Key: string,
unicodeKey: string
) {
const ikm = Utils.fromB64ToArray("criAmKtfzxanbgea5/kelQ==");
const regularSalt = 'salt';
const utf8Salt = 'üser_salt';
const unicodeSalt = '😀salt🙏';
const regularSalt = "salt";
const utf8Salt = "üser_salt";
const unicodeSalt = "😀salt🙏";
const regularInfo = 'info';
const utf8Info = 'üser_info';
const unicodeInfo = '😀info🙏';
const regularInfo = "info";
const utf8Info = "üser_info";
const unicodeInfo = "😀info🙏";
it('should create valid ' + algorithm + ' key from regular input', async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const key = await cryptoFunctionService.hkdf(ikm, regularSalt, regularInfo, 32, algorithm);
expect(Utils.fromBufferToB64(key)).toBe(regularKey);
});
it("should create valid " + algorithm + " key from regular input", async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const key = await cryptoFunctionService.hkdf(ikm, regularSalt, regularInfo, 32, algorithm);
expect(Utils.fromBufferToB64(key)).toBe(regularKey);
});
it('should create valid ' + algorithm + ' key from utf8 input', async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const key = await cryptoFunctionService.hkdf(ikm, utf8Salt, utf8Info, 32, algorithm);
expect(Utils.fromBufferToB64(key)).toBe(utf8Key);
});
it("should create valid " + algorithm + " key from utf8 input", async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const key = await cryptoFunctionService.hkdf(ikm, utf8Salt, utf8Info, 32, algorithm);
expect(Utils.fromBufferToB64(key)).toBe(utf8Key);
});
it('should create valid ' + algorithm + ' key from unicode input', async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const key = await cryptoFunctionService.hkdf(ikm, unicodeSalt, unicodeInfo, 32, algorithm);
expect(Utils.fromBufferToB64(key)).toBe(unicodeKey);
});
it("should create valid " + algorithm + " key from unicode input", async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const key = await cryptoFunctionService.hkdf(ikm, unicodeSalt, unicodeInfo, 32, algorithm);
expect(Utils.fromBufferToB64(key)).toBe(unicodeKey);
});
it('should create valid ' + algorithm + ' key from array buffer input', async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const key = await cryptoFunctionService.hkdf(ikm, Utils.fromUtf8ToArray(regularSalt).buffer,
Utils.fromUtf8ToArray(regularInfo).buffer, 32, algorithm);
expect(Utils.fromBufferToB64(key)).toBe(regularKey);
});
it("should create valid " + algorithm + " key from array buffer input", async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const key = await cryptoFunctionService.hkdf(
ikm,
Utils.fromUtf8ToArray(regularSalt).buffer,
Utils.fromUtf8ToArray(regularInfo).buffer,
32,
algorithm
);
expect(Utils.fromBufferToB64(key)).toBe(regularKey);
});
}
function testHkdfExpand(algorithm: 'sha256' | 'sha512', b64prk: string, outputByteSize: number,
b64ExpectedOkm: string) {
const info = 'info';
function testHkdfExpand(
algorithm: "sha256" | "sha512",
b64prk: string,
outputByteSize: number,
b64ExpectedOkm: string
) {
const info = "info";
it('should create valid ' + algorithm + ' ' + outputByteSize + ' byte okm', async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const okm = await cryptoFunctionService.hkdfExpand(Utils.fromB64ToArray(b64prk), info, outputByteSize,
algorithm);
expect(Utils.fromBufferToB64(okm)).toBe(b64ExpectedOkm);
});
it("should create valid " + algorithm + " " + outputByteSize + " byte okm", async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const okm = await cryptoFunctionService.hkdfExpand(
Utils.fromB64ToArray(b64prk),
info,
outputByteSize,
algorithm
);
expect(Utils.fromBufferToB64(okm)).toBe(b64ExpectedOkm);
});
}
function testHash(algorithm: 'sha1' | 'sha256' | 'sha512' | 'md5', regularHash: string,
utf8Hash: string, unicodeHash: string) {
const regularValue = 'HashMe!!';
const utf8Value = 'HǻshMe!!';
const unicodeValue = '😀HashMe!!!🙏';
function testHash(
algorithm: "sha1" | "sha256" | "sha512" | "md5",
regularHash: string,
utf8Hash: string,
unicodeHash: string
) {
const regularValue = "HashMe!!";
const utf8Value = "HǻshMe!!";
const unicodeValue = "😀HashMe!!!🙏";
it('should create valid ' + algorithm + ' hash from regular input', async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const hash = await cryptoFunctionService.hash(regularValue, algorithm);
expect(Utils.fromBufferToHex(hash)).toBe(regularHash);
});
it("should create valid " + algorithm + " hash from regular input", async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const hash = await cryptoFunctionService.hash(regularValue, algorithm);
expect(Utils.fromBufferToHex(hash)).toBe(regularHash);
});
it('should create valid ' + algorithm + ' hash from utf8 input', async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const hash = await cryptoFunctionService.hash(utf8Value, algorithm);
expect(Utils.fromBufferToHex(hash)).toBe(utf8Hash);
});
it("should create valid " + algorithm + " hash from utf8 input", async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const hash = await cryptoFunctionService.hash(utf8Value, algorithm);
expect(Utils.fromBufferToHex(hash)).toBe(utf8Hash);
});
it('should create valid ' + algorithm + ' hash from unicode input', async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const hash = await cryptoFunctionService.hash(unicodeValue, algorithm);
expect(Utils.fromBufferToHex(hash)).toBe(unicodeHash);
});
it("should create valid " + algorithm + " hash from unicode input", async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const hash = await cryptoFunctionService.hash(unicodeValue, algorithm);
expect(Utils.fromBufferToHex(hash)).toBe(unicodeHash);
});
it('should create valid ' + algorithm + ' hash from array buffer input', async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const hash = await cryptoFunctionService.hash(Utils.fromUtf8ToArray(regularValue).buffer, algorithm);
expect(Utils.fromBufferToHex(hash)).toBe(regularHash);
});
it("should create valid " + algorithm + " hash from array buffer input", async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const hash = await cryptoFunctionService.hash(
Utils.fromUtf8ToArray(regularValue).buffer,
algorithm
);
expect(Utils.fromBufferToHex(hash)).toBe(regularHash);
});
}
function testHmac(algorithm: 'sha1' | 'sha256' | 'sha512', mac: string) {
it('should create valid ' + algorithm + ' hmac', async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const computedMac = await cryptoFunctionService.hmac(Utils.fromUtf8ToArray('SignMe!!').buffer,
Utils.fromUtf8ToArray('secretkey').buffer, algorithm);
expect(Utils.fromBufferToHex(computedMac)).toBe(mac);
});
function testHmac(algorithm: "sha1" | "sha256" | "sha512", mac: string) {
it("should create valid " + algorithm + " hmac", async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const computedMac = await cryptoFunctionService.hmac(
Utils.fromUtf8ToArray("SignMe!!").buffer,
Utils.fromUtf8ToArray("secretkey").buffer,
algorithm
);
expect(Utils.fromBufferToHex(computedMac)).toBe(mac);
});
}
function testHmacFast(algorithm: 'sha1' | 'sha256' | 'sha512', mac: string) {
it('should create valid ' + algorithm + ' hmac', async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const keyByteString = Utils.fromBufferToByteString(Utils.fromUtf8ToArray('secretkey').buffer);
const dataByteString = Utils.fromBufferToByteString(Utils.fromUtf8ToArray('SignMe!!').buffer);
const computedMac = await cryptoFunctionService.hmacFast(dataByteString, keyByteString, algorithm);
expect(Utils.fromBufferToHex(Utils.fromByteStringToArray(computedMac).buffer)).toBe(mac);
});
function testHmacFast(algorithm: "sha1" | "sha256" | "sha512", mac: string) {
it("should create valid " + algorithm + " hmac", async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const keyByteString = Utils.fromBufferToByteString(Utils.fromUtf8ToArray("secretkey").buffer);
const dataByteString = Utils.fromBufferToByteString(Utils.fromUtf8ToArray("SignMe!!").buffer);
const computedMac = await cryptoFunctionService.hmacFast(
dataByteString,
keyByteString,
algorithm
);
expect(Utils.fromBufferToHex(Utils.fromByteStringToArray(computedMac).buffer)).toBe(mac);
});
}
function testRsaGenerateKeyPair(length: 1024 | 2048 | 4096) {
it('should successfully generate a ' + length + ' bit key pair', async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const keyPair = await cryptoFunctionService.rsaGenerateKeyPair(length);
expect(keyPair[0] == null || keyPair[1] == null).toBe(false);
const publicKey = await cryptoFunctionService.rsaExtractPublicKey(keyPair[1]);
expect(Utils.fromBufferToB64(keyPair[0])).toBe(Utils.fromBufferToB64(publicKey));
}, 30000);
it(
"should successfully generate a " + length + " bit key pair",
async () => {
const cryptoFunctionService = getWebCryptoFunctionService();
const keyPair = await cryptoFunctionService.rsaGenerateKeyPair(length);
expect(keyPair[0] == null || keyPair[1] == null).toBe(false);
const publicKey = await cryptoFunctionService.rsaExtractPublicKey(keyPair[1]);
expect(Utils.fromBufferToB64(keyPair[0])).toBe(Utils.fromBufferToB64(publicKey));
},
30000
);
}
function getWebCryptoFunctionService() {
const platformUtilsMock = Substitute.for<PlatformUtilsService>();
platformUtilsMock.isEdge().mimicks(() => navigator.userAgent.indexOf(' Edg/') !== -1);
platformUtilsMock.isIE().mimicks(() => navigator.userAgent.indexOf(' Edg/') === -1 &&
navigator.userAgent.indexOf(' Trident/') !== -1);
const platformUtilsMock = Substitute.for<PlatformUtilsService>();
platformUtilsMock.isEdge().mimicks(() => navigator.userAgent.indexOf(" Edg/") !== -1);
platformUtilsMock
.isIE()
.mimicks(
() =>
navigator.userAgent.indexOf(" Edg/") === -1 &&
navigator.userAgent.indexOf(" Trident/") !== -1
);
return new WebCryptoFunctionService(window, platformUtilsMock);
return new WebCryptoFunctionService(window, platformUtilsMock);
}
function makeStaticByteArray(length: number) {
const arr = new Uint8Array(length);
for (let i = 0; i < length; i++) {
arr[i] = i;
}
return arr;
const arr = new Uint8Array(length);
for (let i = 0; i < length; i++) {
arr[i] = i;
}
return arr;
}