From 9cd1f05929b9aa804dce96819d6a4312290fc72a Mon Sep 17 00:00:00 2001 From: Jonas Date: Wed, 2 Oct 2024 00:29:47 +0200 Subject: [PATCH] improves code quality --- src/v1/adapters/authenticate.ts | 2 +- src/v1/adapters/crypto.ts | 28 ++++++++++++++++++++-------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/v1/adapters/authenticate.ts b/src/v1/adapters/authenticate.ts index 1f71583..3783bc4 100644 --- a/src/v1/adapters/authenticate.ts +++ b/src/v1/adapters/authenticate.ts @@ -13,7 +13,7 @@ export function checkForTokenPresence( token: string | undefined, set: Context["set"], jwtRealm: "propromoRestAdaptersGithub" | "propromoRestAdaptersJira" = "propromoRestAdaptersGithub", - errorMessage = "Token is missing. Create one at https://github.com/settings/tokens.", + errorMessage: string = "Token is missing. Create one at https://github.com/settings/tokens.", ): string { if (!token || token.trim().length === 0) { // Authorization: Bearer diff --git a/src/v1/adapters/crypto.ts b/src/v1/adapters/crypto.ts index 81f1f20..97fc877 100644 --- a/src/v1/adapters/crypto.ts +++ b/src/v1/adapters/crypto.ts @@ -1,6 +1,12 @@ import { PAT_SALT as SALT } from "../../environment"; -export async function encryptString(plaintext: string) { +/** + * Encrypts a string using a password derived key. + * + * @param {string} plaintext - The string to encrypt. + * @returns {Promise} - The encrypted string. + */ +export async function encryptString(plaintext: string): Promise { const plaintextBuffer = new TextEncoder().encode(plaintext); const passwordKey = await crypto.subtle.importKey( @@ -37,16 +43,22 @@ export async function encryptString(plaintext: string) { const encryptedString = btoa( String.fromCharCode.apply(null, Array.from(new Uint8Array(iv))) + - String.fromCharCode.apply( - null, - Array.from(new Uint8Array(encryptedBuffer)), - ), + String.fromCharCode.apply( + null, + Array.from(new Uint8Array(encryptedBuffer)), + ), ); return encryptedString; } -export async function decryptString(encryptedString: string) { +/** + * Decrypts an encrypted string using a password derived key. + * + * @param {string} encryptedString - The encrypted string to decrypt. + * @returns {Promise} - The decrypted string. + */ +export async function decryptString(encryptedString: string): Promise { console.log("encryptedString", encryptedString); const encryptedBuffer = Uint8Array.from(atob(encryptedString), (c) => @@ -54,7 +66,7 @@ export async function decryptString(encryptedString: string) { ); const iv = encryptedBuffer.slice(0, 12); - const ciphertext = encryptedBuffer.slice(12); + const cipherText = encryptedBuffer.slice(12); const passwordKey = await crypto.subtle.importKey( "raw", @@ -83,7 +95,7 @@ export async function decryptString(encryptedString: string) { iv: iv, }, key, - ciphertext, + cipherText, ); const decryptedString = new TextDecoder().decode(decryptedBuffer);