Skip to content

Commit

Permalink
fix: base64url encoding / decoding isn't always available
Browse files Browse the repository at this point in the history
Signed-off-by: Tom Lanser <[email protected]>
  • Loading branch information
Tommylans committed Nov 21, 2024
1 parent 9757fbd commit beb95bc
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 11 deletions.
7 changes: 4 additions & 3 deletions packages/core/src/jsonWeb/createJsonWebToken.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Buffer } from 'buffer'
import { base64ToBase64URL } from '../utils/encoding'

/**
*
Expand All @@ -10,10 +11,10 @@ export const createJsonWebToken = (
payload: Record<string, unknown>,
signature: Uint8Array
) => {
const encodedHeader = Buffer.from(JSON.stringify(header)).toString('base64url')
const encodedPayload = Buffer.from(JSON.stringify(payload)).toString('base64url')
const encodedHeader = base64ToBase64URL(Buffer.from(JSON.stringify(header)).toString('base64'))
const encodedPayload = base64ToBase64URL(Buffer.from(JSON.stringify(payload)).toString('base64'))

const encodedSignature = Buffer.from(signature).toString('base64url')
const encodedSignature = base64ToBase64URL(Buffer.from(signature).toString('base64'))

return `${encodedHeader}.${encodedPayload}.${encodedSignature}`
}
5 changes: 3 additions & 2 deletions packages/core/src/jsonWeb/createJsonWebTokenSignableInput.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Buffer } from 'buffer'
import { base64ToBase64URL } from '../utils/encoding'

/**
*
Expand All @@ -14,8 +15,8 @@ export const createJwtSignableInput = (header: Record<string, unknown>, payload:
throw new Error('Can not create JWT with an empty payload')
}

const encodedHeader = Buffer.from(JSON.stringify(header)).toString('base64url')
const encodedPayload = Buffer.from(JSON.stringify(payload)).toString('base64url')
const encodedHeader = base64ToBase64URL(Buffer.from(JSON.stringify(header)).toString('base64'))
const encodedPayload = base64ToBase64URL(Buffer.from(JSON.stringify(payload)).toString('base64'))

const toBeSignedString = `${encodedHeader}.${encodedPayload}`

Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/jsonWeb/jsonWebToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ export const jsonWebTokenSchema = <
return z.NEVER
}

const decodedHeader = Buffer.from(header, 'base64url').toString()
const decodedClaims = Buffer.from(claims, 'base64url').toString()
const decodedSignature = Buffer.from(signature, 'base64url')
const decodedHeader = Buffer.from(header, 'base64').toString()
const decodedClaims = Buffer.from(claims, 'base64').toString()
const decodedSignature = Buffer.from(signature, 'base64')

const validatedHeader = validate({
schema: headerSchema,
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/jsonWeb/parseJsonWebToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ export const parseJsonWebToken = (jwt: string): JsonWebTokenParts => {
throw new Error('could not find the signature in the JWT')
}

const header = JSON.parse(Buffer.from(encodedHeader, 'base64url').toString())
const claims = JSON.parse(Buffer.from(encodedClaims, 'base64url').toString())
const signature = new Uint8Array(Buffer.from(encodedSignature, 'base64url'))
const header = JSON.parse(Buffer.from(encodedHeader, 'base64').toString())
const claims = JSON.parse(Buffer.from(encodedClaims, 'base64').toString())
const signature = new Uint8Array(Buffer.from(encodedSignature, 'base64'))

return {
header,
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/utils/encoding.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function base64ToBase64URL(base64: string) {
return base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '')
}
1 change: 1 addition & 0 deletions packages/core/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './dateSchema'
export * from './url'
export * from './fetch'
export * from './types'
export * from './encoding'

0 comments on commit beb95bc

Please sign in to comment.