Skip to content

Commit 5cd03a3

Browse files
committed
Rename authHeaders->headers. Add a test for empty AppCheck token.
1 parent 89065b7 commit 5cd03a3

File tree

6 files changed

+50
-25
lines changed

6 files changed

+50
-25
lines changed

packages/firestore/lite/register.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ export function registerFirestore(): void {
4646
const app = container.getProvider('app').getImmediate()!;
4747
const firestoreInstance = new Firestore(
4848
app,
49-
new LiteAuthCredentialsProvider(container.getProvider('auth-internal')),
49+
new LiteAuthCredentialsProvider(
50+
container.getProvider('auth-internal')
51+
),
5052
new LiteAppCheckTokenProvider(
5153
container.getProvider('app-check-internal')
5254
)

packages/firestore/src/api/credentials.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,16 @@ export interface Token {
6969
user?: User;
7070

7171
/** Extra header values to be passed along with a request */
72-
authHeaders: { [header: string]: string };
72+
headers: { [header: string]: string };
7373
}
7474

7575
export class OAuthToken implements Token {
7676
type = 'OAuth' as TokenType;
77-
authHeaders: { [header: string]: string };
77+
headers: { [header: string]: string };
7878
constructor(value: string, public user: User) {
79-
this.authHeaders = {};
79+
this.headers = {};
8080
// Set the headers using Object Literal notation to avoid minification
81-
this.authHeaders['Authorization'] = `Bearer ${value}`;
81+
this.headers['Authorization'] = `Bearer ${value}`;
8282
}
8383
}
8484

@@ -144,7 +144,9 @@ export class EmptyAuthCredentialsProvider implements CredentialsProvider<User> {
144144
* A CredentialsProvider that always returns a constant token. Used for
145145
* emulator token mocking.
146146
*/
147-
export class EmulatorAuthCredentialsProvider implements CredentialsProvider<User> {
147+
export class EmulatorAuthCredentialsProvider
148+
implements CredentialsProvider<User>
149+
{
148150
constructor(private token: Token) {}
149151

150152
/**
@@ -219,7 +221,9 @@ export class LiteAuthCredentialsProvider implements CredentialsProvider<User> {
219221
shutdown(): void {}
220222
}
221223

222-
export class FirebaseAuthCredentialsProvider implements CredentialsProvider<User> {
224+
export class FirebaseAuthCredentialsProvider
225+
implements CredentialsProvider<User>
226+
{
223227
/**
224228
* The auth token listener registered with FirebaseApp, retained here so we
225229
* can unregister it.
@@ -399,19 +403,19 @@ export class FirstPartyToken implements Token {
399403
private iamToken: string | null
400404
) {}
401405

402-
get authHeaders(): { [header: string]: string } {
403-
const headers: { [header: string]: string } = {
406+
get headers(): { [header: string]: string } {
407+
const result: { [header: string]: string } = {
404408
'X-Goog-AuthUser': this.sessionIndex
405409
};
406410
// Use array notation to prevent minification
407411
const authHeader = this.gapi['auth']['getAuthHeaderValueForFirstParty']([]);
408412
if (authHeader) {
409-
headers['Authorization'] = authHeader;
413+
result['Authorization'] = authHeader;
410414
}
411415
if (this.iamToken) {
412-
headers['X-Goog-Iam-Authorization-Token'] = this.iamToken;
416+
result['X-Goog-Iam-Authorization-Token'] = this.iamToken;
413417
}
414-
return headers;
418+
return result;
415419
}
416420
}
417421

@@ -450,13 +454,13 @@ export class FirstPartyAuthCredentialsProvider
450454

451455
export class AppCheckToken implements Token {
452456
type = 'AppCheck' as TokenType;
453-
authHeaders: { [header: string]: string };
457+
headers: { [header: string]: string };
454458

455459
constructor(value: string) {
456-
this.authHeaders = {};
460+
this.headers = {};
457461
if (value && value.length > 0) {
458462
// Set the headers using Object Literal notation to avoid minification
459-
this.authHeaders['x-firebase-appcheck'] = value;
463+
this.headers['x-firebase-appcheck'] = value;
460464
}
461465
}
462466
}

packages/firestore/src/platform/node/grpc_connection.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ function createMetadata(
5252
const metadata = new Metadata();
5353
for (const token of [authToken, appCheckToken]) {
5454
if (token) {
55-
for (const header in token.authHeaders) {
56-
if (token.authHeaders.hasOwnProperty(header)) {
57-
metadata.set(header, token.authHeaders[header]);
55+
for (const header in token.headers) {
56+
if (token.headers.hasOwnProperty(header)) {
57+
metadata.set(header, token.headers[header]);
5858
}
5959
}
6060
}

packages/firestore/src/remote/rest_connection.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,9 @@ export abstract class RestConnection implements Connection {
145145
}
146146
for (const token of [authToken, appCheckToken]) {
147147
if (token) {
148-
for (const header in token.authHeaders) {
149-
if (token.authHeaders.hasOwnProperty(header)) {
150-
headers[header] = token.authHeaders[header];
148+
for (const header in token.headers) {
149+
if (token.headers.hasOwnProperty(header)) {
150+
headers[header] = token.headers[header];
151151
}
152152
}
153153
}

packages/firestore/test/integration/remote/stream.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717

1818
import { expect } from 'chai';
1919

20-
import { EmptyAuthCredentialsProvider, Token } from '../../../src/api/credentials';
20+
import {
21+
EmptyAuthCredentialsProvider,
22+
Token
23+
} from '../../../src/api/credentials';
2124
import { SnapshotVersion } from '../../../src/core/snapshot_version';
2225
import { MutationResult } from '../../../src/model/mutation';
2326
import {

packages/firestore/test/unit/remote/rest_connection.test.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import { expect } from 'chai';
1919

20-
import { Token } from '../../../src/api/credentials';
20+
import { AppCheckToken, Token } from '../../../src/api/credentials';
2121
import { User } from '../../../src/auth/user';
2222
import { DatabaseId, DatabaseInfo } from '../../../src/core/database_info';
2323
import { SDK_VERSION } from '../../../src/core/version';
@@ -90,11 +90,11 @@ describe('RestConnection', () => {
9090
{
9191
user: User.UNAUTHENTICATED,
9292
type: 'OAuth',
93-
authHeaders: { 'Authorization': 'Bearer owner' }
93+
headers: { 'Authorization': 'Bearer owner' }
9494
},
9595
{
9696
type: 'AppCheck',
97-
authHeaders: { 'x-firebase-appcheck': 'some-app-check-token' }
97+
headers: { 'x-firebase-appcheck': 'some-app-check-token' }
9898
}
9999
);
100100
expect(connection.lastHeaders).to.deep.equal({
@@ -106,6 +106,22 @@ describe('RestConnection', () => {
106106
});
107107
});
108108

109+
it('empty app check token is not added to headers', async () => {
110+
await connection.invokeRPC(
111+
'RunQuery',
112+
'projects/testproject/databases/(default)/documents/foo',
113+
{},
114+
null,
115+
new AppCheckToken('')
116+
);
117+
expect(connection.lastHeaders).to.deep.equal({
118+
'Content-Type': 'text/plain',
119+
'X-Firebase-GMPID': 'test-app-id',
120+
'X-Goog-Api-Client': `gl-js/ fire/${SDK_VERSION}`
121+
// Note: AppCheck token should not exist here.
122+
});
123+
});
124+
109125
it('returns success', async () => {
110126
connection.nextResponse = Promise.resolve({ response: true });
111127
const response = await connection.invokeRPC(

0 commit comments

Comments
 (0)