Skip to content

Commit 9599d82

Browse files
author
hirsch88
committed
Secure /info, /swagger and /monitor with a simple basic auth middleware
1 parent 2fc6cc9 commit 9599d82

File tree

8 files changed

+47
-15
lines changed

8 files changed

+47
-15
lines changed

.env.example

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#
44
APP_NAME="express-typescript-boilerplate"
55
APP_ENV="local"
6-
APP_HOST="http://localhost"
6+
APP_HOST="http://localhost:3000"
77
APP_URL_PREFIX="/api"
88
APP_PORT=3000
99

@@ -13,6 +13,12 @@ APP_PORT=3000
1313
LOG_LEVEL="debug"
1414
LOG_ADAPTER="winston"
1515

16+
#
17+
# APPLICATION
18+
#
19+
APP_BASIC_USER="admin"
20+
APP_BASIC_PASSWORD="1234"
21+
1622
#
1723
# API Info
1824
#
@@ -23,14 +29,14 @@ API_INFO_ROUTE="/info"
2329
# Swagger Documentation
2430
#
2531
SWAGGER_ENABLED=true
26-
SWAGGER_ROUTE="/docs"
32+
SWAGGER_ROUTE="/swagger"
2733
SWAGGER_FILE="/src/api/swagger.json"
2834

2935
#
3036
# Monitor
3137
#
3238
MONITOR_ENABLED=true
33-
MONITOR_ROUTE="/status"
39+
MONITOR_ROUTE="/monitor"
3440

3541
#
3642
# DATABASE
@@ -53,5 +59,4 @@ DB_SEEDS_DIR="./src/database/seeds"
5359
#
5460
# Auth0
5561
#
56-
# AUTH0_HOST="https://w3tecch.auth0.com"
5762
AUTH0_HOST="http://localhost:3333"

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
"cors": "^2.8.4",
7373
"dotenv": "^4.0.0",
7474
"express": "^4.16.2",
75+
"express-basic-auth": "^1.1.3",
7576
"express-status-monitor": "^1.0.1",
7677
"faker": "^4.1.0",
7778
"figlet": "^1.2.0",

src/core/ApiInfo.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as express from 'express';
22
import { Environment } from './helpers/Environment';
33
import { SwaggerUI } from './SwaggerUI';
44
import { ApiMonitor } from './ApiMonitor';
5+
import { BasicAuthenticate } from './BasicAuthenticate';
56

67

