Skip to content

Commit 9a85bb2

Browse files
committed
Refactor code to make auth headers.
1 parent fd2cb15 commit 9a85bb2

File tree

2 files changed

+32
-27
lines changed

2 files changed

+32
-27
lines changed

packages/functions/src/public-types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ export interface HttpsCallableStreamOptions {
8484
* both the underlying connection and stream will be terminated.
8585
*/
8686
signal?: AbortSignal;
87+
/**
88+
* If set to true, uses limited-use App Check token for callable function requests from this
89+
* instance of {@link Functions}. You must use limited-use tokens to call functions with
90+
* replay protection enabled. By default, this is false.
91+
*/
92+
limitedUseAppCheckTokens?: boolean;
8793
}
8894

8995
/**

packages/functions/src/service.ts

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ export function httpsCallableFromURL<
226226
data?: RequestData | null,
227227
options?: HttpsCallableStreamOptions
228228
) => {
229-
return streamAtURL(functionsInstance, url, options);
229+
return streamAtURL(functionsInstance, url, options || {});
230230
};
231231
return callable as HttpsCallable<RequestData, ResponseData, StreamData>;
232232
}
@@ -275,6 +275,26 @@ async function postJSON(
275275
};
276276
}
277277

278+
async function makeAuthHeaders(
279+
functionsInstance: FunctionsService,
280+
options: HttpsCallableOptions
281+
) {
282+
const headers: Record<string, string> = {};
283+
const context = await functionsInstance.contextProvider.getContext(
284+
options.limitedUseAppCheckTokens
285+
);
286+
if (context.authToken) {
287+
headers['Authorization'] = 'Bearer ' + context.authToken;
288+
}
289+
if (context.messagingToken) {
290+
headers['Firebase-Instance-ID-Token'] = context.messagingToken;
291+
}
292+
if (context.appCheckToken !== null) {
293+
headers['X-Firebase-AppCheck'] = context.appCheckToken;
294+
}
295+
return headers;
296+
}
297+
278298
/**
279299
* Calls a callable function asynchronously and returns the result.
280300
* @param name The name of the callable trigger.
@@ -306,19 +326,7 @@ async function callAtURL(
306326
const body = { data };
307327

308328
// Add a header for the authToken.
309-
const headers: { [key: string]: string } = {};
310-
const context = await functionsInstance.contextProvider.getContext(
311-
options.limitedUseAppCheckTokens
312-
);
313-
if (context.authToken) {
314-
headers['Authorization'] = 'Bearer ' + context.authToken;
315-
}
316-
if (context.messagingToken) {
317-
headers['Firebase-Instance-ID-Token'] = context.messagingToken;
318-
}
319-
if (context.appCheckToken !== null) {
320-
headers['X-Firebase-AppCheck'] = context.appCheckToken;
321-
}
329+
const headers = await makeAuthHeaders(functionsInstance, options);
322330

323331
// Default timeout to 70s, but let the options override it.
324332
const timeout = options.timeout || 70000;
@@ -381,7 +389,7 @@ function stream(
381389
options?: HttpsCallableStreamOptions
382390
): Promise<HttpsCallableStreamResult> {
383391
const url = functionsInstance._url(name);
384-
return streamAtURL(functionsInstance, url, data, options);
392+
return streamAtURL(functionsInstance, url, data, options || {});
385393
}
386394

387395
/**
@@ -394,23 +402,14 @@ async function streamAtURL(
394402
functionsInstance: FunctionsService,
395403
url: string,
396404
data: unknown,
397-
options?: HttpsCallableStreamOptions
405+
options: HttpsCallableStreamOptions
398406
): Promise<HttpsCallableStreamResult> {
399407
// Encode any special types, such as dates, in the input data.
400408
data = encode(data);
401409
const body = { data };
410+
//
402411
// Add a header for the authToken.
403-
const headers: { [key: string]: string } = {};
404-
const context = await functionsInstance.contextProvider.getContext();
405-
if (context.authToken) {
406-
headers['Authorization'] = 'Bearer ' + context.authToken;
407-
}
408-
if (context.messagingToken) {
409-
headers['Firebase-Instance-ID-Token'] = context.messagingToken;
410-
}
411-
if (context.appCheckToken !== null) {
412-
headers['X-Firebase-AppCheck'] = context.appCheckToken;
413-
}
412+
const headers = await makeAuthHeaders(functionsInstance, options);
414413
headers['Content-Type'] = 'application/json';
415414
headers['Accept'] = 'text/event-stream';
416415

0 commit comments

Comments
 (0)