From 36791231cc4a8d7275608df919947b317467fd92 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Wed, 7 Jul 2021 17:04:19 -0700 Subject: [PATCH] Moved auth library to adapters dir --- bin/server.js | 2 +- src/{lib => adapters}/admin.js | 4 ++++ src/adapters/localdb/models/users.js | 3 +++ test/e2e/automated/a01-auth.rest-e2e.js | 2 +- test/e2e/automated/a09-admin.rest-e2e.js | 2 +- 5 files changed, 10 insertions(+), 3 deletions(-) rename src/{lib => adapters}/admin.js (96%) diff --git a/bin/server.js b/bin/server.js index 90324a1..86f7ce8 100644 --- a/bin/server.js +++ b/bin/server.js @@ -13,7 +13,7 @@ const cors = require('kcors') // Local libraries const config = require('../config') // this first. // const IPFSLib = require('../src/lib/ipfs') -const AdminLib = require('../src/lib/admin') +const AdminLib = require('../src/adapters/admin') const adminLib = new AdminLib() // const JSONRPC = require('../src/rpc') // const rpc = new JSONRPC() diff --git a/src/lib/admin.js b/src/adapters/admin.js similarity index 96% rename from src/lib/admin.js rename to src/adapters/admin.js index 4634da4..763c665 100644 --- a/src/lib/admin.js +++ b/src/adapters/admin.js @@ -7,6 +7,10 @@ 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. + + This library is really more of an Adapter to the internal systems default + admin user. It's not really a central Entity, which is why this library lives + in the Adapter directory. */ 'use strict' diff --git a/src/adapters/localdb/models/users.js b/src/adapters/localdb/models/users.js index 918d294..b8e0b0f 100644 --- a/src/adapters/localdb/models/users.js +++ b/src/adapters/localdb/models/users.js @@ -22,6 +22,7 @@ const User = new mongoose.Schema({ } }) +// Before saving, convert the password to a hash. User.pre('save', function preSave (next) { const user = this @@ -51,6 +52,7 @@ User.pre('save', function preSave (next) { .catch(err => next(err)) }) +// Validate the password by comparing to the saved hash. User.methods.validatePassword = function validatePassword (password) { const user = this @@ -65,6 +67,7 @@ User.methods.validatePassword = function validatePassword (password) { }) } +// Generate a JWT token. User.methods.generateToken = function generateToken () { const user = this diff --git a/test/e2e/automated/a01-auth.rest-e2e.js b/test/e2e/automated/a01-auth.rest-e2e.js index 52099ee..e4f367b 100644 --- a/test/e2e/automated/a01-auth.rest-e2e.js +++ b/test/e2e/automated/a01-auth.rest-e2e.js @@ -12,7 +12,7 @@ const axios = require('axios').default const config = require('../../../config') const app = require('../../../bin/server') const testUtils = require('../../utils/test-utils') -const AdminLib = require('../../../src/lib/admin') +const AdminLib = require('../../../src/adapters/admin') const adminLib = new AdminLib() // const request = supertest.agent(app.listen()) diff --git a/test/e2e/automated/a09-admin.rest-e2e.js b/test/e2e/automated/a09-admin.rest-e2e.js index 4391f2a..cf94d2f 100644 --- a/test/e2e/automated/a09-admin.rest-e2e.js +++ b/test/e2e/automated/a09-admin.rest-e2e.js @@ -1,6 +1,6 @@ const assert = require('chai').assert -const Admin = require('../../../src/lib/admin') +const Admin = require('../../../src/adapters/admin') const sinon = require('sinon')