Skip to content

Commit 5883544

Browse files
josh-cainTretertenJosh Cain
authored
[IAMRISK-3553] Swapped CAPTCHA on each reload (#2560)
### Changes Exactly the same as #2558 , but without package.json version bump. --------- Co-authored-by: Tre Moore <[email protected]> Co-authored-by: Josh Cain <[email protected]>
1 parent c342929 commit 5883544

File tree

2 files changed

+140
-13
lines changed

2 files changed

+140
-13
lines changed

src/__tests__/connection/database/actions.test.js

Lines changed: 101 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
11
import Immutable, { List, Map } from 'immutable';
2-
import { signUp } from '../../../connection/database/actions';
2+
import {
3+
signUp,
4+
resetPasswordSuccess,
5+
showResetPasswordActivity,
6+
showLoginActivity, showSignUpActivity
7+
} from '../../../connection/database/actions';
38
import { swap, setEntity } from '../../../store';
9+
import { swapCaptcha } from "../../../connection/captcha";
410

511
const webApiMock = () => require('core/web_api');
612
const coreActionsMock = () => require('core/actions');
13+
714
jest.mock('core/actions', () => ({
815
validateAndSubmit: jest.fn()
916
}));
1017

18+
jest.mock('../../../connection/captcha', () => {
19+
const originalCaptcha = jest.requireActual('../../../connection/captcha');
20+
return {
21+
__esModule: true,
22+
...originalCaptcha,
23+
swapCaptcha: jest.fn((id, flow, wasInvalid, next) => {
24+
next();
25+
}),
26+
}
27+
});
28+
1129
jest.mock('core/web_api', () => ({
1230
signUp: jest.fn()
1331
}));
@@ -208,4 +226,85 @@ describe('database/actions.js', () => {
208226
}
209227
});
210228
});
211-
});
229+
230+
describe('exported functions', () => {
231+
const id = 2;
232+
const mCaptcha = Immutable.fromJS({
233+
field: {
234+
email: {
235+
236+
},
237+
password: {
238+
value: 'testpass'
239+
},
240+
family_name: {
241+
value: 'test-family-name'
242+
},
243+
given_name: {
244+
value: 'test-given-name'
245+
},
246+
name: {
247+
value: 'test-name'
248+
},
249+
nickname: {
250+
value: 'test-nickname'
251+
},
252+
picture: {
253+
value: 'test-pic'
254+
},
255+
other_prop: {
256+
value: 'test-other'
257+
}
258+
},
259+
database: {
260+
additionalSignUpFields: [
261+
{ name: 'family_name', storage: 'root' },
262+
{ name: 'given_name', storage: 'root' },
263+
{ name: 'name', storage: 'root' },
264+
{ name: 'nickname', storage: 'root' },
265+
{ name: 'picture', storage: 'root' },
266+
{ name: 'other_prop' }
267+
]
268+
},
269+
captcha: {
270+
provider: 'auth0'
271+
},
272+
passwordResetCaptcha: {
273+
provider: 'auth0'
274+
},
275+
});
276+
277+
describe('resetPasswordSuccess', () => {
278+
it('runs swap CAPTCHA', () => {
279+
swap(setEntity, 'lock', id, mCaptcha);
280+
resetPasswordSuccess(id);
281+
expect(swapCaptcha.mock.calls.length).toEqual(1);
282+
});
283+
});
284+
285+
describe('showResetPasswordActivity', () => {
286+
it('runs swap CAPTCHA', () => {
287+
swap(setEntity, 'lock', id, mCaptcha);
288+
showResetPasswordActivity(id);
289+
expect(swapCaptcha.mock.calls.length).toEqual(1);
290+
});
291+
});
292+
293+
describe('showLoginActivity', () => {
294+
it('runs swap CAPTCHA', () => {
295+
swap(setEntity, 'lock', id, mCaptcha);
296+
showLoginActivity(id);
297+
expect(swapCaptcha.mock.calls.length).toEqual(1);
298+
});
299+
});
300+
301+
describe('showSignupActivity', () => {
302+
it('runs swap CAPTCHA', () => {
303+
swap(setEntity, 'lock', id, mCaptcha);
304+
showSignUpActivity(id);
305+
expect(swapCaptcha.mock.calls.length).toEqual(1);
306+
});
307+
});
308+
});
309+
})
310+

