Skip to content

Commit 2f6ddb1

Browse files
committed
feat(auth): show emoji hash
1 parent b7d5d95 commit 2f6ddb1

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

sources/commands/auth/index.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
decryptAppPairingToken,
88
generateAppPairingKeyPair,
99
} from "@/utils/appPairingCrypto";
10+
import { emojiHash } from "@/utils/emojiHash";
1011
import { openBrowser } from "@/utils/browser";
1112
import { renderQrCode } from "@/utils/qrCode";
1213

@@ -225,6 +226,7 @@ async function loginWithAppPairing(context: CommandContext): Promise<string> {
225226
const keyPair = generateAppPairingKeyPair();
226227
const publicKey = keyPair.publicKeyBase64;
227228
const secretKey = keyPair.secretKey;
229+
const emoji = formatEmojiHash(keyPair.publicKeyBytes);
228230

229231
const initial = await requestAppPairing(context.env, appId, publicKey);
230232

@@ -239,7 +241,7 @@ async function loginWithAppPairing(context: CommandContext): Promise<string> {
239241
const pairingUrl = buildPairingUrl(initial.requestId);
240242
const method = await selectAuthMethod();
241243

242-
await presentAppPairing(method, pairingUrl, initial.requestId);
244+
await presentAppPairing(method, pairingUrl, initial.requestId, emoji);
243245
console.log("Waiting for authorization...");
244246

245247
return await pollForAppToken({
@@ -264,10 +266,14 @@ async function selectAuthMethod(): Promise<DeviceAuthMethod> {
264266
async function presentAppPairing(
265267
method: DeviceAuthMethod,
266268
pairingUrl: string,
267-
requestId: string
269+
requestId: string,
270+
emojiHashValue: string | null
268271
): Promise<void> {
269272
console.log(`Pairing request: ${requestId}`);
270273
console.log(`Open this URL to approve the app: ${pairingUrl}`);
274+
if (emojiHashValue) {
275+
console.log(`Emoji hash: ${emojiHashValue}`);
276+
}
271277

272278
if (method === "browser") {
273279
const opened = await openBrowser(pairingUrl);
@@ -281,6 +287,14 @@ async function presentAppPairing(
281287
console.log(qrCode);
282288
}
283289

290+
function formatEmojiHash(publicKeyBytes: Uint8Array): string | null {
291+
const emojis = emojiHash(publicKeyBytes, 4);
292+
if (emojis.length === 0) {
293+
return null;
294+
}
295+
return emojis.join(" ");
296+
}
297+
284298
async function pollForAppToken(opts: {
285299
env: Environment;
286300
appId: string;

sources/utils/appPairingCrypto.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const APP_PAIRING_PUBLIC_KEY_SIZE = 32;
66

77
export type AppPairingKeyPair = {
88
publicKeyBase64: string;
9+
publicKeyBytes: Uint8Array;
910
secretKey: Uint8Array;
1011
};
1112

@@ -14,6 +15,7 @@ export function generateAppPairingKeyPair(): AppPairingKeyPair {
1415
const publicKeyBase64 = Buffer.from(keyPair.publicKey).toString("base64");
1516
return {
1617
publicKeyBase64,
18+
publicKeyBytes: keyPair.publicKey,
1719
secretKey: keyPair.secretKey,
1820
};
1921
}

0 commit comments

Comments
 (0)