Skip to content
This repository has been archived by the owner on Jan 25, 2025. It is now read-only.

feat: handle session persistence for manifest version 3 #9

Merged
merged 7 commits into from
Jan 14, 2023
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
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ The browser plugin for [Keyspace](https://keyspace.cloud) - A secure self-custod
`yarn`

### Build
Build for manifest version 2 with `yarn build`
Build webapp with `yarn build`

Build for manifest version 3 with `yarn v3build`
Build for manifest version 2 with `yarn build:mv2`

Build for manifest version 3 with `yarn build:mv3`

### Rebuild icon manifest

Expand Down
31 changes: 21 additions & 10 deletions config-overrides.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
const webpack = require('webpack')
const CopyWebpackPlugin = require("copy-webpack-plugin");
const manifestVersion = process.env.MANIFEST_VERSION == 3 ? 3 : 2
const CopyWebpackPlugin = require('copy-webpack-plugin')

console.log(`Building for manifest version ${manifestVersion}`)
const manifestVersion = Number(process.env.MANIFEST_VERSION)
const copyPluginOptions = []

if (manifestVersion) {
console.log(`Building for manifest version ${manifestVersion}`)
if (manifestVersion === 2) {
copyPluginOptions.push('./src/manifest.json')
} else
copyPluginOptions.push({
from: './src/manifest.v3.json',
to: 'manifest.json',
})
}

module.exports = function override(config, env) {
config.resolve.fallback = {
Expand All @@ -23,13 +34,6 @@ module.exports = function override(config, env) {
new webpack.optimize.AggressiveSplittingPlugin({
minSize: 20000,
maxSize: 50000,
}),
new CopyWebpackPlugin({
patterns: [
manifestVersion == 3
? { from: './src/manifest.v3.json', to: 'manifest.json' }
: './src/manifest.json',
],
})
)
config.module.rules.push({
Expand All @@ -39,5 +43,12 @@ module.exports = function override(config, env) {
},
})

if (copyPluginOptions.length)
config.plugins.push(
new CopyWebpackPlugin({
patterns: copyPluginOptions,
})
)

return config
}
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "keyspace-plugin",
"version": "1.0.2",
"version": "1.0.3",
"private": false,
"dependencies": {
"@chakra-ui/icons": "^2.0.0",
Expand Down Expand Up @@ -46,7 +46,8 @@
"scripts": {
"start": "react-app-rewired start",
"build": "react-app-rewired build",
"v3build": "cross-env MANIFEST_VERSION=3 react-app-rewired build",
"build:mv2": "cross-env MANIFEST_VERSION=2 react-app-rewired build",
"build:mv3": "cross-env MANIFEST_VERSION=3 react-app-rewired build",
"build-icons": "node build-icons.js",
"test": "react-app-rewired test",
"eject": "react-scripts eject"
Expand Down
33 changes: 22 additions & 11 deletions src/components/App/Layout/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { useAppSelector, useAppDispatch } from '../../../store/hooks'
import { removeSession } from '../../../store/reducers/auth'
import { View } from '../../../types'
import { Logo } from '../../../Logo'
import { manifestVersion } from '../../../utils/browser'

interface SidebarProps {
toggleSidebar?: Function
Expand All @@ -30,18 +31,28 @@ export const Sidebar = (props: SidebarProps) => {
const dispatch = useAppDispatch()

const endSession = () => {
try {
chrome.runtime.sendMessage(
{ cmd: 'removeSession' },
function (response) {
console.log(
`message from background: ${JSON.stringify(response)}`
)
}
)
} catch (error) {
console.log('Error syncing with background script:', error)
if (manifestVersion() === 2) {
try {
chrome.runtime.sendMessage(
{ cmd: 'removeSession' },
function (response) {
console.log(
`message from background: ${JSON.stringify(response)}`
)
}
)
} catch (error) {
console.log('Error syncing with background script:', error)
}
}
else if(manifestVersion() === 3) {
try {
chrome.storage.session.clear()
} catch(error) {
console.log('Error clearing session storage', error)
}
}

dispatch(removeSession())
}

Expand Down
129 changes: 86 additions & 43 deletions src/components/Authentication/Keyroute.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { addSession } from '../../store/reducers/auth'
import { FiAlertTriangle, FiRefreshCcw } from 'react-icons/fi'
import { PopoutButton } from '../Common/PopoutButton'
import { Logo } from '../../Logo'
import { manifestVersion } from '../../utils/browser'

export const Keyroute = () => {
const QR_REFRESH_INTERVAL = 60 //seconds
Expand Down Expand Up @@ -83,32 +84,60 @@ export const Keyroute = () => {
* Sends a 'getSession' message to the background script, and restores the user session if possible
*/
useEffect(() => {
try {
chrome.runtime.sendMessage(
{ cmd: 'getSession' },
function (response) {
if (response.result === 'success') {
const decodedSessionData = {
email: response.message.email,
token: response.message.token,
keyring: {
publicKey: hex2buf(
response.message.keyring.publicKey
),
privateKey: hex2buf(
response.message.keyring.privateKey
),
symmetricKey: hex2buf(
response.message.keyring.symmetricKey
),
},
const getSessionFromBackgroundScript = () => {
try {
chrome.runtime.sendMessage(
{ cmd: 'getSession' },
function (response) {
if (response.result === 'success') {
const decodedSessionData = {
email: response.message.email,
token: response.message.token,
keyring: {
publicKey: hex2buf(
response.message.keyring.publicKey
),
privateKey: hex2buf(
response.message.keyring.privateKey
),
symmetricKey: hex2buf(
response.message.keyring.symmetricKey
),
},
}
dispatch(addSession(decodedSessionData))
}
dispatch(addSession(decodedSessionData))
}
)
} catch (error) {
console.log('Error syncing with background script:', error)
}
}

const getSessionFromStorage = async () => {
try {
const storedData = await chrome.storage.session.get('session')
if (storedData !== undefined && storedData !== null) {
const { session } = storedData
const decodedSessionData = {
email: session.email,
token: session.token,
keyring: {
publicKey: hex2buf(session.keyring.publicKey),
privateKey: hex2buf(session.keyring.privateKey),
symmetricKey: hex2buf(session.keyring.symmetricKey),
},
}
dispatch(addSession(decodedSessionData))
}
)
} catch (error) {
console.log('Error syncing with background script:', error)
} catch (error) {
console.log('Error getting stored session', error)
}
}
if (manifestVersion() === 2) {
getSessionFromBackgroundScript()
} else if (manifestVersion() === 3) {
getSessionFromStorage()
}
}, [dispatch])

Expand Down Expand Up @@ -153,7 +182,7 @@ export const Keyroute = () => {
useEffect(() => {
const initSession = async (payload: KeyroutePayload) => {
if (keypair) {
keyreouteLogin(payload, keypair).then((session) => {
keyreouteLogin(payload, keypair).then(async (session) => {
if (session.email !== undefined) {
toast({
title: 'Logged in!',
Expand Down Expand Up @@ -182,25 +211,39 @@ export const Keyroute = () => {
),
},
}
try {
chrome.runtime.sendMessage(
{
message: encodedSessionData,
cmd: 'saveSession',
},
function (response) {
console.log(
`message from background: ${JSON.stringify(
response
)}`
)
}
)
} catch (error) {
console.log(
'Error syncing with background script:',
error
)
if (manifestVersion() === 2) {
try {
chrome.runtime.sendMessage(
{
message: encodedSessionData,
cmd: 'saveSession',
},
function (response) {
console.log(
`message from background: ${JSON.stringify(
response
)}`
)
}
)
} catch (error) {
console.log(
'Error syncing with background script:',
error
)
}
} else if(manifestVersion() === 3) {
try {
await chrome.storage.session.set({
session: encodedSessionData,
})
console.log('saved session to storage')
} catch (error) {
console.log(
'Error saving session to storage',
error
)
}
}

dispatch(addSession(session))
Expand Down
8 changes: 2 additions & 6 deletions src/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name" :"Keyspace",
"description": "Keyspace wallet browser add-on",
"version": "1.0.2",
"version": "1.0.3",
"manifest_version": 2,
"icons": {
"16": "./images/favicon-16x16.png",
Expand All @@ -25,16 +25,12 @@
"permissions": [
"tabs",
"activeTab",
"contextMenus",
"storage",
"unlimitedStorage",
"clipboardRead",
"clipboardWrite",
"idle",
"http://*/*",
"https://*/*",
"webRequest",
"webRequestBlocking"
"https://*/*"
],
"commands": {
"_execute_browser_action": {
Expand Down
7 changes: 2 additions & 5 deletions src/manifest.v3.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name" :"Keyspace",
"description": "Keyspace wallet browser add-on",
"version": "1.0.2",
"version": "1.0.3",
"manifest_version": 3,
"icons": {
"16": "./images/favicon-16x16.png",
Expand All @@ -24,15 +24,12 @@
"permissions": [
"tabs",
"activeTab",
"contextMenus",
"storage",
"unlimitedStorage",
"clipboardRead",
"clipboardWrite",
"idle",
"http://*/*",
"https://*/*",
"webRequest"
"https://*/*"
],
"commands": {
"_execute_browser_action": {
Expand Down
13 changes: 13 additions & 0 deletions src/utils/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,16 @@ export const tabsQuery = (
}
})
}

/**
* Returns the manifest version
*
* @returns {2 | 3 | null}
*/
export const manifestVersion = () => {
try {
return chrome.runtime.getManifest().manifest_version;
} catch(e) {
return null
}
}