src/connection/database/actions.js

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -259,15 +259,17 @@ export function resetPassword(id) {
259259
});
260260
}
261261

262-
function resetPasswordSuccess(id) {
262+
export function resetPasswordSuccess(id) {
263263
const m = read(getEntity, 'lock', id);
264264
if (hasScreen(m, 'login')) {
265-
swap(
266-
updateEntity,
267-
'lock',
268-
id,
269-
m => setScreen(l.setSubmitting(m, false), 'login', ['']) // array with one empty string tells the function to not clear any field
270-
);
265+
swapCaptcha(id, Flow.PASSWORD_RESET, false, () => {
266+
swap(
267+
updateEntity,
268+
'lock',
269+
id,
270+
m => setScreen(l.setSubmitting(m, false), 'login', ['']) // array with one empty string tells the function to not clear any field
271+
);
272+
});
271273

272274
// TODO: should be handled by box
273275
setTimeout(() => {
@@ -278,7 +280,9 @@ function resetPasswordSuccess(id) {
278280
if (l.ui.autoclose(m)) {
279281
closeLock(id);
280282
} else {
281-
swap(updateEntity, 'lock', id, m => l.setSubmitting(m, false).set('passwordResetted', true));
283+
swapCaptcha(id, Flow.PASSWORD_RESET, false, () => {
284+
swap(updateEntity, 'lock', id, m => l.setSubmitting(m, false).set('passwordResetted', true));
285+
});
282286
}
283287
}
284288
}
@@ -305,15 +309,39 @@ function resetPasswordError(id, error) {
305309
}
306310

307311
export function showLoginActivity(id, fields = ['password']) {
308-
swap(updateEntity, 'lock', id, setScreen, 'login', fields);
312+
const m = read(getEntity, 'lock', id);
313+
const captchaConfig = l.captcha(m);
314+
if (captchaConfig && captchaConfig.get('provider') === 'arkose') {
315+
swap(updateEntity, 'lock', id, setScreen, 'login', fields);
316+
} else {
317+
swapCaptcha(id, 'login', false, () => {
318+
swap(updateEntity, 'lock', id, setScreen, 'login', fields);
319+
});
320+
}
309321
}
310322

311323
export function showSignUpActivity(id, fields = ['password']) {
312-
swap(updateEntity, 'lock', id, setScreen, 'signUp', fields);
324+
const m = read(getEntity, 'lock', id);
325+
const captchaConfig = l.captcha(m);
326+
if (captchaConfig && captchaConfig.get('provider') === 'arkose') {
327+
swap(updateEntity, 'lock', id, setScreen, 'signUp', fields);
328+
} else {
329+
swapCaptcha(id, 'login', false, () => {
330+
swap(updateEntity, 'lock', id, setScreen, 'signUp', fields);
331+
});
332+
}
313333
}
314334

315335
export function showResetPasswordActivity(id, fields = ['password']) {
316-
swap(updateEntity, 'lock', id, setScreen, 'forgotPassword', fields);
336+
const m = read(getEntity, 'lock', id);
337+
const captchaConfig = l.passwordResetCaptcha(m);
338+
if (captchaConfig && captchaConfig.get('provider') === 'arkose') {
339+
swap(updateEntity, 'lock', id, setScreen, 'forgotPassword', fields);
340+
} else {
341+
swapCaptcha(id, 'login', false, () => {
342+
swap(updateEntity, 'lock', id, setScreen, 'forgotPassword', fields);
343+
});
344+
}
317345
}
318346

319347
export function cancelResetPassword(id) {

0 commit comments

Comments
 (0)