Skip to content

Commit

Permalink
Merge pull request #28 from ong-bitcoin-argentina/vus-123/crear-servi…
Browse files Browse the repository at this point in the history
…cio-redis

[VUS-123] Crear servicio para Redis
  • Loading branch information
fedelmar committed Feb 9, 2022
2 parents 45a9eb2 + 7f4d862 commit 490b6c9
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 10 deletions.
8 changes: 8 additions & 0 deletions src/constants/serviceErrors.js
Expand Up @@ -59,4 +59,12 @@ module.exports = {
code: '#service-missingFile',
message: 'Falta el parametro File',
},
missingKey: {
code: '#service-missingKey',
message: 'Falta el parametro Key',
},
missingValue: {
code: '#service-missingValue',
message: 'Falta el parametro Value',
},
};
15 changes: 5 additions & 10 deletions src/middelwares/ValidateUser.js
@@ -1,26 +1,21 @@
const fetch = require('node-fetch');
const redis = require('redis');

const didJWT = require('did-jwt');

const { USER } = require('../constants/Messages');
const { DIDI_SERVER, REDIS_URI } = require('../constants/Constants');
const { DIDI_SERVER } = require('../constants/Constants');
const { sendErrWithStatus } = require('../utils/ResponseHandler');
const { get, set } = require('../services/RedisService');

const url = `${DIDI_SERVER}/user/verifyToken`;
const client = redis.createClient(REDIS_URI);

(async () => {
await client.connect();
})();

// Middelware para verificar que un usuario exista en Didi Server
const validateUser = async (req, res, next) => {
const jwt = req.header('Authorization');
try {
const { iss } = await didJWT.decodeJWT(jwt).payload;
const searchTerm = `verificacion-usuario-${iss}`;

const userInCache = await client.get(searchTerm);
const userInCache = await get(searchTerm);
if (!userInCache) {
const user = await fetch(url, {
method: 'POST',
Expand All @@ -32,7 +27,7 @@ const validateUser = async (req, res, next) => {
const { data } = await user.json();

if (!data) throw USER.ERR.VALIDATE;
await client.setEx(searchTerm, 864000, data.did);
await set(searchTerm, data.did);
}
next();
} catch (e) {
Expand Down
51 changes: 51 additions & 0 deletions src/services/RedisService.js
@@ -0,0 +1,51 @@
const redis = require('redis');
const { REDIS_URI } = require('../constants/Constants');
const { missingKey, missingValue } = require('../constants/serviceErrors');

const client = redis.createClient(REDIS_URI);

(async () => {
await client.connect();
})();

const get = async (key) => {
if (!key) throw missingKey;
try {
const value = await client.get(key);
return value;
} catch (err) {
return err;
}
};

const set = async (key, value) => {
if (!key) throw missingKey;
if (!value) throw missingValue;
try {
const response = await client.setEx(key, 864000, value);
return response;
} catch (err) {
return err;
}
};

const del = async (key) => {
if (!key) throw missingKey;
try {
const response = await client.del(key);
return response;
} catch (err) {
return err;
}
};

const disconnect = () => {
return client.disconnect();
};

module.exports = {
get,
set,
del,
disconnect,
};

0 comments on commit 490b6c9

Please sign in to comment.