1
+ 'use strict' ;
2
+
3
+ Object . defineProperty ( exports , "__esModule" , {
4
+ value : true
5
+ } ) ;
6
+ exports . AccountLockout = undefined ;
7
+
8
+ var _createClass = function ( ) { function defineProperties ( target , props ) { for ( var i = 0 ; i < props . length ; i ++ ) { var descriptor = props [ i ] ; descriptor . enumerable = descriptor . enumerable || false ; descriptor . configurable = true ; if ( "value" in descriptor ) descriptor . writable = true ; Object . defineProperty ( target , descriptor . key , descriptor ) ; } } return function ( Constructor , protoProps , staticProps ) { if ( protoProps ) defineProperties ( Constructor . prototype , protoProps ) ; if ( staticProps ) defineProperties ( Constructor , staticProps ) ; return Constructor ; } ; } ( ) ; // This class handles the Account Lockout Policy settings.
9
+
10
+
11
+ var _node = require ( 'parse/node' ) ;
12
+
13
+ var _node2 = _interopRequireDefault ( _node ) ;
14
+
15
+ function _interopRequireDefault ( obj ) { return obj && obj . __esModule ? obj : { default : obj } ; }
16
+
17
+ function _classCallCheck ( instance , Constructor ) { if ( ! ( instance instanceof Constructor ) ) { throw new TypeError ( "Cannot call a class as a function" ) ; } }
18
+
19
+ var AccountLockout = exports . AccountLockout = function ( ) {
20
+ function AccountLockout ( user , config ) {
21
+ _classCallCheck ( this , AccountLockout ) ;
22
+
23
+ this . _user = user ;
24
+ this . _config = config ;
25
+ }
26
+
27
+ /**
28
+ * set _failed_login_count to value
29
+ */
30
+
31
+
32
+ _createClass ( AccountLockout , [ {
33
+ key : '_setFailedLoginCount' ,
34
+ value : function _setFailedLoginCount ( value ) {
35
+ var query = {
36
+ username : this . _user . username
37
+ } ;
38
+
39
+ var updateFields = {
40
+ _failed_login_count : value
41
+ } ;
42
+
43
+ return this . _config . database . update ( '_User' , query , updateFields ) ;
44
+ }
45
+
46
+ /**
47
+ * check if the _failed_login_count field has been set
48
+ */
49
+
50
+ } , {
51
+ key : '_isFailedLoginCountSet' ,
52
+ value : function _isFailedLoginCountSet ( ) {
53
+ var query = {
54
+ username : this . _user . username ,
55
+ _failed_login_count : { $exists : true }
56
+ } ;
57
+
58
+ return this . _config . database . find ( '_User' , query ) . then ( function ( users ) {
59
+ if ( Array . isArray ( users ) && users . length > 0 ) {
60
+ return true ;
61
+ } else {
62
+ return false ;
63
+ }
64
+ } ) ;
65
+ }
66
+
67
+ /**
68
+ * if _failed_login_count is NOT set then set it to 0
69
+ * else do nothing
70
+ */
71
+
72
+ } , {
73
+ key : '_initFailedLoginCount' ,
74
+ value : function _initFailedLoginCount ( ) {
75
+ var _this = this ;
76
+
77
+ return this . _isFailedLoginCountSet ( ) . then ( function ( failedLoginCountIsSet ) {
78
+ if ( ! failedLoginCountIsSet ) {
79
+ return _this . _setFailedLoginCount ( 0 ) ;
80
+ }
81
+ } ) ;
82
+ }
83
+
84
+ /**
85
+ * increment _failed_login_count by 1
86
+ */
87
+
88
+ } , {
89
+ key : '_incrementFailedLoginCount' ,
90
+ value : function _incrementFailedLoginCount ( ) {
91
+ var query = {
92
+ username : this . _user . username
93
+ } ;
94
+
95
+ var updateFields = { _failed_login_count : { __op : 'Increment' , amount : 1 } } ;
96
+
97
+ return this . _config . database . update ( '_User' , query , updateFields ) ;
98
+ }
99
+
100
+ /**
101
+ * if the failed login count is greater than the threshold
102
+ * then sets lockout expiration to 'currenttime + accountPolicy.duration', i.e., account is locked out for the next 'accountPolicy.duration' minutes
103
+ * else do nothing
104
+ */
105
+
106
+ } , {
107
+ key : '_setLockoutExpiration' ,
108
+ value : function _setLockoutExpiration ( ) {
109
+ var query = {
110
+ username : this . _user . username ,
111
+ _failed_login_count : { $gte : this . _config . accountLockout . threshold }
112
+ } ;
113
+
114
+ var now = new Date ( ) ;
115
+
116
+ var updateFields = {
117
+ _account_lockout_expires_at : _node2 . default . _encode ( new Date ( now . getTime ( ) + this . _config . accountLockout . duration * 60 * 1000 ) )
118
+ } ;
119
+
120
+ return this . _config . database . update ( '_User' , query , updateFields ) . catch ( function ( err ) {
121
+ if ( err && err . code && err . message && err . code === 101 && err . message === 'Object not found.' ) {
122
+ return ; // nothing to update so we are good
123
+ } else {
124
+ throw err ; // unknown error
125
+ }
126
+ } ) ;
127
+ }
128
+
129
+ /**
130
+ * if _account_lockout_expires_at > current_time and _failed_login_count > threshold
131
+ * reject with account locked error
132
+ * else
133
+ * resolve
134
+ */
135
+
136
+ } , {
137
+ key : '_notLocked' ,
138
+ value : function _notLocked ( ) {
139
+ var _this2 = this ;
140
+
141
+ var query = {
142
+ username : this . _user . username ,
143
+ _account_lockout_expires_at : { $gt : _node2 . default . _encode ( new Date ( ) ) } ,
144
+ _failed_login_count : { $gte : this . _config . accountLockout . threshold }
145
+ } ;
146
+
147
+ return this . _config . database . find ( '_User' , query ) . then ( function ( users ) {
148
+ if ( Array . isArray ( users ) && users . length > 0 ) {
149
+ throw new _node2 . default . Error ( _node2 . default . Error . OBJECT_NOT_FOUND , 'Your account is locked due to multiple failed login attempts. Please try again after ' + _this2 . _config . accountLockout . duration + ' minute(s)' ) ;
150
+ }
151
+ } ) ;
152
+ }
153
+
154
+ /**
155
+ * set and/or increment _failed_login_count
156
+ * if _failed_login_count > threshold
157
+ * set the _account_lockout_expires_at to current_time + accountPolicy.duration
158
+ * else
159
+ * do nothing
160
+ */
161
+
162
+ } , {
163
+ key : '_handleFailedLoginAttempt' ,
164
+ value : function _handleFailedLoginAttempt ( ) {
165
+ var _this3 = this ;
166
+
167
+ return this . _initFailedLoginCount ( ) . then ( function ( ) {
168
+ return _this3 . _incrementFailedLoginCount ( ) ;
169
+ } ) . then ( function ( ) {
170
+ return _this3 . _setLockoutExpiration ( ) ;
171
+ } ) ;
172
+ }
173
+
174
+ /**
175
+ * handle login attempt if the Account Lockout Policy is enabled
176
+ */
177
+
178
+ } , {
179
+ key : 'handleLoginAttempt' ,
180
+ value : function handleLoginAttempt ( loginSuccessful ) {
181
+ var _this4 = this ;
182
+
183
+ if ( ! this . _config . accountLockout ) {
184
+ return Promise . resolve ( ) ;
185
+ }
186
+ return this . _notLocked ( ) . then ( function ( ) {
187
+ if ( loginSuccessful ) {
188
+ return _this4 . _setFailedLoginCount ( 0 ) ;
189
+ } else {
190
+ return _this4 . _handleFailedLoginAttempt ( ) ;
191
+ }
192
+ } ) ;
193
+ }
194
+ } ] ) ;
195
+
196
+ return AccountLockout ;
197
+ } ( ) ;
198
+
199
+ exports . default = AccountLockout ;
0 commit comments