Skip to content

Commit

Permalink
Overhaul of ContentScript. CsSwPort. Message Types.
Browse files Browse the repository at this point in the history
  • Loading branch information
edeykholt committed Jan 4, 2025
1 parent a5d935a commit 76f10fc
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 164 deletions.
24 changes: 12 additions & 12 deletions KeriAuth.BrowserExtension/wwwroot/scripts/es6/ExCsInterfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface ICsSwMsg {
}

// Message types from Page to CS, which may be then forwarded to the extension service-worker. Aka "EVENT_TYPE" in the polaris-web code."
export enum CsSwMsgType {
export enum CsSwMsgEnum {
POLARIS_SIGNIFY_EXTENSION = "signify-extension",
POLARIS_SIGNIFY_EXTENSION_CLIENT = "signify-extension-client",
POLARIS_CONFIGURE_VENDOR = "/signify/configure-vendor",
Expand All @@ -21,27 +21,27 @@ export enum CsSwMsgType {
POLARIS_GET_SESSION_INFO = "/signify/get-session-info",
POLARIS_CLEAR_SESSION = "/signify/clear-session",
POLARIS_CREATE_DATA_ATTESTATION = "/signify/credential/create/data-attestation",
POLARIS_GET_CREDENTIAL = "/signify/credential/get"
POLARIS_GET_CREDENTIAL = "/signify/credential/get",
PING = "ping"
}

// Message types from Extension to CS (and typically forward to Page and sometimes of type FooResponse)
export interface ISwCsMsg {
type: string // SwCsMsgType
type: string // SwCsMsgEnum // TODO P2 typof SwCsMsgEnum ?
requestId?: string // response to this requestId
payload?: object
error?: string
}

export enum SwCsMsgType {
HELLO = "hello",
export enum SwCsMsgEnum {
PONG = "pong",
CANCELED = "canceled",
REPLY = "/signify/reply",
FSW = "fromServiceWorker",
// SE = "signify-extension"
FSW = "fromServiceWorker"
}

export interface IExCsMsgHello extends ISwCsMsg {
type: SwCsMsgType.HELLO
export interface ISwCsMsgPong extends ISwCsMsg {
type: SwCsMsgEnum.PONG
}

// This IIdentifier is used in the context of responses from the extension service-worker, CS, to page
Expand Down Expand Up @@ -89,11 +89,11 @@ export interface ReplyMessageData<T = unknown> {
source?: string;
}

export const CsToPageMsgIndicator = "KeriAuthCs";
export const CsTabMsgTag = "KeriAuthCs";

// This interface helps shape ContentScript messages to tab
export interface KeriAuthToPolarisMessageData<T> extends Polaris.MessageData<T> {
source: typeof CsToPageMsgIndicator;
export interface CsTabMsgData<T> extends Polaris.MessageData<T> {
source: typeof CsTabMsgTag;
}

// Signing related types from signify-browser-extension config/types.ts. Here because we don't want dependencies on signify-browser-extension,
Expand Down
32 changes: 6 additions & 26 deletions KeriAuth.BrowserExtension/wwwroot/scripts/es6/SwAppInterop.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,9 @@
/// <reference types="chrome" />

import {
AuthorizeResultCredential,
AuthorizeArgs,
AuthorizeResultIdentifier,
AuthorizeResult,
SignDataArgs,
SignDataResultItem,
SignDataResult,
SignRequestArgs,
SignRequestResult,
ConfigureVendorArgs,
MessageData
} from "../types/polaris-web-client"
import * as PW from "../types/polaris-web-client"

import {
CsSwMsgType,
IExCsMsgHello,
SwCsMsgType,
ISwCsMsg,
ICsSwMsg,
CsToPageMsgIndicator,
KeriAuthToPolarisMessageData,
ISignin,
ICredential,
SwCsMsgEnum,
ReplyMessageData,
ApprovedSignRequest
} from "../es6/ExCsInterfaces.js";
Expand All @@ -47,7 +27,7 @@ export const SwAppInteropModule = {
port.onMessage.addListener((message) => {
console.log("SwAppInterop received port message: ", message);
// TODO P2 message types fromApp vs fromServiceWorker?
if (message && message.type === SwCsMsgType.FSW) {
if (message && message.type === SwCsMsgEnum.FSW) {
dotNetObjectReference.invokeMethodAsync('ReceiveMessage', message.data);
}
});
Expand All @@ -71,12 +51,12 @@ export const SwAppInteropModule = {
switch (payloadTypeName) {
case "CancelResult":
// TODO P2 AuthorizeResult type is the closest match to CancelResult at the moment.
const msgCancelResult = JSON.parse(jsonReplyMessageData) as ReplyMessageData<AuthorizeResult>;
const msgCancelResult = JSON.parse(jsonReplyMessageData) as ReplyMessageData<PW.AuthorizeResult>;
console.log("SwAppInteropModule.sendMessageToServiceWorker messageData3: ", msgCancelResult);
port.postMessage(msgCancelResult);
break;
case "AuthorizeResult":
const msgAuthorizeResult = JSON.parse(jsonReplyMessageData) as ReplyMessageData<AuthorizeResult>;
const msgAuthorizeResult = JSON.parse(jsonReplyMessageData) as ReplyMessageData<PW.AuthorizeResult>;
console.log("SwAppInteropModule.sendMessageToServiceWorker messageData2: ", msgAuthorizeResult);
port.postMessage(msgAuthorizeResult);
break;
Expand All @@ -86,7 +66,7 @@ export const SwAppInteropModule = {
port.postMessage(msgApprovedSignRequest);
break;
case "SignedRequestResult":
const msgSignRequestResult = JSON.parse(jsonReplyMessageData) as ReplyMessageData<SignRequestResult>;
const msgSignRequestResult = JSON.parse(jsonReplyMessageData) as ReplyMessageData<PW.SignRequestResult>;
console.log("SwAppInteropModule.sendMessageToServiceWorker messageData5: ", msgSignRequestResult);
port.postMessage(msgSignRequestResult);
break;
Expand Down
Loading

0 comments on commit 76f10fc

Please sign in to comment.