1
0

Fix unicode payload signing

This commit is contained in:
Richard Lee
2024-01-08 01:30:56 +08:00
committed by Toby
parent d4d7fe1414
commit 7ffbebc87c
2 changed files with 14 additions and 1 deletions

View File

@@ -73,6 +73,11 @@ const payload: Payload = {
name: "John Doe", name: "John Doe",
} }
const unicodePayload: Payload = {
sub: "1234567890",
name: "John Doe 😎",
}
describe.each(Object.entries(data) as [JwtAlgorithm, Dataset][])('%s', (algorithm, data) => { describe.each(Object.entries(data) as [JwtAlgorithm, Dataset][])('%s', (algorithm, data) => {
let token = '' let token = ''
@@ -97,6 +102,11 @@ describe.each(Object.entries(data) as [JwtAlgorithm, Dataset][])('%s', (algorith
expect(token).toMatch(/^[a-zA-Z0-9\-_]+\.[a-zA-Z0-9\-_]+\.[a-zA-Z0-9\-_]+$/) expect(token).toMatch(/^[a-zA-Z0-9\-_]+\.[a-zA-Z0-9\-_]+\.[a-zA-Z0-9\-_]+$/)
}) })
test('sign unciode', async () => {
token = await jwt.sign<Payload>(unicodePayload, data.private, algorithm)
expect(token).toMatch(/^[a-zA-Z0-9\-_]+\.[a-zA-Z0-9\-_]+\.[a-zA-Z0-9\-_]+$/)
})
test('decode internal', async () => { test('decode internal', async () => {
const decoded = jwt.decode(token) const decoded = jwt.decode(token)
expect({ expect({

View File

@@ -157,7 +157,10 @@ function base64UrlToArrayBuffer(b64url: string): ArrayBuffer {
} }
function textToBase64Url(str: string): string { function textToBase64Url(str: string): string {
return btoa(str).replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_') const encoder = new TextEncoder();
const charCodes = encoder.encode(str);
const binaryStr = String.fromCharCode(...charCodes);
return btoa(binaryStr).replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_')
} }
function pemToBinary(pem: string): ArrayBuffer { function pemToBinary(pem: string): ArrayBuffer {