Skip to content

Commit 55b1c0a

Browse files
committed
Added code to pass on credentials if using a cloud workstation
1 parent 4f23f33 commit 55b1c0a

File tree

9 files changed

+61
-17
lines changed

9 files changed

+61
-17
lines changed

common/api-review/util.api.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,11 @@ export function isBrowserExtension(): boolean;
269269
// @public
270270
export function isCloudflareWorker(): boolean;
271271

272+
// Warning: (ae-missing-release-tag) "isCloudWorkstation" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
273+
//
274+
// @public (undocumented)
275+
export function isCloudWorkstation(url: string): boolean;
276+
272277
// Warning: (ae-missing-release-tag) "isElectron" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
273278
//
274279
// @public

packages/app-check/src/client.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { FirebaseApp } from '@firebase/app';
2525
import { ERROR_FACTORY, AppCheckError } from './errors';
2626
import { Provider } from '@firebase/component';
2727
import { AppCheckTokenInternal } from './types';
28+
import { isCloudWorkstation } from '@firebase/util';
2829

2930
/**
3031
* Response JSON returned from AppCheck server endpoint.
@@ -62,6 +63,9 @@ export async function exchangeToken(
6263
body: JSON.stringify(body),
6364
headers
6465
};
66+
if (isCloudWorkstation(url)) {
67+
options.credentials = 'include';
68+
}
6569
let response;
6670
try {
6771
response = await fetch(url, options);

packages/auth/src/api/index.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { FirebaseError, isCloudflareWorker, querystring } from '@firebase/util';
18+
import {
19+
FirebaseError,
20+
isCloudflareWorker,
21+
isCloudWorkstation,
22+
querystring
23+
} from '@firebase/util';
1924

2025
import { AuthErrorCode, NamedErrorParams } from '../core/errors';
2126
import {
@@ -33,6 +38,7 @@ import { IdTokenMfaResponse } from './authentication/mfa';
3338
import { SERVER_ERROR_MAP, ServerError, ServerErrorMap } from './errors';
3439
import { PersistenceType } from '../core/persistence';
3540
import { CookiePersistence } from '../platform_browser/persistence/cookie_storage';
41+
import { FirebaseAuth } from '@firebase/auth-types';
3642

3743
export const enum HttpMethod {
3844
POST = 'POST',
@@ -177,6 +183,10 @@ export async function _performApiRequest<T, V>(
177183
fetchArgs.referrerPolicy = 'no-referrer';
178184
}
179185

186+
if (auth.emulatorConfig && isCloudWorkstation(auth.emulatorConfig.host)) {
187+
fetchArgs.credentials = 'include';
188+
}
189+
180190
return FetchProvider.fetch()(
181191
await _getFinalTarget(auth, auth.config.apiHost, path, query),
182192
fetchArgs

packages/data-connect/src/network/fetch.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { SDK_VERSION } from '../core/version';
2525
import { logDebug, logError } from '../logger';
2626

2727
import { CallerSdkType, CallerSdkTypeEnum } from './transport';
28+
import { isCloudWorkstation } from '@firebase/util';
2829

2930
let connectFetch: typeof fetch | null = globalThis.fetch;
3031
export function initializeFetch(fetchImpl: typeof fetch): void {
@@ -77,14 +78,18 @@ export function dcFetch<T, U>(
7778
headers['X-Firebase-AppCheck'] = appCheckToken;
7879
}
7980
const bodyStr = JSON.stringify(body);
80-
logDebug(`Making request out to ${url} with body: ${bodyStr}`);
81-
82-
return connectFetch(url, {
81+
const fetchOptions: RequestInit = {
8382
body: bodyStr,
8483
method: 'POST',
8584
headers,
8685
signal
87-
})
86+
};
87+
if (isCloudWorkstation(url)) {
88+
fetchOptions.credentials = 'include';
89+
}
90+
logDebug(`Making request out to ${url} with body: ${bodyStr}`);
91+
92+
return connectFetch(url, fetchOptions)
8893
.catch(err => {
8994
throw new DataConnectError(
9095
Code.OTHER,

packages/firestore/src/platform/browser/webchannel_connection.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import { Code, FirestoreError } from '../../util/error';
4646
import { logDebug, logWarn } from '../../util/log';
4747
import { Rejecter, Resolver } from '../../util/promise';
4848
import { StringMap } from '../../util/types';
49+
import { isCloudWorkstation } from '@firebase/util';
4950

5051
const LOG_TAG = 'WebChannelConnection';
5152

packages/firestore/src/platform/browser_lite/fetch_connection.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18+
import { isCloudWorkstation } from '@firebase/util';
1819
import { Token } from '../../api/credentials';
1920
import { Stream } from '../../remote/connection';
2021
import { RestConnection } from '../../remote/rest_connection';
@@ -44,11 +45,15 @@ export class FetchConnection extends RestConnection {
4445
let response: Response;
4546

4647
try {
47-
response = await fetch(url, {
48+
const fetchArgs: RequestInit = {
4849
method: 'POST',
4950
headers,
5051
body: requestJson
51-
});
52+
};
53+
if (isCloudWorkstation(url)) {
54+
fetchArgs.credentials = 'include';
55+
}
56+
response = await fetch(url, fetchArgs);
5257
} catch (e) {
5358
const err = e as { status: number | undefined; statusText: string };
5459
throw new FirestoreError(

packages/storage/src/platform/node/connection.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18+
import { isCloudWorkstation } from '@firebase/util';
1819
import {
1920
Connection,
2021
ConnectionType,
@@ -57,11 +58,7 @@ abstract class FetchConnection<T extends ConnectionType>
5758
this.sent_ = true;
5859

5960
try {
60-
const response = await fetch(url, {
61-
method,
62-
headers: headers || {},
63-
body: body as NodeJS.ArrayBufferView | string
64-
});
61+
const response = await newFetch(url, method, headers, body);
6562
this.headers_ = response.headers;
6663
this.statusCode_ = response.status;
6764
this.errorCode_ = ErrorCode.NO_ERROR;
@@ -161,11 +158,7 @@ export class FetchStreamConnection extends FetchConnection<
161158
this.sent_ = true;
162159

163160
try {
164-
const response = await fetch(url, {
165-
method,
166-
headers: headers || {},
167-
body: body as NodeJS.ArrayBufferView | string
168-
});
161+
const response = await newFetch(url, method, headers, body);
169162
this.headers_ = response.headers;
170163
this.statusCode_ = response.status;
171164
this.errorCode_ = ErrorCode.NO_ERROR;
@@ -186,6 +179,23 @@ export class FetchStreamConnection extends FetchConnection<
186179
}
187180
}
188181

182+
function newFetch(
183+
url: string,
184+
method: string,
185+
headers?: Record<string, string>,
186+
body?: NodeJS.ArrayBufferView | Blob | string
187+
) {
188+
const fetchArgs: RequestInit = {
189+
method,
190+
headers: headers || {},
191+
body: body as NodeJS.ArrayBufferView | string
192+
};
193+
if (isCloudWorkstation(url)) {
194+
fetchArgs.credentials = 'include';
195+
}
196+
return fetch(url, fetchArgs);
197+
}
198+
189199
export function newStreamConnection(): Connection<ReadableStream<Uint8Array>> {
190200
return new FetchStreamConnection();
191201
}

packages/util/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,4 @@ export * from './src/exponential_backoff';
3737
export * from './src/formatters';
3838
export * from './src/compat';
3939
export * from './src/global';
40+
export * from './src/url';

packages/util/src/url.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function isCloudWorkstation(url: string) {
2+
return url.endsWith('cloudworkstations.dev');
3+
}

0 commit comments

Comments
 (0)