Branch data Line data Source code
1 [ + ]: 1 : /** 2 : 1 : * @module redis-users 3 : 1 : * @desc The leaderboard-api Redis users module. 4 : 1 : * @version 1.0.0 5 : 1 : * @author Essam A. El-Sherif 6 : 1 : */ 7 : 1 : 8 : 1 : /** 9 : 1 : * @class 10 : 1 : * @static 11 : 1 : * @desc Class to manage users of the leaderboard system using Redis. 12 : 1 : */ 13 : 1 : export class Users{ 14 : 1 : 15 : 1 : /** 16 : 1 : * @method 17 : 1 : * @instance 18 : 1 : * @memberof module:redis-users.Users 19 : 1 : * @param {object} client - Redis client object. 20 : 1 : * @param {string} keyU - Namespace for Redis users keys. 21 : 1 : * @desc Constructs and returns a Users object. 22 : 1 : */ 23 [ + ]: 1 : constructor(client, keyU){ 24 : 1 : this.client = client; 25 : 1 : this.keyU = keyU; 26 : 1 : } 27 : 1 : 28 : 1 : /** 29 : 1 : * @method 30 : 1 : * @instance 31 : 1 : * @memberof module:redis-users.Users 32 : 1 : * @return {Promise} A Promise object that will fulfill with an array of username's. 33 : 1 : * @desc Returns a Promise object that will fulfill with an array of username's. 34 : 1 : */ 35 [ + ]: 1 : getUsers(){ 36 : 1 : return this.client.sendCommand(['KEYS', `${this.keyU}*`]) 37 [ + ]: 1 : .then((res) => Promise.all( 38 [ + ]: 1 : res.map(key => ({ 39 : 1 : username: key.replace(`${this.keyU}`, ''), 40 : 1 : password: '********' 41 : 1 : })))); 42 : 1 : } 43 : 1 : 44 : 1 : /** 45 : 1 : * @method 46 : 1 : * @instance 47 : 1 : * @memberof module:redis-users.Users 48 : 1 : * @param {string} username - User name in lowercase. 49 : 1 : * @param {string} pwd - MD5 checksum of the password. 50 : 1 : * @return {Promise} A Promise object that will fulfill with user's object. 51 : 1 : * @desc Creates a user or throws on failure. 52 : 1 : * @throws {Error} Throws an error if the username already exists. 53 : 1 : */ 54 [ + ]: 1 : createUser(username, pwd){ 55 : 3 : return this.client.sendCommand(['EXISTS', `${this.keyU}${username}`]) 56 [ + ]: 3 : .then((res) => { 57 [ + ]: 3 : if(res){ 58 : 1 : throw new Error('username already exists'); 59 [ + ]: 1 : } 60 : 2 : return this.client.sendCommand(['HSET', `${this.keyU}${username}`, 'password', pwd]); 61 : 3 : }) 62 [ + ]: 3 : .then(() => ({username, password: '********'})); 63 : 3 : } 64 : 1 : 65 : 1 : /** 66 : 1 : * @method 67 : 1 : * @instance 68 : 1 : * @memberof module:redis-users.Users 69 : 1 : * @param {string} username - User name in lowercase. 70 : 1 : * @param {string} pwd - MD5 checksum of the password. 71 : 1 : * @param {string} newpwd - (OPTIONAL) MD5 checksum of the new password. 72 : 1 : * @return {Promise} A Promise object that will fulfill with user's object. 73 : 1 : * @desc Updates a user or throws on failure. 74 : 1 : * @throws {Error} Throws an error if the username does not exist or incorrect password was given. 75 : 1 : */ 76 [ + ]: 1 : updateUser(username, pwd, newpwd){ 77 : 4 : return this.client.sendCommand(['EXISTS', `${this.keyU}${username}`]) 78 [ + ]: 4 : .then((res) => { 79 [ + ]: 4 : if(res){ 80 : 3 : return this.client.sendCommand(['HGET', `${this.keyU}${username}`, 'password']); 81 [ + ]: 3 : } 82 : 1 : else{ 83 : 1 : throw new Error('username does not exist'); 84 : 1 : } 85 : 4 : }) 86 [ + ]: 4 : .then((res) => { 87 [ + ]: 3 : if(res !== pwd){ 88 : 1 : throw new Error('invalid password'); 89 [ + ]: 1 : } 90 : 2 : else 91 [ + ]: 2 : if(newpwd){ 92 : 1 : return this.client.sendCommand(['HSET', `${this.keyU}${username}`, 'password', newpwd]); 93 : 1 : } 94 : 4 : }) 95 [ + ]: 4 : .then(() => ({username, password: '********'})); 96 : 4 : } 97 : 1 : 98 : 1 : /** 99 : 1 : * @method 100 : 1 : * @instance 101 : 1 : * @memberof module:redis-users.Users 102 : 1 : * @param {string} username - User name in lowercase. 103 : 1 : * @param {string} pwd - MD5 checksum of the password. 104 : 1 : * @return {Promise} A Promise object that will fulfill with user's object. 105 : 1 : * @desc Deletes a user or throws on failure. 106 : 1 : * @throws {Error} Throws an error if the username does not exist or incorrect password was given. 107 : 1 : */ 108 [ + ]: 1 : deleteUser(username, pwd){ 109 : 3 : return this.client.sendCommand(['EXISTS', `${this.keyU}${username}`]) 110 [ + ]: 3 : .then((res) => { 111 [ + ]: 3 : if(res){ 112 : 2 : return this.client.sendCommand(['HGET', `${this.keyU}${username}`, 'password']); 113 [ + ]: 2 : } 114 : 1 : else{ 115 : 1 : throw new Error('username does not exist'); 116 : 1 : } 117 : 3 : }) 118 [ + ]: 3 : .then((res) => { 119 [ + ]: 2 : if(res === pwd){ 120 : 1 : return this.client.sendCommand(['DEL', `${this.keyU}${username}`]); 121 : 1 : } 122 : 1 : else{ 123 : 1 : throw new Error('invalid password'); 124 : 1 : } 125 : 3 : }) 126 [ + ]: 3 : .then(() => ({username, password: '********'})); 127 : 3 : } 128 : 1 : }