Skip to content

Commit 0cab393

Browse files
committed
Better way to apply token headers.
1 parent 56f121c commit 0cab393

File tree

4 files changed

+36
-48
lines changed

4 files changed

+36
-48
lines changed

packages/firestore/src/api/credentials.ts

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,16 @@ export interface Token {
6868
*/
6969
user?: User;
7070

71-
/** Extra header values to be passed along with a request */
72-
headers: { [header: string]: string };
71+
/** Invokes the given callback with the token's key and value. */
72+
applyHeaders(callback: (tokenKey: string, tokenValue: string) => void): void;
7373
}
7474

7575
export class OAuthToken implements Token {
7676
type = 'OAuth' as TokenType;
77-
headers: { [header: string]: string };
78-
constructor(value: string, public user: User) {
79-
this.headers = {};
80-
// Set the headers using Object Literal notation to avoid minification
81-
this.headers['Authorization'] = `Bearer ${value}`;
77+
constructor(private value: string, public user: User) {}
78+
79+
applyHeaders(callback: (tokenKey: string, tokenValue: string) => void): void {
80+
callback('Authorization', `Bearer ${this.value}`);
8281
}
8382
}
8483

@@ -403,19 +402,15 @@ export class FirstPartyToken implements Token {
403402
private iamToken: string | null
404403
) {}
405404

406-
get headers(): { [header: string]: string } {
407-
const result: { [header: string]: string } = {
408-
'X-Goog-AuthUser': this.sessionIndex
409-
};
410-
// Use array notation to prevent minification
405+
applyHeaders(callback: (tokenKey: string, tokenValue: string) => void): void {
406+
callback('X-Goog-AuthUser', this.sessionIndex);
411407
const authHeader = this.gapi['auth']['getAuthHeaderValueForFirstParty']([]);
412408
if (authHeader) {
413-
result['Authorization'] = authHeader;
409+
callback('Authorization', authHeader);
414410
}
415411
if (this.iamToken) {
416-
result['X-Goog-Iam-Authorization-Token'] = this.iamToken;
412+
callback('X-Goog-Iam-Authorization-Token', this.iamToken);
417413
}
418-
return result;
419414
}
420415
}
421416

@@ -454,13 +449,11 @@ export class FirstPartyAuthCredentialsProvider
454449

455450
export class AppCheckToken implements Token {
456451
type = 'AppCheck' as TokenType;
457-
headers: { [header: string]: string };
452+
constructor(private value: string) {}
458453

459-
constructor(value: string) {
460-
this.headers = {};
461-
if (value && value.length > 0) {
462-
// Set the headers using Object Literal notation to avoid minification
463-
this.headers['x-firebase-appcheck'] = value;
454+
applyHeaders(callback: (tokenKey: string, tokenValue: string) => void): void {
455+
if (this.value && this.value.length > 0) {
456+
callback('x-firebase-appcheck', this.value);
464457
}
465458
}
466459
}

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,16 @@ function createMetadata(
5757
authToken === null || authToken.type === 'OAuth',
5858
'If provided, token must be OAuth'
5959
);
60-
6160
const metadata = new Metadata();
62-
for (const token of [authToken, appCheckToken]) {
63-
if (token) {
64-
for (const header in token.headers) {
65-
if (token.headers.hasOwnProperty(header)) {
66-
metadata.set(header, token.headers[header]);
67-
}
68-
}
69-
}
61+
if (authToken) {
62+
authToken.applyHeaders((tokenKey, tokenValue) =>
63+
metadata.set(tokenKey, tokenValue)
64+
);
65+
}
66+
if (appCheckToken) {
67+
appCheckToken.applyHeaders((tokenKey, tokenValue) =>
68+
metadata.set(tokenKey, tokenValue)
69+
);
7070
}
7171
if (appId) {
7272
metadata.set('X-Firebase-GMPID', appId);

packages/firestore/src/remote/rest_connection.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,16 @@ export abstract class RestConnection implements Connection {
143143
if (this.databaseInfo.appId) {
144144
headers['X-Firebase-GMPID'] = this.databaseInfo.appId;
145145
}
146-
for (const token of [authToken, appCheckToken]) {
147-
if (token) {
148-
for (const header in token.headers) {
149-
if (token.headers.hasOwnProperty(header)) {
150-
headers[header] = token.headers[header];
151-
}
152-
}
153-
}
146+
147+
if (authToken) {
148+
authToken.applyHeaders(
149+
(tokenKey, tokenValue) => (headers[tokenKey] = tokenValue)
150+
);
151+
}
152+
if (appCheckToken) {
153+
appCheckToken.applyHeaders(
154+
(tokenKey, tokenValue) => (headers[tokenKey] = tokenValue)
155+
);
154156
}
155157
}
156158

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

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

1818
import { expect } from 'chai';
1919

20-
import { AppCheckToken, Token } from '../../../src/api/credentials';
20+
import { AppCheckToken, OAuthToken, 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';
@@ -87,15 +87,8 @@ describe('RestConnection', () => {
8787
'RunQuery',
8888
'projects/testproject/databases/(default)/documents/foo',
8989
{},
90-
{
91-
user: User.UNAUTHENTICATED,
92-
type: 'OAuth',
93-
headers: { 'Authorization': 'Bearer owner' }
94-
},
95-
{
96-
type: 'AppCheck',
97-
headers: { 'x-firebase-appcheck': 'some-app-check-token' }
98-
}
90+
new OAuthToken('owner', User.UNAUTHENTICATED),
91+
new AppCheckToken('some-app-check-token')
9992
);
10093
expect(connection.lastHeaders).to.deep.equal({
10194
'Authorization': 'Bearer owner',

0 commit comments

Comments
 (0)