|
| 1 | +import { |
| 2 | + Input, |
| 3 | + Select, |
| 4 | +} from "https://deno.land/x/[email protected]/prompt/mod.ts"; |
| 5 | +import { Command } from "https://deno.land/x/[email protected]/command/mod.ts"; |
| 6 | +import { Daemon } from "./daemon.ts"; |
| 7 | +import { ApplePasswordManager } from "./client.ts"; |
| 8 | +import { readBigInt, toBase64 } from "./utils.ts"; |
| 9 | +import { Buffer } from "node:buffer"; |
| 10 | +import { |
| 11 | + type Payload, |
| 12 | + type RenamedPasswordEntry, |
| 13 | + type TOTPEntry, |
| 14 | +} from "./types.ts"; |
| 15 | + |
| 16 | +const PrintEntries = (payload: Payload) => { |
| 17 | + const entries = payload.Entries.map((entry) => { |
| 18 | + if ("USR" in entry) { |
| 19 | + return { |
| 20 | + username: entry.USR, |
| 21 | + domain: entry.sites[0], |
| 22 | + password: entry.PWD || "Not Included", |
| 23 | + } as RenamedPasswordEntry; |
| 24 | + } else if ("code" in entry) { |
| 25 | + return { |
| 26 | + username: entry.username, |
| 27 | + domain: entry.domain, |
| 28 | + code: entry.code || "Not Included", |
| 29 | + } as TOTPEntry; |
| 30 | + } |
| 31 | + }); |
| 32 | + console.log(JSON.stringify(entries)); |
| 33 | +}; |
| 34 | + |
| 35 | +const client = new ApplePasswordManager(); |
| 36 | + |
| 37 | +const otp = new Command() |
| 38 | + .description("Interactively list accounts/OTPs.") |
| 39 | + .action(async () => { |
| 40 | + const action: string = await Select.prompt({ |
| 41 | + message: "Choose an action: ", |
| 42 | + options: ["list OTPs", "get OTPs"], |
| 43 | + }); |
| 44 | + const url = await Input.prompt({ |
| 45 | + message: "Enter URL: ", |
| 46 | + }); |
| 47 | + if (action === "list OTPs") { |
| 48 | + PrintEntries(await client.listOTPForURL(url)); |
| 49 | + } else if (action === "get OTPs") { |
| 50 | + PrintEntries(await client.getOTPForURL(url)); |
| 51 | + } |
| 52 | + }) |
| 53 | + .command("get", "Get a OTP for a website.") |
| 54 | + .arguments("<url:string>") |
| 55 | + .action(async (_, url: string) => { |
| 56 | + if (!url) { |
| 57 | + throw new Error("Missing required argument 'url'."); |
| 58 | + } |
| 59 | + PrintEntries(await client.getOTPForURL(url)); |
| 60 | + }) |
| 61 | + .command("list", "List available OTPs for a website.") |
| 62 | + .arguments("<url:string>") |
| 63 | + .action(async (_, url: string) => { |
| 64 | + if (!url) { |
| 65 | + throw new Error("Missing required argument 'url'."); |
| 66 | + } |
| 67 | + PrintEntries(await client.listOTPForURL(url)); |
| 68 | + }); |
| 69 | + |
| 70 | +const pw = new Command() |
| 71 | + .description("Interactively list accounts/passwords.") |
| 72 | + .action(async () => { |
| 73 | + const action: string = await Select.prompt({ |
| 74 | + message: "Choose an action: ", |
| 75 | + options: ["list accounts", "get password"], |
| 76 | + }); |
| 77 | + const url = await Input.prompt({ |
| 78 | + message: "Enter URL: ", |
| 79 | + }); |
| 80 | + if (action === "list accounts") { |
| 81 | + PrintEntries(await client.getLoginNamesForURL(url)); |
| 82 | + } else if (action === "get password") { |
| 83 | + PrintEntries(await client.getPasswordForURL(url)); |
| 84 | + } |
| 85 | + }) |
| 86 | + .command("get", "Get a password for a website.") |
| 87 | + .arguments("<url:string> [username:string]") |
| 88 | + .action(async (_, url: string, username?: string) => { |
| 89 | + if (!url) { |
| 90 | + throw new Error("Missing required argument 'url'."); |
| 91 | + } |
| 92 | + PrintEntries(await client.getPasswordForURL(url, username)); |
| 93 | + }) |
| 94 | + .command("list", "List available accounts for a website.") |
| 95 | + .arguments("<url:string>") |
| 96 | + .action(async (_, url: string) => { |
| 97 | + if (!url) { |
| 98 | + throw new Error("Missing required argument 'url'."); |
| 99 | + } |
| 100 | + PrintEntries(await client.getLoginNamesForURL(url)); |
| 101 | + }); |
| 102 | + |
| 103 | +const daemon = new Command() |
| 104 | + .description("Start the daemon.") |
| 105 | + .option("-p, --port <port:number>", "Port to listen on.", { default: 10000 }) |
| 106 | + .action(Daemon); |
| 107 | + |
| 108 | +const auth = new Command() |
| 109 | + .description("Authenticate CLI with daemon.") |
| 110 | + .action(async () => { |
| 111 | + await client.requestChallenge(); |
| 112 | + const password = await Input.prompt({ |
| 113 | + message: "Enter PIN: ", |
| 114 | + minLength: 6, |
| 115 | + maxLength: 6, |
| 116 | + }); |
| 117 | + await client.verifyChallenge(password); |
| 118 | + }) |
| 119 | + .command("request", "Request a challenge from the daemon.") |
| 120 | + .action(async () => { |
| 121 | + await client.requestChallenge(); |
| 122 | + console.log(JSON.stringify({ |
| 123 | + salt: toBase64(client.session.salt), |
| 124 | + serverKey: toBase64(client.session.serverPublicKey), |
| 125 | + username: client.session.username, |
| 126 | + clientKey: toBase64(client.session.clientPrivateKey), |
| 127 | + })); |
| 128 | + }) |
| 129 | + .command("response", "Respond to a challenge from the daemon.") |
| 130 | + .option("-p, --pin <pin>", "challenge-response pin.", { required: true }) |
| 131 | + .option("-s, --salt <salt>", "request salt.", { required: true }) |
| 132 | + .option("-sk, --serverKey <serverKey>", "server public key.", { |
| 133 | + required: true, |
| 134 | + }) |
| 135 | + .option("-ck, --clientKey <clientKey>", "client public key.", { |
| 136 | + required: true, |
| 137 | + }) |
| 138 | + .option("-u, --username <username>", "client username.", { required: true }) |
| 139 | + .action(async (options) => { |
| 140 | + const { serverKey, salt, username, clientKey, pin } = options; |
| 141 | + const serverPublicKey = readBigInt(Buffer.from(serverKey, "base64")); |
| 142 | + const clientPrivateKey = readBigInt(Buffer.from(clientKey, "base64")); |
| 143 | + const saltResponse = readBigInt(Buffer.from(salt, "base64")); |
| 144 | + client.session.username = username; |
| 145 | + client.session.clientPrivateKey = clientPrivateKey; |
| 146 | + client.session.salt = saltResponse; |
| 147 | + client.session.serverPublicKey = serverPublicKey; |
| 148 | + await client.verifyChallenge(pin); |
| 149 | + console.log(JSON.stringify({ status: "success" })); |
| 150 | + }); |
| 151 | + |
| 152 | +await new Command() |
| 153 | + .name("apw-cli") |
| 154 | + .version("0.1.0") |
| 155 | + .description("🔑 a CLI for Apple Passwords 🔒") |
| 156 | + .command("auth", auth) |
| 157 | + .command("pw", pw) |
| 158 | + .command("otp", otp) |
| 159 | + .command("start", daemon) |
| 160 | + .parse(Deno.args) |
| 161 | + .finally(() => Deno.exit()); |
0 commit comments