diff --git a/src/base64.ts b/src/base64.ts index b7b8685..e8b2f27 100644 --- a/src/base64.ts +++ b/src/base64.ts @@ -1,3 +1,5 @@ +import { utf8Encoder } from './utf8'; + export const decodeBase64Url = (str: string): Uint8Array => { return decodeBase64(str.replace(/_|-/g, m => ({ _: '/', '-': '+' })[m] ?? m)); }; @@ -23,3 +25,6 @@ export const decodeBase64 = (str: string): Uint8Array => { } return bytes; }; + +const jsonUTF8Stringify = (obj: any): Uint8Array => utf8Encoder.encode(JSON.stringify(obj)); +export const encodeObjectBase64Url = (obj: any): string => encodeBase64Url(jsonUTF8Stringify(obj)); diff --git a/src/credential.ts b/src/credential.ts index 0e9c24f..fbc619b 100644 --- a/src/credential.ts +++ b/src/credential.ts @@ -1,5 +1,4 @@ -import { encodeObjectBase64Url } from './../tests/jwk-utils'; -import { decodeBase64, encodeBase64Url } from './base64'; +import { decodeBase64, encodeBase64Url, encodeObjectBase64Url } from './base64'; import { AppErrorCodes, FirebaseAppError } from './errors'; import { isNonEmptyString, isNonNullObject } from './validator'; diff --git a/tests/jwk-utils.ts b/tests/jwk-utils.ts index a7676cc..08046d0 100644 --- a/tests/jwk-utils.ts +++ b/tests/jwk-utils.ts @@ -1,4 +1,4 @@ -import { encodeBase64Url } from '../src/base64'; +import { encodeBase64Url, encodeObjectBase64Url } from '../src/base64'; import type { KeyFetcher } from '../src/jwk-fetcher'; import { rs256alg } from '../src/jws-verifier'; import type { DecodedHeader, DecodedPayload, JsonWebKeyWithKid } from '../src/jwt-decoder'; @@ -28,10 +28,6 @@ export class TestingKeyFetcher implements KeyFetcher { export const genTime = (ms: number = Date.now()): number => Math.floor(ms / 1000); export const genIss = (projectId: string = 'projectId1234'): string => 'https://securetoken.google.com/' + projectId; -export const encodeObjectBase64Url = (obj: any): string => encodeBase64Url(jsonUTF8Stringify(obj)); - -const jsonUTF8Stringify = (obj: any): Uint8Array => utf8Encoder.encode(JSON.stringify(obj)); - export const signJWT = async (kid: string, payload: DecodedPayload, privateKey: CryptoKey) => { const header: DecodedHeader = { alg: 'RS256',