mirror of
https://github.com/Permissionless-Software-Foundation/ipfs-bch-wallet-consumer.git
synced 2026-09-21 16:52:03 -07:00
added libraries for working with admin account and json files from p2p vps server
This commit is contained in:
@@ -0,0 +1,159 @@
|
||||
/*
|
||||
A library for working with the system admin user. This is an auto-generated
|
||||
account with 'admin' privledges, for interacting with private APIs.
|
||||
|
||||
The admin account is regenerated every time the server is started. This improves
|
||||
security by not having stale passwords for the account. The login information
|
||||
and JWT token for the admin account is written to a JSON file, for easy
|
||||
retrieval by other apps running on the server that may need admin privledges
|
||||
to access private APIs.
|
||||
*/
|
||||
|
||||
'use strict'
|
||||
|
||||
const rp = require('request-promise')
|
||||
const User = require('../models/users')
|
||||
const jsonFiles = require('./utils/json-files')
|
||||
const config = require('../../config')
|
||||
|
||||
// Ensure the environment variable is set
|
||||
process.env.P2PVPS_ENV = process.env.P2PVPS_ENV || 'dev'
|
||||
const env = process.env.P2PVPS_ENV
|
||||
const JSON_FILE = `system-user-${env}.json`
|
||||
|
||||
const LOCALHOST = `http://localhost:${config.port}`
|
||||
const context = {}
|
||||
|
||||
// Create the first user in the system. A 'admin' level system user that is
|
||||
// used by the Listing Manager and test scripts, in order access private API
|
||||
// functions.
|
||||
async function createSystemUser () {
|
||||
// Create the system user.
|
||||
try {
|
||||
context.password = _randomString(20)
|
||||
|
||||
const options = {
|
||||
method: 'POST',
|
||||
uri: `${LOCALHOST}/api/users`,
|
||||
resolveWithFullResponse: true,
|
||||
json: true,
|
||||
body: {
|
||||
user: {
|
||||
username: 'system',
|
||||
password: context.password
|
||||
}
|
||||
}
|
||||
}
|
||||
let result = await rp(options)
|
||||
|
||||
context.username = result.body.user.username
|
||||
context.id = result.body.user._id
|
||||
context.token = result.body.token
|
||||
|
||||
// Get the mongoDB entry
|
||||
const user = await User.findById(context.id)
|
||||
|
||||
// Change the user type to admin
|
||||
user.type = 'admin'
|
||||
// console.log(`user: ${JSON.stringify(user, null, 2)}`)
|
||||
|
||||
// Save the user model.
|
||||
await user.save()
|
||||
|
||||
// Write out the system user information to a JSON file that external
|
||||
// applications like the Task Manager and the test scripts can access.
|
||||
await jsonFiles.writeJSON(context, `${__dirname}/../persist/${JSON_FILE}`)
|
||||
|
||||
return context
|
||||
} catch (err) {
|
||||
// Handle existing system user.
|
||||
if (err.statusCode === 422) {
|
||||
try {
|
||||
// Delete the existing user
|
||||
await deleteExistingSystemUser()
|
||||
|
||||
// Call this function again.
|
||||
return createSystemUser()
|
||||
} catch (err2) {
|
||||
console.error(`Error in util.js/createSystemUser() while trying generate new system user.`)
|
||||
// process.end(1)
|
||||
throw err2
|
||||
}
|
||||
} else {
|
||||
console.log('Error in util.js/createSystemUser: ' + JSON.stringify(err, null, 2))
|
||||
// process.end(1)
|
||||
throw err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function deleteExistingSystemUser () {
|
||||
try {
|
||||
let result = await loginAdmin()
|
||||
|
||||
const token = result.body.token
|
||||
const id = result.body.user._id.toString()
|
||||
|
||||
// Delete the user.
|
||||
const options = {
|
||||
method: 'DELETE',
|
||||
uri: `${LOCALHOST}/api/users/${id}`,
|
||||
resolveWithFullResponse: true,
|
||||
json: true,
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`
|
||||
}
|
||||
}
|
||||
result = await rp(options)
|
||||
// console.log(`result2: ${JSON.stringify(result, null, 2)}`)
|
||||
|
||||
return result.body.success
|
||||
} catch (err) {
|
||||
console.log(`Error in util.js/deleteExistingSystemUser()`)
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
async function loginAdmin () {
|
||||
// console.log(`loginAdmin() running.`)
|
||||
try {
|
||||
let existingUser
|
||||
|
||||
// Read the exising file
|
||||
existingUser = await jsonFiles.readJSON(`${__dirname}/../persist/${JSON_FILE}`)
|
||||
// console.log(`existingUser: ${JSON.stringify(existingUser, null, 2)}`)
|
||||
|
||||
// Log in as the user.
|
||||
let options = {
|
||||
method: 'POST',
|
||||
uri: `${LOCALHOST}/api/auth`,
|
||||
resolveWithFullResponse: true,
|
||||
json: true,
|
||||
body: {
|
||||
username: 'system',
|
||||
password: existingUser.password
|
||||
}
|
||||
}
|
||||
let result = await rp(options)
|
||||
// console.log(`result1: ${JSON.stringify(result, null, 2)}`)
|
||||
|
||||
return result
|
||||
} catch (err) {
|
||||
console.error(`Error in bin/util.js/loginAdmin().`)
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
function _randomString (length) {
|
||||
var text = ''
|
||||
var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
|
||||
for (var i = 0; i < length; i++) {
|
||||
text += possible.charAt(Math.floor(Math.random() * possible.length))
|
||||
}
|
||||
return text
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
createSystemUser,
|
||||
loginAdmin
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
A utility file for reading and writing JSON files.
|
||||
*/
|
||||
|
||||
'use strict'
|
||||
|
||||
const fs = require('fs')
|
||||
|
||||
// Writes out a JSON file of any object passed to the function.
|
||||
// This is used for testing.
|
||||
function writeJSON (obj, fileName) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
try {
|
||||
const fileStr = JSON.stringify(obj, null, 2)
|
||||
|
||||
fs.writeFile(fileName, fileStr, function (err) {
|
||||
if (err) {
|
||||
console.error('Error while trying to write file: ', err)
|
||||
return reject(err)
|
||||
} else {
|
||||
// console.log(`${fileName} written successfully!`)
|
||||
return resolve()
|
||||
}
|
||||
})
|
||||
} catch (err) {
|
||||
console.error('Error trying to write out object in util.js/_writeJSON().', err)
|
||||
return reject(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Read and parse a JSON file.
|
||||
function readJSON (fileName) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
try {
|
||||
fs.readFile(fileName, (err, data) => {
|
||||
if (err) {
|
||||
if (err.code === 'ENOENT') {
|
||||
console.log(`Admin .json file not found!`)
|
||||
} else {
|
||||
console.log(`err: ${JSON.stringify(err, null, 2)}`)
|
||||
}
|
||||
|
||||
return reject(err)
|
||||
}
|
||||
|
||||
const obj = JSON.parse(data)
|
||||
|
||||
return resolve(obj)
|
||||
})
|
||||
} catch (err) {
|
||||
console.error('Error trying to read JSON file in util.js/_readJSON().', err)
|
||||
return reject(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
writeJSON,
|
||||
readJSON
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user