39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
import {
|
|
getFetchAutoProxyInit,
|
|
} from "https://hatter.ink/script/fetch/library/deno-fetch-auto-proxy-mod.ts?202501191421";
|
|
|
|
export class SshKey {
|
|
algorithm: string;
|
|
material: string;
|
|
description?: string;
|
|
|
|
constructor(
|
|
algorithm: string,
|
|
material: string,
|
|
description: string | undefined,
|
|
) {
|
|
this.algorithm = algorithm;
|
|
this.material = material;
|
|
this.description = description;
|
|
}
|
|
|
|
static parseSshKey(key: string): SshKey {
|
|
const keyParts = key.split(/\s+/);
|
|
if (keyParts.length < 2 || keyParts.length > 3) {
|
|
throw `Bad SSH key format ${key}`;
|
|
}
|
|
return new SshKey(
|
|
keyParts[0],
|
|
keyParts[1],
|
|
(keyParts.length > 2) ? keyParts[2] : undefined,
|
|
);
|
|
}
|
|
}
|
|
|
|
export async function fetchKeys(username: string): Promise<Array<SshKey>> {
|
|
const url = `https://github.com/${username}.keys`;
|
|
const response = await fetch(url, getFetchAutoProxyInit());
|
|
const responseText = await response.text();
|
|
return responseText.trim().split("\n").map((k) => SshKey.parseSshKey(k));
|
|
}
|