Skip to content

Commit

Permalink
improves code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasfroeller committed Oct 1, 2024
1 parent f239f87 commit 9cd1f05
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/v1/adapters/authenticate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <token>
Expand Down
28 changes: 20 additions & 8 deletions src/v1/adapters/crypto.ts
Original file line number Diff line number Diff line change
@@ -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<string>} - The encrypted string.
*/
export async function encryptString(plaintext: string): Promise<string> {
const plaintextBuffer = new TextEncoder().encode(plaintext);

const passwordKey = await crypto.subtle.importKey(
Expand Down Expand Up @@ -37,24 +43,30 @@ 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<string>} - The decrypted string.
*/
export async function decryptString(encryptedString: string): Promise<string> {
console.log("encryptedString", encryptedString);

const encryptedBuffer = Uint8Array.from(atob(encryptedString), (c) =>
c.charCodeAt(0),
);

const iv = encryptedBuffer.slice(0, 12);
const ciphertext = encryptedBuffer.slice(12);
const cipherText = encryptedBuffer.slice(12);

const passwordKey = await crypto.subtle.importKey(
"raw",
Expand Down Expand Up @@ -83,7 +95,7 @@ export async function decryptString(encryptedString: string) {
iv: iv,
},
key,
ciphertext,
cipherText,
);

const decryptedString = new TextDecoder().decode(decryptedBuffer);
Expand Down

0 comments on commit 9cd1f05

Please sign in to comment.