Skip to content

Allow Iam tokens to be passed from the console to the SDK #4685

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 31, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions packages/firestore/src/api/credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface FirstPartyCredentialsSettings {
['type']: 'gapi';
['client']: unknown;
['sessionIndex']: string;
['iamToken']: string | null;
}

export interface ProviderCredentialsSettings {
Expand Down Expand Up @@ -292,7 +293,11 @@ export class FirstPartyToken implements Token {
type = 'FirstParty' as TokenType;
user = User.FIRST_PARTY;

constructor(private gapi: Gapi, private sessionIndex: string) {}
constructor(
private gapi: Gapi,
private sessionIndex: string,
private iamToken: string | null
) {}

get authHeaders(): { [header: string]: string } {
const headers: { [header: string]: string } = {
Expand All @@ -303,6 +308,9 @@ export class FirstPartyToken implements Token {
if (authHeader) {
headers['Authorization'] = authHeader;
}
if (this.iamToken) {
headers['X-Goog-Iam-Authorization-Token'] = this.iamToken;
}
return headers;
}
}
Expand All @@ -313,10 +321,16 @@ export class FirstPartyToken implements Token {
* to applications hosted by Google.
*/
export class FirstPartyCredentialsProvider implements CredentialsProvider {
constructor(private gapi: Gapi, private sessionIndex: string) {}
constructor(
private gapi: Gapi,
private sessionIndex: string,
private iamToken: string | null
) {}

getToken(): Promise<Token | null> {
return Promise.resolve(new FirstPartyToken(this.gapi, this.sessionIndex));
return Promise.resolve(
new FirstPartyToken(this.gapi, this.sessionIndex, this.iamToken)
);
}

setChangeListener(changeListener: CredentialChangeListener): void {
Expand Down Expand Up @@ -355,7 +369,8 @@ export function makeCredentialsProvider(
);
return new FirstPartyCredentialsProvider(
client,
credentials['sessionIndex'] || '0'
credentials['sessionIndex'] || '0',
credentials['iamToken'] || null
);

case 'provider':
Expand Down