From 967f26e33d9943266a6ea43a77fd3f32acff729f Mon Sep 17 00:00:00 2001 From: Timo Glastra Date: Fri, 26 Jan 2024 00:07:51 +0700 Subject: [PATCH] fix: base64url react native Signed-off-by: Timo Glastra --- packages/utils/src/base64url.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/utils/src/base64url.ts b/packages/utils/src/base64url.ts index 3c04794..f4e8b3e 100644 --- a/packages/utils/src/base64url.ts +++ b/packages/utils/src/base64url.ts @@ -7,7 +7,7 @@ export class Base64url { * */ public static encode(input: string | Uint8Array | Buffer): string { - return Buffer.from(input).toString('base64url') + return base64ToBase64URL(Buffer.from(input).toString('base64')) } /** @@ -18,7 +18,9 @@ export class Base64url { public static encodeFromJson( input: Record | Array ): string { - return Buffer.from(JSON.stringify(input)).toString('base64url') + return base64ToBase64URL( + Buffer.from(JSON.stringify(input)).toString('base64') + ) } /** @@ -32,7 +34,7 @@ export class Base64url { unknown > >(input: string): T { - return JSON.parse(Buffer.from(input, 'base64url').toString()) as T + return JSON.parse(Buffer.from(input, 'base64').toString()) as T } /** @@ -41,6 +43,10 @@ export class Base64url { * */ public static decode(input: string): Uint8Array { - return Uint8Array.from(Buffer.from(input, 'base64url')) + return Uint8Array.from(Buffer.from(input, 'base64')) } } + +export function base64ToBase64URL(base64: string) { + return base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '') +}