Skip to content

Commit 3028f44

Browse files
committed
✏️ add jwt to protected routes
1 parent 81cf9d4 commit 3028f44

File tree

6 files changed

+138
-6
lines changed

6 files changed

+138
-6
lines changed

.example.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ SYNC_DATE=''
1010
COLLECTION=''
1111
RATE_LIMIT_WINDOW_MS=10
1212
RATE_LIMIT_MAX=5
13+
JWT_SECRET=''

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@
1919
| **src/util/** | helpers - logging, cache, ratelimit, database |
2020
| **test** | Test cases to perform before taking a new build, is also used in combination with github actions before merging the code to master branch |
2121
| **views** | Template files for rendering dynamic content to client side, since this is a api server, there is not much there in this folder. |
22-
| **.env.example** | repos. |
22+
| **.env.example** | Project secrets, usernames, passwords, etc. |
2323
| **.github** | CI - github actions - run test cases on pull request and report if it is safe to merge. |
24-
| **cache** | Cache folder for responses that do not need to connect to database all the time.
24+
| **.vscode** | vs-code workspace configuration, higly recommend using vs-code (install prettier es-lint plugin) |
25+
| **cache** | Cache folder for responses that do not need to connect to database all the time. |
2526

2627
## Manual
2728

@@ -54,7 +55,7 @@ redis-cli
5455

5556
### install nginx
5657

57-
```bash
58+
```bash
5859
apt or yum install nginx
5960
```
6061

@@ -69,6 +70,7 @@ npm install yarn --g
6970
```bash
7071
yarn build
7172
```
73+
7274
### test
7375

7476
```bash
@@ -93,6 +95,7 @@ yarn pm2 start build/server.js
9395

9496
- typescript
9597
- express
98+
- jwt
9699
- jest
97100
- supertest
98101
- handlebars

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"build": "tsc",
99
"lint": "eslint src/**/*.ts",
1010
"format": "eslint src/**/*.ts --fix",
11-
"test": "jest --force-exit --detectOpenHandles"
11+
"test": "jest --force-exit --detectOpenHandles"
1212
},
1313
"keywords": [],
1414
"author": "",
@@ -18,6 +18,7 @@
1818
"@types/errorhandler": "^1.5.0",
1919
"@types/express": "^4.17.8",
2020
"@types/express-handlebars": "^3.1.0",
21+
"@types/express-jwt": "^6.0.1",
2122
"@types/express-rate-limit": "^5.1.1",
2223
"@types/jest": "^26.0.20",
2324
"@types/lusca": "^1.6.2",
@@ -50,6 +51,7 @@
5051
"errorhandler": "^1.5.1",
5152
"express": "^4.17.1",
5253
"express-handlebars": "^5.2.0",
54+
"express-jwt": "^6.0.0",
5355
"express-rate-limit": "^5.2.3",
5456
"mongodb": "^3.6.2",
5557
"morgan": "^1.10.0",

src/app.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import compression from 'compression';
55
import bodyParser from 'body-parser';
66
import exphbs from 'express-handlebars';
77
import lusca from 'lusca';
8+
import jwt from 'express-jwt';
89

