-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.js
52 lines (47 loc) · 1.64 KB
/
auth.js
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
/*
* Author: Vignesh Kumar
* Copyright: vigneshuvi
* Date: 04/02/2016
* This file contains the authentication logics.
*/
'use strict';
var exports = module.exports = {};
// This function contains the basic validation for Auth token and header format.
exports.authentication = function(req, res, next) {
try {
console.log("Web service are validating");
var header = req.headers;
console.log("header", header);
if(header && header.xdate && header.xauthorization) {
var timeStart = parseFloat(header.xdate);
var timeEnd = new Date().getTime();
var minDiff = (timeEnd - timeStart) / 60 / 1000; //in minutes
if (header.xauthorization === "bypass" && minDiff <= 30) { // Validate the auth token and web service request time.
next();
} else if (minDiff > 30) {
var resJson = {
status : false,
message : "Web service request is bad type."
}
return res.status(404).send(resJson);
} else {
return res.sendStatus(404);
}
} else {
return res.sendStatus(404);
}
} catch (exe) {
console.log("Exeception occured while validate the request headers. \n Error :",exe);
return res.sendStatus(404);
}
};
// This function helps to by-pass the basic authentication.
exports.bypass = function(req, res, next) {
try {
console.log("Web service are Bypassed");
next();
} catch (exe) {
console.log("Exeception occured while bypass the request headers. \n Error :",exe);
return res.sendStatus(404);
}
};