Implement create, find, & get specific key information for owner

parent a1d1884e
......@@ -18,7 +18,7 @@ const path = require('path')
process.env['NODE_CONFIG_DIR'] = path.join(__dirname, 'config/')
const configuration = require('@feathersjs/configuration')
const globals = require('./globals')
const { globals } = require('./globals')
const express = require('@feathersjs/express')
......
......@@ -4,8 +4,56 @@
const path = require('path')
const os = require('os')
const fs = require('fs-extra')
const errors = require('@feathersjs/errors')
module.exports = function (app) {
function setOwnerKeys (app, ownerKeys = null) {
// Try to set the owner’s keys, either from the provided object, if it
// exists, or from the file system, if the data file exists, or simply
// set them to null otherwise.
const ownerKeysFilePath = app.get('ownerKeysFilePath')
let ownerEncryptedPrivateSigningKey = null
let ownerPublicSigningKey = null
function generateConvenienceKeysFrom (mainKeyObject) {
// Update the global state of the app to include the owner’s keys.
ownerEncryptedPrivateSigningKey = {
derivedKeySalt: mainKeyObject.derivedKeySalt,
encryptedPrivateSigningKeyNonce: mainKeyObject.encryptedPrivateSigningKeyNonce,
encryptedPrivateSigningKey: mainKeyObject.encryptedPrivateSigningKey
}
ownerPublicSigningKey = mainKeyObject.publicSigningKey
}
if (ownerKeys === null && fs.existsSync(ownerKeysFilePath)) {
// No keys were passed to us and keys exist on the file system, so
// read them from there.
ownerKeys = fs.readJsonSync(ownerKeysFilePath)
generateConvenienceKeysFrom(ownerKeys)
}
if (ownerKeys !== null) {
// The owner keys are being set for the first time. Persist them.
try {
fs.writeJsonSync(ownerKeysFilePath, ownerKeys, {spaces: 2})
} catch (error) {
throw new errors.GeneralError('Failed to persist owner’s keys.', error)
}
generateConvenienceKeysFrom(ownerKeys)
}
// At this point, if ownerKeys is still null, the owner does not exist.
const ownerExists = ownerKeys !== null
app.set('ownerExists', ownerExists)
app.set('ownerKeysFileExists', ownerExists) // Alias, for completeness.
app.set('ownerKeys', ownerKeys)
app.set('ownerEncryptedPrivateSigningKey', ownerEncryptedPrivateSigningKey)
app.set('ownerPublicSigningKey', ownerPublicSigningKey)
}
function globals (app) {
const dataDirectoryPath = path.join(os.homedir(), '.indie', 'site')
const serverSecretFilePath = path.join(dataDirectoryPath, 'server-secret.json')
const ownerKeysFilePath = path.join(dataDirectoryPath, 'owner-keys.json')
......@@ -24,14 +72,16 @@ module.exports = function (app) {
// the application can easily query their state (and, if necessary, update it).
const serverSecretFileExists = fs.existsSync(serverSecretFilePath)
const ownerKeysFileExists = fs.existsSync(ownerKeysFilePath)
const ownerSettingsFileExists = fs.existsSync(ownerSettingsFilePath)
app.set('serverSecretFileExists', serverSecretFileExists)
app.set('ownerKeysFileExists', ownerKeysFileExists)
app.set('ownerSettingsFileExists', ownerSettingsFileExists)
// Aliases
//
// Owner details
//
app.set('ownerExists', ownerKeysFileExists)
setOwnerKeys(app)
}
module.exports = { globals, setOwnerKeys }
const errors = require('@feathersjs/errors')
const fs = require('fs-extra')
const { setOwnerKeys } = require('../../globals')
/* eslint-disable no-unused-vars */
class Service {
......@@ -13,12 +13,18 @@ class Service {
}
async find (params) {
return []
return this.app.get('ownerKeys')
}
async get (id, params) {
return {
id, text: `A new message with ID: ${id}!`
console.log(`ID: ${id}`)
switch (id) {
case 'encrypted-private-signing-key':
return this.app.get('ownerEncryptedPrivateSigningKey')
case 'public-signing-key':
return this.app.get('ownerPublicSigningKey')
default:
throw new errors.MethodNotAllowed()
}
}
......@@ -30,8 +36,6 @@ class Service {
throw error
}
const ownerKeysFilePath = this.app.get('ownerKeysFilePath')
const ownerKeysObject = {
derivedKeySalt: data.derivedKeySalt,
encryptedPrivateSigningKeyNonce: data.encryptedPrivateSigningKeyNonce,
......@@ -40,10 +44,14 @@ class Service {
}
try {
await fs.writeJson(ownerKeysFilePath, ownerKeysObject, {spaces: 2})
// Persist the owner’s keys.
//
// Note: this is synchronous but it will happen only once in the lifetime
// of the site so there is no performance issue.
setOwnerKeys(this.app, ownerKeysObject)
return data
} catch (error) {
throw new errors.GeneralError('Failed to save owner’s keys.', error)
/* re - */ throw error
}
}
......
// Initializes the `owner` service on path `/owner`
const createService = require('./owner.class.js');
const hooks = require('./owner.hooks');
const createService = require('./owner.class.js')
const hooks = require('./owner.hooks')
module.exports = function (app) {
const paginate = app.get('paginate');
const paginate = app.get('paginate')
const options = {
name: 'owner',
paginate
};
}
// Initialize our service with any options it requires
app.use('/owner', createService(options));
app.use('/owner', createService(options))
// Get our initialized service so that we can register hooks and filters
const service = app.service('owner');
const service = app.service('owner')
service.hooks(hooks);
};
service.hooks(hooks)
}
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