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
18 changed files
with
236 additions
and
59 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,5 @@ | ||
--- | ||
'@storipress/karbon': patch | ||
--- | ||
|
||
refactor: use 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
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
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
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,26 @@ | ||
import { Buffer } from 'node:buffer' | ||
import { describe, expect } from 'vitest' | ||
import { fc, it } from '@fast-check/vitest' | ||
import { base64ToUint8Array, textToUint8Array, uint8ArrayToBase64, uint8ArrayToText } from '../encoding' | ||
|
||
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) | ||
}) | ||
}) | ||
|
||
describe('base64ToUint8Array', () => { | ||
it.prop({ s: fc.string() })('can convert base64 to Uint8Array', ({ s }) => { | ||
const base64 = Buffer.from(s).toString('base64') | ||
expect(Buffer.from(base64ToUint8Array(base64)).toString()).toEqual(s) | ||
}) | ||
}) | ||
|
||
describe('base64ToUint8Array & uint8ArrayToBase64', () => { | ||
it.prop({ s: fc.string() })('can convert base64 string from and to uint8array', ({ s }) => { | ||
const base64 = Buffer.from(s).toString('base64') | ||
expect(uint8ArrayToBase64(base64ToUint8Array(base64))).toEqual(base64) | ||
}) | ||
}) |
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,38 @@ | ||
import '../crypto-polyfill' | ||
import { describe, expect } from 'vitest' | ||
import { fc, it } from '@fast-check/vitest' | ||
import { createEncryptJWT, decryptJWT } 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) | ||
}, | ||
) | ||
}) |
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
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,17 @@ | ||
export function base64ToUint8Array(s: string) { | ||
return Uint8Array.from(atob(s), (c) => c.charCodeAt(0)) | ||
} | ||
|
||
export function uint8ArrayToBase64(array: Uint8Array) { | ||
return btoa(String.fromCharCode(...array)) | ||
} | ||
|
||
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) | ||
} |
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,15 @@ | ||
import { CompactEncrypt, compactDecrypt } from '@storipress/jose-browser' | ||
import { textToUint8Array, uint8ArrayToText } from './encoding' | ||
|
||
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) | ||
} |
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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
export { createEncrypt, base64ToUint8Array, createDecrypt } from './cipher' | ||
export { createEncrypt, createDecrypt } from './cipher' | ||
export { filterHTMLTag } from './html-filter' | ||
export * from './jwt' | ||
export * from './encoding' |
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
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
42 changes: 42 additions & 0 deletions
42
packages/karbon/src/runtime/api/__tests__/encrypt-article.spec.ts
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,42 @@ | ||
import { decryptJWT, uint8ArrayToBase64 } from '@storipress/karbon-utils' | ||
import { describe, expect, it } from 'vitest' | ||
import { html } from 'proper-tags' | ||
import { encryptArticle } from '../encrypt-article' | ||
import { ArticlePlan } from '../../types' | ||
import { splitArticle } from '../../lib/split-article' | ||
|
||
const HTML_FIXTURE = html` | ||
<div>free paragraph 1</div> | ||
<div>free paragraph 2</div> | ||
<div>paid paragraph 1</div> | ||
<div>paid paragraph 2</div> | ||
<div>paid paragraph 3</div> | ||
` | ||
|
||
describe('encryptArticle', () => { | ||
it('should encrypt article', async () => { | ||
const key = new Uint8Array(32).fill(0) | ||
const encryptKey = uint8ArrayToBase64(key) | ||
|
||
const article = await encryptArticle({ | ||
id: '1', | ||
encryptKey, | ||
html: HTML_FIXTURE, | ||
plan: ArticlePlan.Member, | ||
segments: splitArticle(HTML_FIXTURE), | ||
}) | ||
|
||
expect(article.freeHTML).toBe( | ||
html` | ||
<div>free paragraph 1</div> | ||
<div>free paragraph 2</div> | ||
`.trim(), | ||
) | ||
|
||
const meta = JSON.parse(await decryptJWT(key, article.paidContent.key)) | ||
expect(meta.id).toBe('1') | ||
expect(meta.plan).toBe(ArticlePlan.Member) | ||
expect(typeof meta.key).toBe('string') | ||
expect(article.segments).toBeInstanceOf(Array) | ||
}) | ||
}) |
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
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,52 @@ | ||
import { base64ToUint8Array, createEncrypt, createEncryptJWT, uint8ArrayToBase64 } from '@storipress/karbon-utils' | ||
import { splitPaidContent } from '../lib/split-paid-content' | ||
import type { NormalSegment, Segment } from '../lib/split-article' | ||
import type { ArticlePlan } from '../types' | ||
|
||
export interface EncryptArticleInput { | ||
id: string | ||
html: string | ||
plan: ArticlePlan | ||
segments: Segment[] | ||
encryptKey: string | ||
previewParagraph?: number | ||
} | ||
export async function encryptArticle({ | ||
html, | ||
id, | ||
plan, | ||
segments, | ||
encryptKey, | ||
previewParagraph = 3, | ||
}: EncryptArticleInput) { | ||
const [preview, paid] = splitPaidContent(html, previewParagraph) | ||
const freeHTML = preview | ||
|
||
const { key, content, iv, encrypt } = await createEncrypt(paid) | ||
|
||
const encryptedKey = await createEncryptJWT( | ||
base64ToUint8Array(encryptKey), | ||
JSON.stringify({ id, plan, key: uint8ArrayToBase64(key) }), | ||
) | ||
const paidContent = { | ||
key: encryptedKey, | ||
content: uint8ArrayToBase64(content), | ||
iv: uint8ArrayToBase64(iv), | ||
} | ||
|
||
const encryptedSegments = await Promise.all( | ||
segments.map(async (segment, index, source): Promise<Segment> => { | ||
const html = (segment as NormalSegment).html | ||
const noEncrypt = html === undefined || (index < previewParagraph && source.length > previewParagraph) | ||
if (noEncrypt) return segment | ||
|
||
const content = await encrypt(html) | ||
return { | ||
id: 'paid', | ||
type: segment.type, | ||
paidContent: uint8ArrayToBase64(content), | ||
} | ||
}), | ||
) | ||
return { freeHTML, paidContent, segments: encryptedSegments } | ||
} |
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
Oops, something went wrong.