Skip to content

Commit

Permalink
fix: make credentials storing more liberal
Browse files Browse the repository at this point in the history
  • Loading branch information
dbajpeyi committed Dec 4, 2024
1 parent 225ca95 commit 15e9ed9
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 158 deletions.
34 changes: 8 additions & 26 deletions dist/autofill-debug.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 8 additions & 26 deletions dist/autofill.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions src/Form/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,7 @@ class Form {
*/
getValuesReadyForStorage() {
const formValues = this.getRawValues();
const hasOnlyOneCredential = this.inputs.credentials.size === 1;
return prepareFormValuesForStorage(formValues, hasOnlyOneCredential);
return prepareFormValuesForStorage(formValues);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Form/Form.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ describe('Test the form class reading values correctly', () => {
<input type="password" value="" autocomplete="new-password" />
<button type="submit">Sign up</button>
</form>`,
expHasValues: false,
expValues: { credentials: undefined },
expHasValues: true,
expValues: { credentials: { username: 'testUsername' } },
},
{
testCase: 'form where the password is <=3 characters long',
Expand All @@ -95,8 +95,8 @@ describe('Test the form class reading values correctly', () => {
<input type="password" value="abc" autocomplete="new-password" />
<button type="submit">Sign up</button>
</form>`,
expHasValues: false,
expValues: { credentials: undefined },
expHasValues: true,
expValues: { credentials: { username: 'testUsername' } },
},
{
testCase: 'form with hidden email field',
Expand Down
20 changes: 3 additions & 17 deletions src/Form/formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,18 +159,6 @@ const getMMAndYYYYFromString = (expiration) => {
);
};

/**
* @param {InternalDataStorageObject} credentials
* @return {boolean}
*/
const shouldStoreCredentials = ({ credentials }, hasOnlyOneCredential) => {
if (credentials.password) {
return Boolean(credentials.password);
} else {
return hasOnlyOneCredential && Boolean(credentials.username);
}
};

/**
* @param {InternalDataStorageObject} credentials
* @return {boolean}
Expand Down Expand Up @@ -207,10 +195,9 @@ const formatPhoneNumber = (phone) => phone.replaceAll(/[^0-9|+]/g, '');
* Formats form data into an object to send to the device for storage
* If values are insufficient for a complete entry, they are discarded
* @param {InternalDataStorageObject} formValues
* @param {boolean} hasOnlyOneCredential
* @return {DataStorageObject}
*/
const prepareFormValuesForStorage = (formValues, hasOnlyOneCredential) => {
const prepareFormValuesForStorage = (formValues) => {
/** @type {Partial<InternalDataStorageObject>} */
let { credentials, identities, creditCards } = formValues;

Expand All @@ -219,9 +206,8 @@ const prepareFormValuesForStorage = (formValues, hasOnlyOneCredential) => {
creditCards.cardName = identities?.fullName || formatFullName(identities);
}

/** Fixes for credentials **/
// Don't store if there isn't enough data
if (shouldStoreCredentials(formValues, hasOnlyOneCredential)) {
/** Fixes for credentials */
if (credentials.username || credentials.password) {
// If we don't have a username to match a password, let's see if the email is available
if (credentials.password && !credentials.username && identities.emailAddress) {
credentials.username = identities.emailAddress;
Expand Down
53 changes: 22 additions & 31 deletions src/Form/formatters.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,44 +62,35 @@ describe('Can strip phone formatting characters', () => {
describe('prepareFormValuesForStorage()', () => {
describe('handling credentials', () => {
it('accepts for username only', () => {
const values = prepareFormValuesForStorage(
{
credentials: { username: '[email protected]' },
// @ts-ignore
creditCards: {},
// @ts-ignore
identities: {},
},
true,
);
const values = prepareFormValuesForStorage({
credentials: { username: '[email protected]' },
// @ts-ignore
creditCards: {},
// @ts-ignore
identities: {},
});
expect(values.credentials?.username).toBe('[email protected]');
});
it('accepts password only', () => {
const values = prepareFormValuesForStorage(
{
// @ts-ignore
credentials: { password: '123456' },
// @ts-ignore
creditCards: {},
// @ts-ignore
identities: {},
},
false,
);
const values = prepareFormValuesForStorage({
// @ts-ignore
credentials: { password: '123456' },
// @ts-ignore
creditCards: {},
// @ts-ignore
identities: {},
});
expect(values.credentials?.password).toBe('123456');
});
it('accepts username+password', () => {
const inputCredentials = { username: '[email protected]', password: '123456' };
const values = prepareFormValuesForStorage(
{
credentials: inputCredentials,
// @ts-ignore
creditCards: {},
// @ts-ignore
identities: {},
},
false,
);
const values = prepareFormValuesForStorage({
credentials: inputCredentials,
// @ts-ignore
creditCards: {},
// @ts-ignore
identities: {},
});
expect(values.credentials).toEqual(inputCredentials);
});
});
Expand Down
34 changes: 8 additions & 26 deletions swift-package/Resources/assets/autofill-debug.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 15e9ed9

Please sign in to comment.