Skip to content

Commit 3aa0b36

Browse files
gracebenzGrace Benz
andauthored
Allow Iam tokens to be passed from the console to the SDK (#4685)
* Add support for passing in iamToken * Update types and clean up log statements Co-authored-by: Grace Benz <[email protected]>
1 parent 9a9f647 commit 3aa0b36

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

packages/firestore/src/api/credentials.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export interface FirstPartyCredentialsSettings {
3434
['type']: 'gapi';
3535
['client']: unknown;
3636
['sessionIndex']: string;
37+
['iamToken']: string | null;
3738
}
3839

3940
export interface ProviderCredentialsSettings {
@@ -292,7 +293,11 @@ export class FirstPartyToken implements Token {
292293
type = 'FirstParty' as TokenType;
293294
user = User.FIRST_PARTY;
294295

295-
constructor(private gapi: Gapi, private sessionIndex: string) {}
296+
constructor(
297+
private gapi: Gapi,
298+
private sessionIndex: string,
299+
private iamToken: string | null
300+
) {}
296301

297302
get authHeaders(): { [header: string]: string } {
298303
const headers: { [header: string]: string } = {
@@ -303,6 +308,9 @@ export class FirstPartyToken implements Token {
303308
if (authHeader) {
304309
headers['Authorization'] = authHeader;
305310
}
311+
if (this.iamToken) {
312+
headers['X-Goog-Iam-Authorization-Token'] = this.iamToken;
313+
}
306314
return headers;
307315
}
308316
}
@@ -313,10 +321,16 @@ export class FirstPartyToken implements Token {
313321
* to applications hosted by Google.
314322
*/
315323
export class FirstPartyCredentialsProvider implements CredentialsProvider {
316-
constructor(private gapi: Gapi, private sessionIndex: string) {}
324+
constructor(
325+
private gapi: Gapi,
326+
private sessionIndex: string,
327+
private iamToken: string | null
328+
) {}
317329

318330
getToken(): Promise<Token | null> {
319-
return Promise.resolve(new FirstPartyToken(this.gapi, this.sessionIndex));
331+
return Promise.resolve(
332+
new FirstPartyToken(this.gapi, this.sessionIndex, this.iamToken)
333+
);
320334
}
321335

322336
setChangeListener(changeListener: CredentialChangeListener): void {
@@ -355,7 +369,8 @@ export function makeCredentialsProvider(
355369
);
356370
return new FirstPartyCredentialsProvider(
357371
client,
358-
credentials['sessionIndex'] || '0'
372+
credentials['sessionIndex'] || '0',
373+
credentials['iamToken'] || null
359374
);
360375

361376
case 'provider':

0 commit comments

Comments
 (0)