Generate IDs using random words
This commit is contained in:
parent
c4034e5144
commit
c8400ea3ea
1 changed files with 14 additions and 11 deletions
|
|
@ -5,6 +5,7 @@
|
||||||
const Path = require('path')
|
const Path = require('path')
|
||||||
const Storage = require('node-persist')
|
const Storage = require('node-persist')
|
||||||
const expect = require('expect')
|
const expect = require('expect')
|
||||||
|
const words = require('random-words')
|
||||||
|
|
||||||
const {
|
const {
|
||||||
authorizeDevice,
|
authorizeDevice,
|
||||||
|
|
@ -14,13 +15,15 @@ const {
|
||||||
isAuthorizedDevice
|
isAuthorizedDevice
|
||||||
} = require('./ECC')
|
} = require('./ECC')
|
||||||
|
|
||||||
|
const uuid = () => words({ exactly: 24 }).join('-')
|
||||||
|
|
||||||
const storageDirectory = Path.resolve(__dirname, `./.test-storage`)
|
const storageDirectory = Path.resolve(__dirname, `./.test-storage`)
|
||||||
|
|
||||||
console.log(`Storage directory: ${storageDirectory}`)
|
console.log(`Storage directory: ${storageDirectory}`)
|
||||||
|
|
||||||
describe('generateKeyPair()', () => {
|
describe('generateKeyPair()', () => {
|
||||||
it('generates a keypair', () => {
|
it('generates a keypair', () => {
|
||||||
const pair = generateKeyPair('jgjhfhjhjghjghjgkghfkhgfkh')
|
const pair = generateKeyPair(uuid())
|
||||||
|
|
||||||
expect(pair.privateKey).toBeInstanceOf(Buffer)
|
expect(pair.privateKey).toBeInstanceOf(Buffer)
|
||||||
expect(typeof pair.privateKeyBase64 === 'string').toBeTruthy()
|
expect(typeof pair.privateKeyBase64 === 'string').toBeTruthy()
|
||||||
|
|
@ -28,7 +31,7 @@ describe('generateKeyPair()', () => {
|
||||||
expect(typeof pair.publicKeyBase64 === 'string').toBeTruthy()
|
expect(typeof pair.publicKeyBase64 === 'string').toBeTruthy()
|
||||||
})
|
})
|
||||||
it('returns the same pair for the same device', () => {
|
it('returns the same pair for the same device', () => {
|
||||||
const id = 'fbuiio3089fhfunjancj,'
|
const id = uuid()
|
||||||
const pair = generateKeyPair(id)
|
const pair = generateKeyPair(id)
|
||||||
const pairAgain = generateKeyPair(id)
|
const pairAgain = generateKeyPair(id)
|
||||||
|
|
||||||
|
|
@ -42,7 +45,7 @@ describe('authorizeDevice()/isAuthorizedDevice()', () => {
|
||||||
await Storage.init({
|
await Storage.init({
|
||||||
dir: storageDirectory
|
dir: storageDirectory
|
||||||
})
|
})
|
||||||
const deviceId = 'ajksjkihjgjhfkjasbdjkabsf'
|
const deviceId = uuid()
|
||||||
const pair = generateKeyPair(deviceId)
|
const pair = generateKeyPair(deviceId)
|
||||||
await authorizeDevice({ deviceId, publicKey: pair.publicKeyBase64 })
|
await authorizeDevice({ deviceId, publicKey: pair.publicKeyBase64 })
|
||||||
expect(isAuthorizedDevice({ deviceId })).toBeTruthy()
|
expect(isAuthorizedDevice({ deviceId })).toBeTruthy()
|
||||||
|
|
@ -57,11 +60,11 @@ describe('encryptMessage()/decryptMessage()', () => {
|
||||||
)
|
)
|
||||||
it('throws if provided with an unauthorized device id when encrypting', async () => {
|
it('throws if provided with an unauthorized device id when encrypting', async () => {
|
||||||
expect.hasAssertions()
|
expect.hasAssertions()
|
||||||
const deviceId = 'jfio2fb3h803fabsc018hfuIUFiufh9310u'
|
const deviceId = uuid()
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await encryptMessage({
|
await encryptMessage({
|
||||||
message: 'klashdkljaskldjkasjlkdjaslkjd',
|
message: uuid(),
|
||||||
deviceId
|
deviceId
|
||||||
})
|
})
|
||||||
throw new Error('encryptMessage() did not throw')
|
throw new Error('encryptMessage() did not throw')
|
||||||
|
|
@ -77,11 +80,11 @@ describe('encryptMessage()/decryptMessage()', () => {
|
||||||
await decryptMessage({
|
await decryptMessage({
|
||||||
deviceId,
|
deviceId,
|
||||||
encryptedMessage: {
|
encryptedMessage: {
|
||||||
ciphertext: 'kajjshfkjhaskjdh',
|
ciphertext: uuid(),
|
||||||
ephemPublicKey: 'akjshfjkashkjhasf',
|
ephemPublicKey: uuid(),
|
||||||
iv: 'lkasjdklahsfkjhasf',
|
iv: uuid(),
|
||||||
mac: 'alkshfkjashfkjasf',
|
mac: uuid(),
|
||||||
metadata: 'aklshdkjasd'
|
metadata: uuid()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
throw new Error('decryptMessage() did not throw')
|
throw new Error('decryptMessage() did not throw')
|
||||||
|
|
@ -91,7 +94,7 @@ describe('encryptMessage()/decryptMessage()', () => {
|
||||||
})
|
})
|
||||||
it('encrypts and decrypts messages when given a known device id', async () => {
|
it('encrypts and decrypts messages when given a known device id', async () => {
|
||||||
expect.hasAssertions()
|
expect.hasAssertions()
|
||||||
const deviceId = 'jfio2fbnjkabscfjjpifpoijf018hfuIUFiufh9310u'
|
const deviceId = uuid()
|
||||||
|
|
||||||
const pair = generateKeyPair(deviceId)
|
const pair = generateKeyPair(deviceId)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue