1- import crypto from ' node:crypto' ;
1+ import crypto from " node:crypto" ;
22
33/*
44 * Configuration Constants
@@ -16,7 +16,7 @@ import crypto from 'node:crypto';
1616 * The key length is 256 bits since we are using AES-256-GCM.
1717 */
1818const PBKDF2_HMAC_SHA256_KEY_DERIVATION = {
19- DIGEST : ' sha256' ,
19+ DIGEST : " sha256" ,
2020 ITERATIONS : 600_000 ,
2121 KEY_LENGTH_BYTES : 32 , // 256 bits
2222 SALT_LENGTH_BYTES : 16 , // 128 bits
@@ -30,7 +30,7 @@ const PBKDF2_HMAC_SHA256_KEY_DERIVATION = {
3030 * recommendations.
3131 */
3232const AES_256_GCM_ENCRYPTION = {
33- ALGORITHM : ' aes-256-gcm' ,
33+ ALGORITHM : " aes-256-gcm" ,
3434 IV_LENGTH_BYTES : 12 , // 96 bits
3535 TAG_LENGTH_BYTES : 16 , // 128 bits
3636} as const ;
@@ -157,12 +157,12 @@ function isPassphraseEncryptionOutput(
157157 value : unknown ,
158158) : value is PassphraseEncryptionOutput_t {
159159 // The input should be a non-null object
160- if ( ! ( value && typeof value === ' object' ) ) return false ;
160+ if ( ! ( value && typeof value === " object" ) ) return false ;
161161 // The object should have these properties
162- if ( ! ( ' salt' in value ) ) return false ;
163- if ( ! ( 'iv' in value ) ) return false ;
164- if ( ! ( ' tag' in value ) ) return false ;
165- if ( ! ( ' ciphertext' in value ) ) return false ;
162+ if ( ! ( " salt" in value ) ) return false ;
163+ if ( ! ( "iv" in value ) ) return false ;
164+ if ( ! ( " tag" in value ) ) return false ;
165+ if ( ! ( " ciphertext" in value ) ) return false ;
166166 // The properties should be the correct type
167167 if ( ! isSalt ( value . salt ) ) return false ;
168168 if ( ! isIV ( value . iv ) ) return false ;
@@ -174,14 +174,14 @@ function isPassphraseEncryptionOutput(
174174function isEncodedPassphraseEncryptionOutput (
175175 value : unknown ,
176176) : value is EncodedPassphraseEncryptionOutput_t {
177- if ( typeof value !== ' string' ) return false ;
178- const parts = value . split ( '.' ) ;
177+ if ( typeof value !== " string" ) return false ;
178+ const parts = value . split ( "." ) ;
179179 if ( parts . length !== 4 ) return false ;
180180 const [ salt , iv , tag , ciphertext ] = parts ;
181- if ( ! isSalt ( Buffer . from ( salt , ' base64' ) ) ) return false ;
182- if ( ! isIV ( Buffer . from ( iv , ' base64' ) ) ) return false ;
183- if ( ! isTag ( Buffer . from ( tag , ' base64' ) ) ) return false ;
184- if ( ! isCiphertext ( Buffer . from ( ciphertext , ' base64' ) ) ) return false ;
181+ if ( ! isSalt ( Buffer . from ( salt , " base64" ) ) ) return false ;
182+ if ( ! isIV ( Buffer . from ( iv , " base64" ) ) ) return false ;
183+ if ( ! isTag ( Buffer . from ( tag , " base64" ) ) ) return false ;
184+ if ( ! isCiphertext ( Buffer . from ( ciphertext , " base64" ) ) ) return false ;
185185 return true ;
186186}
187187
@@ -191,49 +191,49 @@ function isEncodedPassphraseEncryptionOutput(
191191
192192function asPassphrase ( value : unknown ) : Passphrase_t {
193193 if ( ! isPassphrase ( value ) ) {
194- throw new Error ( ' Invalid passphrase.' ) ;
194+ throw new Error ( " Invalid passphrase." ) ;
195195 }
196196 return value ;
197197}
198198
199199function asSalt ( value : unknown ) : Salt_t {
200200 if ( ! isSalt ( value ) ) {
201- throw new Error ( ' Invalid salt.' ) ;
201+ throw new Error ( " Invalid salt." ) ;
202202 }
203203 return value ;
204204}
205205
206206function asTag ( value : unknown ) : Tag_t {
207207 if ( ! isTag ( value ) ) {
208- throw new Error ( ' Invalid tag.' ) ;
208+ throw new Error ( " Invalid tag." ) ;
209209 }
210210 return value ;
211211}
212212
213213function asIV ( value : unknown ) : IV_t {
214214 if ( ! isIV ( value ) ) {
215- throw new Error ( ' Invalid IV.' ) ;
215+ throw new Error ( " Invalid IV." ) ;
216216 }
217217 return value ;
218218}
219219
220220function asCiphertext ( value : unknown ) : Ciphertext_t {
221221 if ( ! isCiphertext ( value ) ) {
222- throw new Error ( ' Invalid ciphertext.' ) ;
222+ throw new Error ( " Invalid ciphertext." ) ;
223223 }
224224 return value ;
225225}
226226
227227function asPlaintext ( value : unknown ) : Plaintext_t {
228228 if ( ! isPlaintext ( value ) ) {
229- throw new Error ( ' Invalid plaintext.' ) ;
229+ throw new Error ( " Invalid plaintext." ) ;
230230 }
231231 return value ;
232232}
233233
234234function asSymmetricKey ( value : unknown ) : SymmetricKey_t {
235235 if ( ! isSymmetricKey ( value ) ) {
236- throw new Error ( ' Invalid symmetric key.' ) ;
236+ throw new Error ( " Invalid symmetric key." ) ;
237237 }
238238 return value ;
239239}
@@ -242,7 +242,7 @@ function asPassphraseEncryptionOutput(
242242 value : unknown ,
243243) : PassphraseEncryptionOutput_t {
244244 if ( ! isPassphraseEncryptionOutput ( value ) ) {
245- throw new Error ( ' Invalid encryption output.' ) ;
245+ throw new Error ( " Invalid encryption output." ) ;
246246 }
247247 return value ;
248248}
@@ -257,7 +257,7 @@ function asEncodedPassphraseEncryptionOutput(
257257 value : unknown ,
258258) : EncodedPassphraseEncryptionOutput_t {
259259 if ( ! isEncodedPassphraseEncryptionOutput ( value ) ) {
260- throw new Error ( ' Invalid encoded encryption output.' ) ;
260+ throw new Error ( " Invalid encoded encryption output." ) ;
261261 }
262262 return value ;
263263}
@@ -373,25 +373,25 @@ function encodeEncryptedToken(
373373 tag : Tag_t ,
374374 ciphertext : Ciphertext_t ,
375375) : string {
376- const encodedSalt = salt . toString ( ' base64' ) ;
377- const encodedIV = iv . toString ( ' base64' ) ;
378- const encodedTag = tag . toString ( ' base64' ) ;
379- const encodedCiphertext = ciphertext . toString ( ' base64' ) ;
376+ const encodedSalt = salt . toString ( " base64" ) ;
377+ const encodedIV = iv . toString ( " base64" ) ;
378+ const encodedTag = tag . toString ( " base64" ) ;
379+ const encodedCiphertext = ciphertext . toString ( " base64" ) ;
380380 return `${ encodedSalt } .${ encodedIV } .${ encodedTag } .${ encodedCiphertext } ` ;
381381}
382382
383383function decodeEncryptedToken (
384384 encodedToken : EncodedPassphraseEncryptionOutput_t ,
385385) : PassphraseEncryptionOutput_t {
386- const parts = encodedToken . split ( '.' ) ;
386+ const parts = encodedToken . split ( "." ) ;
387387 if ( parts . length !== 4 ) {
388- throw new Error ( ' Expected 4 components in encoded token.' ) ;
388+ throw new Error ( " Expected 4 components in encoded token." ) ;
389389 }
390390 const [ encodedSalt , encodedIV , encodedTag , encodedCiphertext ] = parts ;
391- const salt = asSalt ( Buffer . from ( encodedSalt , ' base64' ) ) ;
392- const iv = asIV ( Buffer . from ( encodedIV , ' base64' ) ) ;
393- const tag = asTag ( Buffer . from ( encodedTag , ' base64' ) ) ;
394- const ciphertext = asCiphertext ( Buffer . from ( encodedCiphertext , ' base64' ) ) ;
391+ const salt = asSalt ( Buffer . from ( encodedSalt , " base64" ) ) ;
392+ const iv = asIV ( Buffer . from ( encodedIV , " base64" ) ) ;
393+ const tag = asTag ( Buffer . from ( encodedTag , " base64" ) ) ;
394+ const ciphertext = asCiphertext ( Buffer . from ( encodedCiphertext , " base64" ) ) ;
395395 return { salt, iv, tag, ciphertext } ;
396396}
397397
@@ -425,12 +425,9 @@ function decodeEncryptionOutput(
425425 * @param oauthToken the OAuth token to encrypt
426426 * @returns the encrypted token, encoded as a string
427427 */
428- export function encrypt (
429- embedSecret : string ,
430- oauthToken : string ,
431- ) : string {
432- const passphrase = asPassphrase ( Buffer . from ( embedSecret , 'utf8' ) ) ;
433- const plaintext = asPlaintext ( Buffer . from ( oauthToken , 'utf8' ) ) ;
428+ export function encrypt ( embedSecret : string , oauthToken : string ) : string {
429+ const passphrase = asPassphrase ( Buffer . from ( embedSecret , "utf8" ) ) ;
430+ const plaintext = asPlaintext ( Buffer . from ( oauthToken , "utf8" ) ) ;
434431 const encryptionOutput = encryptWithPassphrase ( passphrase , plaintext ) ;
435432 return encodeEncryptionOutput ( encryptionOutput ) ;
436433}
@@ -442,11 +439,8 @@ export function encrypt(
442439 * @param encryptedToken the encrypted OAuth token to decrypt
443440 * @returns the decrypted token
444441 */
445- export function decrypt (
446- embedSecret : string ,
447- encryptedToken : string ,
448- ) : string {
449- const passphrase = asPassphrase ( Buffer . from ( embedSecret , 'utf8' ) ) ;
442+ export function decrypt ( embedSecret : string , encryptedToken : string ) : string {
443+ const passphrase = asPassphrase ( Buffer . from ( embedSecret , "utf8" ) ) ;
450444 const encryptionOutput = decodeEncryptionOutput (
451445 asEncodedPassphraseEncryptionOutput ( encryptedToken ) ,
452446 ) ;
@@ -457,5 +451,11 @@ export function decrypt(
457451 encryptionOutput . tag ,
458452 encryptionOutput . ciphertext ,
459453 ) ;
460- return plaintext . toString ( ' utf8' ) ;
454+ return plaintext . toString ( " utf8" ) ;
461455}
456+
457+ export const _testExports = {
458+ PBKDF2_HMAC_SHA256_KEY_DERIVATION ,
459+ AES_256_GCM_ENCRYPTION ,
460+ asEncodedPassphraseEncryptionOutput,
461+ } ;
0 commit comments