Ind.ie is now Small Technology Foundation.
Commit 242daec0 authored by wim-vantomme's avatar wim-vantomme

Added keys service.

Added POST /keys
Added GET /keys/param
parent 8ac844f2
......@@ -30,6 +30,14 @@ class Keys {
encryptPrivateKey () {
this.encryptedPrivateKey = sodium.crypto_pwhash(this.keySettings.keyLength, this.privateKey, this.password, this.keySettings.opslimit, this.keySettings.memlimit, this.keySettings.algorithm)
}
stringify (value) {
return sodium.to_hex(value)
}
parse (value) {
return sodium.from_hex(value)
}
}
module.exports = Keys
......@@ -26,11 +26,11 @@ form.addEventListener('submit', (e) => {
keyPair.encryptPassword(password)
keyPair.encryptPrivateKey()
console.log(keyPair)
const postKeys = axios.post('register',
const postKeys = axios.post('keys',
{
salt: keyPair.salt,
publicKey: keyPair.publicKey,
privateKey: keyPair.encryptedPrivateKey
salt: keyPair.stringify(keyPair.salt),
publicKey: keyPair.stringify(keyPair.publicKey),
privateKey: keyPair.stringify(keyPair.encryptedPrivateKey)
})
postKeys.then((values) => {
......
......@@ -28,9 +28,9 @@
}
},
"@feathersjs/errors": {
"version": "3.2.1",
"resolved": "https://registry.npmjs.org/@feathersjs/errors/-/errors-3.2.1.tgz",
"integrity": "sha512-jUmYTE5Aw/FGqZeX16kddfakvMMhxQb8WR1ZDq0tuokaJzGxJmH2MuVlvt5Vmbp5DQ6zb7+JROJE7dmtfrE83g==",
"version": "3.2.2",
"resolved": "https://registry.npmjs.org/@feathersjs/errors/-/errors-3.2.2.tgz",
"integrity": "sha512-j2SiZBUimcUAFXGnmiByQKV+8/OehnJFGP5wT3XKjU6nyiLft6Av5ti1/sRV+ji2Tu80H6YVfhIoVG8ndEpBnA==",
"requires": {
"debug": "3.1.0"
}
......@@ -41,7 +41,7 @@
"integrity": "sha512-oxXZs3QAZB80fBajb7nh3Pbongm8ESXVnJw1EuX7fui4JOXh2qGw9hf8e3xKAZ/fmkHmgofdA1A3iqIzBg3Isw==",
"requires": {
"@feathersjs/commons": "1.4.0",
"@feathersjs/errors": "3.2.1",
"@feathersjs/errors": "3.2.2",
"debug": "3.1.0",
"express": "4.16.2",
"uberproto": "1.2.0"
......@@ -63,7 +63,7 @@
"resolved": "https://registry.npmjs.org/@feathersjs/socket-commons/-/socket-commons-3.1.5.tgz",
"integrity": "sha512-cnMqVNGR0fOM00UqRmslmPiUZ2UKToTxesoGwZvhvq4unF032KzS4gz8bZ7qYsin9HmAP/E2gs2v0119CR7LKQ==",
"requires": {
"@feathersjs/errors": "3.2.1",
"@feathersjs/errors": "3.2.2",
"debug": "3.1.0",
"lodash": "4.17.4",
"url-pattern": "1.0.3"
......
......@@ -31,6 +31,14 @@ class Keys {
encryptPrivateKey () {
this.encryptedPrivateKey = sodium.crypto_pwhash(this.keySettings.keyLength, this.privateKey, this.password, this.keySettings.opslimit, this.keySettings.memlimit, this.keySettings.algorithm)
}
stringify (value) {
return sodium.to_hex(value)
}
parse (value) {
return sodium.from_hex(value)
}
}
module.exports = Keys
......@@ -62,6 +62,14 @@ class Keys {
encryptPrivateKey () {
this.encryptedPrivateKey = sodium.crypto_pwhash(this.keySettings.keyLength, this.privateKey, this.password, this.keySettings.opslimit, this.keySettings.memlimit, this.keySettings.algorithm)
}
stringify (value) {
return sodium.to_hex(value)
}
parse (value) {
return sodium.from_hex(value)
}
}
module.exports = Keys
......@@ -95,11 +103,11 @@ form.addEventListener('submit', (e) => {
keyPair.encryptPassword(password)
keyPair.encryptPrivateKey()
console.log(keyPair)
const postKeys = axios.post('register',
const postKeys = axios.post('keys',
{
salt: keyPair.salt,
publicKey: keyPair.publicKey,
privateKey: keyPair.encryptedPrivateKey
salt: keyPair.stringify(keyPair.salt),
publicKey: keyPair.stringify(keyPair.publicKey),
privateKey: keyPair.stringify(keyPair.encryptedPrivateKey)
})
postKeys.then((values) => {
module.exports = function (app) { // eslint-disable-line no-unused-vars
const keys = require('./keys/keys.service.js');
module.exports = function (app) {
app.configure(keys);
};
const fileUtils = require('../../utils/fileUtils')
const errors = require('@feathersjs/errors')
class Service {
constructor (options) {
this.options = options || {}
}
async get (id, params) {
let keys
try {
keys = await fileUtils.readFile('./server/files/keys.json')
keys = JSON.parse(keys)
} catch (err) {
const error = new errors.GeneralError('Ooops something went wrong', err)
throw error
}
switch (id) {
case 'privatekey':
delete keys.publicKey
return keys
case 'publickey':
delete keys.salt
delete keys.encryptedPrivateKey
return keys
default:
const error = new errors.MethodNotAllowed()
throw error
}
}
async create (data, params) {
const keys = {
salt: data.salt,
encryptedPrivateKey: data.privateKey,
publicKey: data.publicKey
}
try {
await fileUtils.writeFile('keys.json', JSON.stringify(keys, undefined, 2), './server/files')
return data
} catch (err) {
const error = new errors.GeneralError('Ooops something went wrong', err)
throw error
}
}
}
module.exports = function (options) {
return new Service(options)
}
module.exports.Service = Service
module.exports = {
before: {
all: [],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
},
after: {
all: [],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
},
error: {
all: [],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
}
};
// Initializes the `keys` service on path `/keys`
const createService = require('./keys.class.js')
const hooks = require('./keys.hooks')
module.exports = function (app) {
const paginate = app.get('paginate')
const options = {
name: 'keys',
paginate
}
// Initialize our service with any options it requires
app.use('/keys', createService(options))
// Get our initialized service so that we can register hooks and filters
const service = app.service('keys')
service.hooks(hooks)
};
const fs = require('fs')
const path = require('path')
// Writes a string to the filesystem.
function writeFile (fileName, value, pathName) {
return new Promise((resolve, reject) => {
makeDir(pathName).then(() => {
fs.writeFile(path.join(pathName, fileName), value, (err) => {
if (err) {
reject(err)
}
resolve()
})
})
})
}
// Creates a dir on the filesystem if it does not exist.
function makeDir (pathName) {
return new Promise((resolve, reject) => {
if (!fs.existsSync(pathName)) {
fs.mkdir(pathName, (err) => {
if (err) {
reject(err)
}
resolve()
})
} else {
resolve()
}
})
}
// Reads a file from the filesystem.
function readFile (fileName) {
return new Promise((resolve, reject) => {
fs.readFile(fileName, 'utf8', (err, data) => {
if (err) {
reject(err)
}
resolve(data)
})
})
}
module.exports = {
writeFile,
readFile
}
const assert = require('assert');
const app = require('../../server/app');
describe('\'keys\' service', () => {
it('registered the service', () => {
const service = app.service('keys');
assert.ok(service, 'Registered the service');
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment