Implement owner service create() method for registering an owner

(Registering means saving their non-secret key information.)
parent efab93dd
......@@ -30,4 +30,8 @@ module.exports = function (app) {
app.set('serverSecretFileExists', serverSecretFileExists)
app.set('ownerKeysFileExists', ownerKeysFileExists)
app.set('ownerSettingsFileExists', ownerSettingsFileExists)
// Aliases
app.set('ownerExists', ownerKeysFileExists)
}
const errors = require('@feathersjs/errors')
const fs = require('fs-extra')
/* eslint-disable no-unused-vars */
class Service {
constructor (options) {
this.options = options || {};
this.options = options || {}
}
// This method is required to be able to pass the feathers app object.
setup (app, appPath) {
this.app = app
}
async find (params) {
return [];
return []
}
async get (id, params) {
return {
id, text: `A new message with ID: ${id}!`
};
}
}
// Registers a new owner.
async create (data, params) {
if (Array.isArray(data)) {
return await Promise.all(data.map(current => this.create(current)));
// TODO: Do not use sync method
if (this.app.get('ownerExists')) {
const error = new errors.Forbidden('Owner already exists.')
throw error
}
return data;
const ownerKeysFilePath = this.app.get('ownerKeysFilePath')
const ownerKeysObject = {
derivedKeySalt: data.derivedKeySalt,
encryptedPrivateSigningKeyNonce: data.encryptedPrivateSigningKeyNonce,
encryptedPrivateSigningKey: data.encryptedPrivateSigningKey,
publicSigningKey: data.publicSigningKey
}
try {
await fs.writeJson(ownerKeysFilePath, ownerKeysObject, {spaces: 2})
return data
} catch (error) {
throw new errors.GeneralError('Failed to save owner’s keys.', error)
}
}
async update (id, data, params) {
return data;
return data
}
async patch (id, data, params) {
return data;
return data
}
async remove (id, params) {
return { id };
return { id }
}
}
module.exports = function (options) {
return new Service(options);
};
return new Service(options)
}
module.exports.Service = Service;
module.exports.Service = 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