Skip to content

Commit

Permalink
feat: add aegis -> tokenvault tool (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
amanharwara authored Mar 14, 2022
1 parent 2443386 commit dd52751
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 4 deletions.
53 changes: 53 additions & 0 deletions dist/sntools.js
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,59 @@ class Tools {
readNext();
}

convertAegisFile(file) {
const processedData = [];
const dateString = new Date().toLocaleDateString().replace(/\//g, "-");
const defaultTag = {
uuid: this.generateUUID(),
content_type: "Tag",
content: {
title: `${dateString}-import`,
references: []
}
};
processedData.push(defaultTag);
const data = this.parseJsonAegis(file.content);
const note = {
created_at: new Date(file.lastModified),
updated_at: new Date(file.lastModified),
uuid: this.generateUUID(),
content_type: "Note",
content: {
title: file.name.split(".")[0],
text: data,
references: []
}
};
this.setClientUpdatedAt(note, note.updated_at);
processedData.push(note);
defaultTag.content.references.push({
content_type: "Note",
uuid: note.uuid
});
return {
items: processedData
};
}

parseJsonAegis(file) {
try {
const json = JSON.parse(file);
const entries = json.db.entries.map(entry => {
return {
service: entry.issuer,
account: entry.name,
secret: entry.info.secret,
notes: entry.note
};
});
return JSON.stringify(entries);
} catch (error) {
console.error(error);
return null;
}
}

convertSimplenoteFiles(rawNotes) {
const finalNotes = [];

Expand Down
54 changes: 54 additions & 0 deletions lib/sntools.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,60 @@ export class Tools {
readNext();
}

convertAegisFile(file) {
const processedData = [];

const dateString = new Date().toLocaleDateString().replace(/\//g, "-");
const defaultTag = {
uuid: this.generateUUID(),
content_type: "Tag",
content: {
title: `${dateString}-import`,
references: [],
},
};
processedData.push(defaultTag);

const data = this.parseJsonAegis(file.content);
const note = {
created_at: new Date(file.lastModified),
updated_at: new Date(file.lastModified),
uuid: this.generateUUID(),
content_type: "Note",
content: {
title: file.name.split(".")[0],
text: data,
references: [],
},
};
this.setClientUpdatedAt(note, note.updated_at);
processedData.push(note);
defaultTag.content.references.push({
content_type: "Note",
uuid: note.uuid,
});

return { items: processedData };
}

parseJsonAegis(file) {
try {
const json = JSON.parse(file);
const entries = json.db.entries.map((entry) => {
return {
service: entry.issuer,
account: entry.name,
secret: entry.info.secret,
notes: entry.note,
};
});
return JSON.stringify(entries);
} catch (error) {
console.error(error);
return null;
}
}

convertSimplenoteFiles(rawNotes) {
const finalNotes = [];

Expand Down
67 changes: 64 additions & 3 deletions lib/sntools.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { readFileSync } from 'fs';
import { readFileSync, statSync } from 'fs';
import { join } from 'path';
import { Tools } from './sntools';

Expand Down Expand Up @@ -125,7 +125,7 @@ describe('sn-tools', () => {
uuid: expect.stringMatching(uuidFormat),
content_type: 'Note',
content: {
title: 'Imported note 2 from ENote',
title: 'Imported note 2 from Evernote',
text: '<div>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</div>',
references: [],
appData: {
Expand All @@ -142,7 +142,7 @@ describe('sn-tools', () => {
uuid: expect.stringMatching(uuidFormat),
content_type: 'Note',
content: {
title: 'Imported note 2 from ENote',
title: 'Imported note 2 from Evernote',
text: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
references: [],
appData: {
Expand Down Expand Up @@ -175,6 +175,67 @@ describe('sn-tools', () => {
});
});

describe('convertAegisToSN()', () => {
const filePath = join(__dirname, '../test/data/aegis/backup.json');
const content = readFileSync(filePath);
const metadata = statSync(filePath);
const parsedFile = tools.convertAegisFile({
name: 'backup.json',
lastModified: metadata.mtime,
content,
});

it('should return 2 valid items', () => {
expect(parsedFile).not.toBe(undefined);
expect(parsedFile.items).toBeDefined();
expect(parsedFile.items.length).toBe(2); // 1 Note and 1 Tag

expect(parsedFile).not.toBe(undefined);
expect(parsedFile.items).toBeDefined();
expect(parsedFile.items.length).toBe(2); // 1 Note and 1 Tag
});

const { items } = parsedFile;

const firstItem = items[0];
const secondItem = items[1];

test("first item should be a Tag", () => {
expect(firstItem).toEqual({
uuid: expect.stringMatching(uuidFormat),
content_type: 'Tag',
content: {
title: `${new Date('2022-03-13').toLocaleDateString().replace(/\//g, '-')}-import`,
references: [
{
content_type: 'Note',
uuid: secondItem.uuid
}
]
}
});
});

test("second item should be a Note", () => {
expect(secondItem).toEqual({
created_at: new Date(metadata.mtime),
updated_at: new Date(metadata.mtime),
uuid: expect.stringMatching(uuidFormat),
content_type: "Note",
content: {
title: "backup",
text: "[{\"service\":\"TestMail\",\"account\":\"[email protected]\",\"secret\":\"TESTMAILTESTMAILTESTMAILTESTMAIL\",\"notes\":\"Some note\"},{\"service\":\"Some Service\",\"account\":\"[email protected]\",\"secret\":\"SOMESERVICESOMESERVICESOMESERVIC\",\"notes\":\"Some other service\"}]",
references: [],
appData: {
"org.standardnotes.sn": {
client_updated_at: new Date(metadata.mtime),
},
},
},
});
});
});

describe('convertGKeepNotes()', () => {
const filesPath = join(__dirname, '../test/data/google-keep');
const exportFile1 = readFileSync(join(filesPath, 'note-1.json'));
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "sntools",
"author": "Standard Notes <[email protected]>",
"license": "AGPL-3.0",
"version": "1.0.2",
"version": "1.1.0",
"main": "dist/sntools.js",
"scripts": {
"start": "webpack serve --config webpack.dev.js --progress --hot",
Expand Down
40 changes: 40 additions & 0 deletions test/data/aegis/backup.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"version": 1,
"header": {
"slots": null,
"params": null
},
"db": {
"version": 2,
"entries": [
{
"type": "totp",
"uuid": "c74a11c4-4f23-417b-818a-e11f6a4d51d7",
"name": "[email protected]",
"issuer": "TestMail",
"note": "Some note",
"icon": null,
"info": {
"secret": "TESTMAILTESTMAILTESTMAILTESTMAIL",
"algo": "SHA1",
"digits": 6,
"period": 30
}
},
{
"type": "totp",
"uuid": "803ed58f-b2c4-386c-9aad-645a47309124",
"name": "[email protected]",
"issuer": "Some Service",
"note": "Some other service",
"icon": null,
"info": {
"secret": "SOMESERVICESOMESERVICESOMESERVIC",
"algo": "SHA1",
"digits": 6,
"period": 30
}
}
]
}
}

0 comments on commit dd52751

Please sign in to comment.