@@ -38,6 +38,7 @@ import {
38
38
HttpsError ,
39
39
onCallHandler ,
40
40
Request ,
41
+ AuthData ,
41
42
} from "../../common/providers/https" ;
42
43
import { initV2Endpoint , ManifestEndpoint } from "../../runtime/manifest" ;
43
44
import { GlobalOptions , SupportedRegion } from "../options" ;
@@ -166,7 +167,7 @@ export interface HttpsOptions extends Omit<GlobalOptions, "region" | "enforceApp
166
167
/**
167
168
* Options that can be set on a callable HTTPS function.
168
169
*/
169
- export interface CallableOptions extends HttpsOptions {
170
+ export interface CallableOptions < T = any > extends HttpsOptions {
170
171
/**
171
172
* Determines whether Firebase AppCheck is enforced.
172
173
* When true, requests with invalid tokens autorespond with a 401
@@ -206,8 +207,39 @@ export interface CallableOptions extends HttpsOptions {
206
207
* Defaults to 30 seconds.
207
208
*/
208
209
heartbeatSeconds ?: number | null ;
210
+
211
+ /**
212
+ * Callback for whether a request is authorized.
213
+ *
214
+ * Designed to allow reusable auth policies to be passed as an options object. Two built-in reusable policies exist:
215
+ * isSignedIn and hasClaim.
216
+ */
217
+ authPolicy ?: ( auth : AuthData | null , data : T ) => boolean | Promise < boolean > ;
209
218
}
210
219
220
+ /**
221
+ * An auth policy that requires a user to be signed in.
222
+ */
223
+ export const isSignedIn =
224
+ ( ) =>
225
+ ( auth : AuthData | null ) : boolean =>
226
+ ! ! auth ;
227
+
228
+ /**
229
+ * An auth policy that requires a user to be both signed in and have a specific claim (optionally with a specific value)
230
+ */
231
+ export const hasClaim =
232
+ ( claim : string , value ?: string ) =>
233
+ ( auth : AuthData | null ) : boolean => {
234
+ if ( ! auth ) {
235
+ return false ;
236
+ }
237
+ if ( ! ( claim in auth . token ) ) {
238
+ return false ;
239
+ }
240
+ return ! value || auth . token [ claim ] === value ;
241
+ } ;
242
+
211
243
/**
212
244
* Handles HTTPS requests.
213
245
*/
@@ -233,6 +265,7 @@ export interface CallableFunction<T, Return> extends HttpsFunction {
233
265
*/
234
266
run ( data : CallableRequest < T > ) : Return ;
235
267
}
268
+
236
269
/**
237
270
* Handles HTTPS requests.
238
271
* @param opts - Options to set on this function
@@ -355,7 +388,7 @@ export function onRequest(
355
388
* @returns A function that you can export and deploy.
356
389
*/
357
390
export function onCall < T = any , Return = any | Promise < any > > (
358
- opts : CallableOptions ,
391
+ opts : CallableOptions < T > ,
359
392
handler : ( request : CallableRequest < T > , response ?: CallableProxyResponse ) => Return
360
393
) : CallableFunction < T , Return extends Promise < unknown > ? Return : Promise < Return > > ;
361
394
@@ -368,7 +401,7 @@ export function onCall<T = any, Return = any | Promise<any>>(
368
401
handler : ( request : CallableRequest < T > , response ?: CallableProxyResponse ) => Return
369
402
) : CallableFunction < T , Return extends Promise < unknown > ? Return : Promise < Return > > ;
370
403
export function onCall < T = any , Return = any | Promise < any > > (
371
- optsOrHandler : CallableOptions | ( ( request : CallableRequest < T > ) => Return ) ,
404
+ optsOrHandler : CallableOptions < T > | ( ( request : CallableRequest < T > ) => Return ) ,
372
405
handler ?: ( request : CallableRequest < T > , response ?: CallableProxyResponse ) => Return
373
406
) : CallableFunction < T , Return extends Promise < unknown > ? Return : Promise < Return > > {
374
407
let opts : CallableOptions ;
@@ -388,14 +421,14 @@ export function onCall<T = any, Return = any | Promise<any>>(
388
421
}
389
422
390
423
// fix the length of handler to make the call to handler consistent
391
- const fixedLen = ( req : CallableRequest < T > , resp ?: CallableProxyResponse ) =>
392
- withInit ( handler ) ( req , resp ) ;
424
+ const fixedLen = ( req : CallableRequest < T > , resp ?: CallableProxyResponse ) => handler ( req , resp ) ;
393
425
let func : any = onCallHandler (
394
426
{
395
427
cors : { origin, methods : "POST" } ,
396
428
enforceAppCheck : opts . enforceAppCheck ?? options . getGlobalOptions ( ) . enforceAppCheck ,
397
429
consumeAppCheckToken : opts . consumeAppCheckToken ,
398
430
heartbeatSeconds : opts . heartbeatSeconds ,
431
+ authPolicy : opts . authPolicy ,
399
432
} ,
400
433
fixedLen ,
401
434
"gcfv2"
0 commit comments