Skip to content

Commit

Permalink
fix: base64url react native
Browse files Browse the repository at this point in the history
Signed-off-by: Timo Glastra <[email protected]>
  • Loading branch information
TimoGlastra committed Jan 25, 2024
1 parent 8fb68d5 commit 967f26e
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions packages/utils/src/base64url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
}

/**
Expand All @@ -18,7 +18,9 @@ export class Base64url {
public static encodeFromJson(
input: Record<string, unknown> | Array<unknown>
): string {
return Buffer.from(JSON.stringify(input)).toString('base64url')
return base64ToBase64URL(
Buffer.from(JSON.stringify(input)).toString('base64')
)
}

/**
Expand All @@ -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
}

/**
Expand All @@ -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, '')
}

0 comments on commit 967f26e

Please sign in to comment.