File size: 48,227 Bytes
eb67da4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 |
/**
* Module contaning functions for basic user operations: login, logout, setup, settings
* @module frontend/express/libs/members
* @example
* var plugins = require('../../plugins/pluginManager.js'); //need for db
* var countlyDb = plugins.dbConnection(countlyConfig); //get db connection
* var membersUtility = require("./libs/members.js");
* membersUtility.db = countlyDB; //setting db before using any function
*
*/
var authorize = require('./../../../api/utils/authorizer.js'); //for token validations
var common = require('./../../../api/utils/common.js');
var plugins = require('./../../../plugins/pluginManager.js');
var { getUserApps } = require('./../../../api/utils/rights.js');
var configs = require('./../config', 'dont-enclose');
var countlyMail = require('./../../../api/parts/mgmt/mail.js');
var countlyStats = require('./../../../api/parts/data/stats.js');
var request = require('request');
var url = require('url');
var crypto = require('crypto');
var argon2 = require('argon2');
var versionInfo = require('./../version.info'),
COUNTLY_TYPE = versionInfo.type;
/** @lends module:frontend/express/libs/members */
var membersUtility = { };
//Helper functions
/**
* @property {object} db - Data base connection. Needs to be set befoe callng any other function.
*/
membersUtility.db = null;
/**
* @property {object} countlyConfig - countly configuration object
*/
membersUtility.countlyConfig = configs;
if (membersUtility.countlyConfig.web && membersUtility.countlyConfig.web.track === "all") {
membersUtility.countlyConfig.web.track = null;
}
/**
* @property {object} emptyPermission - empty crud permission
*/
membersUtility.emptyPermission = {
"permission": {
"c": {},
"r": {},
"u": {},
"d": {},
"_": {
"a": [],
"u": [
[]
]
}
}
};
/** Checks remote configuration and sets variables to configuration object
* @param {object} countlyConfigOrig - configuration settings object. Original(ar read from file)
* @param {object} countlyConfig - contiguration. Changes if are done on this object.
*/
membersUtility.recheckConfigs = function(countlyConfigOrig, countlyConfig) {
var checkUrl = "https://count.ly/configurations/ce/tracking";
if (COUNTLY_TYPE !== "777a2bf527a18e0fffe22fb5b3e322e68d9c07a6") {
checkUrl = "https://count.ly/configurations/ee/tracking";
}
if (!plugins.getConfig("api").offline_mode) {
request(checkUrl, function(error, response, body) {
if (typeof body === "string") {
try {
body = JSON.parse(body);
}
catch (ex) {
body = null;
}
}
if (body) {
if (countlyConfigOrig.web.use_intercom && typeof body.intercom !== "undefined") {
countlyConfig.web.use_intercom = body.intercom;
}
if (typeof countlyConfigOrig.web.track === "undefined" && typeof body.stats !== "undefined") {
if (body.stats) {
countlyConfig.web.track = null;
}
else {
countlyConfig.web.track = "none";
}
}
}
});
}
};
var origConf = JSON.parse(JSON.stringify(membersUtility.countlyConfig));
membersUtility.recheckConfigs(origConf, membersUtility.countlyConfig);
/**
* Is hashed string argon2?
* @param {string} hashedStr | argon2 hashed string
* @returns {boolean} return true if string hashed by argon2
*/
function isArgon2Hash(hashedStr) {
return hashedStr && hashedStr.includes("$argon2");
}
/**
* Verify argon2 hash string
* @param {string} hashedStr - argon2 hashed string
* @param {string} str - string for verify
* @returns {promise} verify promise
**/
function verifyArgon2Hash(hashedStr, str) {
return argon2.verify(hashedStr, str);
}
/**
* Create argon2 hash string
* @param {string} str - string to hash
* @returns {promise} hash promise
**/
function argon2Hash(str) {
return argon2.hash(str);
}
/**
* Update user password to new sha512 hash
* @param {string} id - id of the user document
* @param {string} password - password to hash
* @param {object} countlyDb - data base object
**/
function updateUserPasswordToArgon2(id, password, countlyDb) {
countlyDb.collection('members').update({ _id: id}, { $set: { password: password}});
}
/**
* Create sha1 hash string
* @param {string} str - string to hash
* @param {boolean} addSalt - should salt be added
* @returns {string} hashed string
**/
function sha1Hash(str, addSalt) {
var salt = (addSalt) ? new Date().getTime() : "";
return crypto.createHmac('sha1', salt + "").update(str + "").digest('hex');
}
/**
* Create sha512 hash string
* @param {string} str - string to hash
* @param {boolean} addSalt - should salt be added
* @returns {string} hashed string
**/
function sha512Hash(str, addSalt) {
var salt = (addSalt) ? new Date().getTime() : "";
return crypto.createHmac('sha512', salt + "").update(str + "").digest('hex');
}
/**
* Verify member for Argon2 Hash
* @param {string} username | User name
* @param {password} password | Password string
* @param {object} countlyDb - data base object
* @param {Function} callback | Callback function
*/
function verifyMemberArgon2Hash(username, password, countlyDb, callback) {
countlyDb.collection('members').findOne({$and: [{ $or: [ {"username": username}, {"email": username}]}]}, (err, member) => {
if (member) {
if (isArgon2Hash(member.password)) {
verifyArgon2Hash(member.password, password).then(match => {
if (match) {
callback(undefined, member);
}
else {
callback("Password is wrong!");
}
}).catch(function() {
callback("Password is wrong!");
});
}
else {
var password_SHA1 = sha1Hash(password);
var password_SHA5 = sha512Hash(password);
if (member.password === password_SHA1 || member.password === password_SHA5) {
argon2Hash(password).then(password_ARGON2 => {
updateUserPasswordToArgon2(member._id, password_ARGON2, countlyDb);
callback(undefined, member);
}).catch(function() {
callback("Password is wrong!");
});
}
else {
callback("Password is wrong!");
}
}
}
else {
callback("Username is wrong!");
}
});
}
/**
* Validate password based on configured settings
* @param {string} password - password to validatePassword
* @returns {vary} returns string if there is error, or false if everything is ok
**/
function validatePassword(password) {
if (password.length < plugins.getConfig("security").password_min) {
return "management-users.password.length";
}
if (plugins.getConfig("security").password_char && !/[A-Z]/.test(password)) {
return "management-users.password.has-char";
}
if (plugins.getConfig("security").password_number && !/\d/.test(password)) {
return "management-users.password.has-number";
}
if (plugins.getConfig("security").password_symbol && !/[^A-Za-z\d]/.test(password)) {
return "management-users.password.has-special";
}
return false;
}
/**
* Function gets session timeout in ms.
* @param {object} req - requets object
* @returns {integer} Session timeout in ms.
*/
var getSessionTimeoutInMs = function(req) {
var myTimeoutValue = parseInt(plugins.getConfig("frontend", req.session && req.session.settings).session_timeout, 10) * 1000 * 60;
//max value used by set timeout function
if (myTimeoutValue > 2147483647) {
myTimeoutValue = 1800000;
}//30 minutes
return myTimeoutValue;
};
/**
* Sets variables for logged in session
* @param {object} req - request object
* @param {object} member - member object
* @param {object} countlyDb -data base reference
* @param {function} callback - callback function, called after token and variables are set. Returns nothing.
**/
function setLoggedInVariables(req, member, countlyDb, callback) {
req.session.uid = member._id;
req.session.gadm = (member.global_admin === true);
req.session.email = member.email;
req.session.settings = member.settings;
var reuse = true;
if (req.session.temporary_token) {
reuse = false;
}
authorize.save({
db: countlyDb,
multi: true,
owner: req.session.uid,
tryReuse: reuse,
ttl: getSessionTimeoutInMs(req) / 1000,
purpose: "LoggedInAuth",
callback: function(err2, token) {
if (err2) {
console.log(err2);
}
if (token) {
req.session.auth_token = token;
}
callback();
}
});
}
/** Clears all inforamtion about user from session parameters. Used when logging ut user.
* @param {object} req - request object
* @param {object} res - response object
*/
membersUtility.clearReqAndRes = function(req, res) {
if (req.session) {
req.session.uid = null;
req.session.gadm = null;
req.session.email = null;
req.session.settings = null;
res.clearCookie('uid');
res.clearCookie('gadm');
req.session.destroy(function() {});
}
};
/**
* Verifies a user's credentials without logging in.
*
* @param {string} username - username or the email address of the user
* @param {string} password - password
* @param {function} callback - callback function. First parameter in callback function is member object if logging in is successful.
* @example
* membersUtility.verifyCredentials(username, password, function(member) {
if (member) {
// logged in
}
else {
// failed
}
});
**/
membersUtility.verifyCredentials = function(username, password, callback) {
if (username && password) {
username = (username + "").trim();
var secret = membersUtility.countlyConfig.passwordSecret || "";
password = password + secret;
verifyMemberArgon2Hash(username, password, membersUtility.db, (err, member) => {
if (member) {
callback(member);
}
else {
callback(undefined);
}
});
}
else {
callback(undefined);
}
};
/**
* Update Stats for member.
*
* @param {object} member - member properties
* @example
* membersUtility.updateStats(member );
**/
membersUtility.updateStats = function(member) {
var countlyConfig = membersUtility.countlyConfig;
if ((!countlyConfig.web.track || countlyConfig.web.track === "GA" && member.global_admin || countlyConfig.web.track === "noneGA" && !member.global_admin) && !plugins.getConfig("api").offline_mode) {
countlyStats.getUser(membersUtility.db, member, function(statsObj) {
const userApps = getUserApps(member);
var custom = {
apps: (userApps) ? userApps.length : 0,
platforms: {"$addToSet": statsObj["total-platforms"]},
events: statsObj["total-events"],
pushes: statsObj["total-msg-sent"],
crashes: statsObj["total-crash-groups"],
users: statsObj["total-users"]
};
var date = new Date();
request({
uri: "https://stats.count.ly/i",
method: "GET",
timeout: 4E3,
qs: {
device_id: member.email,
app_key: "386012020c7bf7fcb2f1edf215f1801d6146913f",
timestamp: Math.round(date.getTime() / 1000),
hour: date.getHours(),
dow: date.getDay(),
user_details: JSON.stringify(
{
custom: custom
}
)
}
}, function() {});
});
}
};
/**
* Tries to log in user based passed userame and password. Calls "plugins"
* methods to notify successful and unsucessful logging in attempts. If
* successful, sets all session variables and auth token. Passes the member
* object to the callback if retrieved succesfully, but not necessarily logged
* in succesfully i.e. a member object will still be returned even if the member
* was locked. Also passes a boolean parameter to the callback indicating if the
* login was succesful.
*
* @param {object} req - request object
* @param {string} req.body.username - username
* @param {string} req.body.password - password
* @param {object} res - response object
* @param {function} callback - callback function. First parameter in callback
* function is member object, if it could be retrieved succesfully. Second
* parameter is a boolean that is true when logged in succesfully.
* @example
* membersUtility.login(req, res, function(member) {
if(member) {
// logged in
}
else {
// failed
}
});
**/
membersUtility.login = function(req, res, callback) {
membersUtility.verifyCredentials(req.body.username, req.body.password, (member) => {
if (member === undefined || member.locked) {
plugins.callMethod("loginFailed", {req: req, data: req.body});
callback(member, false);
}
else {
plugins.callMethod("loginSuccessful", {req: req, data: member});
// update stats
membersUtility.updateStats(member);
req.session.regenerate(function() {
// will have a new session here
var update = {last_login: Math.round(new Date().getTime() / 1000)};
if (typeof member.password_changed === "undefined") {
update.password_changed = Math.round(new Date().getTime() / 1000);
}
if (req.body.lang && req.body.lang !== member.lang) {
update.lang = req.body.lang;
}
membersUtility.db.collection('members').update({_id: member._id}, {$set: update}, function() {});
if (parseInt(plugins.getConfig("frontend", member.settings).session_timeout, 10)) {
req.session.expires = Date.now() + parseInt(plugins.getConfig("frontend", member.settings).session_timeout, 10) * 1000 * 60;
}
if (member.upgrade) {
res.set({
'Cache-Control': 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0',
'Expires': '0',
'Pragma': 'no-cache'
});
}
setLoggedInVariables(req, member, membersUtility.db, function() {
callback(member, true);
});
});
}
});
};
/**
* Tries to log in user without verification for external authentication.
* Similar behavior as the membersUtility.login just bypass the verification
* as the user is already authenticated by external authentication mechanism
* such as Active Directory, Azure AD or Ldap
*
* @param {object} req - request object
* @param {string} req.body.username - username
* @param {object} res - response object
* @param {function} callback - callback function. First parameter in callback
* function is member object, if it could be retrieved succesfully. Second
* parameter is a boolean that is true when logged in succesfully.
* @example
* membersUtility.loginWithExternalAuthentication(req, res, function(member) {
if(member) {
// logged in
}
else {
// failed
}
});
**/
membersUtility.loginWithExternalAuthentication = function(req, res, callback) {
if (!req.body || !req.body.username) {
callback(undefined);
}
var username = (req.body.username + "").trim();
membersUtility.db.collection('members').findOne({username}, (err, member) => {
if (member === undefined || member.locked) {
plugins.callMethod("loginFailed", {req: req, data: req.body});
callback(member, false);
}
else {
plugins.callMethod("loginSuccessful", {req: req, data: member});
// update stats
membersUtility.updateStats(member);
req.session.regenerate(function() {
// will have a new session here
var update = {last_login: Math.round(new Date().getTime() / 1000)};
if (req.body.lang && req.body.lang !== member.lang) {
update.lang = req.body.lang;
}
membersUtility.db.collection('members').update({_id: member._id}, {$set: update}, function() {});
if (parseInt(plugins.getConfig("frontend", member.settings).session_timeout, 10)) {
req.session.expires = Date.now() + parseInt(plugins.getConfig("frontend", member.settings).session_timeout, 10) * 1000 * 60;
}
if (member.upgrade) {
res.set({
'Cache-Control': 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0',
'Expires': '0',
'Pragma': 'no-cache'
});
}
setLoggedInVariables(req, member, membersUtility.db, function() {
req.session.settings = member.settings;
callback(member, true);
});
});
}
});
};
/**
* Removes all other active sessions for user
* @param {string} userId - id of the user for which to remove sessions
* @param {string} my_token - current auth token
* @param {string} my_session - current session id
* @param {object} countlyDb -data base reference
**/
function killOtherSessionsForUser(userId, my_token, my_session, countlyDb) {
countlyDb.collection('sessions_').find({"session": { $regex: userId }}).toArray(function(err, sessions) {
var delete_us = [];
if (sessions) {
for (var i = 0; i < sessions.length; i++) {
var parsed_data = "";
try {
parsed_data = JSON.parse(sessions[i].session);
}
catch (error) {
console.log(error);
}
if ((!my_session || sessions[i]._id !== my_session) && parsed_data && parsed_data.uid === userId) {
delete_us.push(sessions[i]._id);
}
}
if (delete_us.length > 0) {
countlyDb.collection('sessions_').remove({'_id': {$in: delete_us}});
}
}
});
//delete other auth tokens with purpose:"LoggedInAuth"
if (my_token) {
countlyDb.collection('auth_tokens').remove({'owner': countlyDb.ObjectID(userId), 'purpose': "LoggedInAuth", '_id': {$ne: my_token}});
}
else {
countlyDb.collection('auth_tokens').remove({'owner': countlyDb.ObjectID(userId), 'purpose': "LoggedInAuth"});
}
}
/**
* Logins user with token
* @param {object} req - request object
* @param {function} callback - callback function
**/
membersUtility.loginWithToken = function(req, callback) {
var token = req.params.token;
var pathUrl = req.url.replace(membersUtility.countlyConfig.path, "");
var urlParts = url.parse(pathUrl, true);
var fullPath = urlParts.pathname;
authorize.verify_return({
db: membersUtility.db,
token: token,
req_path: fullPath,
return_data: true,
callback: function(valid) {
if (!valid) {
plugins.callMethod("tokenLoginFailed", {req: req, data: {token: token}});
return callback(undefined);
}
membersUtility.db.collection('members').findOne({"_id": membersUtility.db.ObjectID(valid.owner)}, function(err, member) {
if (err || !member) {
plugins.callMethod("tokenLoginFailed", {req: req, data: {token: token, token_owner: valid.owner}});
callback(undefined);
}
else {
plugins.callMethod("tokenLoginSuccessful", {req: req, data: {username: member.username}});
if (valid.temporary) {
req.session.temporary_token = true;
}
setLoggedInVariables(req, member, membersUtility.db, function() {
req.session.settings = member.settings;
callback(member);
});
}
});
}
});
};
/**
* Logs out user - clears session info for request and response object
* @param {object} req - request object
* @param {object} res - response object
**/
membersUtility.logout = function(req, res) {
if (req.session) {
if (req.session.uid && req.session.email) {
plugins.callMethod("userLogout", {req: req, data: {uid: req.session.uid, email: req.session.email, query: req.query}});
}
if (req.session.auth_token) {
membersUtility.db.collection("auth_tokens").remove({_id: req.session.auth_token});
//louout also other users logged in with same credentials
if (!req.session.temporary_token) {
killOtherSessionsForUser(req.session.uid, null, null, membersUtility.db);
}
req.session.auth_token = null;
}
membersUtility.clearReqAndRes(req, res);
}
};
/**
* Function to extend user session. Changes time when session expires and also extends token(if passed).
* @param {object} req - request object
* @param {string} req.session.auth_token - auth token
*/
membersUtility.extendSession = function(req) {
req.session.expires = Date.now() + getSessionTimeoutInMs(req);
if (req.session.auth_token) {
var ChangeTime = getSessionTimeoutInMs(req);
if (ChangeTime > 0) {
authorize.extend_token({token: req.session.auth_token, db: membersUtility.db, extendTill: Date.now() + ChangeTime}, function(err) {
if (err) {
console.log(err);
}
});
}
else { //changed to not expire
authorize.extend_token({token: req.session.auth_token, db: membersUtility.db, extendBy: 0}, function(err) {
if (err) {
console.log(err);
}
});
}
}
};
/**
* Sets up first user in Countly(if there is none). Req object is used to get mandatory variables from req.body and also there are variables set to have logged in session for new user.
* @param {object} req - request object
* @param {string} req.body.full_name - Full name. Mandatory.
* @param {string} req.body.username - Username. Mandatory.
* @param {string} req.body.password - Password. Mandatory.
* @param {string} req.body.email - E-mail. Mandatory.
* @param {function} callback - Function with one return value - error (if there is one)
* @example
* membersUtility.setup(req, res, countlyConfig, function(error) {
* if(error) {
* //there is error while setting up user
* // error === "Wrong request parameters" - not all mandatory parameters passed or there was error during creating user
* // error === "User exists" - There is already at least one user.
* // error === "....." - mongo error while getting user count.
* }
* else {
* //Success
* }
* });
**/
membersUtility.setup = function(req, callback) {
membersUtility.db.collection('members').count(function(err, memberCount) {
if (!err && memberCount === 0) {
//check password
const argProps = {
'full_name': {
'required': true,
'type': 'String'
},
'username': {
'required': true,
'type': 'String'
},
'password': {
'required': true,
'type': 'String',
'min-length': plugins.getConfig("security").password_min,
'has-number': plugins.getConfig("security").password_number,
'has-upchar': plugins.getConfig("security").password_char,
'has-special': plugins.getConfig("security").password_symbol
},
'email': {
'required': true,
'type': 'String'
},
};
var memberCreateValidation = common.validateArgs(req.body, argProps, true);
if (!(req.body = memberCreateValidation.obj)) {
callback({
message: memberCreateValidation.errors,
passMinLen: plugins.getConfig("security").password_min,
});
return;
}
var secret = membersUtility.countlyConfig.passwordSecret || "";
argon2Hash(req.body.password + secret).then(password => {
req.body.email = (req.body.email + "").trim();
req.body.username = (req.body.username + "").trim();
var doc = {
"full_name": req.body.full_name,
"username": req.body.username,
"password": password,
"email": req.body.email,
"global_admin": true,
created_at: Math.floor(((new Date()).getTime()) / 1000),
password_changed: Math.floor(((new Date()).getTime()) / 1000),
permission: membersUtility.emptyPermission
};
if (req.body.lang) {
doc.lang = req.body.lang;
}
crypto.randomBytes(48, function(errorBuff, buffer) {
doc.api_key = common.md5Hash(buffer.toString('hex') + Math.random());
membersUtility.db.collection('members').insert(doc, {safe: true}, function(err2, member) {
member = member.ops;
setLoggedInVariables(req, member[0], membersUtility.db, function() {
req.session.install = true;
callback();
});
});
});
}).catch(function() {
callback("Wrong request parameters");
});
}
else if (err) {
callback(err);
}
else {
callback("User exists");
}
});
};
/**
* Function validates if email is not used by any other member.
* @param {string} email - mandatory. E-mail to check
* @param {function} callback - function with one return value. Returns true if email is not used, false if taken.
* @example
* membersUtility.checkEmail(email, function(isFree) {
* if(isFree ===true) {
* //email is not taken
* }
* });
*
*
*/
membersUtility.checkEmail = function(email, callback) {
email = (email + "").trim();
membersUtility.db.collection('members').findOne({email: email}, function(err, member) {
if (member || err) {
callback(false);
}
else {
callback(true);
}
});
};
/**
* Function validates if username is not used by any other member.
* @param {string} username - mandatory. Username to check.
* @param {function} callback - function with one return value. Returns true if username is free, false if taken.
* @example
* membersUtility.checkUsername(username, function(isFree) {
* if(isFree ===true) {
* //username is not taken
* }
* });
*
*
*/
membersUtility.checkUsername = function(username, callback) {
username = (username + "").trim();
membersUtility.db.collection('members').findOne({username: username}, function(err, member) {
if (member || err) {
callback(false);
}
else {
callback(true);
}
});
};
/**
* Sends user password reseting information to given e-mail.
* @param {object} req - request object.
* @param {string} req.body.email - mandatory. User email.
* @param {string} req.body.lang - optional. Language.(default "en" - english)
* @param {function} callback - function with one return value. Returns member object if successful.
* @example
* membersUtility.forgot(req, function(member) {
* if(member) {
* //member found
* }
* else {
* //e-mail not passed or user with this e-mail not found
* }
* });
*/
membersUtility.forgot = function(req, callback) {
if (!req || !req.body || !req.body.email) {
callback(undefined); //to be sure email is passed
}
else {
var email = (req.body.email + "").trim();
membersUtility.db.collection('members').findOne({"email": email}, function(err, member) {
if (member) {
var timestamp = Math.round(new Date().getTime() / 1000),
// BUG: CWE-640 Weak Password Recovery Mechanism for Forgotten Password
// prid = sha512Hash(member.username + member.full_name, timestamp);
// FIXED:
prid = crypto.randomBytes(32).toString('hex');
member.lang = member.lang || req.body.lang || "en";
membersUtility.db.collection('password_reset').insert({"prid": prid, "user_id": member._id, "timestamp": timestamp}, {safe: true}, function() {
countlyMail.sendPasswordResetInfo(member, prid);
plugins.callMethod("passwordRequest", {req: req, data: req.body}); //used in systemlogs
callback(member);
});
}
else {
callback(undefined);
}
});
}
};
/**
* Resets user password
* @param {object} req - request object
* @param {string} req.body.password - mandatory. new password.
* @param {string} req.body.again - mandatory.
* @param {string} req.body.prid - mandatory. Password reset id.
* @param {function} callback - function with one two return values. First one is password validation error(false if no error) and second one is member object if reset is sucessful.
*/
membersUtility.reset = function(req, callback) {
var result = validatePassword(req.body.password);
if (result === false) {
if (req.body.password && req.body.again && req.body.prid) {
req.body.prid += "";
var secret = membersUtility.countlyConfig.passwordSecret || "";
argon2Hash(req.body.password + secret).then(password => {
membersUtility.db.collection('password_reset').findOne({ prid: req.body.prid }, function(err, passwordReset) {
membersUtility.db.collection('members').findAndModify({ _id: passwordReset.user_id }, {}, { '$set': { "password": password } }, function(err2, member) {
member = member && member.ok ? member.value : null;
killOtherSessionsForUser(passwordReset.user_id + "", null, null, membersUtility.db);
plugins.callMethod("passwordReset", { req: req, data: member }); //only req, used for systemolgs
callback(false, member);
});
membersUtility.db.collection('password_reset').remove({ prid: req.body.prid }, function() { });
});
}).catch(function() {
callback(false, undefined);
});
}
else {
callback(false, undefined);
}
}
else {
callback(result, undefined);
}
};
/**
* Saves changed user settings
* @param {object} req - request object
* @param {string} req.body.username - mandatory - username (current or new one to chacge to)
* @param {string} req.body.api_key - mandatory. User API KEY (current or the one to change to)
* @param {string} req.body.old_pwd - Old password. Optional. Passed if changing password.
* @param {string} req.body.new_pwd - New password. Optional. Passed if changing password.
* @param {function} callback - function with two return values. First one is true - if successful (false if not sucessful) and the second one - error message(in some cases).
*/
membersUtility.settings = function(req, callback) {
var updatedUser = {};
if (req.body.username && req.body.api_key) {
if (req.body.api_key.length !== 32) {
callback(false, "user-settings.api-key-length");
return;
}
if (!req.body.api_key.match(/^[0-9a-zA-Z]+([0-9]+)([a-z]+)[0-9a-zA-Z]+$/)) {
callback(false, "user-settings.api-key-restrict");
return;
}
req.body.username = (req.body.username + "").trim();
if (req.body.member_image && req.body.member_image !== "delete") {
updatedUser.member_image = req.body.member_image;
}
if (req.body.member_image === "delete") {
updatedUser.member_image = "";
}
updatedUser.username = req.body.username;
updatedUser.api_key = req.body.api_key;
if (req.body.lang) {
updatedUser.lang = req.body.lang;
}
var change = JSON.parse(JSON.stringify(updatedUser));
membersUtility.db.collection('members').findOne({"_id": membersUtility.db.ObjectID(req.session.uid + "")}, function(err, member) {
if (err || !member) {
callback(false);
return;
}
membersUtility.db.collection('members').findOne({username: req.body.username}, async function(err2, user) {
if (err) {
callback(false);
return;
}
member.change = change;
if ((user && user._id + "" !== req.session.uid + "") || err2) {
callback(false, "username-exists");
}
else {
var secret = membersUtility.countlyConfig.passwordSecret || "";
req.body.new_pwd = req.body.new_pwd + secret;
if (req.body.old_pwd && req.body.old_pwd.length) {
if (isArgon2Hash(member.password)) {
var match;
try {
match = await verifyArgon2Hash(member.password, req.body.old_pwd);
}
catch (ex) {
match = null;
}
if (!match) {
return callback(false, "user-settings.old-password-not-match");
}
}
else {
var password_SHA1 = sha1Hash(req.body.old_pwd);
var password_SHA5 = sha512Hash(req.body.old_pwd);
if (member.password === password_SHA1 || member.password === password_SHA5) {
argon2Hash(req.body.old_pwd).then(password_ARGON2 => {
updateUserPasswordToArgon2(member._id, password_ARGON2, membersUtility.db);
}).catch(function() {
console.log("Problem updating password");
});
}
else {
return callback(false, "user-settings.old-password-not-match");
}
}
// eslint-disable-next-line require-atomic-updates
member.change.password = true;
try {
var newPassword_SHA5 = sha512Hash(req.body.new_pwd),
newPassword_ARGON2 = await argon2Hash(req.body.new_pwd);
}
catch (ex) {
callback(false);
return;
}
let isUsedBefore = false;
if (plugins.getConfig('security').password_rotation > 0) {
// Check if used before
const promises = [];
const passwordHistory = member.password_history || [];
for (let i = 0; i < passwordHistory.length; i++) {
const oldPassword = passwordHistory[i];
if (isArgon2Hash(oldPassword)) {
promises.push(verifyArgon2Hash(oldPassword, req.body.new_pwd));
}
else if (oldPassword === newPassword_SHA5) {
isUsedBefore = true;
break;
}
}
if (!isUsedBefore && promises.length > 0) {
try {
const promiseResults = await Promise.all(promises);
isUsedBefore = promiseResults.some(x => x === true);
}
catch (ex) {
callback(false);
return;
}
}
}
if (req.body.new_pwd !== req.body.old_pwd && !isUsedBefore) {
var passRes = validatePassword(req.body.new_pwd);
if (passRes === false) {
updatedUser.password = newPassword_ARGON2;
updatedUser.password_changed = Math.round(new Date().getTime() / 1000);
membersUtility.db.collection('members').update({"_id": membersUtility.db.ObjectID(req.session.uid + "")}, {'$set': updatedUser, $push: {password_history: {$each: [newPassword_ARGON2], $slice: -parseInt(plugins.getConfig('security').password_rotation)}}}, {safe: true}, function(err3, result) {
if (result && result.result && result.result.ok && result.result.nModified > 0 && !err3) {
killOtherSessionsForUser(req.session.uid, req.session.auth_token, req.sessionID, membersUtility.db);
plugins.callMethod("userSettings", {req: req, data: member});
callback(true, updatedUser.password_changed + "");
}
else {
callback(false, "user-settings.old-password-not-match");
return;
}
});
}
else {
callback(false, passRes);
}
}
else {
callback(false, "user-settings.password-not-old");
}
}
else {
membersUtility.db.collection('members').update({"_id": membersUtility.db.ObjectID(req.session.uid + "")}, {'$set': updatedUser}, {safe: true}, function(err3, result) {
if (result && !err3) {
plugins.callMethod("userSettings", {req: req, data: member});
callback(true);
}
else {
callback(false);
}
});
}
}
});
});
}
else {
callback(false);
return;
}
};
/**
* Searches for a user with the given username/email. Useful for the input from the login prompt.
* @param {string} input - username or the email address of the user we are looking for
* @param {function} callback - function with one parameter, the member object if a user is found, undefined otherwise
*/
membersUtility.findByUsernameOrEmail = function(input, callback) {
input = (input + "").trim();
membersUtility.db.collection('members').findOne({$or: [{username: input}, {email: input}]}, function(err, member) {
if (err) {
console.log(`Database error searching for user: ${err}`);
}
callback(member);
});
};
/**
* Find Members
* @param {Object} query query
* @returns {Object[]} list of members
*/
membersUtility.findMembers = async function(query = {}) {
return new Promise((resolve, reject) => {
this.db.collection('members').find(query).toArray((err, members) => {
if (err) {
reject(err);
}
else {
resolve(members);
}
});
});
};
/**
* Update Member
* @param {Object} query query
* @param {Object} data data to update
* @param {boolean} upsert upsert
* @returns {Object} list of members
*/
membersUtility.updateMember = async function(query = {}, data = {}, upsert = true) {
return new Promise((resolve, reject) => {
this.db.collection('members').update(query, { $set: data }, { upsert }, (err) => {
if (err) {
reject(err);
}
else {
resolve(data);
}
});
});
};
/**
* Remove Members
* @param {Object} query query
* @returns {Object[]} list of members
*/
membersUtility.removeMembers = async function(query = {}) {
return new Promise((resolve, reject) => {
if (!Object.keys(query).length) {
return reject('Invalid query to remove members');
}
this.db.collection('members').remove(query, (err, response) => {
if (err) {
reject(err);
}
else {
resolve(response);
}
});
});
};
/**
* Create User for external authentication provider
* @method createMember
* @param {Object} data user data
* @param {string} provider auth provider
* @param {boolean} deleteDuplicate delete duplicate
* @returns {Promise<any>} created or updated user data
*/
membersUtility.createMember = async function(data, provider = '', deleteDuplicate = false) {
let user = {};
if (!data || !Object.keys(data).length) {
throw new Error('Invalid user data provided');
}
user._id = data._id || data.id || data.sub || data.username;
user.email = data.email || '';
user.username = data.username || data.email;
user.full_name = data.full_name || data.name || `${data.firstName} ${data.lastName}` || "";
user.global_admin = data.global_admin || false;
user.locked = (typeof data.locked !== "undefined") ? data.locked : false;
user.provider = provider;
// can be deprecated after all data migrated to provider property
user.isAD = (provider === 'ad' || provider === 'azure');
user.isCognito = provider === 'cognito';
user.created_at = data.created_at || Math.floor(((new Date()).getTime()) / 1000);
user.admin_of = data.admin_of || [];
user.user_of = data.user_of || [];
user.restrict = data.restrict || [];
user.app_restrict = data.app_restrict || {};
user.permission = data.permission || membersUtility.emptyPermission;
if (data.admin_of && data.admin_of.length) {
user.user_of = [...new Set([...data.admin_of, ...data.user_of])];
}
if (data.group_id && data.group_id.length) {
user.group_id = data.group_id;
}
// legacy rbac
if (data.marketing_of && data.marketing_of.length) {
user.admin_of.push(...data.marketing_of);
}
const buffer = crypto.randomBytes(48);
user.api_key = data.api_key || common.md5Hash(buffer.toString('hex') + Math.random());
user.password = data.password || common.md5Hash(data.api_key);
// push approver permission
user.approver = !!data.approver;
user.approver_bypass = !!data.approver_bypass;
const query = user.email
? {
$or: [
{ _id: user._id },
{ email: user.email }
]
}
: { _id: user._id };
try {
const existingMembers = await membersUtility.findMembers(query);
if (deleteDuplicate && (existingMembers.length >= 2 || (existingMembers.length === 1 && existingMembers[0]._id !== user._id))) {
await membersUtility.removeMembers(query);
}
const memberData = await membersUtility.updateMember(query, user, true);
return memberData;
}
catch (error) {
console.error(`create member error ${provider} ${error.message}`, error);
throw new Error(error);
}
};
/**
* Merge permission objects
* @method mergePermissions
* @param {object} current - current permission object
* @param {object} addition - permission object to merge
*/
membersUtility.mergePermissions = (current, addition) => {
var crud = {"c": true, "r": true, "u": true, "d": true};
for (var p in addition) {
//permission does not exist yet, just copy it
if (!current[p]) {
current[p] = addition[p];
}
else if (crud[p]) {
//iterating through the apps
if (addition[p]) {
if (!current[p]) {
current[p] = {};
}
for (var app in addition[p]) {
if (addition[p][app]) {
if (!current[p][app]) {
current[p][app] = {};
}
if (addition[p][app].all) {
current[p][app].all = true;
}
if (addition[p][app].allowed) {
current[p][app].allowed = current[p][app].allowed || {};
for (var feat in addition[p][app].allowed) {
if (addition[p][app].allowed[feat]) {
current[p][app].allowed[feat] = true;
}
else if (typeof current[p][app].allowed[feat] === "undefined") {
current[p][app].allowed[feat] = addition[p][app].allowed[feat];
}
}
}
}
}
}
}
else if (p === "_") {
//merge admin by merging arrays
if (addition[p].a) {
if (!current[p].a) {
current[p].a = [];
}
current[p].a = [...(new Set([...current[p].a, ...addition[p].a]))];
}
if (addition[p].u) {
if (!current[p].u) {
current[p].u = [];
}
current[p].u = [...(new Set([...current[p].u, ...addition[p].u]))];
}
}
}
};
module.exports = membersUtility; |