Skip to content

Commit

Permalink
[IAMRISK-3553] Swapped CAPTCHA on each reload (#2560)
Browse files Browse the repository at this point in the history
### 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]>
  • Loading branch information
3 people committed May 30, 2024
1 parent c342929 commit 5883544
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 13 deletions.
103 changes: 101 additions & 2 deletions src/__tests__/connection/database/actions.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
import Immutable, { List, Map } from 'immutable';
import { signUp } from '../../../connection/database/actions';
import {
signUp,
resetPasswordSuccess,
showResetPasswordActivity,
showLoginActivity, showSignUpActivity
} from '../../../connection/database/actions';
import { swap, setEntity } from '../../../store';
import { swapCaptcha } from "../../../connection/captcha";

const webApiMock = () => require('core/web_api');
const coreActionsMock = () => require('core/actions');

jest.mock('core/actions', () => ({
validateAndSubmit: jest.fn()
}));

jest.mock('../../../connection/captcha', () => {
const originalCaptcha = jest.requireActual('../../../connection/captcha');
return {
__esModule: true,
...originalCaptcha,
swapCaptcha: jest.fn((id, flow, wasInvalid, next) => {
next();
}),
}
});

jest.mock('core/web_api', () => ({
signUp: jest.fn()
}));
Expand Down Expand Up @@ -208,4 +226,85 @@ describe('database/actions.js', () => {
}
});
});
});

describe('exported functions', () => {
const id = 2;
const mCaptcha = Immutable.fromJS({
field: {
email: {
value: '[email protected]'
},
password: {
value: 'testpass'
},
family_name: {
value: 'test-family-name'
},
given_name: {
value: 'test-given-name'
},
name: {
value: 'test-name'
},
nickname: {
value: 'test-nickname'
},
picture: {
value: 'test-pic'
},
other_prop: {
value: 'test-other'
}
},
database: {
additionalSignUpFields: [
{ name: 'family_name', storage: 'root' },
{ name: 'given_name', storage: 'root' },
{ name: 'name', storage: 'root' },
{ name: 'nickname', storage: 'root' },
{ name: 'picture', storage: 'root' },
{ name: 'other_prop' }
]
},
captcha: {
provider: 'auth0'
},
passwordResetCaptcha: {
provider: 'auth0'
},
});

describe('resetPasswordSuccess', () => {
it('runs swap CAPTCHA', () => {
swap(setEntity, 'lock', id, mCaptcha);
resetPasswordSuccess(id);
expect(swapCaptcha.mock.calls.length).toEqual(1);
});
});

describe('showResetPasswordActivity', () => {
it('runs swap CAPTCHA', () => {
swap(setEntity, 'lock', id, mCaptcha);
showResetPasswordActivity(id);
expect(swapCaptcha.mock.calls.length).toEqual(1);
});
});

describe('showLoginActivity', () => {
it('runs swap CAPTCHA', () => {
swap(setEntity, 'lock', id, mCaptcha);
showLoginActivity(id);
expect(swapCaptcha.mock.calls.length).toEqual(1);
});
});

describe('showSignupActivity', () => {
it('runs swap CAPTCHA', () => {
swap(setEntity, 'lock', id, mCaptcha);
showSignUpActivity(id);
expect(swapCaptcha.mock.calls.length).toEqual(1);
});
});
});
})

50 changes: 39 additions & 11 deletions src/connection/database/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,15 +259,17 @@ export function resetPassword(id) {
});
}

function resetPasswordSuccess(id) {
export function resetPasswordSuccess(id) {
const m = read(getEntity, 'lock', id);
if (hasScreen(m, 'login')) {
swap(
updateEntity,
'lock',
id,
m => setScreen(l.setSubmitting(m, false), 'login', ['']) // array with one empty string tells the function to not clear any field
);
swapCaptcha(id, Flow.PASSWORD_RESET, false, () => {
swap(
updateEntity,
'lock',
id,
m => setScreen(l.setSubmitting(m, false), 'login', ['']) // array with one empty string tells the function to not clear any field
);
});

// TODO: should be handled by box
setTimeout(() => {
Expand All @@ -278,7 +280,9 @@ function resetPasswordSuccess(id) {
if (l.ui.autoclose(m)) {
closeLock(id);
} else {
swap(updateEntity, 'lock', id, m => l.setSubmitting(m, false).set('passwordResetted', true));
swapCaptcha(id, Flow.PASSWORD_RESET, false, () => {
swap(updateEntity, 'lock', id, m => l.setSubmitting(m, false).set('passwordResetted', true));
});
}
}
}
Expand All @@ -305,15 +309,39 @@ function resetPasswordError(id, error) {
}

export function showLoginActivity(id, fields = ['password']) {
swap(updateEntity, 'lock', id, setScreen, 'login', fields);
const m = read(getEntity, 'lock', id);
const captchaConfig = l.captcha(m);
if (captchaConfig && captchaConfig.get('provider') === 'arkose') {
swap(updateEntity, 'lock', id, setScreen, 'login', fields);
} else {
swapCaptcha(id, 'login', false, () => {
swap(updateEntity, 'lock', id, setScreen, 'login', fields);
});
}
}

export function showSignUpActivity(id, fields = ['password']) {
swap(updateEntity, 'lock', id, setScreen, 'signUp', fields);
const m = read(getEntity, 'lock', id);
const captchaConfig = l.captcha(m);
if (captchaConfig && captchaConfig.get('provider') === 'arkose') {
swap(updateEntity, 'lock', id, setScreen, 'signUp', fields);
} else {
swapCaptcha(id, 'login', false, () => {
swap(updateEntity, 'lock', id, setScreen, 'signUp', fields);
});
}
}

export function showResetPasswordActivity(id, fields = ['password']) {
swap(updateEntity, 'lock', id, setScreen, 'forgotPassword', fields);
const m = read(getEntity, 'lock', id);
const captchaConfig = l.passwordResetCaptcha(m);
if (captchaConfig && captchaConfig.get('provider') === 'arkose') {
swap(updateEntity, 'lock', id, setScreen, 'forgotPassword', fields);
} else {
swapCaptcha(id, 'login', false, () => {
swap(updateEntity, 'lock', id, setScreen, 'forgotPassword', fields);
});
}
}

export function cancelResetPassword(id) {
Expand Down

0 comments on commit 5883544

Please sign in to comment.