123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.default = exports.AccountLockout = void 0;
- var _node = _interopRequireDefault(require("parse/node"));
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
- class AccountLockout {
- constructor(user, config) {
- this._user = user;
- this._config = config;
- }
-
- _setFailedLoginCount(value) {
- const query = {
- username: this._user.username
- };
- const updateFields = {
- _failed_login_count: value
- };
- return this._config.database.update('_User', query, updateFields);
- }
-
- _isFailedLoginCountSet() {
- const query = {
- username: this._user.username,
- _failed_login_count: {
- $exists: true
- }
- };
- return this._config.database.find('_User', query).then(users => {
- if (Array.isArray(users) && users.length > 0) {
- return true;
- } else {
- return false;
- }
- });
- }
-
- _initFailedLoginCount() {
- return this._isFailedLoginCountSet().then(failedLoginCountIsSet => {
- if (!failedLoginCountIsSet) {
- return this._setFailedLoginCount(0);
- }
- });
- }
-
- _incrementFailedLoginCount() {
- const query = {
- username: this._user.username
- };
- const updateFields = {
- _failed_login_count: {
- __op: 'Increment',
- amount: 1
- }
- };
- return this._config.database.update('_User', query, updateFields);
- }
-
- _setLockoutExpiration() {
- const query = {
- username: this._user.username,
- _failed_login_count: {
- $gte: this._config.accountLockout.threshold
- }
- };
- const now = new Date();
- const updateFields = {
- _account_lockout_expires_at: _node.default._encode(new Date(now.getTime() + this._config.accountLockout.duration * 60 * 1000))
- };
- return this._config.database.update('_User', query, updateFields).catch(err => {
- if (err && err.code && err.message && err.code === _node.default.Error.OBJECT_NOT_FOUND && err.message === 'Object not found.') {
- return;
- } else {
- throw err;
- }
- });
- }
-
- _notLocked() {
- const query = {
- username: this._user.username,
- _account_lockout_expires_at: {
- $gt: _node.default._encode(new Date())
- },
- _failed_login_count: {
- $gte: this._config.accountLockout.threshold
- }
- };
- return this._config.database.find('_User', query).then(users => {
- if (Array.isArray(users) && users.length > 0) {
- throw new _node.default.Error(_node.default.Error.OBJECT_NOT_FOUND, 'Your account is locked due to multiple failed login attempts. Please try again after ' + this._config.accountLockout.duration + ' minute(s)');
- }
- });
- }
-
- _handleFailedLoginAttempt() {
- return this._initFailedLoginCount().then(() => {
- return this._incrementFailedLoginCount();
- }).then(() => {
- return this._setLockoutExpiration();
- });
- }
-
- handleLoginAttempt(loginSuccessful) {
- if (!this._config.accountLockout) {
- return Promise.resolve();
- }
- return this._notLocked().then(() => {
- if (loginSuccessful) {
- return this._setFailedLoginCount(0);
- } else {
- return this._handleFailedLoginAttempt();
- }
- });
- }
-
- unlockAccount() {
- if (!this._config.accountLockout || !this._config.accountLockout.unlockOnPasswordReset) {
- return Promise.resolve();
- }
- return this._config.database.update('_User', {
- username: this._user.username
- }, {
- _failed_login_count: {
- __op: 'Delete'
- },
- _account_lockout_expires_at: {
- __op: 'Delete'
- }
- });
- }
- }
- exports.AccountLockout = AccountLockout;
- var _default = exports.default = AccountLockout;
|