Skip to content

Commit

Permalink
Scope approvals to documentId (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
turbocrime authored Jun 24, 2024
1 parent 93cc701 commit 2287a89
Show file tree
Hide file tree
Showing 22 changed files with 1,544 additions and 1,195 deletions.
5 changes: 5 additions & 0 deletions .changeset/giant-otters-whisper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'chrome-extension': patch
---

scope init to documentId
225 changes: 0 additions & 225 deletions apps/extension/src/approve-origin.test.ts

This file was deleted.

77 changes: 0 additions & 77 deletions apps/extension/src/approve-origin.ts

This file was deleted.

38 changes: 33 additions & 5 deletions apps/extension/src/content-scripts/injected-connection-port.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,44 @@
import { PraxMessage } from './message-event';
import { CRSessionClient } from '@penumbra-zone/transport-chrome/session-client';
import { PraxConnection } from '../message/prax';
import { PraxMessage } from './message-event';

// this script will init the page session upon instruction from an extension
// worker. init does not arrive in direct response to an emitted message, it is
// independent.

// handler to listen for init command for this document
const initOnce = (
req: unknown,
// content script message handlers are activated only by another
// script in the same extension using chrome.tabs.sendMessage
sender: chrome.runtime.MessageSender,
// this handler will only ever send an empty response
emptyResponse: (no?: never) => void,
) => {
if (req !== PraxConnection.Init) {
// boolean return in handlers signals intent to respond
return false;
}

// this inits the session client that transports messages on the DOM channel through the Chrome runtime
const initOnce = (req: unknown, _sender: chrome.runtime.MessageSender, respond: () => void) => {
if (req !== PraxConnection.Init) return false;
chrome.runtime.onMessage.removeListener(initOnce);

if (sender.id !== PRAX) {
throw new Error(`Unexpected sender ${sender.id}`);
}

// create session, post port to window where the injected global can catch it.
const port = CRSessionClient.init(PRAX);
window.postMessage({ [PRAX]: port } satisfies PraxMessage<MessagePort>, '/', [port]);
respond();

// handler is done
emptyResponse();

// boolean return in handlers signals intent to respond
return true;
};

// attach handler
chrome.runtime.onMessage.addListener(initOnce);

// announce
void chrome.runtime.sendMessage(PraxConnection.Init);
17 changes: 5 additions & 12 deletions apps/extension/src/content-scripts/injected-penumbra-global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,12 @@ const requestResponseListener = (msg: MessageEvent<unknown>) => {
if (msg.origin === window.origin) {
if (isPraxFailureMessageEvent(msg)) {
// @ts-expect-error - ts can't understand the injected string
const status = msg.data[PRAX] as PraxConnection;
const status: unknown = msg.data[PRAX];
const failure = new Error('Connection request failed');
switch (status) {
case PraxConnection.Denied:
failure.cause = PenumbraRequestFailure.Denied;
break;
case PraxConnection.NeedsLogin:
failure.cause = PenumbraRequestFailure.NeedsLogin;
break;
default:
failure.cause = 'Unknown';
break;
}
failure.cause =
typeof status === 'string' && status in PenumbraRequestFailure
? status
: `Unknown failure: ${String(status)}`;
request.reject(failure);
}
}
Expand Down
Loading

0 comments on commit 2287a89

Please sign in to comment.