This commit is contained in:
+45
-11
@@ -23,15 +23,24 @@ export interface BotCredentials {
|
||||
function decodeBase64Url(value: string): Uint8Array {
|
||||
const base64 = value.replaceAll("-", "+").replaceAll("_", "/");
|
||||
const padded = base64.padEnd(Math.ceil(base64.length / 4) * 4, "=");
|
||||
return Uint8Array.from(atob(padded), (character) => character.codePointAt(0) ?? 0);
|
||||
return Uint8Array.from(
|
||||
atob(padded),
|
||||
(character) => character.codePointAt(0) ?? 0,
|
||||
);
|
||||
}
|
||||
|
||||
function encodeBase64Url(value: Uint8Array): string {
|
||||
const binary = Array.from(value, (byte) => String.fromCodePoint(byte)).join("");
|
||||
return btoa(binary).replaceAll("+", "-").replaceAll("/", "_").replaceAll("=", "");
|
||||
const binary = Array.from(value, (byte) => String.fromCodePoint(byte)).join(
|
||||
"",
|
||||
);
|
||||
return btoa(binary)
|
||||
.replaceAll("+", "-")
|
||||
.replaceAll("/", "_")
|
||||
.replaceAll("=", "");
|
||||
}
|
||||
|
||||
const buffer = (value: Uint8Array): ArrayBuffer => Uint8Array.from(value).buffer;
|
||||
const buffer = (value: Uint8Array): ArrayBuffer =>
|
||||
Uint8Array.from(value).buffer;
|
||||
|
||||
async function importMasterKey(
|
||||
encoded: string,
|
||||
@@ -41,13 +50,28 @@ async function importMasterKey(
|
||||
if (bytes.length !== 32) {
|
||||
throw new Error("master-key must be 32 bytes encoded as base64url");
|
||||
}
|
||||
const key = await webcrypto.subtle.importKey("raw", buffer(bytes), "AES-GCM", false, [usage]);
|
||||
const key = await webcrypto.subtle.importKey(
|
||||
"raw",
|
||||
buffer(bytes),
|
||||
"AES-GCM",
|
||||
false,
|
||||
[usage],
|
||||
);
|
||||
return key;
|
||||
}
|
||||
|
||||
export async function decryptCredential(encrypted: string, masterKey: string): Promise<string> {
|
||||
const [version, encodedNonce = "", encodedCiphertext = "", extra = ""] = encrypted.split(":");
|
||||
if (version !== "v1" || encodedNonce === "" || encodedCiphertext === "" || extra !== "") {
|
||||
export async function decryptCredential(
|
||||
encrypted: string,
|
||||
masterKey: string,
|
||||
): Promise<string> {
|
||||
const [version, encodedNonce = "", encodedCiphertext = "", extra = ""] =
|
||||
encrypted.split(":");
|
||||
if (
|
||||
version !== "v1" ||
|
||||
encodedNonce === "" ||
|
||||
encodedCiphertext === "" ||
|
||||
extra !== ""
|
||||
) {
|
||||
throw new Error("credential must use v1:<nonce>:<ciphertext>");
|
||||
}
|
||||
const nonce = decodeBase64Url(encodedNonce);
|
||||
@@ -89,7 +113,11 @@ function currentSpelling(type: unknown): unknown {
|
||||
return OLD_PROFILE_TYPES.get(type) ?? type;
|
||||
}
|
||||
|
||||
async function parseProfile(entry: unknown, index: number, masterKey: string): Promise<BotProfile> {
|
||||
async function parseProfile(
|
||||
entry: unknown,
|
||||
index: number,
|
||||
masterKey: string,
|
||||
): Promise<BotProfile> {
|
||||
const at = `profiles[${index}]`;
|
||||
if (!isMapping(entry)) {
|
||||
throw new TypeError(`${at}: expected a mapping`);
|
||||
@@ -123,7 +151,10 @@ async function parseProfile(entry: unknown, index: number, masterKey: string): P
|
||||
};
|
||||
}
|
||||
|
||||
export async function parseCredentials(text: string, masterKey: string): Promise<BotCredentials> {
|
||||
export async function parseCredentials(
|
||||
text: string,
|
||||
masterKey: string,
|
||||
): Promise<BotCredentials> {
|
||||
const value: unknown = JSON.parse(text);
|
||||
if (!isMapping(value)) {
|
||||
throw new TypeError("credentials must be a mapping");
|
||||
@@ -147,5 +178,8 @@ export async function parseCredentials(text: string, masterKey: string): Promise
|
||||
return profile;
|
||||
}),
|
||||
);
|
||||
return { reviewerToken: await decryptCredential(reviewerToken, masterKey), profiles: parsed };
|
||||
return {
|
||||
reviewerToken: await decryptCredential(reviewerToken, masterKey),
|
||||
profiles: parsed,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user