Skip to content

Commit

Permalink
fixed import
Browse files Browse the repository at this point in the history
  • Loading branch information
Code-Hex committed Feb 19, 2024
1 parent d66186a commit 626c062
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 10 deletions.
5 changes: 5 additions & 0 deletions src/base64.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { utf8Encoder } from './utf8';

export const decodeBase64Url = (str: string): Uint8Array => {
return decodeBase64(str.replace(/_|-/g, m => ({ _: '/', '-': '+' })[m] ?? m));
};
Expand All @@ -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));
3 changes: 1 addition & 2 deletions src/credential.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
4 changes: 2 additions & 2 deletions tests/firebase-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Credential } from '../src';
import { encodeBase64Url } from '../src/base64';
import { encodeBase64Url, encodeObjectBase64Url } from '../src/base64';
import type { GoogleOAuthAccessToken } from '../src/credential';
import type { EmulatorEnv } from '../src/emulator';
import { emulatorHost } from '../src/emulator';
Expand All @@ -8,7 +8,7 @@ import { PublicKeySignatureVerifier } from '../src/jws-verifier';
import { FIREBASE_AUDIENCE, type FirebaseIdToken } from '../src/token-verifier';
import { utf8Encoder } from '../src/utf8';
import { isNonEmptyString, isNonNullObject } from '../src/validator';
import { signJWT, genTime, genIss, TestingKeyFetcher, encodeObjectBase64Url } from './jwk-utils';
import { signJWT, genTime, genIss, TestingKeyFetcher } from './jwk-utils';

export const projectId = 'project12345'; // see package.json
export const userId = 'userId12345';
Expand Down
6 changes: 1 addition & 5 deletions tests/jwk-utils.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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',
Expand Down
3 changes: 2 additions & 1 deletion tests/jwt-decoder.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { describe, it, expect } from 'vitest';
import { encodeObjectBase64Url } from '../src/base64';
import { JwtError, JwtErrorCode } from '../src/errors';
import type { DecodedHeader, DecodedPayload } from '../src/jwt-decoder';
import { RS256Token } from '../src/jwt-decoder';
import { genTime, genIss, signJWT, TestingKeyFetcher, encodeObjectBase64Url } from './jwk-utils';
import { genTime, genIss, signJWT, TestingKeyFetcher } from './jwk-utils';

describe('TokenDecoder', () => {
const kid = 'kid123456';
Expand Down

0 comments on commit 626c062

Please sign in to comment.