-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.js
112 lines (103 loc) · 2.65 KB
/
functions.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
const mongoose = require("mongoose");
const request = require("request");
const jwt = require("jsonwebtoken");
function linkShortener(long, short, callback) {
let returnData = {};
if (!short) slugMaker().then(short => {
console.log(short);
mongoose.model("ShortDomain").findOne({long: long}, (err, url) => {
if (url) {
// url already in database, return shortlink
returnData = url;
console.log(returnData);
callback(returnData);
}
mongoose.model("ShortDomain").create({
long: long,
short: short
}, (err, sd) => {
if (err) returnData.err = err;
returnData = sd;
console.log(returnData);
callback(returnData);
});
});
});
}
/**
* Deprecate Functions and API Routes
* @param req
* @param res
* @param next
*/
function deprecate(req, res, next) {
res.json({"sucess": false, msg: "This Route/Function is deprecated, please refrain from using it."});
}
/**
* Validate Google reCaptchas
* @param gResponse
* @param callback
*/
function validateReCAPTCHA(gResponse, callback) {
const secretKey = config.reCaptcha.privateKey;
request.post(
`https://www.google.com/recaptcha/api/siteverify?secret=${secretKey}&response=${gResponse}`,
function (error, response, body) {
body = JSON.parse(body);
callback(body.error, body.success);
}
);
}
/**
* Verify JSON Web Tokens
* @param jwt_token
* @param callback
*/
function verifyJWT(jwt_token, callback) {
jwt.verify(jwt_token, config.jwtSecret, (e, u) => {
if (u && u.id) callback(e, u.id);
else callback(e);
});
}
/**
* requireLogin for User Specific areas
* @param req Request send by $User to the server
* @param res Response to be send by the server
* @param next The next handler
*/
function requireLogin(req, res, next) {
verifyJWT(req.cookies.token, (err, userId) => {
console.log(userId);
mongoose.model("User").findOne({_id: userId}, (err, user) => {
if (!user) {
res.redirect('/login');
} else {
next();
}
});
});
}
/**
* reuireAuthentication for user-specific API routes
* @param req
* @param res
* @param next
*/
function requireAuthentication(req, res, next) {
let tk = req.body.token || req.query.token || req.headers['x-access-token'] || req.cookies.token;
if (!tk) res.json({success: false, msg: "Failed to authenticate with Token."});
verifyJWT(tk, (err, userId) => {
mongoose.model("User").findOne({_id: userId}, (err, user) => {
if (user) {
req.authentication = {
user: user,
token: tk
};
next();
} else {
res.end(JSON.stringify({success: false, msg: "Failed to authenticate with Token."}));
console.log("API Access without valid Authentication. Token: ", tk)
}
});
});
}