Skip to content

Commit

Permalink
take app authentication token from launcher env if available, removed…
Browse files Browse the repository at this point in the history
… deprecated framework specific zome call signing logic
  • Loading branch information
matthme committed Apr 26, 2024
1 parent 54e7845 commit 1a098c5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 119 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
"@bitgo/blake2b": "^3.2.4",
"@holochain/serialization": "^0.1.0-beta-rc.3",
"@msgpack/msgpack": "^2.8.0",
"@tauri-apps/api": "^1.4.0",
"emittery": "^1.0.1",
"isomorphic-ws": "^5.0.0",
"js-base64": "^3.7.5",
Expand Down
7 changes: 7 additions & 0 deletions src/api/app/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { UnsubscribeFunction } from "emittery";
import {
AgentPubKey,
AppAuthenticationToken,
AppInfo,
CapSecret,
CellId,
Expand All @@ -14,6 +15,7 @@ import {
Nonce256Bit,
RoleName,
Timestamp,
WebsocketConnectionOptions,
ZomeName,
} from "../../index.js";

Expand Down Expand Up @@ -282,3 +284,8 @@ export interface AppClient {
): Promise<DisableCloneCellResponse>;
networkInfo(args: AppNetworkInfoRequest): Promise<NetworkInfoResponse>;
}

export interface AppWebsocketConnectionOptions
extends WebsocketConnectionOptions {
token?: AppAuthenticationToken;
}
32 changes: 15 additions & 17 deletions src/api/app/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Emittery, { UnsubscribeFunction } from "emittery";
import { omit } from "lodash-es";

