Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Forget user session on window close #15205

Merged
merged 5 commits into from
Aug 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ Template.mailMessagesInstructions.events({
to_emails: selectedEmails.get().map((email) => email.text).toString(),
subject,
messages: selectedMessages.get(),
language: localStorage.getItem('userLanguage'),
language: Meteor._localStorage.getItem('userLanguage'),
};

Meteor.call('mailMessages', data, function(err, result) {
Expand Down
3 changes: 2 additions & 1 deletion app/e2e/client/accountEncryption.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Template } from 'meteor/templating';
import { Meteor } from 'meteor/meteor';
import { ReactiveVar } from 'meteor/reactive-var';
import toastr from 'toastr';
import s from 'underscore.string';
Expand All @@ -12,7 +13,7 @@ Template.accountEncryption.helpers({
return settings.get('E2E_Enable');
},
allowKeyChange() {
return localStorage.getItem('public_key') && localStorage.getItem('private_key');
return Meteor._localStorage.getItem('public_key') && Meteor._localStorage.getItem('private_key');
},
canConfirmNewKey() {
const encryptionKey = Template.instance().encryptionKey.get();
Expand Down
34 changes: 17 additions & 17 deletions app/e2e/client/rocketchat.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ class E2E {
}

this.started = true;
let public_key = localStorage.getItem('public_key');
let private_key = localStorage.getItem('private_key');
let public_key = Meteor._localStorage.getItem('public_key');
let private_key = Meteor._localStorage.getItem('private_key');

await this.loadKeysFromDB();

Expand Down Expand Up @@ -143,12 +143,12 @@ class E2E {
// TODO: Split in 2 methods to persist keys
if (!this.db_public_key || !this.db_private_key) {
await call('e2e.setUserPublicAndPivateKeys', {
public_key: localStorage.getItem('public_key'),
private_key: await this.encodePrivateKey(localStorage.getItem('private_key'), this.createRandomPassword()),
public_key: Meteor._localStorage.getItem('public_key'),
private_key: await this.encodePrivateKey(Meteor._localStorage.getItem('private_key'), this.createRandomPassword()),
});
}

const randomPassword = localStorage.getItem('e2e.randomPassword');
const randomPassword = Meteor._localStorage.getItem('e2e.randomPassword');
if (randomPassword) {
const passwordRevealText = TAPi18n.__('E2E_password_reveal_text', {
postProcess: 'sprintf',
Expand All @@ -174,7 +174,7 @@ class E2E {
if (!confirm) {
return;
}
localStorage.removeItem('e2e.randomPassword');
Meteor._localStorage.removeItem('e2e.randomPassword');
this.closeAlert();
});
},
Expand All @@ -196,8 +196,8 @@ class E2E {
alerts.close();
}

localStorage.removeItem('public_key');
localStorage.removeItem('private_key');
Meteor._localStorage.removeItem('public_key');
Meteor._localStorage.removeItem('private_key');
this.instancesByRoomId = {};
this.privateKey = null;
this.enabled.set(false);
Expand Down Expand Up @@ -233,12 +233,12 @@ class E2E {

async changePassword(newPassword) {
await call('e2e.setUserPublicAndPivateKeys', {
public_key: localStorage.getItem('public_key'),
private_key: await this.encodePrivateKey(localStorage.getItem('private_key'), newPassword),
public_key: Meteor._localStorage.getItem('public_key'),
private_key: await this.encodePrivateKey(Meteor._localStorage.getItem('private_key'), newPassword),
});

if (localStorage.getItem('e2e.randomPassword')) {
localStorage.setItem('e2e.randomPassword', newPassword);
if (Meteor._localStorage.getItem('e2e.randomPassword')) {
Meteor._localStorage.setItem('e2e.randomPassword', newPassword);
}
}

Expand All @@ -254,12 +254,12 @@ class E2E {
}

async loadKeys({ public_key, private_key }) {
localStorage.setItem('public_key', public_key);
Meteor._localStorage.setItem('public_key', public_key);

try {
this.privateKey = await importRSAKey(EJSON.parse(private_key), ['decrypt']);

localStorage.setItem('private_key', private_key);
Meteor._localStorage.setItem('private_key', private_key);
} catch (error) {
return console.error('E2E -> Error importing private key: ', error);
}
Expand All @@ -278,15 +278,15 @@ class E2E {
try {
const publicKey = await exportJWKKey(key.publicKey);

localStorage.setItem('public_key', JSON.stringify(publicKey));
Meteor._localStorage.setItem('public_key', JSON.stringify(publicKey));
} catch (error) {
return console.error('E2E -> Error exporting public key: ', error);
}

try {
const privateKey = await exportJWKKey(key.privateKey);

localStorage.setItem('private_key', JSON.stringify(privateKey));
Meteor._localStorage.setItem('private_key', JSON.stringify(privateKey));
} catch (error) {
return console.error('E2E -> Error exporting private key: ', error);
}
Expand All @@ -300,7 +300,7 @@ class E2E {

createRandomPassword() {
const randomPassword = `${ Random.id(3) }-${ Random.id(3) }-${ Random.id(3) }`.toLowerCase();
localStorage.setItem('e2e.randomPassword', randomPassword);
Meteor._localStorage.setItem('e2e.randomPassword', randomPassword);
return randomPassword;
}

Expand Down
11 changes: 6 additions & 5 deletions app/emoji/client/lib/EmojiPicker.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import _ from 'underscore';
import { Blaze } from 'meteor/blaze';
import { Meteor } from 'meteor/meteor';
import { Template } from 'meteor/templating';
import { ReactiveVar } from 'meteor/reactive-var';
import { Tracker } from 'meteor/tracker';
Expand Down Expand Up @@ -27,8 +28,8 @@ export const EmojiPicker = {
}
this.initiated = true;

this.recent = window.localStorage.getItem('emoji.recent') ? window.localStorage.getItem('emoji.recent').split(',') : [];
this.tone = window.localStorage.getItem('emoji.tone') || 0;
this.recent = Meteor._localStorage.getItem('emoji.recent') ? Meteor._localStorage.getItem('emoji.recent').split(',') : [];
this.tone = Meteor._localStorage.getItem('emoji.tone') || 0;

Blaze.render(Template.emojiPicker, document.body);

Expand All @@ -55,7 +56,7 @@ export const EmojiPicker = {
},
setTone(tone) {
this.tone = tone;
window.localStorage.setItem('emoji.tone', tone);
Meteor._localStorage.setItem('emoji.tone', tone);
},
getTone() {
return this.tone;
Expand Down Expand Up @@ -130,7 +131,7 @@ export const EmojiPicker = {

updatePositions = true;

window.localStorage.setItem('emoji.recent', this.recent);
Meteor._localStorage.setItem('emoji.recent', this.recent);
emoji.packages.base.emojisByCategory.recent = this.recent;
this.updateRecent('recent');
},
Expand All @@ -140,7 +141,7 @@ export const EmojiPicker = {
return;
}
this.recent.splice(pos, 1);
window.localStorage.setItem('emoji.recent', this.recent);
Meteor._localStorage.setItem('emoji.recent', this.recent);
},
updateRecent(category) {
updateRecentEmoji(category);
Expand Down
2 changes: 1 addition & 1 deletion app/oauth2-server-config/client/oauth/oauth2-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Template.authorize.onCreated(function() {

Template.authorize.helpers({
getToken() {
return localStorage.getItem(Accounts.LOGIN_TOKEN_KEY);
return Meteor._localStorage.getItem(Accounts.LOGIN_TOKEN_KEY);
},
getClient() {
return ChatOAuthApps.findOne();
Expand Down
6 changes: 3 additions & 3 deletions app/setup-wizard/client/final.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Users } from '../../models';
import { hasRole } from '../../authorization';

Template.setupWizardFinal.onCreated(function() {
const isSetupWizardDone = localStorage.getItem('wizardFinal');
const isSetupWizardDone = Meteor._localStorage.getItem('wizardFinal');
if (isSetupWizardDone === null) {
FlowRouter.go('setup-wizard');
}
Expand Down Expand Up @@ -44,8 +44,8 @@ Template.setupWizardFinal.onRendered(function() {
Template.setupWizardFinal.events({
'click .js-finish'() {
settings.set('Show_Setup_Wizard', 'completed', function() {
localStorage.removeItem('wizard');
localStorage.removeItem('wizardFinal');
Meteor._localStorage.removeItem('wizard');
Meteor._localStorage.removeItem('wizardFinal');
FlowRouter.go('home');
});
},
Expand Down
10 changes: 5 additions & 5 deletions app/setup-wizard/client/setupWizard.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ Template.setupWizard.onCreated(async function() {
this.wizardSettings = new ReactiveVar([]);
this.allowStandaloneServer = new ReactiveVar(false);

if (localStorage.getItem('wizardFinal')) {
if (Meteor._localStorage.getItem('wizardFinal')) {
return FlowRouter.go('setup-wizard-final');
}

const jsonString = localStorage.getItem('wizard');
const jsonString = Meteor._localStorage.getItem('wizard');
const state = (jsonString && JSON.parse(jsonString)) || statusDefault;
this.state.set(state);

Expand All @@ -132,7 +132,7 @@ Template.setupWizard.onCreated(async function() {
this.autorun(() => {
const state = this.state.all();
state['registration-pass'] = '';
localStorage.setItem('wizard', JSON.stringify(state));
Meteor._localStorage.setItem('wizard', JSON.stringify(state));
});

this.autorun(async (c) => {
Expand Down Expand Up @@ -196,8 +196,8 @@ Template.setupWizard.events({
}
case 4: {
persistSettings(t.state.all(), () => {
localStorage.removeItem('wizard');
localStorage.setItem('wizardFinal', true);
Meteor._localStorage.removeItem('wizard');
Meteor._localStorage.setItem('wizardFinal', true);

if (t.state.get('registerServer')) {
Meteor.call('cloud:registerWorkspace', (error) => {
Expand Down
10 changes: 5 additions & 5 deletions app/ui-account/client/accountPreferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,11 @@ Template.accountPreferences.onCreated(function() {
});

this.clearForm = function() {
this.find('#language').value = localStorage.getItem('userLanguage');
this.find('#language').value = Meteor._localStorage.getItem('userLanguage');
};

this.shouldUpdateLocalStorageSetting = function(setting, newValue) {
return localStorage.getItem(setting) !== newValue;
return Meteor._localStorage.getItem(setting) !== newValue;
};

this.save = function() {
Expand Down Expand Up @@ -190,22 +190,22 @@ Template.accountPreferences.onCreated(function() {

const selectedLanguage = $('#language').val();
if (this.shouldUpdateLocalStorageSetting('userLanguage', selectedLanguage)) {
localStorage.setItem('userLanguage', selectedLanguage);
Meteor._localStorage.setItem('userLanguage', selectedLanguage);
data.language = selectedLanguage;
reload = true;
}

const enableAutoAway = JSON.parse($('#enableAutoAway').find('input:checked').val());
data.enableAutoAway = enableAutoAway;
if (this.shouldUpdateLocalStorageSetting('enableAutoAway', enableAutoAway)) {
localStorage.setItem('enableAutoAway', enableAutoAway);
Meteor._localStorage.setItem('enableAutoAway', enableAutoAway);
reload = true;
}

const idleTimeLimit = $('input[name=idleTimeLimit]').val() === '' ? settings.get('Accounts_Default_User_Preferences_idleTimeLimit') : parseInt($('input[name=idleTimeLimit]').val());
data.idleTimeLimit = idleTimeLimit;
if (this.shouldUpdateLocalStorageSetting('idleTimeLimit', idleTimeLimit)) {
localStorage.setItem('idleTimeLimit', idleTimeLimit);
Meteor._localStorage.setItem('idleTimeLimit', idleTimeLimit);
reload = true;
}

Expand Down
3 changes: 2 additions & 1 deletion app/ui-login/client/login/footer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ReactiveVar } from 'meteor/reactive-var';
import { Meteor } from 'meteor/meteor';
import { Template } from 'meteor/templating';
import { TAPi18n } from 'meteor/rocketchat:tap-i18n';

Expand All @@ -22,7 +23,7 @@ Template.loginFooter.onCreated(function() {
}
};

const currentLanguage = localStorage.getItem('userLanguage');
const currentLanguage = Meteor._localStorage.getItem('userLanguage');
this.suggestAnotherLanguageFor(currentLanguage);
});

Expand Down
18 changes: 12 additions & 6 deletions app/ui-master/server/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,19 @@ settings.get('Accounts_ForgetUserSessionOnWindowClose', (key, value) => {
if (value) {
Inject.rawModHtml(key, (html) => {
const script = `
<script>
if (Meteor._localStorage._data === undefined && window.sessionStorage) {
Meteor._localStorage = window.sessionStorage;
}
</script>
<script>
window.addEventListener('load', function() {
if (window.localStorage) {
Object.keys(window.localStorage).forEach(function(key) {
window.sessionStorage.setItem(key, window.localStorage.getItem(key));
});
window.localStorage.clear();
Meteor._localStorage = window.sessionStorage;
}
});
</script>
`;
return html.replace(/<\/body>/, `${ script }\n</body>`);
return html + script;
});
} else {
Inject.rawModHtml(key, (html) => html);
Expand Down
4 changes: 3 additions & 1 deletion app/ui-utils/client/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Meteor } from 'meteor/meteor';

const url = new URL(window.location);
const keys = new Set();
export const getConfig = (key) => {
keys.add(key);
return url.searchParams.get(key) || localStorage.getItem(`rc-config-${ key }`);
return url.searchParams.get(key) || Meteor._localStorage.getItem(`rc-config-${ key }`);
};
3 changes: 2 additions & 1 deletion app/ui-utils/client/lib/menu.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Session } from 'meteor/session';
import { Meteor } from 'meteor/meteor';
import _ from 'underscore';
import EventEmitter from 'wolfy87-eventemitter';

Expand Down Expand Up @@ -42,7 +43,7 @@ export const menu = new class extends EventEmitter {
}

get isRtl() {
return isRtl(localStorage.getItem('userLanguage'));
return isRtl(Meteor._localStorage.getItem('userLanguage'));
}

touchstart(e) {
Expand Down
5 changes: 3 additions & 2 deletions app/ui-utils/lib/Message.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { TAPi18n } from 'meteor/rocketchat:tap-i18n';
import { Meteor } from 'meteor/meteor';
import s from 'underscore.string';

import { MessageTypes } from './MessageTypes';
Expand All @@ -14,8 +15,8 @@ export const Message = {
// Render message
return;
} if (messageType.message) {
if (!language && typeof localStorage !== 'undefined') {
language = localStorage.getItem('userLanguage');
if (!language) {
language = Meteor._localStorage.getItem('userLanguage');
}
const data = (typeof messageType.data === 'function' && messageType.data(msg)) || {};
return TAPi18n.__(messageType.message, data, language);
Expand Down
8 changes: 4 additions & 4 deletions app/ui/client/lib/chatMessages.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ import { emoji } from '../../../emoji/client';
const messageBoxState = {
saveValue: _.debounce(({ rid, tmid }, value) => {
const key = ['messagebox', rid, tmid].filter(Boolean).join('_');
value ? localStorage.setItem(key, value) : localStorage.removeItem(key);
value ? Meteor._localStorage.setItem(key, value) : Meteor._localStorage.removeItem(key);
}, 1000),

restoreValue: ({ rid, tmid }) => {
const key = ['messagebox', rid, tmid].filter(Boolean).join('_');
return localStorage.getItem(key);
return Meteor._localStorage.getItem(key);
},

restore: ({ rid, tmid }, input) => {
Expand All @@ -57,9 +57,9 @@ const messageBoxState = {
},

purgeAll: () => {
Object.keys(localStorage)
Object.keys(Meteor._localStorage)
.filter((key) => key.indexOf('messagebox_') === 0)
.forEach((key) => localStorage.removeItem(key));
.forEach((key) => Meteor._localStorage.removeItem(key));
},
};

Expand Down
Loading