Skip to content
This repository was archived by the owner on Jan 20, 2020. It is now read-only.

Commit 1b5c36e

Browse files
author
Luciano Nooijen
committed
Work in progress on auth helper
1 parent 1dc24b6 commit 1b5c36e

File tree

6 files changed

+88
-18
lines changed

6 files changed

+88
-18
lines changed

.env.example

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ DB_PASS_TEST=root
2525
# Authentication
2626
SALT_ROUNDS=10
2727
JWT_SECRET=secret
28-
JWT_EXPIRES_IN=7d
28+
JWT_EXPIRES_IN_DAYS=7

helpers/auth-helper.js

+29-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
const bcrypt = require('bcrypt');
2+
const jwt = require('jwt-simple');
23

3-
const { SALT_ROUNDS, JWT_SECRET, JWT_EXPIRES_IN } = process.env;
4+
const { SALT_ROUNDS, JWT_SECRET, JWT_EXPIRES_IN_DAYS } = process.env;
45

56
const decimalRadix = 10;
67
const saltRounds = parseInt(SALT_ROUNDS, decimalRadix);
8+
const jwtExpiresInDays = parseInt(JWT_EXPIRES_IN_DAYS, decimalRadix);
79

810
const generatePasswordHash = async plainTextPassword => {
911
const hashedPassword = await new Promise((resolve, reject) =>
@@ -25,18 +27,39 @@ const checkPasswordHash = async (plainTextPassword, hashedPassword) => {
2527
return false;
2628
};
2729

28-
const generateJWT = () => {
29-
return null;
30+
const calculateExpiryDate = () => {
31+
const date = new Date();
32+
const expiryDate = date.setDate(date.getDate() + jwtExpiresInDays);
33+
return expiryDate;
3034
};
3135

32-
// TODO: Integrate Typescript, generate enum
33-
const checkJWT = () => {};
36+
// TODO: Make it possible to mock payload of x days ago
37+
const generatePayload = data => {
38+
const payload = {
39+
expires: calculateExpiryDate(),
40+
data,
41+
};
42+
return payload;
43+
};
44+
45+
const generateJWT = data => {
46+
const payload = generatePayload(data);
47+
const token = jwt.encode(payload, JWT_SECRET);
48+
return token;
49+
};
50+
51+
const decodeJWT = token => {
52+
const decoded = jwt.decode(token, JWT_SECRET);
53+
return decoded;
54+
};
3455

3556
const authHelper = {
57+
calculateExpiryDate,
58+
generatePayload,
3659
generatePasswordHash,
3760
checkPasswordHash,
3861
generateJWT,
39-
checkJWT,
62+
decodeJWT,
4063
};
4164

4265
module.exports = authHelper;

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"dotenv": "^6.1.0",
4646
"express": "^4.16.4",
4747
"helmet": "^3.15.0",
48+
"jwt-simple": "^0.5.5",
4849
"knex": "^0.15.2",
4950
"morgan": "^1.9.1",
5051
"pg": "^7.6.1",

tests/helpers/auth-helper.test.ts

+52-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
import { useEnvVars } from '../config/index';
2-
31
const { authHelper } = require('../../helpers');
42

3+
const { JWT_SECRET, JWT_EXPIRES_IN_DAYS } = process.env;
4+
55
const testPassword = 'the_test_password';
66
const getPasswordHash = async () =>
77
authHelper.generatePasswordHash(testPassword);
88

9-
useEnvVars();
10-
119
describe('Authentication helper', () => {
1210
test('generatePasswordHash should not return the password', async () => {
1311
expect.assertions(1);
@@ -28,7 +26,54 @@ describe('Authentication helper', () => {
2826
expect(passwordCheck).toBe(true);
2927
});
3028

31-
// test('generateJWT should return a string', () => {
32-
// const authToken = authHelper.generateAuthToken()
33-
// });
29+
test('generateJWT should return a string', () => {
30+
const payloadData = {
31+
user: 'Test',
32+
};
33+
const authToken = authHelper.generateJWT(payloadData);
34+
expect(typeof authToken).toBe('string');
35+
});
36+
37+
test('decodeJWT should return the correct payload', () => {
38+
const payloadData = {
39+
user: 'Test',
40+
};
41+
const payload = authHelper.generatePayload(payloadData);
42+
const authToken = authHelper.generateJWT(payloadData);
43+
const decodedPayload = authHelper.decodeJWT(authToken);
44+
expect(decodedPayload).toEqual(payload);
45+
});
46+
47+
test('JWT expiry data create a valid date object', () => {
48+
const payload = {
49+
user: 'Test',
50+
};
51+
const authToken = authHelper.generateJWT(payload);
52+
const decodedPayload = authHelper.decodeJWT(authToken);
53+
const payloadDate = new Date(decodedPayload.expires);
54+
expect(payloadDate).toBeInstanceOf(Date);
55+
});
56+
57+
test('JWT expiry date should be in the future', () => {
58+
const payload = {
59+
user: 'Test',
60+
};
61+
const authToken = authHelper.generateJWT(payload);
62+
const decodedPayload = authHelper.decodeJWT(authToken);
63+
const payloadDate = decodedPayload.expires;
64+
expect(payloadDate).toBeGreaterThan(Date.now());
65+
});
66+
67+
test('JWT should expire in JWT_EXPIRES_IN_DAYS days', () => {
68+
const payload = {
69+
user: 'Test',
70+
};
71+
const authToken = authHelper.generateJWT(payload);
72+
const decodedPayload = authHelper.decodeJWT(authToken);
73+
const payloadDate = decodedPayload.expires;
74+
const expectedExpiryDate = authHelper.calculateExpiryDate();
75+
76+
// Precise to 10 seconds TODO: Test this better
77+
expect(Math.floor(payloadDate / 10)).toBe(Math.floor(expectedExpiryDate / 10));
78+
});
3479
});

tests/knexfile-generator.test.ts

-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
/* eslint-disable function-paren-newline */
22

3-
import { useEnvVars } from './config/index';
4-
53
const generateKnexConfig = require('../database/generate-knexfile');
64
const {
75
getNodeEnvSpecificDefaults,
86
getConfigEntry,
97
} = require('../database/generate-knexfile');
108

11-
useEnvVars();
12-
139
const {
1410
NODE_ENV,
1511
KNEX_DEBUG,

yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -3826,6 +3826,11 @@ jsx-ast-utils@^2.0.1:
38263826
dependencies:
38273827
array-includes "^3.0.3"
38283828

3829+
jwt-simple@^0.5.5:
3830+
version "0.5.5"
3831+
resolved "https://registry.yarnpkg.com/jwt-simple/-/jwt-simple-0.5.5.tgz#53f60ceab266708450a22cafa6fd93cfa2ccfa3a"
3832+
integrity sha512-KEyanRIDHooo8KuBxY3CC019NbwHtxdsxzRJUfaGqcxMrvBPBkosN+RUxx1nZFI6yrErq3KTW8HI/qrNIxHe0g==
3833+
38293834
kind-of@^1.1.0:
38303835
version "1.1.0"
38313836
resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-1.1.0.tgz#140a3d2d41a36d2efcfa9377b62c24f8495a5c44"

0 commit comments

Comments
 (0)