Skip to content

Commit 975db73

Browse files
committed
fixed some code around credentials
1 parent 356b74b commit 975db73

File tree

6 files changed

+31
-23
lines changed

6 files changed

+31
-23
lines changed

src/client.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { version } from '../package.json';
22
import type { ApiSettings } from './api-requests';
33
import type { Credential } from './credential';
4-
import { type EmulatorEnv } from './emulator';
4+
import { useEmulator, type EmulatorEnv } from './emulator';
55
import { AppErrorCodes, FirebaseAppError } from './errors';
66

77
/**
@@ -60,7 +60,10 @@ export class BaseClient {
6060
private retryConfig: RetryConfig = defaultRetryConfig()
6161
) {}
6262

63-
private async getToken(): Promise<string> {
63+
private async getToken(env?: EmulatorEnv): Promise<string> {
64+
if (useEmulator(env)) {
65+
return 'owner';
66+
}
6467
const result = await this.credential.getAccessToken();
6568
return result.access_token;
6669
}
@@ -71,7 +74,7 @@ export class BaseClient {
7174
const requestValidator = apiSettings.getRequestValidator();
7275
requestValidator(requestData);
7376
}
74-
const token = await this.getToken();
77+
const token = await this.getToken(env);
7578
const method = apiSettings.getHttpMethod();
7679
const signal = AbortSignal.timeout(25000); // 25s
7780
return await this.fetchWithRetry<T>(fullUrl, {

src/credential.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,6 @@ export interface Credential {
3737
getAccessToken(): Promise<GoogleOAuthAccessToken>;
3838
}
3939

40-
/**
41-
* Implementation of Credential that uses with emulator.
42-
*/
43-
export class EmulatorCredential implements Credential {
44-
public async getAccessToken(): Promise<GoogleOAuthAccessToken> {
45-
return {
46-
access_token: 'owner',
47-
expires_in: 90 * 3600 * 3600,
48-
};
49-
}
50-
}
51-
5240
const GOOGLE_TOKEN_AUDIENCE = 'https://accounts.google.com/o/oauth2/token';
5341
const GOOGLE_AUTH_TOKEN_HOST = 'accounts.google.com';
5442
const GOOGLE_AUTH_TOKEN_PATH = '/o/oauth2/token';

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { Credential } from './credential';
33
import type { KeyStorer } from './key-store';
44
import { WorkersKVStore } from './key-store';
55

6-
export { type Credential, ServiceAccountCredential, EmulatorCredential } from './credential';
6+
export { type Credential, ServiceAccountCredential } from './credential';
77
export { emulatorHost, useEmulator } from './emulator';
88
export type { KeyStorer };
99
export type { EmulatorEnv } from './emulator';

tests/auth.test.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import { describe, expect, it } from 'vitest';
22
import { BaseAuth } from '../src/auth';
3-
import { EmulatorCredential } from '../src/credential';
43
import { FirebaseAuthError } from '../src/errors';
54
import type { EmulatorEnv, KeyStorer } from './../src/index';
6-
import { EmulatedSigner, FirebaseTokenGenerator, projectId, signInWithCustomToken } from './firebase-utils';
5+
import {
6+
EmulatedSigner,
7+
FirebaseTokenGenerator,
8+
NopCredential,
9+
projectId,
10+
signInWithCustomToken,
11+
} from './firebase-utils';
712

813
const env: EmulatorEnv = {
914
FIREBASE_AUTH_EMULATOR_HOST: 'localhost:9099',
@@ -25,7 +30,7 @@ describe('createSessionCookie()', () => {
2530

2631
it('creates a valid Firebase session cookie', async () => {
2732
const keyStorer = new InMemoryKeyStorer('cache-key');
28-
const auth = new BaseAuth(projectId, keyStorer, new EmulatorCredential());
33+
const auth = new BaseAuth(projectId, keyStorer, new NopCredential());
2934

3035
const signer = new EmulatedSigner();
3136
const tokenGenerator = new FirebaseTokenGenerator(signer);
@@ -63,7 +68,7 @@ describe('createSessionCookie()', () => {
6368
describe('verifySessionCookie()', () => {
6469
const uid = sessionCookieUids[0];
6570
const keyStorer = new InMemoryKeyStorer('cache-key');
66-
const auth = new BaseAuth(projectId, keyStorer, new EmulatorCredential());
71+
const auth = new BaseAuth(projectId, keyStorer, new NopCredential());
6772
it('fails when called with an invalid session cookie', async () => {
6873
await expect(auth.verifySessionCookie('invalid-token')).rejects.toThrowError(FirebaseAuthError);
6974
});

tests/client.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { BaseClient, buildApiUrl } from '../src/client';
44
import type { EmulatorEnv } from '../src/emulator';
55
import { FirebaseAppError } from '../src/errors';
66
import { fetchMock } from './fetch';
7+
import { NopCredential } from './firebase-utils';
78

89
describe('buildApiUrl', () => {
910
it('should build correct url for production environment', () => {
@@ -55,7 +56,7 @@ describe('BaseClient', () => {
5556
})
5657
.reply(200, JSON.stringify(want));
5758

58-
const client = new TestClient(projectid);
59+
const client = new TestClient(projectid, new NopCredential());
5960
const res = await client.fetch<{ data: string }>(apiSettings, {});
6061

6162
expect(res).toEqual(want);
@@ -80,7 +81,7 @@ describe('BaseClient', () => {
8081
})
8182
.reply(200, JSON.stringify(want));
8283

83-
const client = new TestClient(projectid, {
84+
const client = new TestClient(projectid, new NopCredential(), {
8485
maxRetries: 4,
8586
statusCodes: [503],
8687
maxDelayInMillis: 1000,
@@ -102,7 +103,7 @@ describe('BaseClient', () => {
102103
.reply(503, JSON.stringify(errorResponse))
103104
.times(5);
104105

105-
const client = new TestClient(projectid, {
106+
const client = new TestClient(projectid, new NopCredential(), {
106107
maxRetries: 4,
107108
statusCodes: [503],
108109
maxDelayInMillis: 1000,

tests/firebase-utils.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import type { Credential } from '../src';
12
import { encodeBase64Url } from '../src/base64';
3+
import type { GoogleOAuthAccessToken } from '../src/credential';
24
import type { EmulatorEnv } from '../src/emulator';
35
import { emulatorHost } from '../src/emulator';
46
import { AuthClientErrorCode, FirebaseAuthError } from '../src/errors';
@@ -262,3 +264,12 @@ export async function signInWithCustomToken(
262264
const json = await res.json();
263265
return json as signInWithCustomTokenResponse;
264266
}
267+
268+
export class NopCredential implements Credential {
269+
getAccessToken(): Promise<GoogleOAuthAccessToken> {
270+
return Promise.resolve({
271+
access_token: 'owner',
272+
expires_in: 9 * 3600,
273+
});
274+
}
275+
}

0 commit comments

Comments
 (0)