Compare commits

..

5 Commits

Author SHA1 Message Date
Kayne Ruse 39ddd8158a Updated dependencies 2022-07-23 11:48:36 +01:00
Kayne Ruse 50f0996fb7 Updated configure-script.js 2022-06-15 23:38:56 +01:00
Kayne Ruse 89b2b6ed7b Patched a security hole in the validation hooks 2022-06-15 23:33:06 +01:00
Kayne Ruse 8a5957d6b4 Integration test in place 2022-06-11 01:06:48 +01:00
Kayne Ruse 423a4652c1 Chipping away at writing the tests 2022-06-11 00:32:51 +01:00
8 changed files with 669 additions and 1579 deletions
+4 -1
View File
@@ -29,4 +29,7 @@ DB_LOGGING=
SECRET_ACCESS=access SECRET_ACCESS=access
# Make sure this value is kept secret # Make sure this value is kept secret
SECRET_REFRESH=refresh SECRET_REFRESH=refresh
# Post-signup hook JSON array (MUST include http:// or https://)
HOOK_POST_VALIDATION_ARRAY=
+4 -2
View File
@@ -28,8 +28,10 @@ Content-Type: application/json
//DOCS: Used for validating the email address specified above //DOCS: Used for validating the email address specified above
GET /auth/validation?username=example&token=12345678 GET /auth/validation?username=example&token=12345678
//DOCS: If the environment variable HOOK_POST_VALIDATION is set to a URL, then the server will send a GET message to that URL with the newly created account's index //DOCS: If the environment variable HOOK_POST_VALIDATION_ARRAY is set to a JSON array of valid URLs, then the server will send a GET message to each URL with the newly created account's index
GET https://{HOOK_POST_VALIDATION}?accountIndex={index} //DOCS: The GET requests will have a JWT authorization header
HOOK_POST_VALIDATION_ARRAY=["http://example.com", "http://example2.com"]
GET {HOOK_POST_VALIDATION_ARRAY[i]}?accountIndex={index}
### ###
+2 -2
View File
@@ -30,7 +30,7 @@ const question = (prompt, def = null) => {
//project configuration //project configuration
const appName = await question('App Name', 'auth'); const appName = await question('App Name', 'auth');
const appWebAddress = await question('Web Addr', `${appName}.example.com`); const appWebAddress = await question('Web Addr', `${appName}.example.com`);
const postValidationHook = await question('Post Validation Hook', ''); const postValidationHookArray = await question('Post Validation Hook Array', '');
const resetAddress = await question('Reset Addr', `example.com/reset`); const resetAddress = await question('Reset Addr', `example.com/reset`);
const appPort = await question('App Port', '3200'); const appPort = await question('App Port', '3200');
@@ -71,7 +71,7 @@ services:
environment: environment:
- WEB_PROTOCOL=https - WEB_PROTOCOL=https
- WEB_ADDRESS=${appWebAddress} - WEB_ADDRESS=${appWebAddress}
- HOOK_POST_VALIDATION=${postValidationHook} - HOOK_POST_VALIDATION_ARRAY=${postValidationHookArray}
- WEB_RESET_ADDRESS=${resetAddress} - WEB_RESET_ADDRESS=${resetAddress}
- WEB_PORT=${appPort} - WEB_PORT=${appPort}
- DB_HOSTNAME=database - DB_HOSTNAME=database
+471 -1567
View File
File diff suppressed because it is too large Load Diff
+35 -6
View File
@@ -1,5 +1,6 @@
const { pendingSignups, accounts } = require('../database/models'); const { pendingSignups, accounts } = require('../database/models');
const fetch = require('node-fetch'); const fetch = require('node-fetch');
const jwt = require('jsonwebtoken');
//auth/validation //auth/validation
const route = async (req, res) => { const route = async (req, res) => {
@@ -38,14 +39,42 @@ const route = async (req, res) => {
res.status(200).send('Validation succeeded!'); res.status(200).send('Validation succeeded!');
//post-validation hook //post-validation hook
if (process.env.HOOK_POST_VALIDATION) { if (process.env.HOOK_POST_VALIDATION_ARRAY) {
const probe = await fetch(`https://${process.env.HOOK_POST_VALIDATION}?accountIndex=${account.index}`); try {
hooks = JSON.parse(process.env.HOOK_POST_VALIDATION_ARRAY);
if (!probe.ok) { if (!Array.isArray(hooks)) {
console.error('Could not probe the post validation hook'); throw 'isArray() check failed';
}
//authenticate the hooks
const bearer = jwt.sign({ type: 'hook authentication' }, process.env.SECRET_ACCESS, { expiresIn: '5m', issuer: 'auth' });
//promise for each given hook
const promises = hooks.map(async hook => {
if (typeof hook != 'string') {
throw 'hook is not a string';
}
const probe = await fetch(`${hook}?accountIndex=${account.index}`, {
method: 'GET',
headers: {
'Authorization': `Bearer ${bearer}`
}
});
if (!probe.ok) {
throw `Could not probe the post validation hook: ${hook}`;
}
//discard the result
});
Promise.all(promises);
}
catch(e) {
console.error('HOOK_POST_VALIDATION_ARRAY is not a valid array of strings in JSON format: ' + e);
} }
//discard the result
} }
}; };
+1 -1
View File
@@ -3,7 +3,7 @@ const jwt = require('jsonwebtoken');
//middleware to authenticate the JWT token //middleware to authenticate the JWT token
module.exports = (req, res, next) => { module.exports = (req, res, next) => {
const authHeader = req.headers['authorization']; const authHeader = req.headers['authorization'];
const token = authHeader?.split (' ')[1]; //'Bearer token' const token = authHeader?.split(' ')[1]; //'Bearer token'
if (!token) { if (!token) {
return res.status(401).send('No token found'); return res.status(401).send('No token found');
+65
View File
@@ -0,0 +1,65 @@
describe('Integration Test Suite', () => {
beforeEach(() => {
jest.resetModules();
//mock dotenv
jest.doMock('dotenv', () => ({
config: () => null
}));
//mock express
jest.doMock('express', () => {
const express = () => ({
identity: 'app',
use: () => null,
get: () => null,
});
express.Router = () => ({
identity: 'Router',
use: () => null,
get: () => null,
post: () => null,
patch: () => null,
delete: () => null,
});
express.json = () => 'json';
return express;
});
//mock http
jest.doMock('http', () => ({
Server: app => {
expect(app.identity).toBe('app');
return {
listen: (port, cb) => cb()
}
}
}));
//mock sequelize
class Seq {
sync() {}
define() {}
static INTEGER() {}
};
jest.doMock('sequelize', () => {
return Seq;
});
//mock node-cron
jest.doMock('node-cron', () => {
return {
schedule: () => null
}
});
});
test('Start The Server', () => {
const serv = require('../server/server');
});
});
+87
View File
@@ -0,0 +1,87 @@
describe('token-auth', () => {
beforeEach(() => {
jest.resetModules();
//mock out jsonwebtoken
jest.doMock('jsonwebtoken', () => ({
verify: (token, secretAccess, callback) => {
if (token != 'invalid') {
expect(token).toBe('testtoken');
return callback(null, { username: 'username' });
} else {
expect(token).toBe('invalid');
return callback('Misc. error');
}
},
}));
});
test('Required Functionality', () => {
const tokenAuth = require('../../server/utilities/token-auth');
const req = {
headers: {
authorization: 'Bearer testtoken'
}
};
const res = {
status: code => {
expect(code).toBe(null);
return msg => { throw msg; };
}
};
tokenAuth(req, res, () => null);
expect(req.user.username).toBe('username');
});
test('Missing Token', () => {
const tokenAuth = require('../../server/utilities/token-auth');
const req = {
headers: {
//
}
};
const res = {
status: code => {
expect(code).toBe(401);
return {
send: msg => {
expect(msg).toBe('No token found');
return null;
}
};
}
};
tokenAuth(req, res, () => null);
});
test('Invalid Token', () => {
const tokenAuth = require('../../server/utilities/token-auth');
const req = {
headers: {
authorization: 'Bearer invalid'
}
};
const res = {
status: code => {
expect(code).toBe(403);
return {
send: msg => {
expect(msg).toBe('Misc. error');
return null;
}
};
}
};
tokenAuth(req, res, () => null);
});
});