UNPKG

@aarconada/urserver

Version:

Basic Server definitions to develope REST API with a node + express Server

147 lines (138 loc) 6.21 kB
/** * Created by ubuntu on 10/09/18. */ 'use strict'; const redis = require('./redis'); const response = require('./response'); const server = require('./server')(); module.exports.sessionCheckWithoutRequest = function(bearerToken) { server.debug('Start to Check session'); if (bearerToken) { return redis.getRedisValue(bearerToken) .then((foundedSession, err) => { if (err || !foundedSession || foundedSession.length === 0 || foundedSession[0] === null) { server.debug('Unknown session token'); return null; } server.debug('Founded session?', foundedSession !== null); return JSON.parse(foundedSession[0]); }).catch(err => { server.debug('Redis value not founded'); return null; }); } else { server.debug('Session token missing'); return null; } }; module.exports.sessionCheck = function(req) { server.debug('Start to Check session'); const currentToken = req.bearerToken; if (currentToken) { return redis.getRedisValue(currentToken) .then((foundedSession, err) => { if (err || !foundedSession || foundedSession.length === 0 || foundedSession[0] === null) { server.debug('Unknown session token'); return response.unknown_session; } server.debug('Founded session?', foundedSession !== null); req.currentSession = JSON.parse(foundedSession[0]); return response.success; }).catch(err => { server.debug('Redis value not founded'); return response.unknown_session; }); } else { server.debug('Session token missing'); return response.missing_token; } }; module.exports.createSession = function(id, bearerToken, role, authenticationType, data, filters) { var sessionData = { id : id, role : role, authenticationType : authenticationType, bearerToken : bearerToken, data : data, filters : filters }; var basicSessionData = { bearerToken : bearerToken }; redis.setRedisValue((id * 10000) + (role * 100) + authenticationType, JSON.stringify(basicSessionData)); redis.setRedisValue(bearerToken, JSON.stringify(sessionData)); }; module.exports.removeSession = function(bearerToken) { return redis.getRedisValue(bearerToken) .then((foundedSession, err) => { if (err || !foundedSession || foundedSession.length === 0 || foundedSession[0] === null) { server.debug('Unknown session token'); return response.unknown_session; } const currentSession = JSON.parse(foundedSession[0]); redis.removeRedisValue(bearerToken); redis.removeRedisValue((currentSession.id * 10000) + (currentSession.role * 100) + currentSession.authenticationType); return response.success; }).catch(err => { server.debug('Redis value not founded'); return response.unknown_session; }); }; module.exports.getSessionByIds = function(id, role, authenticationType) { return redis.getRedisValue((id * 10000) + (role * 100) + authenticationType) .then((foundedSession, err) => { if (err || !foundedSession || foundedSession.length === 0 || foundedSession[0] === null) { server.debug('Unknown session token'); return response.unknown_session; } const currentSession = JSON.parse(foundedSession[0]); return redis.getRedisValue(currentSession.bearerToken) .then((foundedSession, err) => { if (err || !foundedSession || foundedSession.length === 0 || foundedSession[0] === null) { server.debug('Unknown session token'); return response.unknown_session; } const foundedSessionParsed = JSON.parse(foundedSession[0]); return foundedSessionParsed; }).catch(err => { server.debug('Redis value not founded'); return response.unknown_session; }); }).catch(err => { server.debug('Redis value not founded'); return response.unknown_session; }); }; module.exports.removeSessionByIds = function(id, role, authenticationType) { return redis.getRedisValue((id * 10000) + (role * 100) + authenticationType) .then((foundedSession, err) => { if (err || !foundedSession || foundedSession.length === 0 || foundedSession[0] === null) { server.debug('Unknown session token'); return response.unknown_session; } const currentSession = JSON.parse(foundedSession[0]); redis.removeRedisValue(currentSession.bearerToken); redis.removeRedisValue(id); return response.success; }).catch(err => { server.debug('Redis value not founded'); return response.unknown_session; }); }; module.exports.updateSessionData = function(bearerToken, sessionData, sessionFilters) { return redis.getRedisValue(bearerToken) .then((foundedSession, err) => { if (err || !foundedSession || foundedSession.length === 0 || foundedSession[0] === null) { server.debug('Unknown session token'); return response.unknown_session; } const currentSession = JSON.parse(foundedSession[0]); currentSession.data = sessionData; currentSession.filters = sessionFilters; redis.setRedisValue(bearerToken, JSON.stringify(currentSession)); return response.success; }).catch(err => { server.debug('Redis value not founded'); return response.unknown_session; }); };