Comparing version 1.2.2 to 1.3.0
@@ -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={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; | ||
'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,dataToken){var value;return _regenerator2.default.wrap(function _callee$(_context){while(1){switch(_context.prev=_context.next){case 0:value={rjwt:rjwt};if(dataToken){(0,_assign2.default)(value,{dataToken:dataToken});}_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.dataSession)(0,_assign2.default)(session,{dataSession:_options.dataSession});if(_options.dataToken)dataToken=_options.dataToken;}_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,valueSession){var decode,key,ttl,id,value;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(!valueSession){_context14.next=23;break;}_context14.t0=JSON;_context14.next=20;return this.d.getValueByKey(key);case 20:_context14.t1=_context14.sent;value=_context14.t0.parse.call(_context14.t0,_context14.t1);if(value.dataSession)(0,_assign2.default)(decode,{dataSession:value.dataSession});case 23:return _context14.abrupt('return',decode);case 26:_context14.prev=26;_context14.t2=_context14['catch'](0);throw _chalk2.default.redBright(_context14.t2);case 29:case'end':return _context14.stop();}}},_callee14,this,[[0,26]]);}));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; |
41
index.js
@@ -1,4 +0,5 @@ | ||
var RedisJWT = require('./dist/index'); | ||
import RedisJwt from './dist/index'; | ||
import express from 'express'; | ||
var r = new RedisJWT({ | ||
const r = new RedisJwt({ | ||
//host: '/tmp/redis.sock', //unix domain | ||
@@ -15,2 +16,3 @@ host: '127.0.0.1', //can be IP or hostname | ||
const app = express(); | ||
@@ -35,23 +37,20 @@ // Events | ||
// Router | ||
var express = require('express'); | ||
app.get('/', (req, res) => { | ||
var app = express(); | ||
app.get('/', function (req, res) { | ||
// Create | ||
// sign | ||
r.sign('507f191e810c19729de860ea', { | ||
ttl: '1 minutes', | ||
data: { | ||
redis: { hello: 'world' }, | ||
token: { world: 'hello' }, | ||
request: req | ||
}, | ||
ttl: '15 minutes', | ||
dataToken: { hello: 'world' }, | ||
dataSession: { | ||
hello: 'world', | ||
headers: req.headers | ||
} | ||
}).then(sign => { | ||
// Verify | ||
// verify | ||
r.verify(sign, true).then(decode => { | ||
// Exec | ||
// exec | ||
var rexec = r.exec(); | ||
@@ -61,3 +60,3 @@ | ||
// Call | ||
// call | ||
var rcall = r.call(); | ||
@@ -73,5 +72,3 @@ | ||
}).catch(err => { | ||
console.log('error verify-> ', err); | ||
}) | ||
}).catch(err => console.log('error verify-> ', err)); | ||
}); | ||
@@ -81,4 +78,2 @@ | ||
app.listen(3000, function () { | ||
console.log('Server listening on port 3000!'); | ||
}); | ||
app.listen(3000, () => console.log('Server listening on port 3000!')); |
{ | ||
"name": "redis-jwt", | ||
"version": "1.2.2", | ||
"version": "1.3.0", | ||
"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", | ||
@@ -11,5 +11,4 @@ "main": "dist/index.js", | ||
"scripts": { | ||
"start": "nodemon src/index.js --exec babel-node", | ||
"build": "babel src -d dist", | ||
"serve": "npm run build && node index.js", | ||
"start": "npm run compile & nodemon index.js --exec babel-node", | ||
"compile": "babel --watch src -d dist", | ||
"test": "mocha --compilers js:babel-register" | ||
@@ -44,3 +43,4 @@ }, | ||
"express": "^4.15.4", | ||
"mocha": "^3.5.0" | ||
"mocha": "^3.5.1", | ||
"nodemon": "^1.12.0" | ||
}, | ||
@@ -47,0 +47,0 @@ "dependencies": { |
286
README.md
@@ -25,2 +25,3 @@ # redis-jwt | ||
Yarn | ||
```bash | ||
@@ -34,25 +35,26 @@ yarn add redis-jwt | ||
import RedisJWT from 'redis-jwt'; | ||
import RedisJwt from 'redis-jwt'; | ||
const r = new RedisJWT({ | ||
//host: '/tmp/redis.sock', //unix domain | ||
host: '127.0.0.1', //can be IP or hostname | ||
port: 6379, // port | ||
maxretries: 10, //reconnect retries, default 10 | ||
//auth: '123', //optional password, if needed | ||
db: 0, //optional db selection | ||
secret: 'secret_key', // secret key for Tokens! | ||
multiple: false, // single or multiple sessions by user | ||
kea: true // Enable notify-keyspace-events KEA | ||
const r = new RedisJwt({ | ||
//host: '/tmp/redis.sock', //unix domain | ||
host: '127.0.0.1', //can be IP or hostname | ||
port: 6379, // port | ||
maxretries: 10, //reconnect retries, default 10 | ||
//auth: '123', //optional password, if needed | ||
db: 0, //optional db selection | ||
secret: 'secret_key', // secret key for Tokens! | ||
multiple: false, // single or multiple sessions by user | ||
kea: false // Enable notify-keyspace-events KEA | ||
}); | ||
r.sign('507f191e810c19729de860ea').then(token => { | ||
r.verify(token).then(result => { | ||
// object redis-jwt | ||
}).catch(err => { | ||
// Wrong token | ||
}); | ||
r.verify(token).then(decode => { | ||
// [Object] | ||
}).catch(err => { | ||
// Wrong token | ||
}); | ||
}); | ||
``` | ||
### Example Redis-jwt with Express | ||
@@ -62,5 +64,5 @@ | ||
import RedisJWT from 'redis-jwt'; | ||
import RedisJwt from 'redis-jwt'; | ||
import express from 'express'; | ||
const r = new RedisJWT(); | ||
const r = new RedisJwt(); | ||
const app = express(); | ||
@@ -70,12 +72,15 @@ | ||
app.get('/login', (req, res) => { | ||
r.sign('507f191e810c19729de860ea', { | ||
ttl: '15 minutes', | ||
data: { | ||
redis: {hello: 'world'}, | ||
token: {world: 'hello'}, | ||
request: req | ||
}, | ||
r.sign('507f191e810c19729de860ea', { | ||
ttl: '15 minutes', | ||
dataToken: { // Public | ||
hello: 'world' | ||
}, | ||
dataSession: { // Private | ||
hello: 'world', | ||
headers : req.headers | ||
} | ||
} | ||
}).then(token => { | ||
res.json({token}); | ||
}); | ||
res.json({token}); | ||
}); | ||
}); | ||
@@ -91,10 +96,10 @@ | ||
return (req, res, next) => { | ||
const token = req.headers['authorization']; | ||
r.verify(token).then(rjwt => | ||
// here you can get user from DB by id (rjwt.id) | ||
req.user = rjwt; | ||
next(); | ||
}).catch(err => { | ||
const token = req.headers['authorization']; | ||
r.verify(token).then(decode => | ||
// here you can get user from DB by id (decode.id) | ||
req.user = decode; | ||
next(); | ||
}).catch(err => { | ||
res.status(401).json({err}) | ||
}) | ||
}) | ||
} | ||
@@ -113,41 +118,27 @@ } | ||
// Basic | ||
r.sign('507f191e810c19729de860ea').then.. | ||
// Basic | ||
r.sign('507f191e810c19729de860ea').then.. | ||
// TTL : 50 seconds, 15 minutes, 5 hours, 3 days, 1 year ... | ||
r.sign('507f191e810c19729de860ea', { | ||
ttl: '15 minutes' | ||
}).then... | ||
// TTL : 50 seconds, 10 minutes, 5 hours, 3 days, 1 year ... | ||
r.sign('507f191e810c19729de860ea', { | ||
ttl: '15 minutes' | ||
}).then... | ||
// Save data in redis : Object are saved in redis-jwt | ||
r.sign('507f191e810c19729de860ea', { | ||
data: { | ||
redis: {hello: 'world'} | ||
} | ||
}).then... | ||
// Save data in token : Object are saved in token | ||
r.sign('507f191e810c19729de860ea', { | ||
dataToken: {world: 'hello'} | ||
}).then... | ||
// Save data in token : Object are saved in token | ||
r.sign('507f191e810c19729de860ea', { | ||
data: { | ||
token: {world: 'hello'} | ||
} | ||
}).then... | ||
// Save data in redis : Object are saved in redis-jwt | ||
r.sign('507f191e810c19729de860ea', { | ||
dataSession: {hello: 'world'} | ||
}).then... | ||
// Request : request are saved in redis-jwt (ip,agent) | ||
r.sign('507f191e810c19729de860ea', { | ||
data: { | ||
request: req | ||
} | ||
}).then... | ||
// Example TTL + dataToken + dataSession | ||
r.sign('507f191e810c19729de860ea', { | ||
ttl: '15 minutes', | ||
dataToken: {world: 'hello'}, | ||
dataSession: {hello: 'world'} | ||
}).then... | ||
// Example TTL + Data + Request | ||
r.sign('507f191e810c19729de860ea', { | ||
ttl: '15 minutes', | ||
data: { | ||
redis: {hello: 'world'}, | ||
token: {world: 'hello'}, | ||
request: req | ||
} | ||
}).then... | ||
``` | ||
@@ -159,31 +150,32 @@ | ||
// Basic | ||
r.verify(token).then(decode => { | ||
/* | ||
{ | ||
"rjwt": "507f191e810c19729de860ea:ZYYlwOGqTmx", | ||
"data": { token: [Object] } | ||
"iat": 1504334208, | ||
"id": "507f191e810c19729de860ea", | ||
"ttl": 60 | ||
} | ||
*/ | ||
}).catch(err => { | ||
// Wrong token | ||
}) | ||
// Basic | ||
r.verify(token).then(decode => { | ||
/* | ||
{ | ||
"rjwt": "507f191e810c19729de860ea:ZYYlwOGqTmx", | ||
"dataToken": [Object] | ||
"iat": 1504334208, | ||
"id": "507f191e810c19729de860ea", | ||
"ttl": 60 | ||
} | ||
*/ | ||
}).catch(err => { | ||
// Wrong token | ||
}) | ||
// Get data from redis | ||
r.verify(token, true).then(decode => { | ||
/* | ||
{ | ||
"rjwt": "507f191e810c19729de860ea:ZYYlwOGqTmx", | ||
"data": { token: [Object], redis: [Object], request: [Object] } | ||
"iat": 1504334208, | ||
"id": "507f191e810c19729de860ea", | ||
"ttl": 60 | ||
} | ||
*/ | ||
}).catch(err => { | ||
// Wrong token | ||
}) | ||
// Get data from redis | ||
r.verify(token, true).then(decode => { | ||
/* | ||
{ | ||
"rjwt": "507f191e810c19729de860ea:ZYYlwOGqTmx", | ||
"dataToken": [Object] | ||
"dataSession": [Object] ----> get data session | ||
"iat": 1504334208, | ||
"id": "507f191e810c19729de860ea", | ||
"ttl": 60 | ||
} | ||
*/ | ||
}).catch(err => { | ||
// Wrong token | ||
}) | ||
@@ -196,14 +188,14 @@ ``` | ||
// Execute Redis comands | ||
const exec = r.exec(); | ||
exec.rawCall(['keys', `507f191e810c19729de860ea:*`], (err, result) => { | ||
/* | ||
[ | ||
"507f191e810c19729de860ea:ZYYlwOGqTmx", | ||
"507f191e810c19729de860ea:d39K8J249Hd", | ||
] | ||
*/ | ||
}); | ||
// Execute Redis comands | ||
const exec = r.exec(); | ||
exec.rawCall(['keys', `507f191e810c19729de860ea:*`], (err, result) => { | ||
/* | ||
[ | ||
"507f191e810c19729de860ea:ZYYlwOGqTmx", | ||
"507f191e810c19729de860ea:d39K8J249Hd", | ||
] | ||
*/ | ||
}); | ||
``` | ||
@@ -215,36 +207,36 @@ | ||
// Method's redis-jwt | ||
const call = r.call(); | ||
// Method's redis-jwt | ||
const call = r.call(); | ||
// Test Ping | ||
call.ping().then.. | ||
// Create | ||
call.create(key, value, ttl).then.. | ||
// exits by key | ||
call.exists(key).then.. | ||
// Get ttl by Key | ||
call.ttl(key).then.. | ||
// Get values by key | ||
call.getValueByKey(key).then.. | ||
// Get values by Pattern | ||
call.getValuesByPattern(pattern).then.. | ||
// Get count by Pattern | ||
call.getCountByPattern(pattern).then.. | ||
// Get info | ||
call.getInfo(section).then.. | ||
// Destroy by key | ||
call.destroy(key).then.. | ||
// Destroy multiple by key | ||
call.destroyMultiple(key).then.. | ||
// Test Ping | ||
call.ping().then.. | ||
// Create | ||
call.create(key, value, ttl).then.. | ||
// exits by key | ||
call.exists(key).then.. | ||
// Get ttl by Key | ||
call.ttl(key).then.. | ||
// Get values by key | ||
call.getValueByKey(key).then.. | ||
// Get values by Pattern | ||
call.getValuesByPattern(pattern).then.. | ||
// Get count by Pattern | ||
call.getCountByPattern(pattern).then.. | ||
// Get info | ||
call.getInfo(section).then.. | ||
// Destroy by key | ||
call.destroy(key).then.. | ||
// Destroy multiple by key | ||
call.destroyMultiple(key).then.. | ||
``` | ||
@@ -258,3 +250,3 @@ | ||
r.on('ready', () => { | ||
console.log('redis-jwt-> ready!'); | ||
console.log('redis-jwt-> ready!'); | ||
}); | ||
@@ -264,3 +256,3 @@ | ||
r.on('connected', () => { | ||
console.log('redis-jwt-> connected!'); | ||
console.log('redis-jwt-> connected!'); | ||
}); | ||
@@ -270,3 +262,3 @@ | ||
r.on('disconnected', () => { | ||
console.log('redis-jwt-> disconnected!'); | ||
console.log('redis-jwt-> disconnected!'); | ||
}); | ||
@@ -276,3 +268,3 @@ | ||
r.on('error', (err) => { | ||
console.log('redis-jwt-> error!', err); | ||
console.log('redis-jwt-> error!', err); | ||
}); | ||
@@ -288,10 +280,6 @@ | ||
### Serve | ||
### Compile | ||
`npm run serve` | ||
`npm run compile` | ||
### Build | ||
`npm run build` | ||
### Test | ||
@@ -298,0 +286,0 @@ |
@@ -17,8 +17,8 @@ import Redis from 'redis-fast-driver'; | ||
// sign token | ||
async sign(rjwt, data) { | ||
async sign(rjwt, dataToken) { | ||
let value = { rjwt }; | ||
if (data) { | ||
Object.assign(value, { data: { token: data } }); | ||
if (dataToken) { | ||
Object.assign(value, { dataToken }); | ||
} | ||
@@ -250,3 +250,3 @@ | ||
let key = `${_id}:${this.makeid(11)}`; | ||
let _ttl = null; | ||
let ttl = null; | ||
let session = { rjwt: key }; | ||
@@ -256,27 +256,13 @@ let dataToken = null; | ||
if (_options) { | ||
// If have TTL | ||
if (_options.ttl) | ||
_ttl = _options.ttl; | ||
ttl = _options.ttl; | ||
if (_options.data) { | ||
// If have request save agent and ip | ||
if ('request' in _options.data) { | ||
Object.assign(session, { | ||
request: { | ||
agent: _options.data.request.headers['user-agent'], | ||
ip: _options.data.request.headers['x-forwarded-for'] || _options.data.request.connection.remoteAddress | ||
} | ||
}); | ||
} | ||
// If have data save in redis | ||
if (_options.dataSession) | ||
Object.assign(session, { dataSession: _options.dataSession }); | ||
// If have data save in redis | ||
if ('redis' in _options.data) | ||
Object.assign(session, { redis: _options.data.redis }); | ||
// If have data save in token | ||
if ('token' in _options.data) | ||
dataToken = _options.data.token; | ||
} | ||
// If have data save in token | ||
if (_options.dataToken) | ||
dataToken = _options.dataToken; | ||
} | ||
@@ -291,3 +277,3 @@ | ||
// Set in Redis | ||
await this.d.create(key, data, _ttl); | ||
await this.d.create(key, data, ttl); | ||
@@ -304,3 +290,3 @@ return token; | ||
async verify(token, valueRedis) { | ||
async verify(token, valueSession) { | ||
@@ -330,11 +316,6 @@ try { | ||
// if full get value from redis | ||
if (valueRedis) { | ||
let data = JSON.parse(await this.d.getValueByKey(key)); | ||
delete data.rjwt; | ||
if (decode.data) { | ||
Object.assign(decode.data, data); | ||
} else if(data.redis || data.request) { | ||
Object.assign(decode, { data }); | ||
} | ||
if (valueSession) { | ||
let value = JSON.parse(await this.d.getValueByKey(key)); | ||
if (value.dataSession) | ||
Object.assign(decode, { dataSession: value.dataSession }); | ||
} | ||
@@ -341,0 +322,0 @@ |
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
33780
10
405
277