This repository has been archived by the owner on Nov 20, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@storipress/karbon-utils': minor | ||
--- | ||
|
||
feat: add jwt utils |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import '../crypto-polyfill' | ||
import { describe, expect } from 'vitest' | ||
import { fc, it } from '@fast-check/vitest' | ||
import { createEncryptJWT, decryptJWT, textToUint8Array, uint8ArrayToText } from '../jwt' | ||
|
||
describe('createEncryptJWT', () => { | ||
// Returns an encrypted JWT string when given a valid key and plaintext | ||
it('should return an encrypted JWT string when given a valid key and plaintext', async () => { | ||
const key = new Uint8Array(32) | ||
const plaintext = 'Hello, world!' | ||
const result = await createEncryptJWT(key, plaintext) | ||
expect(typeof result).toBe('string') | ||
}) | ||
|
||
// Returns an error when given a key of incorrect length | ||
it('should return an error when given a key of incorrect length', async () => { | ||
const key = new Uint8Array(16) | ||
const plaintext = 'Hello, world!' | ||
await expect(createEncryptJWT(key, plaintext)).rejects.toThrowError() | ||
}) | ||
}) | ||
|
||
describe('createEncryptJWT & decryptJWT', () => { | ||
it.prop({ plaintext: fc.string(), key: fc.uint8Array({ minLength: 32, maxLength: 32 }) })( | ||
'can encrypt and decrypt', | ||
async ({ plaintext, key }) => { | ||
const result = await createEncryptJWT(key, plaintext) | ||
|
||
expect(typeof result).toBe('string') | ||
|
||
expect(result).not.toBe(plaintext) | ||
|
||
const decrypted = await decryptJWT(key, result) | ||
|
||
expect(decrypted).toBe(plaintext) | ||
}, | ||
) | ||
}) | ||
|
||
describe('textToUint8Array & uint8ArrayToText', () => { | ||
it.prop({ text: fc.string() })('can convert text to and from uint8array', ({ text }) => { | ||
const array = textToUint8Array(text) | ||
const result = uint8ArrayToText(array) | ||
expect(result).toBe(text) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { CompactEncrypt, compactDecrypt } from '@storipress/jose-browser' | ||
|
||
export async function createEncryptJWT(key: Uint8Array, plaintext: string) { | ||
const compactEncrypt = new CompactEncrypt(textToUint8Array(plaintext)).setProtectedHeader({ | ||
enc: 'A256GCM', | ||
alg: 'dir', | ||
}) | ||
return await compactEncrypt.encrypt(key) | ||
} | ||
|
||
export async function decryptJWT(key: Uint8Array, jwt: string) { | ||
const { plaintext } = await compactDecrypt(jwt, key) | ||
return uint8ArrayToText(plaintext) | ||
} | ||
|
||
export function textToUint8Array(text: string) { | ||
const encoder = new TextEncoder() | ||
return encoder.encode(text) | ||
} | ||
|
||
export function uint8ArrayToText(array: Uint8Array) { | ||
const decoder = new TextDecoder() | ||
return decoder.decode(array) | ||
} |