Skip to content

Commit

Permalink
Add fix for "New Identity" extension behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
ruihildt committed Jun 15, 2023
1 parent d513a0b commit 6f0c1a6
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "mullvad-browser-extension",
"displayName": "Mullvad Browser Extension",
"version": "0.8.1",
"version": "0.8.2",
"description": "Improve your Mullvad VPN experience, in your browser.",
"private": true,
"engines": {
Expand Down
28 changes: 13 additions & 15 deletions src/background/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { onMessage } from 'webext-bridge/background';
import { addExtListeners } from '@/helpers/extensions';
import {
DataAccount,
getMullvadAccount,
initLetaLogin,
backgroundLetaLogin,
letaLogin,
letaLogout,
dailyLogin,
} from '@/helpers/leta';

// only on dev mode
Expand All @@ -18,8 +18,13 @@ if (import.meta.hot) {
// Add listeners on extension actions
addExtListeners();

// Mullvad Leta Auto Login on extension start
initLetaLogin();
// Autologin to Leta on startup
backgroundLetaLogin();

// "New Identity" doesn't restart the extensions, so we need a workaround.
// When a new window is created, we will automatically login again.
// See: https://gitlab.torproject.org/tpo/applications/tor-browser/-/issues/41833
browser.windows.onCreated.addListener(() => backgroundLetaLogin());

// Add cookie messaging listeners
onMessage<DataAccount>('leta-login', async ({ data }) => {
Expand All @@ -31,21 +36,14 @@ onMessage('leta-logout', () => {
letaLogout();
});

// Alarm to refresh Mullvad Leta auth cookie
// The cookie expires after 24h, so renewal is set for every 23h
browser.alarms.create('leta-cookie-refresh', {
// The cookie expires after 24h, so we use an alarm to relogin
// in case a user keeps the browser open for more than a day
browser.alarms.create('daily-login', {
delayInMinutes: 1380,
periodInMinutes: 1380,
});

const refreshCookie = async (alarm: browser.alarms.Alarm) => {
if (alarm.name === 'leta-cookie-refresh') {
const account = await getMullvadAccount();
await letaLogin(account);
}
};

browser.alarms.onAlarm.addListener(refreshCookie);
browser.alarms.onAlarm.addListener(dailyLogin);

// `browser.cookies` operations are only available in the background context
export const setCookie = async (cookie: browser.cookies._SetDetails) => {
Expand Down
8 changes: 7 additions & 1 deletion src/helpers/leta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,17 @@ export const letaLogout = async () => {
removeAuthCookie(isFPI);
};

export const initLetaLogin = async () => {
export const backgroundLetaLogin = async () => {
const mullvadAccount = await getMullvadAccount();
if (!mullvadAccount) {
throw new Error('No Mullvad VPN account found in extension storage.');
} else {
letaLogin(mullvadAccount);
}
};

export const dailyLogin = async (alarm: browser.alarms.Alarm) => {
if (alarm.name === 'daily-login') {
await backgroundLetaLogin();
}
};
4 changes: 2 additions & 2 deletions updates.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
}
},
{
"version": "0.8.0",
"update_link": "https://cdn.mullvad.net/browser-extension/0.8.0/mullvad-browser-extension-0.8.0.xpi",
"version": "0.8.2",
"update_link": "https://cdn.mullvad.net/browser-extension/0.8.2/mullvad-browser-extension-0.8.2.xpi",
"applications": {
"gecko": { "strict_min_version": "91.1.0" }
}
Expand Down

0 comments on commit 6f0c1a6

Please sign in to comment.