diff --git a/libraries/deno-commons-mod.ts b/libraries/deno-commons-mod.ts index 7cf1ed0..a211414 100644 --- a/libraries/deno-commons-mod.ts +++ b/libraries/deno-commons-mod.ts @@ -377,6 +377,53 @@ export function encodeBase64Url(input: ArrayBufferLike): string { ); } +interface GetKeyRingResult { + service: string; + user: string; + password: string; +} + +export function getKeyRingPassword( + service: string, + user: string, +): string | null { + const command = new Deno.Command("keyring.rs", { + args: ["-g", "--json", "-S", service, "-U", user], + }); + const { code, stdout, stderr } = command.outputSync(); + const stdoutString = new TextDecoder().decode(stdout); + const stderrString = new TextDecoder().decode(stderr); + if (code != 0) { + if (stderrString && stderrString.includes("Error: NoEntry")) { + return null; + } + throw new Error( + `keyring.rs -g failed, code: ${code}, stdout: ${stdoutString}, stderr: ${stderrString}`, + ); + } + const result = JSON.parse(stdoutString) as GetKeyRingResult; + return result.password; +} + +export function setKeyRingPassword( + service: string, + user: string, + password: string, +): void { + const command = new Deno.Command("keyring.rs", { + args: ["-s", "-S", service, "-U", user, "-P", password], + }); + const { code, stdout, stderr } = command.outputSync(); + const stdoutString = new TextDecoder().decode(stdout); + const stderrString = new TextDecoder().decode(stderr); + if (code != 0) { + throw new Error( + `keyring.rs -s failed, code: ${code}, stdout: ${stdoutString}, stderr: ${stderrString}`, + ); + } + return; +} + Deno.test("isOn", () => { assertEquals(false, isOn(undefined)); assertEquals(false, isOn("")); @@ -456,3 +503,11 @@ Deno.test("base64Url", () => { ), ); }); + +Deno.test("test-key-ring-rs", () => { + setKeyRingPassword("test-service", "test-user", "test-password"); + assertEquals( + "test-password", + getKeyRingPassword("test-service", "test-user"), + ); +}); diff --git a/libraries/deno-teencrypt-mod.ts b/libraries/deno-teencrypt-mod.ts index 8f687e1..4937682 100644 --- a/libraries/deno-teencrypt-mod.ts +++ b/libraries/deno-teencrypt-mod.ts @@ -3,7 +3,7 @@ import { encodeBase64Url, hexStringToUint8Array, resolveFilename, -} from "https://global.hatter.ink/script/get/@9/deno-commons-mod.ts"; +} from "https://global.hatter.ink/script/get/@10/deno-commons-mod.ts"; import {getRandomValues} from "node:crypto"; import {assertEquals} from "jsr:@std/assert";