From 20edf94d92f1b2144b9f869546d5781376b5a40f Mon Sep 17 00:00:00 2001 From: Guillaume Vincent Date: Fri, 3 Feb 2017 15:04:32 +0100 Subject: [PATCH] Load password profile if site matching fix https://github.com/lesspass/lesspass/issues/95 --- src/domain/url-parser.js | 6 +++++- src/store/actions.js | 4 ++++ src/store/mutation-types.js | 3 ++- src/store/mutations.js | 14 ++++++++++++ src/views/ConfigureOptions.vue | 1 - src/views/PasswordGenerator.vue | 3 +-- test/store.mutations.js | 48 ++++++++++++++++++++++++++++++++++++++++- test/url-parser.js | 5 +++-- 8 files changed, 76 insertions(+), 8 deletions(-) diff --git a/src/domain/url-parser.js b/src/domain/url-parser.js index 284f069..2e5c53e 100644 --- a/src/domain/url-parser.js +++ b/src/domain/url-parser.js @@ -18,7 +18,11 @@ export function getSite() { return new Promise(resolve => { if (typeof chrome !== 'undefined' && typeof chrome.tabs !== 'undefined' && typeof chrome.tabs.query !== 'undefined') { chrome.tabs.query({active: true, currentWindow: true}, tabs => { - resolve(getDomainName(tabs[0].url)); + const url = tabs[0].url; + resolve({ + site: getDomainName(url), + url: url + }); }); } else { resolve(''); diff --git a/src/store/actions.js b/src/store/actions.js index 94f6825..d4d8f9a 100644 --- a/src/store/actions.js +++ b/src/store/actions.js @@ -12,6 +12,10 @@ export const loadPasswordFirstTime = ({commit}) => { commit(types.LOAD_PASSWORD_FIRST_TIME); }; +export const loadPasswordForSite = ({commit}, payload) => { + commit(types.LOAD_PASSWORD_FOR_SITE, payload); +}; + export const saveDefaultPassword = ({commit}, payload) => { commit(types.SET_DEFAULT_PASSWORD, payload); }; diff --git a/src/store/mutation-types.js b/src/store/mutation-types.js index 6546967..c07182a 100644 --- a/src/store/mutation-types.js +++ b/src/store/mutation-types.js @@ -6,4 +6,5 @@ export const SET_PASSWORDS = 'SET_PASSWORDS'; export const DELETE_PASSWORD = 'DELETE_PASSWORD'; export const SET_BASE_URL = 'SET_BASE_URL'; export const SET_VERSION = 'SET_VERSION'; -export const LOAD_PASSWORD_FIRST_TIME = 'LOAD_PASSWORD_FIRST_TIME'; \ No newline at end of file +export const LOAD_PASSWORD_FIRST_TIME = 'LOAD_PASSWORD_FIRST_TIME'; +export const LOAD_PASSWORD_FOR_SITE = 'LOAD_PASSWORD_FOR_SITE'; diff --git a/src/store/mutations.js b/src/store/mutations.js index cab5c04..19768d8 100644 --- a/src/store/mutations.js +++ b/src/store/mutations.js @@ -45,5 +45,19 @@ export default { if (tenMinutesAgo > state.lastUse) { setState(state, 'password', state.defaultPassword); } + }, + [types.LOAD_PASSWORD_FOR_SITE](state, {site, url}){ + state.password.site = site; + const passwords = state.passwords; + for (let i = 0; i < state.passwords.length; i++) { + const password = passwords[i]; + if (password.site.endsWith(site)) { + setState(state, 'password', password); + } + if (typeof url !== 'undefined' && url.includes(password.site)) { + setState(state, 'password', password); + break; + } + } } }; diff --git a/src/views/ConfigureOptions.vue b/src/views/ConfigureOptions.vue index f93273e..e8967c9 100644 --- a/src/views/ConfigureOptions.vue +++ b/src/views/ConfigureOptions.vue @@ -43,7 +43,6 @@ }, 3000); }, updatePassword(password){ - console.log(password); this.options = password; } } diff --git a/src/views/PasswordGenerator.vue b/src/views/PasswordGenerator.vue index feb7320..9068c10 100644 --- a/src/views/PasswordGenerator.vue +++ b/src/views/PasswordGenerator.vue @@ -180,8 +180,7 @@ getSite().then(site => { if (site) { - this.password.site = site; - this.$store.dispatch('savePassword', {password: this.password}); + this.$store.dispatch('loadPasswordForSite', site); } }); diff --git a/test/store.mutations.js b/test/store.mutations.js index 38f7be8..657bde7 100644 --- a/test/store.mutations.js +++ b/test/store.mutations.js @@ -186,6 +186,52 @@ test('LOAD_PASSWORD_FIRST_TIME last use null', t => { }; const LOAD_PASSWORD_FIRST_TIME = mutations[types.LOAD_PASSWORD_FIRST_TIME]; LOAD_PASSWORD_FIRST_TIME(state); - t.is(state.password.version,2); + t.is(state.password.version, 2); timekeeper.reset(); +}); + +test('LOAD_PASSWORD_FOR_SITE', t => { + const state = { + password: { + site: '' + }, + passwords: [ + {id: '1', site: 'www.example.org'}, + {id: '2', site: 'www.google.com'} + ] + }; + const LOAD_PASSWORD_FOR_SITE = mutations[types.LOAD_PASSWORD_FOR_SITE]; + LOAD_PASSWORD_FOR_SITE(state, {site: 'google.com'}); + t.is(state.password.id, '2'); + t.is(state.password.site, 'www.google.com'); +}); + +test('LOAD_PASSWORD_FOR_SITE no passwords', t => { + const state = { + password: { + site: '' + }, + passwords: [] + }; + const LOAD_PASSWORD_FOR_SITE = mutations[types.LOAD_PASSWORD_FOR_SITE]; + LOAD_PASSWORD_FOR_SITE(state, {site: 'google.com'}); + t.false('id' in state.password); + t.is(state.password.site, 'google.com'); +}); + +test('LOAD_PASSWORD_FOR_SITE multiple accounts matching criteria', t => { + const state = { + password: { + site: '' + }, + passwords: [ + {id: '1', site: 'www.example.org'}, + {id: '2', site: 'www.google.com'}, + {id: '3', site: 'account.google.com'}, + ] + }; + const LOAD_PASSWORD_FOR_SITE = mutations[types.LOAD_PASSWORD_FOR_SITE]; + LOAD_PASSWORD_FOR_SITE(state, {site: 'google.com', url: 'https://www.google.com'}); + t.is(state.password.id, '2'); + t.is(state.password.site, 'www.google.com'); }); \ No newline at end of file diff --git a/test/url-parser.js b/test/url-parser.js index e303b07..20f1d1d 100644 --- a/test/url-parser.js +++ b/test/url-parser.js @@ -26,8 +26,9 @@ test('get current tab', t => { } } }; - return urlParser.getSite().then(site => { - t.is(site, 'example.org') + return urlParser.getSite().then(response => { + t.is(response.url, url); + t.is(response.site, 'example.org') }); });