knownUser.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /* eslint-disable no-underscore-dangle */
  2. import { Meteor } from 'meteor/meteor';
  3. import { Accounts } from 'meteor/accounts-base';
  4. class KnownUser {
  5. constructor(settings) {
  6. this.unchangedSettings = settings;
  7. this.settings = settings;
  8. }
  9. startup() {
  10. if (!(this.unchangedSettings instanceof Function)) {
  11. this.updateSettings();
  12. }
  13. this.scheduleUnlocksForLockedAccounts();
  14. KnownUser.unlockAccountsIfLockoutAlreadyExpired();
  15. this.hookIntoAccounts();
  16. }
  17. updateSettings() {
  18. const settings = KnownUser.knownUsers();
  19. if (settings) {
  20. settings.forEach(function updateSetting({ key, value }) {
  21. this.settings[key] = value;
  22. });
  23. }
  24. this.validateSettings();
  25. }
  26. validateSettings() {
  27. if (
  28. !this.settings.failuresBeforeLockout ||
  29. this.settings.failuresBeforeLockout < 0
  30. ) {
  31. throw new Error('"failuresBeforeLockout" is not positive integer');
  32. }
  33. if (
  34. !this.settings.lockoutPeriod ||
  35. this.settings.lockoutPeriod < 0
  36. ) {
  37. throw new Error('"lockoutPeriod" is not positive integer');
  38. }
  39. if (
  40. !this.settings.failureWindow ||
  41. this.settings.failureWindow < 0
  42. ) {
  43. throw new Error('"failureWindow" is not positive integer');
  44. }
  45. }
  46. scheduleUnlocksForLockedAccounts() {
  47. const lockedAccountsCursor = Meteor.users.find(
  48. {
  49. 'services.accounts-lockout.unlockTime': {
  50. $gt: Number(new Date()),
  51. },
  52. },
  53. {
  54. fields: {
  55. 'services.accounts-lockout.unlockTime': 1,
  56. },
  57. },
  58. );
  59. const currentTime = Number(new Date());
  60. lockedAccountsCursor.forEach((user) => {
  61. let lockDuration = KnownUser.unlockTime(user) - currentTime;
  62. if (lockDuration >= this.settings.lockoutPeriod) {
  63. lockDuration = this.settings.lockoutPeriod * 1000;
  64. }
  65. if (lockDuration <= 1) {
  66. lockDuration = 1;
  67. }
  68. Meteor.setTimeout(
  69. KnownUser.unlockAccount.bind(null, user._id),
  70. lockDuration,
  71. );
  72. });
  73. }
  74. static unlockAccountsIfLockoutAlreadyExpired() {
  75. const currentTime = Number(new Date());
  76. const query = {
  77. 'services.accounts-lockout.unlockTime': {
  78. $lt: currentTime,
  79. },
  80. };
  81. const data = {
  82. $unset: {
  83. 'services.accounts-lockout.unlockTime': 0,
  84. 'services.accounts-lockout.failedAttempts': 0,
  85. },
  86. };
  87. Meteor.users.update(query, data);
  88. }
  89. hookIntoAccounts() {
  90. Accounts.validateLoginAttempt(this.validateLoginAttempt.bind(this));
  91. Accounts.onLogin(KnownUser.onLogin);
  92. }
  93. validateLoginAttempt(loginInfo) {
  94. if (
  95. // don't interrupt non-password logins
  96. loginInfo.type !== 'password' ||
  97. loginInfo.user === undefined ||
  98. // Don't handle errors unless they are due to incorrect password
  99. (loginInfo.error !== undefined && loginInfo.error.reason !== 'Incorrect password')
  100. ) {
  101. return loginInfo.allowed;
  102. }
  103. // If there was no login error and the account is NOT locked, don't interrupt
  104. const unlockTime = KnownUser.unlockTime(loginInfo.user);
  105. if (loginInfo.error === undefined && unlockTime === 0) {
  106. return loginInfo.allowed;
  107. }
  108. if (this.unchangedSettings instanceof Function) {
  109. this.settings = this.unchangedSettings(loginInfo.user);
  110. this.validateSettings();
  111. }
  112. const userId = loginInfo.user._id;
  113. let failedAttempts = 1 + KnownUser.failedAttempts(loginInfo.user);
  114. const firstFailedAttempt = KnownUser.firstFailedAttempt(loginInfo.user);
  115. const currentTime = Number(new Date());
  116. const canReset = (currentTime - firstFailedAttempt) > (1000 * this.settings.failureWindow);
  117. if (canReset) {
  118. failedAttempts = 1;
  119. KnownUser.resetAttempts(failedAttempts, userId);
  120. }
  121. const canIncrement = failedAttempts < this.settings.failuresBeforeLockout;
  122. if (canIncrement) {
  123. KnownUser.incrementAttempts(failedAttempts, userId);
  124. }
  125. const maxAttemptsAllowed = this.settings.failuresBeforeLockout;
  126. const attemptsRemaining = maxAttemptsAllowed - failedAttempts;
  127. if (unlockTime > currentTime) {
  128. let duration = unlockTime - currentTime;
  129. duration = Math.ceil(duration / 1000);
  130. duration = duration > 1 ? duration : 1;
  131. KnownUser.tooManyAttempts(duration);
  132. }
  133. if (failedAttempts === maxAttemptsAllowed) {
  134. this.setNewUnlockTime(failedAttempts, userId);
  135. let duration = this.settings.lockoutPeriod;
  136. duration = Math.ceil(duration);
  137. duration = duration > 1 ? duration : 1;
  138. return KnownUser.tooManyAttempts(duration);
  139. }
  140. return KnownUser.incorrectPassword(
  141. failedAttempts,
  142. maxAttemptsAllowed,
  143. attemptsRemaining,
  144. );
  145. }
  146. static resetAttempts(
  147. failedAttempts,
  148. userId,
  149. ) {
  150. const currentTime = Number(new Date());
  151. const query = { _id: userId };
  152. const data = {
  153. $set: {
  154. 'services.accounts-lockout.failedAttempts': failedAttempts,
  155. 'services.accounts-lockout.lastFailedAttempt': currentTime,
  156. 'services.accounts-lockout.firstFailedAttempt': currentTime,
  157. },
  158. };
  159. Meteor.users.update(query, data);
  160. }
  161. static incrementAttempts(
  162. failedAttempts,
  163. userId,
  164. ) {
  165. const currentTime = Number(new Date());
  166. const query = { _id: userId };
  167. const data = {
  168. $set: {
  169. 'services.accounts-lockout.failedAttempts': failedAttempts,
  170. 'services.accounts-lockout.lastFailedAttempt': currentTime,
  171. },
  172. };
  173. Meteor.users.update(query, data);
  174. }
  175. setNewUnlockTime(
  176. failedAttempts,
  177. userId,
  178. ) {
  179. const currentTime = Number(new Date());
  180. const newUnlockTime = (1000 * this.settings.lockoutPeriod) + currentTime;
  181. const query = { _id: userId };
  182. const data = {
  183. $set: {
  184. 'services.accounts-lockout.failedAttempts': failedAttempts,
  185. 'services.accounts-lockout.lastFailedAttempt': currentTime,
  186. 'services.accounts-lockout.unlockTime': newUnlockTime,
  187. },
  188. };
  189. Meteor.users.update(query, data);
  190. Meteor.setTimeout(
  191. KnownUser.unlockAccount.bind(null, userId),
  192. this.settings.lockoutPeriod * 1000,
  193. );
  194. }
  195. static onLogin(loginInfo) {
  196. //get the data from oidc login and remove again?
  197. if(loginInfo.type ==='oidc'){
  198. Meteor.call('groupRoutineOnLogin', loginInfo.user.services.oidc, loginInfo.user._id);
  199. return;
  200. }
  201. if (loginInfo.type !== 'password') {
  202. return;
  203. }
  204. const userId = loginInfo.user._id;
  205. const query = { _id: userId };
  206. const data = {
  207. $unset: {
  208. 'services.accounts-lockout.unlockTime': 0,
  209. 'services.accounts-lockout.failedAttempts': 0,
  210. },
  211. };
  212. Meteor.users.update(query, data);
  213. }
  214. static incorrectPassword(
  215. failedAttempts,
  216. maxAttemptsAllowed,
  217. attemptsRemaining,
  218. ) {
  219. throw new Meteor.Error(
  220. 403,
  221. 'Incorrect password',
  222. JSON.stringify({
  223. message: 'Incorrect password',
  224. failedAttempts,
  225. maxAttemptsAllowed,
  226. attemptsRemaining,
  227. }),
  228. );
  229. }
  230. static tooManyAttempts(duration) {
  231. throw new Meteor.Error(
  232. 403,
  233. 'Too many attempts',
  234. JSON.stringify({
  235. message: 'Wrong passwords were submitted too many times. Account is locked for a while.',
  236. duration,
  237. }),
  238. );
  239. }
  240. static knownUsers() {
  241. let knownUsers;
  242. try {
  243. knownUsers = Meteor.settings['accounts-lockout'].knownUsers;
  244. } catch (e) {
  245. knownUsers = false;
  246. }
  247. return knownUsers || false;
  248. }
  249. static unlockTime(user) {
  250. let unlockTime;
  251. try {
  252. unlockTime = user.services['accounts-lockout'].unlockTime;
  253. } catch (e) {
  254. unlockTime = 0;
  255. }
  256. return unlockTime || 0;
  257. }
  258. static failedAttempts(user) {
  259. let failedAttempts;
  260. try {
  261. failedAttempts = user.services['accounts-lockout'].failedAttempts;
  262. } catch (e) {
  263. failedAttempts = 0;
  264. }
  265. return failedAttempts || 0;
  266. }
  267. static lastFailedAttempt(user) {
  268. let lastFailedAttempt;
  269. try {
  270. lastFailedAttempt = user.services['accounts-lockout'].lastFailedAttempt;
  271. } catch (e) {
  272. lastFailedAttempt = 0;
  273. }
  274. return lastFailedAttempt || 0;
  275. }
  276. static firstFailedAttempt(user) {
  277. let firstFailedAttempt;
  278. try {
  279. firstFailedAttempt = user.services['accounts-lockout'].firstFailedAttempt;
  280. } catch (e) {
  281. firstFailedAttempt = 0;
  282. }
  283. return firstFailedAttempt || 0;
  284. }
  285. static unlockAccount(userId) {
  286. const query = { _id: userId };
  287. const data = {
  288. $unset: {
  289. 'services.accounts-lockout.unlockTime': 0,
  290. 'services.accounts-lockout.failedAttempts': 0,
  291. },
  292. };
  293. Meteor.users.update(query, data);
  294. }
  295. }
  296. export default KnownUser;