Comparing version 1.2.1 to 1.2.2
@@ -1,1 +0,1 @@ | ||
'use strict';var _stringify=require('babel-runtime/core-js/json/stringify');var _stringify2=_interopRequireDefault(_stringify);var _promise=require('babel-runtime/core-js/promise');var _promise2=_interopRequireDefault(_promise);var _regenerator=require('babel-runtime/regenerator');var _regenerator2=_interopRequireDefault(_regenerator);var _assign=require('babel-runtime/core-js/object/assign');var _assign2=_interopRequireDefault(_assign);var _asyncToGenerator2=require('babel-runtime/helpers/asyncToGenerator');var _asyncToGenerator3=_interopRequireDefault(_asyncToGenerator2);var _classCallCheck2=require('babel-runtime/helpers/classCallCheck');var _classCallCheck3=_interopRequireDefault(_classCallCheck2);var _createClass2=require('babel-runtime/helpers/createClass');var _createClass3=_interopRequireDefault(_createClass2);var _redisFastDriver=require('redis-fast-driver');var _redisFastDriver2=_interopRequireDefault(_redisFastDriver);var _jsonwebtoken=require('jsonwebtoken');var _jsonwebtoken2=_interopRequireDefault(_jsonwebtoken);var _shelljs=require('shelljs');var _shelljs2=_interopRequireDefault(_shelljs);var _chalk=require('chalk');var _chalk2=_interopRequireDefault(_chalk);var _ms=require('ms');var _ms2=_interopRequireDefault(_ms);function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj};}var Jwt=function(){function Jwt(secret){(0,_classCallCheck3.default)(this,Jwt);this.secret=secret;}(0,_createClass3.default)(Jwt,[{key:'sign',value:function(){var _ref=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function _callee(rjwt,data){var value;return _regenerator2.default.wrap(function _callee$(_context){while(1){switch(_context.prev=_context.next){case 0:value={rjwt:rjwt};if(data){(0,_assign2.default)(value,{data:{token:data}});}_context.prev=2;_context.next=5;return _jsonwebtoken2.default.sign(value,this.secret);case 5:return _context.abrupt('return',_context.sent);case 8:_context.prev=8;_context.t0=_context['catch'](2);throw'redis-jwt-> Error creating token...';case 11:case'end':return _context.stop();}}},_callee,this,[[2,8]]);}));function sign(_x,_x2){return _ref.apply(this,arguments);}return sign;}()},{key:'verify',value:function(){var _ref2=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function _callee2(token){return _regenerator2.default.wrap(function _callee2$(_context2){while(1){switch(_context2.prev=_context2.next){case 0:_context2.prev=0;return _context2.abrupt('return',_jsonwebtoken2.default.verify(token,this.secret));case 4:_context2.prev=4;_context2.t0=_context2['catch'](0);throw'redis-jwt-> Error verify token...';case 7:case'end':return _context2.stop();}}},_callee2,this,[[0,4]]);}));function verify(_x3){return _ref2.apply(this,arguments);}return verify;}()}]);return Jwt;}();var Driver=function(){function Driver(config){(0,_classCallCheck3.default)(this,Driver);this.config=config;this.r=new _redisFastDriver2.default(this.config);var self=this;setTimeout(function(){self.ping().then(function(result){if(!result){console.log(_chalk2.default.redBright('redis-jwt-> Could not establish a connection'));throw'Error';}});},2000);}(0,_createClass3.default)(Driver,[{key:'exec',value:function exec(){return this.r;}},{key:'on',value:function on(event,cb){return this.r.on(event,cb);}},{key:'ping',value:function(){var _ref3=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function _callee3(){return _regenerator2.default.wrap(function _callee3$(_context3){while(1){switch(_context3.prev=_context3.next){case 0:_context3.next=2;return this.r.rawCall(['ping']);case 2:return _context3.abrupt('return',_context3.sent);case 3:case'end':return _context3.stop();}}},_callee3,this);}));function ping(){return _ref3.apply(this,arguments);}return ping;}()},{key:'create',value:function(){var _ref4=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function _callee4(key,value,ttl){var id;return _regenerator2.default.wrap(function _callee4$(_context4){while(1){switch(_context4.prev=_context4.next){case 0:_context4.prev=0;id=key.split(':')[0];if(this.config.multiple){_context4.next=5;break;}_context4.next=5;return this.destroyMultiple(id);case 5:if(!ttl){_context4.next=10;break;}_context4.next=8;return this.r.rawCall(['SETEX',key,(0,_ms2.default)(ttl)/1000,value]);case 8:_context4.next=12;break;case 10:_context4.next=12;return this.r.rawCall(['SET',key,value]);case 12:_context4.next=17;break;case 14:_context4.prev=14;_context4.t0=_context4['catch'](0);throw'Error 1 Redis '+_context4.t0;case 17:return _context4.abrupt('return',true);case 18:case'end':return _context4.stop();}}},_callee4,this,[[0,14]]);}));function create(_x4,_x5,_x6){return _ref4.apply(this,arguments);}return create;}()},{key:'exists',value:function(){var _ref5=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function _callee5(key){var _this=this;return _regenerator2.default.wrap(function _callee5$(_context5){while(1){switch(_context5.prev=_context5.next){case 0:return _context5.abrupt('return',new _promise2.default(function(resolve,reject){_this.r.rawCall(['EXISTS',key],function(err,result){if(result)resolve(result);resolve(null);});}));case 1:case'end':return _context5.stop();}}},_callee5,this);}));function exists(_x7){return _ref5.apply(this,arguments);}return exists;}()},{key:'ttl',value:function(){var _ref6=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function _callee6(key){var _this2=this;return _regenerator2.default.wrap(function _callee6$(_context6){while(1){switch(_context6.prev=_context6.next){case 0:return _context6.abrupt('return',new _promise2.default(function(resolve,reject){_this2.r.rawCall(['ttl',key],function(err,result){if(result)resolve(result);resolve(null);});}));case 1:case'end':return _context6.stop();}}},_callee6,this);}));function ttl(_x8){return _ref6.apply(this,arguments);}return ttl;}()},{key:'getValueByKey',value:function(){var _ref7=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function _callee7(key){var _this3=this;return _regenerator2.default.wrap(function _callee7$(_context7){while(1){switch(_context7.prev=_context7.next){case 0:return _context7.abrupt('return',new _promise2.default(function(resolve,reject){_this3.r.rawCall(['get',key],function(err,result){if(result)resolve(result);resolve(null);});}));case 1:case'end':return _context7.stop();}}},_callee7,this);}));function getValueByKey(_x9){return _ref7.apply(this,arguments);}return getValueByKey;}()},{key:'getValuesByPattern',value:function(){var _ref8=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function _callee8(pattern){var _this4=this;return _regenerator2.default.wrap(function _callee8$(_context8){while(1){switch(_context8.prev=_context8.next){case 0:return _context8.abrupt('return',new _promise2.default(function(resolve,reject){_this4.r.rawCall(['keys',pattern+':*'],function(err,keys){var query=['MGET'].concat(keys);_this4.r.rawCall(query,function(err,result){if(result)resolve(result);resolve(null);});});}));case 1:case'end':return _context8.stop();}}},_callee8,this);}));function getValuesByPattern(_x10){return _ref8.apply(this,arguments);}return getValuesByPattern;}()},{key:'getCountByPattern',value:function(){var _ref9=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function _callee9(pattern){var _this5=this;return _regenerator2.default.wrap(function _callee9$(_context9){while(1){switch(_context9.prev=_context9.next){case 0:return _context9.abrupt('return',new _promise2.default(function(resolve,reject){_this5.r.rawCall(['keys',pattern+':*'],function(err,result){if(result)resolve(result.length);resolve(null);});}));case 1:case'end':return _context9.stop();}}},_callee9,this);}));function getCountByPattern(_x11){return _ref9.apply(this,arguments);}return getCountByPattern;}()},{key:'getInfo',value:function(){var _ref10=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function _callee10(section){var _this6=this;return _regenerator2.default.wrap(function _callee10$(_context10){while(1){switch(_context10.prev=_context10.next){case 0:return _context10.abrupt('return',new _promise2.default(function(resolve,reject){var query=section==='DBSIZE'?['DBSIZE']:['INFO',section];_this6.r.rawCall(query,function(err,result){if(result)resolve(result);resolve(null);});}));case 1:case'end':return _context10.stop();}}},_callee10,this);}));function getInfo(_x12){return _ref10.apply(this,arguments);}return getInfo;}()},{key:'destroy',value:function(){var _ref11=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function _callee11(key){var _this7=this;return _regenerator2.default.wrap(function _callee11$(_context11){while(1){switch(_context11.prev=_context11.next){case 0:return _context11.abrupt('return',new _promise2.default(function(resolve,reject){_this7.r.rawCall(['DEL',key],function(err,result){if(result)resolve(result);resolve(null);});}));case 1:case'end':return _context11.stop();}}},_callee11,this);}));function destroy(_x13){return _ref11.apply(this,arguments);}return destroy;}()},{key:'destroyMultiple',value:function(){var _ref12=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function _callee12(key){var _this8=this;return _regenerator2.default.wrap(function _callee12$(_context12){while(1){switch(_context12.prev=_context12.next){case 0:return _context12.abrupt('return',new _promise2.default(function(resolve,reject){_this8.r.rawCall(['KEYS',key+':*'],function(err,keys){keys.join();keys.splice(0,0,"DEL");_this8.r.rawCall(keys,function(err,result){if(result)resolve(result);resolve(null);});});}));case 1:case'end':return _context12.stop();}}},_callee12,this);}));function destroyMultiple(_x14){return _ref12.apply(this,arguments);}return destroyMultiple;}()}]);return Driver;}();var RedisJwt=function(){function RedisJwt(config){(0,_classCallCheck3.default)(this,RedisJwt);if(config.kea){if(_shelljs2.default.exec('redis-cli config set notify-keyspace-events KEA').code!==0){_shelljs2.default.echo('Error: notify-keyspace-events KEA failed');_shelljs2.default.exit(1);}}this.config={};this.config.name='redis-jwt';this.config.host=config.host||'127.0.0.1';this.config.port=config.port||6379;this.config.db=config.db||0;this.config.maxretries=config.maxretries||10;this.config.auth=config.auth||false;this.config.secret=config.secret||'secret_key';this.config.multiple=!config.multiple?false:true;this.d=new Driver(this.config);this.j=new Jwt(this.config.secret);}(0,_createClass3.default)(RedisJwt,[{key:'sign',value:function(){var _ref13=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function _callee13(_id,_options){var key,_ttl,session,dataToken,token,data;return _regenerator2.default.wrap(function _callee13$(_context13){while(1){switch(_context13.prev=_context13.next){case 0:_context13.prev=0;key=_id+':'+this.makeid(11);_ttl=null;session={_key:key};dataToken=null;if(_options){if(_options.ttl)_ttl=_options.ttl;if(_options.data){if('request'in _options.data){(0,_assign2.default)(session,{request:{agent:_options.data.request.headers['user-agent'],ip:_options.data.request.headers['x-forwarded-for']||_options.data.request.connection.remoteAddress}});}if('redis'in _options.data)(0,_assign2.default)(session,{redis:_options.data.redis});if('token'in _options.data)dataToken=_options.data.token;}}_context13.next=8;return this.j.sign(key,dataToken);case 8:token=_context13.sent;data=(0,_stringify2.default)(session);_context13.next=12;return this.d.create(key,data,_ttl);case 12:return _context13.abrupt('return',token);case 15:_context13.prev=15;_context13.t0=_context13['catch'](0);throw _chalk2.default.redBright(_context13.t0);case 18:case'end':return _context13.stop();}}},_callee13,this,[[0,15]]);}));function sign(_x15,_x16){return _ref13.apply(this,arguments);}return sign;}()},{key:'verify',value:function(){var _ref14=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function _callee14(token,valueRedis){var decode,key,ttl,id,data;return _regenerator2.default.wrap(function _callee14$(_context14){while(1){switch(_context14.prev=_context14.next){case 0:_context14.prev=0;_context14.next=3;return this.j.verify(token);case 3:decode=_context14.sent;if(decode){_context14.next=6;break;}return _context14.abrupt('return',false);case 6:key=decode.rjwt;_context14.next=9;return this.d.exists(key);case 9:if(_context14.sent){_context14.next=11;break;}return _context14.abrupt('return',false);case 11:_context14.next=13;return this.d.ttl(key);case 13:ttl=_context14.sent;id=decode.rjwt.split(':')[0];(0,_assign2.default)(decode,{id:id,ttl:ttl});if(!valueRedis){_context14.next=24;break;}_context14.t0=JSON;_context14.next=20;return this.d.getValueByKey(key);case 20:_context14.t1=_context14.sent;data=_context14.t0.parse.call(_context14.t0,_context14.t1);delete data._key;if(decode.data){(0,_assign2.default)(decode.data,data);}else if(data.redis||data.request){(0,_assign2.default)(decode,{data:data});}case 24:return _context14.abrupt('return',decode);case 27:_context14.prev=27;_context14.t2=_context14['catch'](0);throw _chalk2.default.redBright(_context14.t2);case 30:case'end':return _context14.stop();}}},_callee14,this,[[0,27]]);}));function verify(_x17,_x18){return _ref14.apply(this,arguments);}return verify;}()},{key:'on',value:function on(event,cb){return this.d.on(event,cb);}},{key:'call',value:function call(){return this.d;}},{key:'exec',value:function exec(){return this.d.exec();}},{key:'makeid',value:function makeid(length){var text="";var possible="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";for(var i=0;i<length;i++){text+=possible.charAt(Math.floor(Math.random()*possible.length));}return text;}}]);return RedisJwt;}();module.exports=RedisJwt; | ||
'use strict';var _stringify=require('babel-runtime/core-js/json/stringify');var _stringify2=_interopRequireDefault(_stringify);var _promise=require('babel-runtime/core-js/promise');var _promise2=_interopRequireDefault(_promise);var _regenerator=require('babel-runtime/regenerator');var _regenerator2=_interopRequireDefault(_regenerator);var _assign=require('babel-runtime/core-js/object/assign');var _assign2=_interopRequireDefault(_assign);var _asyncToGenerator2=require('babel-runtime/helpers/asyncToGenerator');var _asyncToGenerator3=_interopRequireDefault(_asyncToGenerator2);var _classCallCheck2=require('babel-runtime/helpers/classCallCheck');var _classCallCheck3=_interopRequireDefault(_classCallCheck2);var _createClass2=require('babel-runtime/helpers/createClass');var _createClass3=_interopRequireDefault(_createClass2);var _redisFastDriver=require('redis-fast-driver');var _redisFastDriver2=_interopRequireDefault(_redisFastDriver);var _jsonwebtoken=require('jsonwebtoken');var _jsonwebtoken2=_interopRequireDefault(_jsonwebtoken);var _shelljs=require('shelljs');var _shelljs2=_interopRequireDefault(_shelljs);var _chalk=require('chalk');var _chalk2=_interopRequireDefault(_chalk);var _ms=require('ms');var _ms2=_interopRequireDefault(_ms);function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj};}var Jwt=function(){function Jwt(secret){(0,_classCallCheck3.default)(this,Jwt);this.secret=secret;}(0,_createClass3.default)(Jwt,[{key:'sign',value:function(){var _ref=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function _callee(rjwt,data){var value;return _regenerator2.default.wrap(function _callee$(_context){while(1){switch(_context.prev=_context.next){case 0:value={rjwt:rjwt};if(data){(0,_assign2.default)(value,{data:{token:data}});}_context.prev=2;_context.next=5;return _jsonwebtoken2.default.sign(value,this.secret);case 5:return _context.abrupt('return',_context.sent);case 8:_context.prev=8;_context.t0=_context['catch'](2);throw'redis-jwt-> Error creating token...';case 11:case'end':return _context.stop();}}},_callee,this,[[2,8]]);}));function sign(_x,_x2){return _ref.apply(this,arguments);}return sign;}()},{key:'verify',value:function(){var _ref2=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function _callee2(token){return _regenerator2.default.wrap(function _callee2$(_context2){while(1){switch(_context2.prev=_context2.next){case 0:_context2.prev=0;return _context2.abrupt('return',_jsonwebtoken2.default.verify(token,this.secret));case 4:_context2.prev=4;_context2.t0=_context2['catch'](0);throw'redis-jwt-> Error verify token...';case 7:case'end':return _context2.stop();}}},_callee2,this,[[0,4]]);}));function verify(_x3){return _ref2.apply(this,arguments);}return verify;}()}]);return Jwt;}();var Driver=function(){function Driver(config){(0,_classCallCheck3.default)(this,Driver);this.config=config;this.r=new _redisFastDriver2.default(this.config);var self=this;setTimeout(function(){self.ping().then(function(result){if(!result){console.log(_chalk2.default.redBright('redis-jwt-> Could not establish a connection'));throw'Error';}});},2000);}(0,_createClass3.default)(Driver,[{key:'exec',value:function exec(){return this.r;}},{key:'on',value:function on(event,cb){return this.r.on(event,cb);}},{key:'ping',value:function(){var _ref3=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function _callee3(){return _regenerator2.default.wrap(function _callee3$(_context3){while(1){switch(_context3.prev=_context3.next){case 0:_context3.next=2;return this.r.rawCall(['ping']);case 2:return _context3.abrupt('return',_context3.sent);case 3:case'end':return _context3.stop();}}},_callee3,this);}));function ping(){return _ref3.apply(this,arguments);}return ping;}()},{key:'create',value:function(){var _ref4=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function _callee4(key,value,ttl){var id;return _regenerator2.default.wrap(function _callee4$(_context4){while(1){switch(_context4.prev=_context4.next){case 0:_context4.prev=0;id=key.split(':')[0];if(this.config.multiple){_context4.next=5;break;}_context4.next=5;return this.destroyMultiple(id);case 5:if(!ttl){_context4.next=10;break;}_context4.next=8;return this.r.rawCall(['SETEX',key,(0,_ms2.default)(ttl)/1000,value]);case 8:_context4.next=12;break;case 10:_context4.next=12;return this.r.rawCall(['SET',key,value]);case 12:_context4.next=17;break;case 14:_context4.prev=14;_context4.t0=_context4['catch'](0);throw'Error 1 Redis '+_context4.t0;case 17:return _context4.abrupt('return',true);case 18:case'end':return _context4.stop();}}},_callee4,this,[[0,14]]);}));function create(_x4,_x5,_x6){return _ref4.apply(this,arguments);}return create;}()},{key:'exists',value:function(){var _ref5=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function _callee5(key){var _this=this;return _regenerator2.default.wrap(function _callee5$(_context5){while(1){switch(_context5.prev=_context5.next){case 0:return _context5.abrupt('return',new _promise2.default(function(resolve,reject){_this.r.rawCall(['EXISTS',key],function(err,result){if(result)resolve(result);resolve(null);});}));case 1:case'end':return _context5.stop();}}},_callee5,this);}));function exists(_x7){return _ref5.apply(this,arguments);}return exists;}()},{key:'ttl',value:function(){var _ref6=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function _callee6(key){var _this2=this;return _regenerator2.default.wrap(function _callee6$(_context6){while(1){switch(_context6.prev=_context6.next){case 0:return _context6.abrupt('return',new _promise2.default(function(resolve,reject){_this2.r.rawCall(['ttl',key],function(err,result){if(result)resolve(result);resolve(null);});}));case 1:case'end':return _context6.stop();}}},_callee6,this);}));function ttl(_x8){return _ref6.apply(this,arguments);}return ttl;}()},{key:'getValueByKey',value:function(){var _ref7=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function _callee7(key){var _this3=this;return _regenerator2.default.wrap(function _callee7$(_context7){while(1){switch(_context7.prev=_context7.next){case 0:return _context7.abrupt('return',new _promise2.default(function(resolve,reject){_this3.r.rawCall(['get',key],function(err,result){if(result)resolve(result);resolve(null);});}));case 1:case'end':return _context7.stop();}}},_callee7,this);}));function getValueByKey(_x9){return _ref7.apply(this,arguments);}return getValueByKey;}()},{key:'getValuesByPattern',value:function(){var _ref8=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function _callee8(pattern){var _this4=this;return _regenerator2.default.wrap(function _callee8$(_context8){while(1){switch(_context8.prev=_context8.next){case 0:return _context8.abrupt('return',new _promise2.default(function(resolve,reject){_this4.r.rawCall(['keys',pattern+':*'],function(err,keys){var query=['MGET'].concat(keys);_this4.r.rawCall(query,function(err,result){if(result)resolve(result);resolve(null);});});}));case 1:case'end':return _context8.stop();}}},_callee8,this);}));function getValuesByPattern(_x10){return _ref8.apply(this,arguments);}return getValuesByPattern;}()},{key:'getCountByPattern',value:function(){var _ref9=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function _callee9(pattern){var _this5=this;return _regenerator2.default.wrap(function _callee9$(_context9){while(1){switch(_context9.prev=_context9.next){case 0:return _context9.abrupt('return',new _promise2.default(function(resolve,reject){_this5.r.rawCall(['keys',pattern+':*'],function(err,result){if(result)resolve(result.length);resolve(null);});}));case 1:case'end':return _context9.stop();}}},_callee9,this);}));function getCountByPattern(_x11){return _ref9.apply(this,arguments);}return getCountByPattern;}()},{key:'getInfo',value:function(){var _ref10=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function _callee10(section){var _this6=this;return _regenerator2.default.wrap(function _callee10$(_context10){while(1){switch(_context10.prev=_context10.next){case 0:return _context10.abrupt('return',new _promise2.default(function(resolve,reject){var query=section==='DBSIZE'?['DBSIZE']:['INFO',section];_this6.r.rawCall(query,function(err,result){if(result)resolve(result);resolve(null);});}));case 1:case'end':return _context10.stop();}}},_callee10,this);}));function getInfo(_x12){return _ref10.apply(this,arguments);}return getInfo;}()},{key:'destroy',value:function(){var _ref11=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function _callee11(key){var _this7=this;return _regenerator2.default.wrap(function _callee11$(_context11){while(1){switch(_context11.prev=_context11.next){case 0:return _context11.abrupt('return',new _promise2.default(function(resolve,reject){_this7.r.rawCall(['DEL',key],function(err,result){if(result)resolve(result);resolve(null);});}));case 1:case'end':return _context11.stop();}}},_callee11,this);}));function destroy(_x13){return _ref11.apply(this,arguments);}return destroy;}()},{key:'destroyMultiple',value:function(){var _ref12=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function _callee12(key){var _this8=this;return _regenerator2.default.wrap(function _callee12$(_context12){while(1){switch(_context12.prev=_context12.next){case 0:return _context12.abrupt('return',new _promise2.default(function(resolve,reject){_this8.r.rawCall(['KEYS',key+':*'],function(err,keys){keys.join();keys.splice(0,0,"DEL");_this8.r.rawCall(keys,function(err,result){if(result)resolve(result);resolve(null);});});}));case 1:case'end':return _context12.stop();}}},_callee12,this);}));function destroyMultiple(_x14){return _ref12.apply(this,arguments);}return destroyMultiple;}()}]);return Driver;}();var RedisJwt=function(){function RedisJwt(config){(0,_classCallCheck3.default)(this,RedisJwt);if(config.kea){if(_shelljs2.default.exec('redis-cli config set notify-keyspace-events KEA').code!==0){_shelljs2.default.echo('Error: notify-keyspace-events KEA failed');_shelljs2.default.exit(1);}}this.config={};this.config.name='redis-jwt';this.config.host=config.host||'127.0.0.1';this.config.port=config.port||6379;this.config.db=config.db||0;this.config.maxretries=config.maxretries||10;this.config.auth=config.auth||false;this.config.secret=config.secret||'secret_key';this.config.multiple=!config.multiple?false:true;this.d=new Driver(this.config);this.j=new Jwt(this.config.secret);}(0,_createClass3.default)(RedisJwt,[{key:'sign',value:function(){var _ref13=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function _callee13(_id,_options){var key,_ttl,session,dataToken,token,data;return _regenerator2.default.wrap(function _callee13$(_context13){while(1){switch(_context13.prev=_context13.next){case 0:_context13.prev=0;key=_id+':'+this.makeid(11);_ttl=null;session={rjwt:key};dataToken=null;if(_options){if(_options.ttl)_ttl=_options.ttl;if(_options.data){if('request'in _options.data){(0,_assign2.default)(session,{request:{agent:_options.data.request.headers['user-agent'],ip:_options.data.request.headers['x-forwarded-for']||_options.data.request.connection.remoteAddress}});}if('redis'in _options.data)(0,_assign2.default)(session,{redis:_options.data.redis});if('token'in _options.data)dataToken=_options.data.token;}}_context13.next=8;return this.j.sign(key,dataToken);case 8:token=_context13.sent;data=(0,_stringify2.default)(session);_context13.next=12;return this.d.create(key,data,_ttl);case 12:return _context13.abrupt('return',token);case 15:_context13.prev=15;_context13.t0=_context13['catch'](0);throw _chalk2.default.redBright(_context13.t0);case 18:case'end':return _context13.stop();}}},_callee13,this,[[0,15]]);}));function sign(_x15,_x16){return _ref13.apply(this,arguments);}return sign;}()},{key:'verify',value:function(){var _ref14=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function _callee14(token,valueRedis){var decode,key,ttl,id,data;return _regenerator2.default.wrap(function _callee14$(_context14){while(1){switch(_context14.prev=_context14.next){case 0:_context14.prev=0;_context14.next=3;return this.j.verify(token);case 3:decode=_context14.sent;if(decode){_context14.next=6;break;}return _context14.abrupt('return',false);case 6:key=decode.rjwt;_context14.next=9;return this.d.exists(key);case 9:if(_context14.sent){_context14.next=11;break;}return _context14.abrupt('return',false);case 11:_context14.next=13;return this.d.ttl(key);case 13:ttl=_context14.sent;id=decode.rjwt.split(':')[0];(0,_assign2.default)(decode,{id:id,ttl:ttl});if(!valueRedis){_context14.next=24;break;}_context14.t0=JSON;_context14.next=20;return this.d.getValueByKey(key);case 20:_context14.t1=_context14.sent;data=_context14.t0.parse.call(_context14.t0,_context14.t1);delete data.rjwt;if(decode.data){(0,_assign2.default)(decode.data,data);}else if(data.redis||data.request){(0,_assign2.default)(decode,{data:data});}case 24:return _context14.abrupt('return',decode);case 27:_context14.prev=27;_context14.t2=_context14['catch'](0);throw _chalk2.default.redBright(_context14.t2);case 30:case'end':return _context14.stop();}}},_callee14,this,[[0,27]]);}));function verify(_x17,_x18){return _ref14.apply(this,arguments);}return verify;}()},{key:'on',value:function on(event,cb){return this.d.on(event,cb);}},{key:'call',value:function call(){return this.d;}},{key:'exec',value:function exec(){return this.d.exec();}},{key:'makeid',value:function makeid(length){var text="";var possible="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";for(var i=0;i<length;i++){text+=possible.charAt(Math.floor(Math.random()*possible.length));}return text;}}]);return RedisJwt;}();module.exports=RedisJwt; |
{ | ||
"name": "redis-jwt", | ||
"version": "1.2.1", | ||
"version": "1.2.2", | ||
"description": "Management of sessions by Redis and JWT for horizontal scalability, with the possibility of having one session at a time or multiple for the same user", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -250,3 +250,3 @@ import Redis from 'redis-fast-driver'; | ||
let _ttl = null; | ||
let session = { _key: key }; | ||
let session = { rjwt: key }; | ||
let dataToken = null; | ||
@@ -328,3 +328,3 @@ | ||
let data = JSON.parse(await this.d.getValueByKey(key)); | ||
delete data._key; | ||
delete data.rjwt; | ||
@@ -331,0 +331,0 @@ if (decode.data) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package