9-
import { PORT } from './util/secrets';
10+
import { JWT_SECRET, PORT } from './util/secrets';
1011
import * as homeController from './controllers/home';
1112
import * as apiController from './controllers/api';
1213
import * as unmatchedController from './controllers/unmatched';
@@ -36,6 +37,9 @@ app.use(
3637

3738
app.use(express.static(path.join(__dirname, 'public'), { maxAge: 31557600000 }));
3839

40+
// jwt for protected resources
41+
app.use('/top', jwt({ secret: JWT_SECRET, algorithms: ['RS256'] }));
42+
3943
app.get('/', homeController.index);
4044
app.get('/top', apiController.getTop);
4145
app.get('*', unmatchedController.index);

src/util/secrets.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import fs from 'fs';
22

33
import dotenv from 'dotenv';
4+
import jwt from 'express-jwt';
45

56
import logger from './logger';
67

@@ -24,6 +25,7 @@ export const MONGODB_COLLECTION = process.env['COLLECTION'] || '';
2425
export const PORT = process.env['PORT'];
2526
export const RATE_LIMIT_WINDOW_MS = <number>(process.env['RATE_LIMIT_WINDOW_MS'] || 30);
2627
export const RATE_LIMIT_MAX = <number>(process.env['RATE_LIMIT_MAX'] || 2);
28+
export const JWT_SECRET = <jwt.Options['secret']>(process.env['JWT_SECRET'] || '');
2729

2830
if (!MONGODB_URI) {
2931
if (prod) {

yarn.lock

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,14 @@
642642
resolved "https://registry.yarnpkg.com/@types/express-handlebars/-/express-handlebars-3.1.0.tgz#b931e6b51fef4efb25ee120394a42c03cb7a0d23"
643643
integrity sha512-Bn6j/tfhAnZEAbMtcNUFk6ESu1I6PE2pYLbUn1PR1MyNonUuQErlQ71n9DPppHK7uAuMCfgcF0oT28Lh0ej4SQ==
644644

645+
"@types/express-jwt@^6.0.1":
646+
version "6.0.1"
647+
resolved "https://registry.yarnpkg.com/@types/express-jwt/-/express-jwt-6.0.1.tgz#616cbd149438345084c41544d7dd49cfeca60079"
648+
integrity sha512-zB/oXzS8/NTWUzAG343frlqUrsygHPeyYMVcbJ8YYk7rF1G15eUapPgWh0HdeFi51ajFkkUOU+Q540z1Eu4hJQ==
649+
dependencies:
650+
"@types/express" "*"
651+
"@types/express-unless" "*"
652+
645653
"@types/express-rate-limit@^5.1.1":
646654
version "5.1.1"
647655
resolved "https://registry.yarnpkg.com/@types/express-rate-limit/-/express-rate-limit-5.1.1.tgz#e5b0239d18c1580e52ae56dce4248333302a1dc8"
@@ -667,6 +675,13 @@
667675
"@types/qs" "*"
668676
"@types/range-parser" "*"
669677

678+
"@types/express-unless@*":
679+
version "0.5.1"
680+
resolved "https://registry.yarnpkg.com/@types/express-unless/-/express-unless-0.5.1.tgz#4f440b905e42bbf53382b8207bc337dc5ff9fd1f"
681+
integrity sha512-5fuvg7C69lemNgl0+v+CUxDYWVPSfXHhJPst4yTLcqi4zKJpORCxnDrnnilk3k0DTq/WrAUdvXFs01+vUqUZHw==
682+
dependencies:
683+
"@types/express" "*"
684+
670685
"@types/express@*":
671686
version "4.17.11"
672687
resolved "https://registry.yarnpkg.com/@types/express/-/express-4.17.11.tgz#debe3caa6f8e5fcda96b47bd54e2f40c4ee59545"
@@ -1170,6 +1185,11 @@ astral-regex@^1.0.0:
11701185
resolved "https://registry.yarnpkg.com/astral-regex/-/astral-regex-1.0.0.tgz#6c8c3fb827dd43ee3918f27b82782ab7658a6fd9"
11711186
integrity sha512-+Ryf6g3BKoRc7jfp7ad8tM4TtMiaWvbF/1/sQcZPkkS7ag3D5nMBCe2UfOTONtAkaG0tO0ij3C5Lwmf1EiyjHg==
11721187

1188+
async@^1.5.0:
1189+
version "1.5.2"
1190+
resolved "https://registry.yarnpkg.com/async/-/async-1.5.2.tgz#ec6a61ae56480c0c3cb241c95618e20892f9672a"
1191+
integrity sha1-7GphrlZIDAw8skHJVhjiCJL5Zyo=
1192+
11731193
async@^3.1.0:
11741194
version "3.2.0"
11751195
resolved "https://registry.yarnpkg.com/async/-/async-3.2.0.tgz#b3a2685c5ebb641d3de02d161002c60fc9f85720"
@@ -1386,6 +1406,11 @@ bson@^1.1.4:
13861406
resolved "https://registry.yarnpkg.com/bson/-/bson-1.1.5.tgz#2aaae98fcdf6750c0848b0cba1ddec3c73060a34"
13871407
integrity sha512-kDuEzldR21lHciPQAIulLs1LZlCXdLziXI6Mb/TDkwXhb//UORJNPXgcRs2CuO4H0DcMkpfT3/ySsP3unoZjBg==
13881408

1409+
1410+
version "1.0.1"
1411+
resolved "https://registry.yarnpkg.com/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz#f8e71132f7ffe6e01a5c9697a4c6f3e48d5cc819"
1412+
integrity sha1-+OcRMvf/5uAaXJaXpMbz5I1cyBk=
1413+
13891414
[email protected], buffer-from@^1.0.0:
13901415
version "1.1.1"
13911416
resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.1.tgz#32713bc028f75c02fdb710d7c7bcec1f2c6070ef"
@@ -2043,6 +2068,13 @@ ecc-jsbn@~0.1.1:
20432068
jsbn "~0.1.0"
20442069
safer-buffer "^2.1.0"
20452070

2071+
2072+
version "1.0.11"
2073+
resolved "https://registry.yarnpkg.com/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz#ae0f0fa2d85045ef14a817daa3ce9acd0489e5bf"
2074+
integrity sha512-nagl3RYrbNv6kQkeJIpt6NJZy8twLB/2vtz6yN9Z4vRKHN4/QZJIEbqohALSgwKdnksuY3k5Addp5lg8sVoVcQ==
2075+
dependencies:
2076+
safe-buffer "^5.0.1"
2077+
20462078
20472079
version "1.1.1"
20482080
resolved "https://registry.yarnpkg.com/ee-first/-/ee-first-1.1.1.tgz#590c61156b0ae2f4f0255732a158b266bc56b21d"
@@ -2474,11 +2506,26 @@ express-handlebars@^5.2.0:
24742506
graceful-fs "^4.2.4"
24752507
handlebars "^4.7.6"
24762508

2509+
express-jwt@^6.0.0:
2510+
version "6.0.0"
2511+
resolved "https://registry.yarnpkg.com/express-jwt/-/express-jwt-6.0.0.tgz#20886c730983ffb1c706a4383235df86eff349b8"
2512+
integrity sha512-C26y9myRjx7CyhZ+BAT3p+gQyRCoDZ7qo8plCvLDaRT6je6ALIAQknT6XLVQGFKwIy/Ux7lvM2MNap5dt0T7gA==
2513+
dependencies:
2514+
async "^1.5.0"
2515+
express-unless "^0.3.0"
2516+
jsonwebtoken "^8.1.0"
2517+
lodash.set "^4.0.0"
2518+
24772519
express-rate-limit@^5.2.3:
24782520
version "5.2.3"
24792521
resolved "https://registry.yarnpkg.com/express-rate-limit/-/express-rate-limit-5.2.3.tgz#ae73b3dc723decd697797611bd96e9b34a912f6c"
24802522
integrity sha512-cjQH+oDrEPXxc569XvxhHC6QXqJiuBT6BhZ70X3bdAImcnHnTNMVuMAJaT0TXPoRiEErUrVPRcOTpZpM36VbOQ==
24812523

2524+
express-unless@^0.3.0:
2525+
version "0.3.1"
2526+
resolved "https://registry.yarnpkg.com/express-unless/-/express-unless-0.3.1.tgz#2557c146e75beb903e2d247f9b5ba01452696e20"
2527+
integrity sha1-JVfBRudb65A+LSR/m1ugFFJpbiA=
2528+
24822529
express@^4.17.1:
24832530
version "4.17.1"
24842531
resolved "https://registry.yarnpkg.com/express/-/express-4.17.1.tgz#4491fc38605cf51f8629d39c2b5d026f98a4c134"
@@ -3967,6 +4014,22 @@ json5@^1.0.1:
39674014
dependencies:
39684015
minimist "^1.2.0"
39694016

4017+
jsonwebtoken@^8.1.0:
4018+
version "8.5.1"
4019+
resolved "https://registry.yarnpkg.com/jsonwebtoken/-/jsonwebtoken-8.5.1.tgz#00e71e0b8df54c2121a1f26137df2280673bcc0d"
4020+
integrity sha512-XjwVfRS6jTMsqYs0EsuJ4LGxXV14zQybNd4L2r0UvbVnSF9Af8x7p5MzbJ90Ioz/9TI41/hTCvznF/loiSzn8w==
4021+
dependencies:
4022+
jws "^3.2.2"
4023+
lodash.includes "^4.3.0"
4024+
lodash.isboolean "^3.0.3"
4025+
lodash.isinteger "^4.0.4"
4026+
lodash.isnumber "^3.0.3"
4027+
lodash.isplainobject "^4.0.6"
4028+
lodash.isstring "^4.0.1"
4029+
lodash.once "^4.0.0"
4030+
ms "^2.1.1"
4031+
semver "^5.6.0"
4032+
39704033
jsprim@^1.2.2:
39714034
version "1.4.1"
39724035
resolved "https://registry.yarnpkg.com/jsprim/-/jsprim-1.4.1.tgz#313e66bc1e5cc06e438bc1b7499c2e5c56acb6a2"
@@ -3977,6 +4040,23 @@ jsprim@^1.2.2:
39774040
json-schema "0.2.3"
39784041
verror "1.10.0"
39794042

4043+
jwa@^1.4.1:
4044+
version "1.4.1"
4045+
resolved "https://registry.yarnpkg.com/jwa/-/jwa-1.4.1.tgz#743c32985cb9e98655530d53641b66c8645b039a"
4046+
integrity sha512-qiLX/xhEEFKUAJ6FiBMbes3w9ATzyk5W7Hvzpa/SLYdxNtng+gcurvrI7TbACjIXlsJyr05/S1oUhZrc63evQA==
4047+
dependencies:
4048+
buffer-equal-constant-time "1.0.1"
4049+
ecdsa-sig-formatter "1.0.11"
4050+
safe-buffer "^5.0.1"
4051+
4052+
jws@^3.2.2:
4053+
version "3.2.2"
4054+
resolved "https://registry.yarnpkg.com/jws/-/jws-3.2.2.tgz#001099f3639468c9414000e99995fa52fb478304"
4055+
integrity sha512-YHlZCB6lMTllWDtSPHz/ZXTsi8S00usEV6v1tjq8tOUZzw7DpSDWVXjXDre6ed1w/pd495ODpHZYSdkRTsa0HA==
4056+
dependencies:
4057+
jwa "^1.4.1"
4058+
safe-buffer "^5.0.1"
4059+
39804060
keyv@^3.0.0:
39814061
version "3.1.0"
39824062
resolved "https://registry.yarnpkg.com/keyv/-/keyv-3.1.0.tgz#ecc228486f69991e49e9476485a5be1e8fc5c4d9"
@@ -4087,11 +4167,51 @@ locate-path@^5.0.0:
40874167
dependencies:
40884168
p-locate "^4.1.0"
40894169

4170+
lodash.includes@^4.3.0:
4171+
version "4.3.0"
4172+
resolved "https://registry.yarnpkg.com/lodash.includes/-/lodash.includes-4.3.0.tgz#60bb98a87cb923c68ca1e51325483314849f553f"
4173+
integrity sha1-YLuYqHy5I8aMoeUTJUgzFISfVT8=
4174+
4175+
lodash.isboolean@^3.0.3:
4176+
version "3.0.3"
4177+
resolved "https://registry.yarnpkg.com/lodash.isboolean/-/lodash.isboolean-3.0.3.tgz#6c2e171db2a257cd96802fd43b01b20d5f5870f6"
4178+
integrity sha1-bC4XHbKiV82WgC/UOwGyDV9YcPY=
4179+
4180+
lodash.isinteger@^4.0.4:
4181+
version "4.0.4"
4182+
resolved "https://registry.yarnpkg.com/lodash.isinteger/-/lodash.isinteger-4.0.4.tgz#619c0af3d03f8b04c31f5882840b77b11cd68343"
4183+
integrity sha1-YZwK89A/iwTDH1iChAt3sRzWg0M=
4184+
4185+
lodash.isnumber@^3.0.3:
4186+
version "3.0.3"
4187+
resolved "https://registry.yarnpkg.com/lodash.isnumber/-/lodash.isnumber-3.0.3.tgz#3ce76810c5928d03352301ac287317f11c0b1ffc"
4188+
integrity sha1-POdoEMWSjQM1IwGsKHMX8RwLH/w=
4189+
4190+
lodash.isplainobject@^4.0.6:
4191+
version "4.0.6"
4192+
resolved "https://registry.yarnpkg.com/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz#7c526a52d89b45c45cc690b88163be0497f550cb"
4193+
integrity sha1-fFJqUtibRcRcxpC4gWO+BJf1UMs=
4194+
4195+
lodash.isstring@^4.0.1:
4196+
version "4.0.1"
4197+
resolved "https://registry.yarnpkg.com/lodash.isstring/-/lodash.isstring-4.0.1.tgz#d527dfb5456eca7cc9bb95d5daeaf88ba54a5451"
4198+
integrity sha1-1SfftUVuynzJu5XV2ur4i6VKVFE=
4199+
40904200
lodash.merge@^4.6.0:
40914201
version "4.6.2"
40924202
resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a"
40934203
integrity sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==
40944204

4205+
lodash.once@^4.0.0:
4206+
version "4.1.1"
4207+
resolved "https://registry.yarnpkg.com/lodash.once/-/lodash.once-4.1.1.tgz#0dd3971213c7c56df880977d504c88fb471a97ac"
4208+
integrity sha1-DdOXEhPHxW34gJd9UEyI+0cal6w=
4209+
4210+
lodash.set@^4.0.0:
4211+
version "4.3.2"
4212+
resolved "https://registry.yarnpkg.com/lodash.set/-/lodash.set-4.3.2.tgz#d8757b1da807dde24816b0d6a84bea1a76230b23"
4213+
integrity sha1-2HV7HagH3eJIFrDWqEvqGnYjCyM=
4214+
40954215
lodash.sortby@^4.7.0:
40964216
version "4.7.0"
40974217
resolved "https://registry.yarnpkg.com/lodash.sortby/-/lodash.sortby-4.7.0.tgz#edd14c824e2cc9c1e0b0a1b42bb5210516a42438"
@@ -5421,7 +5541,7 @@ semver-diff@^3.1.1:
54215541
dependencies:
54225542
semver "^6.3.0"
54235543

5424-
"semver@2 || 3 || 4 || 5", semver@^5.1.0, semver@^5.4.1, semver@^5.5.0, semver@^5.7.1:
5544+
"semver@2 || 3 || 4 || 5", semver@^5.1.0, semver@^5.4.1, semver@^5.5.0, semver@^5.6.0, semver@^5.7.1:
54255545
version "5.7.1"
54265546
resolved "https://registry.yarnpkg.com/semver/-/semver-5.7.1.tgz#a954f931aeba508d307bbf069eff0c01c96116f7"
54275547
integrity sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==

0 commit comments

Comments
 (0)