@@ -6,6 +6,7 @@ const TestServer = require('../test_server');
66const Promise = require ( 'bluebird' ) ;
77const restifyClients = Promise . promisifyAll ( require ( 'restify-clients' ) ) ;
88const mcHelper = require ( '../cache-helper' ) ;
9+ Promise . promisifyAll ( mcHelper , { multiArgs : true } ) ;
910
1011function randomEmail ( ) {
1112 return Math . floor ( Math . random ( ) * 10000 ) + '@email.com' ;
@@ -18,13 +19,25 @@ function randomIp() {
1819 return [ getSubnet ( ) , getSubnet ( ) , getSubnet ( ) , getSubnet ( ) ] . join ( '.' ) ;
1920}
2021
22+ function randomUid ( ) {
23+ return Math . floor ( Math . random ( ) * 10000 ) + '' ;
24+ }
25+
2126const config = require ( '../../lib/config' ) . getProperties ( ) ;
2227config . userDefinedRateLimitRules . totpCodeRules . limits . periodMs = 1000 ;
2328config . userDefinedRateLimitRules . totpCodeRules . limits . rateLimitIntervalMs = 1000 ;
2429config . userDefinedRateLimitRules . tokenCodeRules . limits . max = 2 ;
2530config . userDefinedRateLimitRules . tokenCodeRules . limits . periodMs = 1000 ;
2631config . userDefinedRateLimitRules . tokenCodeRules . limits . rateLimitIntervalMs = 1000 ;
2732
33+ config . userDefinedRateLimitRules . recoveryPhoneCreateCodeRules . limits . max = 5 ;
34+ config . userDefinedRateLimitRules . recoveryPhoneCreateCodeRules . limits . periodMs = 5000 ;
35+ config . userDefinedRateLimitRules . recoveryPhoneCreateCodeRules . limits . rateLimitIntervalMs = 1000 ;
36+
37+ config . userDefinedRateLimitRules . recoveryPhoneConfirmCodeRules . limits . max = 5 ;
38+ config . userDefinedRateLimitRules . recoveryPhoneConfirmCodeRules . limits . periodMs = 5000 ;
39+ config . userDefinedRateLimitRules . recoveryPhoneConfirmCodeRules . limits . rateLimitIntervalMs = 1000 ;
40+
2841const ACTIONS = [ 'verifyTotpCode' , 'verifyTokenCode' ] ;
2942
3043const testServer = new TestServer ( config ) ;
@@ -136,6 +149,91 @@ ACTIONS.forEach((action) => {
136149 } ) ;
137150} ) ;
138151
152+ const recoveryPhoneActions = [
153+ 'recoveryPhoneCreate' ,
154+ 'recoveryPhoneConfirmCode' ,
155+ ] ;
156+ recoveryPhoneActions . forEach ( ( action ) => {
157+ test ( `clear everything for ${ action } ` , async ( t ) => {
158+ try {
159+ await mcHelper . clearEverythingAsync ( ) ;
160+ t . pass ( 'cleared redis' ) ;
161+ } catch ( err ) {
162+ t . fail ( err ) ;
163+ } finally {
164+ t . end ( ) ;
165+ }
166+ } ) ;
167+
168+ test ( '/checkAuthenticated `' + action + '` by uid' , async ( t ) => {
169+ const uid = randomUid ( ) ;
170+ const ip = randomIp ( ) ;
171+ // Send requests until throttled
172+ let [ req , res , obj ] = await client . postAsync ( '/checkAuthenticated' , {
173+ ip,
174+ action,
175+ uid,
176+ } ) ;
177+ t . equal ( res . statusCode , 200 , 'returns a 200' ) ;
178+ t . equal ( obj . block , false , 'not rate limited' ) ;
179+
180+ [ req , res , obj ] = await client . postAsync ( '/checkAuthenticated' , {
181+ ip,
182+ action,
183+ uid,
184+ } ) ;
185+ t . equal ( res . statusCode , 200 , 'returns a 200' ) ;
186+ t . equal ( obj . block , false , 'not rate limited' ) ;
187+
188+ [ req , res , obj ] = await client . postAsync ( '/checkAuthenticated' , {
189+ ip,
190+ action,
191+ uid,
192+ } ) ;
193+ t . equal ( res . statusCode , 200 , 'returns a 200' ) ;
194+ t . equal ( obj . block , false , 'not rate limited' ) ;
195+
196+ [ req , res , obj ] = await client . postAsync ( '/checkAuthenticated' , {
197+ ip,
198+ action,
199+ uid,
200+ } ) ;
201+ t . equal ( res . statusCode , 200 , 'returns a 200' ) ;
202+ t . equal ( obj . block , false , 'not rate limited' ) ;
203+
204+ [ req , res , obj ] = await client . postAsync ( '/checkAuthenticated' , {
205+ ip,
206+ action,
207+ uid,
208+ } ) ;
209+ t . equal ( res . statusCode , 200 , 'returns a 200' ) ;
210+ t . equal ( obj . block , false , 'not rate limited' ) ;
211+
212+ [ req , res , obj ] = await client . postAsync ( '/checkAuthenticated' , {
213+ ip,
214+ action,
215+ uid,
216+ } ) ;
217+ t . equal ( res . statusCode , 200 , 'returns a 200' ) ;
218+ t . equal ( obj . block , true , 'rate limited' ) ;
219+ t . equal ( obj . retryAfter , 1 , 'rate limit retry amount' ) ;
220+
221+ // Wait for limit to expire
222+ await Promise . delay ( 1010 ) ;
223+
224+ [ req , res , obj ] = await client . postAsync ( '/checkAuthenticated' , {
225+ ip,
226+ action,
227+ uid,
228+ } ) ;
229+ t . equal ( res . statusCode , 200 , 'returns a 200' ) ;
230+ t . equal ( obj . block , false , 'not rate limited' ) ;
231+
232+ t . pass ( req ) ;
233+ t . end ( ) ;
234+ } ) ;
235+ } ) ;
236+
139237test ( 'teardown' , async function ( t ) {
140238 await testServer . stop ( ) ;
141239 t . end ( ) ;
0 commit comments