Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cors peer websocket preauth #510

Merged
merged 4 commits into from
Jan 9, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ export const Unsubscribe = (
}
};

export const Notify = async (command: WebsocketCommand | EstablishConnectionRequest) => {
export const Notify = async (command: WebsocketCommand) => {
if (!webSocketClient) throw new Error('No active websocket to message across');
if (isDebug) console.debug(`[WebsocketProvider] Send command (${JSON.stringify(command)})`);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ export type TypedConnectionNotification =
| ReactionNotification;

export interface WebsocketCommand {
command: string;
command: WebSocketCommands;
data: string;
}

type WebSocketCommands =
| 'establishConnectionRequest'
| 'processTransitInstructions'
| 'processInbox'
| 'ping'
| 'whoIsOnline';
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,6 @@ const ConnectSocket = async (
sharedSecret: activeSs,
});

// we need to preauth before we can connect
await directGuestClient
.createAxiosClient()
.post('/notify/peer/preauth', undefined, {
validateStatus: () => true,
})
.catch((error) => {
console.error({ error });
reject('[WebsocketProviderOverPeer] Preauth failed');
});

const url = `wss://${directGuestClient.getRoot().split('//')[1]}/api/guest/v1/notify/peer/ws`;

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
Expand All @@ -119,10 +108,13 @@ const ConnectSocket = async (
batchSize: 1,
};

NotifyOverPeer({
command: 'establishConnectionRequest',
data: JSON.stringify(establishConnectionRequest),
});
EstablishConnectionOverPeer(
{
command: 'establishConnectionRequest',
data: JSON.stringify(establishConnectionRequest),
},
tokenToConnectOverPeer.authenticationToken64
);
};

const setupPing = () => {
Expand Down Expand Up @@ -285,7 +277,26 @@ export const UnsubscribeOverPeer = (
}
};

export const NotifyOverPeer = async (command: WebsocketCommand | EstablishConnectionRequest) => {
const EstablishConnectionOverPeer = async (
command: WebsocketCommand,
clientAuthenticationToken64: string
) => {
if (!webSocketClient) throw new Error('No active websocket to message across');
if (isDebug)
console.debug(`[WebsocketProviderOverPeer] Send command (${JSON.stringify(command)})`);

const json = jsonStringify64(command);
const establishConnectionMessage = await encryptData(json, getRandomIv(), activeSs);

webSocketClient.send(
JSON.stringify({
sharedSecretEncryptedOptions: establishConnectionMessage,
clientAuthToken64: clientAuthenticationToken64,
})
);
};

export const NotifyOverPeer = async (command: WebsocketCommand) => {
if (!webSocketClient) throw new Error('No active websocket to message across');
if (isDebug)
console.debug(`[WebsocketProviderOverPeer] Send command (${JSON.stringify(command)})`);
Expand Down
Loading