import { AgentPubKey, CellId, RoleName } from "../../types.js";
import { AppAuthenticationToken, AppInfo, CellType } from "../admin";
import { AppInfo, CellType } from "../admin";
import {
catchError,
DEFAULT_TIMEOUT,
Expand All @@ -13,7 +13,6 @@ import {
Requester,
requesterTransformer,
Transformer,
WebsocketConnectionOptions,
} from "../common.js";
import {
AppCallZomeRequest,
Expand All @@ -39,12 +38,11 @@ import {
EnableCloneCellResponse,
NetworkInfoRequest,
NetworkInfoResponse,
AppWebsocketConnectionOptions,
} from "./types.js";
import {
getHostZomeCallSigner,
getLauncherEnvironment,
signZomeCallElectron,
signZomeCallTauri,
} from "../../environments/launcher";
import { decode, encode } from "@msgpack/msgpack";
import {
Expand Down Expand Up @@ -161,10 +159,7 @@ export class AppWebsocket implements AppClient {
* @param options - {@link (WebsocketConnectionOptions:interface)}
* @returns A new instance of an AppWebsocket.
*/
static async connect(
token: AppAuthenticationToken,
options: WebsocketConnectionOptions = {}
) {
static async connect(options: AppWebsocketConnectionOptions = {}) {
// Check if we are in the launcher's environment, and if so, redirect the url to connect to
const env = getLauncherEnvironment();

Expand All @@ -180,7 +175,17 @@ export class AppWebsocket implements AppClient {
}

const client = await WsClient.connect(options.url, options.wsClientOptions);
await client.authenticate({ token });
if (env?.APP_INTERFACE_TOKEN) {
await client.authenticate({ token: env.APP_INTERFACE_TOKEN });
} else {
if (!options.token) {
throw new HolochainError(
"AppAuthenticationTokenMissing",
`unable to connect to Conductor API - no app authentication token provided.`
);
}
await client.authenticate({ token: options.token });
}

const appInfo = await (
this.requester(client, "app_info", DEFAULT_TIMEOUT) as Requester<
Expand Down Expand Up @@ -426,14 +431,7 @@ const callZomeTransform: Transformer<
if (hostSigner) {
return hostSigner.signZomeCall(request);
} else {
const env = getLauncherEnvironment();
if (!env) {
return signZomeCall(request);
}
if (env.FRAMEWORK === "electron") {
return signZomeCallElectron(request);
}
return signZomeCallTauri(request);
return signZomeCall(request);
}
},
output: (response) => decode(response),
Expand Down
103 changes: 2 additions & 101 deletions src/environments/launcher.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { encode } from "@msgpack/msgpack";
import { invoke } from "@tauri-apps/api/tauri";
import { CallZomeRequest } from "../api";
import { AppAuthenticationToken, CallZomeRequest } from "../api";
import { CallZomeRequestSigned, CallZomeRequestUnsigned } from "../api";
import { getNonceExpiration, randomNonce } from "../api";
import { InstalledAppId } from "../types.js";

export interface LauncherEnvironment {
APP_INTERFACE_PORT?: number;
ADMIN_INTERFACE_PORT?: number;
INSTALLED_APP_ID?: InstalledAppId;
FRAMEWORK?: "tauri" | "electron";
APP_INTERFACE_TOKEN?: AppAuthenticationToken;
}

export interface HostZomeCallSigner {
Expand Down Expand Up @@ -40,30 +37,6 @@ declare global {
}
}

type TauriByteArray = number[]; // Tauri requires a number array instead of a Uint8Array

interface CallZomeRequestSignedTauri
extends Omit<
CallZomeRequestSigned,
"cap_secret" | "cell_id" | "provenance" | "nonce"
> {
cell_id: [TauriByteArray, TauriByteArray];
provenance: TauriByteArray;
nonce: TauriByteArray;
expires_at: number;
}

interface CallZomeRequestUnsignedTauri
extends Omit<
CallZomeRequestUnsigned,
"cap_secret" | "cell_id" | "provenance" | "nonce"
> {
cell_id: [TauriByteArray, TauriByteArray];
provenance: TauriByteArray;
nonce: TauriByteArray;
expires_at: number;
}

interface CallZomeRequestSignedElectron
extends Omit<
CallZomeRequestSigned,
Expand Down Expand Up @@ -101,75 +74,3 @@ interface CallZomeRequestUnsignedElectron
nonce: Array<number>;
expiresAt: number;
}

export const signZomeCallTauri = async (request: CallZomeRequest) => {
const zomeCallUnsigned: CallZomeRequestUnsignedTauri = {
provenance: Array.from(request.provenance),
cell_id: [Array.from(request.cell_id[0]), Array.from(request.cell_id[1])],
zome_name: request.zome_name,
fn_name: request.fn_name,
payload: Array.from(encode(request.payload)),
nonce: Array.from(await randomNonce()),
expires_at: getNonceExpiration(),
};

const signedZomeCallTauri: CallZomeRequestSignedTauri = await invoke(
"sign_zome_call",
{ zomeCallUnsigned }
);

const signedZomeCall: CallZomeRequestSigned = {
provenance: Uint8Array.from(signedZomeCallTauri.provenance),
cap_secret: null,
cell_id: [
Uint8Array.from(signedZomeCallTauri.cell_id[0]),
Uint8Array.from(signedZomeCallTauri.cell_id[1]),
],
zome_name: signedZomeCallTauri.zome_name,
fn_name: signedZomeCallTauri.fn_name,
payload: Uint8Array.from(signedZomeCallTauri.payload),
signature: Uint8Array.from(signedZomeCallTauri.signature),
expires_at: signedZomeCallTauri.expires_at,
nonce: Uint8Array.from(signedZomeCallTauri.nonce),
};

return signedZomeCall;
};

export const signZomeCallElectron = async (request: CallZomeRequest) => {
if (!window.electronAPI) {
throw Error(
"Unable to signZomeCallElectron. window.electronAPI not defined"
);
}

const zomeCallUnsignedElectron: CallZomeRequestUnsignedElectron = {
provenance: Array.from(request.provenance),
cellId: [Array.from(request.cell_id[0]), Array.from(request.cell_id[1])],
zomeName: request.zome_name,
fnName: request.fn_name,
payload: Array.from(encode(request.payload)),
nonce: Array.from(await randomNonce()),
expiresAt: getNonceExpiration(),
};

const zomeCallSignedElectron: CallZomeRequestSignedElectron =
await window.electronAPI.signZomeCall(zomeCallUnsignedElectron);

const zomeCallSigned: CallZomeRequestSigned = {
provenance: Uint8Array.from(zomeCallSignedElectron.provenance),
cap_secret: null,
cell_id: [
Uint8Array.from(zomeCallSignedElectron.cellId[0]),
Uint8Array.from(zomeCallSignedElectron.cellId[1]),
],
zome_name: zomeCallSignedElectron.zomeName,
fn_name: zomeCallSignedElectron.fnName,
payload: Uint8Array.from(zomeCallSignedElectron.payload),
signature: Uint8Array.from(zomeCallSignedElectron.signature),
expires_at: zomeCallSignedElectron.expiresAt,
nonce: Uint8Array.from(zomeCallSignedElectron.nonce),
};

return zomeCallSigned;
};

0 comments on commit 1a098c5

Please sign in to comment.