Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1dba538

Browse files
committedFeb 6, 2024
feat: add basic auth
1 parent ccaddbb commit 1dba538

File tree

5 files changed

+39
-1
lines changed

5 files changed

+39
-1
lines changed
 

‎.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ APP_ENV=dev
33
STORAGE_LOCATION=files
44
SYSTEM_SHUTDOWN_DELAY=0
55
MONGODB_URL='mongodb://localhost:27017/example?maxPoolSize=10&socketTimeoutMS=10000&connectTimeoutMS=10000&useUnifiedTopology=true'
6+
AUTH_BASIC_USERNAME=
7+
AUTH_BASIC_PASSWORD=

‎bin/app/server.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const upload = multer({ dest: configs.get("/storage").location });
77
const express = require('express');
88
const bodyParser = require('body-parser');
99

10+
const basicAuthHelper = require('../helpers/auth/basic_auth');
11+
1012
const userHandler = require('../modules/user/handlers/api_handler');
1113

1214
class AppServer {
@@ -16,14 +18,15 @@ class AppServer {
1618
this.app.use(bodyParser.json());
1719
this.app.use(bodyParser.urlencoded({ extended: false }));
1820
this.app.use(upload.any());
21+
this.app.use(basicAuthHelper.init());
1922

2023
this.init();
2124

2225
this.app.get('/', (_, res) => {
2326
res.json({ status: true, data: null, message: 'server is running...', code: 200 })
2427
});
2528

26-
this.app.post('/api/users/v1/register', userHandler.registerUser);
29+
this.app.post('/api/users/v1/register', basicAuthHelper.authenticate, userHandler.registerUser);
2730
this.app.post('/api/users/v1/auth', userHandler.authUser);
2831
}
2932

‎bin/helpers/auth/basic_auth.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
const configs = require('../configs/global_config');
3+
const validate = require('validate.js');
4+
const passport = require('passport');
5+
const { BasicStrategy } = require('passport-http');
6+
7+
8+
passport.use(new BasicStrategy((username, password, done) => {
9+
const basic = configs.get('/auth/basic');
10+
if(
11+
validate.isEmpty(basic)
12+
|| (basic.username == username && basic.password == password)
13+
) {
14+
return done(null, {});
15+
}
16+
return done(null, false);
17+
}));
18+
19+
const authenticate = passport.authenticate('basic', { session: false });
20+
const init = () => passport.initialize();
21+
22+
module.exports = {
23+
authenticate,
24+
init,
25+
};

‎bin/helpers/configs/global_config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ const config = {
77
system: {
88
shutdownDelay: parseInt(process.env.SYSTEM_SHUTDOWN_DELAY) || 500,
99
},
10+
auth: {
11+
basic: {
12+
username: process.env.AUTH_BASIC_USERNAME,
13+
password: process.env.AUTH_BASIC_PASSWORD,
14+
}
15+
},
1016
storage: {
1117
location: process.env.STORAGE_LOCATION,
1218
},

‎package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
"mongodb": "5.9.2",
2424
"multer": "^1.4.5-lts.1",
2525
"nodemon": "^3.0.3",
26+
"passport": "^0.7.0",
27+
"passport-http": "^0.3.0",
2628
"uuid": "^9.0.1",
2729
"validate.js": "^0.13.1",
2830
"winston": "^3.11.0"

0 commit comments

Comments
 (0)
Please sign in to comment.