-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic.js
More file actions
56 lines (41 loc) · 1.25 KB
/
basic.js
File metadata and controls
56 lines (41 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Requiring module
const express = require("express");
const fs = require("fs");
var path = require('path');
const app = express();
function authentication(req, res, next) {
var authheader = req.headers.authorization;
console.log(req.headers);
if (!authheader) {
var err = new Error('You are not authenticated!');
res.setHeader('WWW-Authenticate', 'Basic');
err.status = 401;
return next(err)
}
var auth = new Buffer.from(authheader.split(' ')[1],
'base64').toString().split(':');
var user = auth[0];
var pass = auth[1];
if (user == 'admin' && pass == 'password') {
// If Authorized user
next();
} else {
var err = new Error('You are not authenticated!');
res.setHeader('WWW-Authenticate', 'Basic');
err.status = 401;
return next(err);
}
}
// First step is the authentication of the client
app.use(authentication)
//app.use(express.static(path.join(__dirname, 'public')));
app.get('/basic', (req, res) => {
res.end("Call success with AAD auth!");
});
app.get('/basic2', (req, res) => {
res.end("Call success with AAD auth! 2");
});
// Server setup
app.listen(3000, "localhost", () => {
console.log("Server is Running ");
})