Create: Backend branch
This commit is contained in:
parent
f91febf919
commit
76ac0c292c
260 changed files with 10 additions and 12964 deletions
26
config/functions.config.js
Normal file
26
config/functions.config.js
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
const request = require("request");
|
||||
const VideoCategories = require("../models/objects/video.categories.model");
|
||||
|
||||
function asyncRequest(uri, option){
|
||||
return new Promise(function(resolve){
|
||||
request(uri, option,function (error, response, body){
|
||||
resolve({response: response, body: JSON.parse(body)});
|
||||
});
|
||||
});
|
||||
}
|
||||
module.exports.asyncRequest = asyncRequest;
|
||||
|
||||
function asyncInterest(interest, source){
|
||||
return new Promise(function(resolve){
|
||||
for(const i in VideoCategories){
|
||||
for(const j in VideoCategories[i].categories){
|
||||
if((VideoCategories[i].categories[j].name === interest || VideoCategories[i].categories[j].id === interest)
|
||||
&& VideoCategories[i].categories[j].source === source){
|
||||
resolve(VideoCategories[i].interest);
|
||||
}
|
||||
}
|
||||
}
|
||||
resolve(null);
|
||||
});
|
||||
}
|
||||
module.exports.asyncInterest = asyncInterest;
|
||||
26
config/host.config.js
Normal file
26
config/host.config.js
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
if(process.env.YOUTUBE_API_KEY === undefined ||
|
||||
process.env.YOUTUBE_API_KEY === '' ||
|
||||
process.env.DAILYMOTION_API_KEY === undefined ||
|
||||
process.env.DAILYMOTION_API_KEY === ''){
|
||||
console.log('Error Env YOUTUBE_API_KEY & DAILYMOTION_API_KEY Variables');
|
||||
process.exit();
|
||||
}
|
||||
|
||||
console.log('Env variables YOUTUBE_API_KEY & DAILYMOTION_API_KEY received');
|
||||
|
||||
module.exports = {
|
||||
youtube: {
|
||||
name: "Youtube",
|
||||
shortname: "yt",
|
||||
baseAPIUrl: 'https://youtube.googleapis.com/youtube/v3',
|
||||
baseChannelUrl: 'https://www.youtube.com/channel/',
|
||||
YOUTUBE_API_KEY: process.env.YOUTUBE_API_KEY
|
||||
},
|
||||
dailymotion: {
|
||||
name: "Dailymotion",
|
||||
shortname: "dm",
|
||||
baseAPIUrl: 'https://api.dailymotion.com',
|
||||
baseChannelUrl: 'https://www.dailymotion.com/',
|
||||
DAILYMOTION_API_KEY: process.env.DAILYMOTION_API_KEY
|
||||
}
|
||||
};
|
||||
4
config/mongodb.config.js
Normal file
4
config/mongodb.config.js
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
module.exports = {
|
||||
prodUrl: process.env.DATABASE,
|
||||
devUrl: "mongodb://127.0.0.1:27017/polynotfound"
|
||||
};
|
||||
9
config/response.config.js
Normal file
9
config/response.config.js
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
function sendMessage (res, successCode, data, token=null) {
|
||||
res.status(200).json({ status: 'success', successCode: successCode, token: token, data: data });
|
||||
}
|
||||
|
||||
function sendError (res, statusCode, errorCode, reason, token=null) {
|
||||
res.status(statusCode).json({ status: 'error', errorCode: errorCode, token: token, reason: reason});
|
||||
}
|
||||
|
||||
module.exports = { sendMessage, sendError };
|
||||
109
config/sessionJWT.config.js
Normal file
109
config/sessionJWT.config.js
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
const sessionJWTConfig = require ('jsonwebtoken');
|
||||
require('dotenv').config({ path: './app-backend/.env' });
|
||||
const {sendError} = require ("./response.config");
|
||||
|
||||
if(process.env.JWTRS256_PRIVATE_KEY === undefined || process.env.JWTRS256_PUBLIC_KEY === undefined){
|
||||
console.log('Error Env JWTRS256_PRIVATE_KEY & JWTRS256_PUBLIC_KEY Variables');
|
||||
process.exit();
|
||||
}
|
||||
|
||||
console.log('Env variables JWTRS256_PRIVATE_KEY & JWTRS256_PUBLIC_KEY received');
|
||||
const JWTRS256_PRIVATE_KEY = Buffer.from(process.env.JWTRS256_PRIVATE_KEY, 'base64').toString('utf-8');
|
||||
const JWTRS256_PUBLIC_KEY = Buffer.from(process.env.JWTRS256_PUBLIC_KEY, 'base64').toString('utf-8');
|
||||
|
||||
|
||||
function createSessionJWT (id, email, profileImageUrl, role) {
|
||||
return sessionJWTConfig.sign(
|
||||
{
|
||||
id: id,
|
||||
email: email,
|
||||
profileImageUrl: profileImageUrl,
|
||||
role: role,
|
||||
midExp: Math.floor(Date.now() / 1000) + 1800
|
||||
},
|
||||
JWTRS256_PRIVATE_KEY,
|
||||
{
|
||||
algorithm: 'RS256',
|
||||
expiresIn: '1h'
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
function createSessionCookie(req, res, payload) {
|
||||
let jwtToken;
|
||||
if (typeof payload.id !== 'undefined' &&
|
||||
typeof payload.email !== 'undefined' &&
|
||||
typeof payload.profileImageUrl !== 'undefined' &&
|
||||
typeof payload.role !== 'undefined' &&
|
||||
typeof payload.midExp !== 'undefined' &&
|
||||
(Math.floor(Date.now() / 1000) <= payload.midExp)) {
|
||||
jwtToken = req.headers.cookie;
|
||||
}
|
||||
else {
|
||||
jwtToken = createSessionJWT(payload.id, payload.email, payload.profileImageUrl, payload.role);
|
||||
}
|
||||
res.cookie('SESSIONID', jwtToken, {httpOnly:true, secure:false});
|
||||
}
|
||||
|
||||
function decodeSessionCookie(sessionid) {
|
||||
if (typeof sessionid === 'undefined') {
|
||||
return {id: -1, email: -1, profileImageUrl: -1, role: -1};
|
||||
}
|
||||
try {
|
||||
const token = sessionJWTConfig.verify(
|
||||
sessionid,
|
||||
JWTRS256_PUBLIC_KEY,
|
||||
{algorithms: ['RS256']});
|
||||
return {token: token};
|
||||
}
|
||||
catch (err) {
|
||||
return {id: -1, email: -1, profileImageUrl: -1, role: -1};
|
||||
}
|
||||
}
|
||||
|
||||
function getSession(sessionid) {
|
||||
return decodeSessionCookie(sessionid);
|
||||
}
|
||||
module.exports.getSession = getSession
|
||||
|
||||
function setSessionCookie (req, res, session) {
|
||||
createSessionCookie(req, res, session);
|
||||
}
|
||||
module.exports.setSessionCookie = setSessionCookie;
|
||||
|
||||
function getToken(session) {
|
||||
if (typeof session === 'undefined' || typeof session.token === 'undefined') return -1;
|
||||
return session.token;
|
||||
}
|
||||
module.exports.getToken = getToken;
|
||||
|
||||
function checkLogin(req, res, role=null){
|
||||
if(typeof req.cookies !== 'undefined'){
|
||||
const session = getSession(req.cookies.SESSIONID);
|
||||
const token = getToken(session);
|
||||
if(typeof token.email === 'undefined' ||
|
||||
token.email === -1 ||
|
||||
typeof token.id === 'undefined' ||
|
||||
token.id === -1){
|
||||
return sendError(res, 500, 102, "User not authenticated.");
|
||||
} else {
|
||||
token.midExp = new Date(token.midExp*1000);
|
||||
token.iat = new Date(token.iat*1000);
|
||||
token.exp = new Date(token.exp*1000);
|
||||
if(role === null){
|
||||
return token;
|
||||
} else {
|
||||
if(typeof token.role !== 'undefined' &&
|
||||
((Array.isArray(role) && role.includes(token.role)) ||
|
||||
( typeof role === 'object' && typeof token.role.permission !== 'undefined' && token.role.permission >= role.permission && token.role.isAccepted === true))){
|
||||
return token;
|
||||
} else {
|
||||
return sendError(res, 500, 106, "User doesn't have permission.", token);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return sendError(res, 500, -1, "Cookies don't exist.");
|
||||
}
|
||||
}
|
||||
module.exports.checkLogin = checkLogin;
|
||||
Reference in a new issue