Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 76fdbc0d13 | |||
| 39ddd8158a | |||
| 50f0996fb7 | |||
| 89b2b6ed7b | |||
| 8a5957d6b4 | |||
| 423a4652c1 | |||
| 7f9274eb3f | |||
| 4043507e01 | |||
| 12fb02484a |
@@ -30,3 +30,6 @@ 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=
|
||||||
@@ -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
@@ -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
|
||||||
|
|||||||
Generated
+537
-1633
File diff suppressed because it is too large
Load Diff
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "auth-server",
|
"name": "auth-server",
|
||||||
"version": "1.4.12",
|
"version": "1.4.14",
|
||||||
"description": "An API centric auth server. Uses Sequelize and mariaDB by default.",
|
"description": "An API centric auth server. Uses Sequelize and mariaDB by default.",
|
||||||
"main": "server/server.js",
|
"main": "server/server.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
@@ -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} with accountIndex = ${account.index}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
//discard the result
|
||||||
|
});
|
||||||
|
|
||||||
|
await 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
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -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');
|
||||||
|
|||||||
@@ -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');
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -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);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -37,7 +37,6 @@ const TokenProvider = props => {
|
|||||||
method: 'DELETE',
|
method: 'DELETE',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'Access-Control-Allow-Origin': '*',
|
|
||||||
'Authorization': `Bearer ${bearer}`
|
'Authorization': `Bearer ${bearer}`
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
@@ -50,8 +49,7 @@ const TokenProvider = props => {
|
|||||||
const response = await fetch(`${process.env.AUTH_URI}/auth/token`, {
|
const response = await fetch(`${process.env.AUTH_URI}/auth/token`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json'
|
||||||
'Access-Control-Allow-Origin': '*'
|
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
token: refreshToken
|
token: refreshToken
|
||||||
@@ -91,8 +89,7 @@ const TokenProvider = props => {
|
|||||||
const response = await fetch(`${process.env.AUTH_URI}/auth/token`, {
|
const response = await fetch(`${process.env.AUTH_URI}/auth/token`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json'
|
||||||
'Access-Control-Allow-Origin': '*'
|
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
token: refreshToken
|
token: refreshToken
|
||||||
|
|||||||
Reference in New Issue
Block a user