78
export class ApiInfo {
@@ -10,23 +11,25 @@ export class ApiInfo {
1011
return process.env.APP_URL_PREFIX + process.env.API_INFO_ROUTE;
1112
}
1213

13-
public setup(app: express.Application): void {
14+
public setup(application: express.Application): void {
1415
if (Environment.isTruthy(process.env.API_INFO_ENABLED)) {
15-
app.get(
16+
application.get(
1617
ApiInfo.getRoute(),
1718
// @ts-ignore: False type definitions from express
19+
BasicAuthenticate(),
20+
// @ts-ignore: False type definitions from express
1821
(req: myExpress.Request, res: myExpress.Response) => {
1922
const pkg = Environment.getPkg();
2023
const links = {
2124
links: {}
2225
};
2326
if (Environment.isTruthy(process.env.SWAGGER_ENABLED)) {
2427
links.links['swagger'] =
25-
`${app.get('host')}:${app.get('port')}${SwaggerUI.getRoute()}`;
28+
`${application.get('host')}${SwaggerUI.getRoute()}`;
2629
}
2730
if (Environment.isTruthy(process.env.MONITOR_ENABLED)) {
2831
links.links['monitor'] =
29-
`${app.get('host')}:${app.get('port')}${ApiMonitor.getRoute()}`;
32+
`${application.get('host')}${ApiMonitor.getRoute()}`;
3033
}
3134
return res.json({
3235
name: pkg.name,

src/core/ApiMonitor.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as express from 'express';
22
import * as monitor from 'express-status-monitor';
33
import { Environment } from './helpers/Environment';
4+
import { BasicAuthenticate } from './BasicAuthenticate';
45

56

67
export class ApiMonitor {
@@ -12,7 +13,7 @@ export class ApiMonitor {
1213
public setup(app: express.Application): void {
1314
if (Environment.isTruthy(process.env.MONITOR_ENABLED)) {
1415
app.use(monitor());
15-
app.get(ApiMonitor.getRoute(), monitor().pageRoute);
16+
app.get(ApiMonitor.getRoute(), BasicAuthenticate(), monitor().pageRoute);
1617
}
1718
}
1819
}

src/core/BasicAuthenticate.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import * as basicAuth from 'express-basic-auth';
2+
3+
4+
export const BasicAuthenticate = (): any => {
5+
return basicAuth({
6+
users: {
7+
[process.env.APP_BASIC_USER]: process.env.APP_BASIC_PASSWORD
8+
},
9+
challenge: true
10+
});
11+
};

src/core/Server.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,21 @@ export class Server {
6565
*/
6666
public onStartUp(app: express.Application): void {
6767
this.log.debug(``);
68-
this.log.debug(`Aloha, your app is ready on ${app.get('host')}:${app.get('port')}${process.env.APP_URL_PREFIX}`);
68+
this.log.debug(`Aloha, your app is ready on ${app.get('host')}${process.env.APP_URL_PREFIX}`);
6969
this.log.debug(`To shut it down, press <CTRL> + C at any time.`);
7070
this.log.debug(``);
7171
this.log.debug('-------------------------------------------------------');
7272
this.log.debug(`Environment : ${Environment.getNodeEnv()}`);
7373
this.log.debug(`Version : ${Environment.getPkg().version}`);
7474
this.log.debug(``);
7575
if (Environment.isTruthy(process.env.API_INFO_ENABLED)) {
76-
this.log.debug(`API Info : ${app.get('host')}:${app.get('port')}${ApiInfo.getRoute()}`);
76+
this.log.debug(`API Info : ${app.get('host')}${ApiInfo.getRoute()}`);
7777
}
7878
if (Environment.isTruthy(process.env.SWAGGER_ENABLED)) {
79-
this.log.debug(`Swagger : ${app.get('host')}:${app.get('port')}${SwaggerUI.getRoute()}`);
79+
this.log.debug(`Swagger : ${app.get('host')}${SwaggerUI.getRoute()}`);
8080
}
8181
if (Environment.isTruthy(process.env.MONITOR_ENABLED)) {
82-
this.log.debug(`Monitor : ${app.get('host')}:${app.get('port')}${ApiMonitor.getRoute()}`);
82+
this.log.debug(`Monitor : ${app.get('host')}${ApiMonitor.getRoute()}`);
8383
}
8484
this.log.debug('-------------------------------------------------------');
8585
this.log.debug('');

src/core/SwaggerUI.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ import * as express from 'express';
22
import * as path from 'path';
33
import * as swaggerUi from 'swagger-ui-express';
44
import { Environment } from './helpers/Environment';
5+
import { BasicAuthenticate } from './BasicAuthenticate';
56

67

78
export class SwaggerUI {
89

910
public static getRoute(): string {
10-
return process.env.APP_URL_PREFIX + process.env.SWAGGER_ROUTE;
11+
return process.env.SWAGGER_ROUTE;
1112
}
1213

1314
public setup(app: express.Application): void {
@@ -25,7 +26,7 @@ export class SwaggerUI {
2526
};
2627

2728
// Initialize swagger-jsdoc -> returns validated swagger spec in json format
28-
app.use(SwaggerUI.getRoute(), swaggerUi.serve, swaggerUi.setup(swaggerFile));
29+
app.use(SwaggerUI.getRoute(), BasicAuthenticate(), swaggerUi.serve, swaggerUi.setup(swaggerFile));
2930
}
3031
}
3132
}

yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,10 @@ base64url@2.0.0, base64url@^2.0.0:
681681
version "2.0.0"
682682
resolved "https://registry.yarnpkg.com/base64url/-/base64url-2.0.0.tgz#eac16e03ea1438eff9423d69baa36262ed1f70bb"
683683

684+
basic-auth@^1.1.0:
685+
version "1.1.0"
686+
resolved "https://registry.yarnpkg.com/basic-auth/-/basic-auth-1.1.0.tgz#45221ee429f7ee1e5035be3f51533f1cdfd29884"
687+
684688
basic-auth@~2.0.0:
685689
version "2.0.0"
686690
resolved "https://registry.yarnpkg.com/basic-auth/-/basic-auth-2.0.0.tgz#015db3f353e02e56377755f962742e8981e7bbba"
@@ -1569,6 +1573,12 @@ expect@^21.2.1:
15691573
jest-message-util "^21.2.1"
15701574
jest-regex-util "^21.2.0"
15711575

1576+
express-basic-auth@^1.1.3:
1577+
version "1.1.3"
1578+
resolved "https://registry.yarnpkg.com/express-basic-auth/-/express-basic-auth-1.1.3.tgz#18924c02fef18d9efe58e22847ee31e240749f33"
1579+
dependencies:
1580+
basic-auth "^1.1.0"
1581+
15721582
express-status-monitor@^1.0.1:
15731583
version "1.0.1"
15741584
resolved "https://registry.yarnpkg.com/express-status-monitor/-/express-status-monitor-1.0.1.tgz#311288347b7aabfeaec0a01547e55c77652bb298"

0 commit comments

Comments
 (0)