Skip to content

Commit

Permalink
Merge pull request #33 from TimoGlastra/fix/base64-url-react-native
Browse files Browse the repository at this point in the history
fix: base64url react native
  • Loading branch information
berendsliedrecht authored Jan 29, 2024
2 parents 4ac0e1d + 321abcf commit e71401c
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 e71401c

Please sign in to comment.