-
Notifications
You must be signed in to change notification settings - Fork 200
/
identity.ts
364 lines (329 loc) · 10.5 KB
/
identity.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// The MIT License (MIT)
//
// Copyright (c) 2022 Firebase
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
/**
* Cloud functions to handle events from Google Cloud Identity Platform.
* @packageDocumentation
*/
import { BlockingFunction } from '../../cloud-functions';
import {
AuthBlockingEvent,
AuthBlockingEventType,
AuthUserRecord,
BeforeCreateResponse,
BeforeSignInResponse,
HttpsError,
wrapHandler,
} from '../../common/providers/identity';
import * as options from '../options';
export { AuthUserRecord, AuthBlockingEvent, HttpsError };
/** @hidden Internally used when parsing the options. */
interface InternalOptions {
opts: options.GlobalOptions;
idToken: boolean;
accessToken: boolean;
refreshToken: boolean;
}
/**
* All function options plus idToken, accessToken, and refreshToken.
*/
export interface BlockingOptions {
/** Pass the ID Token credential to the function. */
idToken?: boolean;
/** Pass the Access Token credential to the function. */
accessToken?: boolean;
/** Pass the Refresh Token credential to the function. */
refreshToken?: boolean;
/**
* Region where functions should be deployed.
*/
region?: options.SupportedRegion | string;
/**
* Amount of memory to allocate to a function.
* A value of null restores the defaults of 256MB.
*/
memory?: options.MemoryOption | null;
/**
* Timeout for the function in sections, possible values are 0 to 540.
* HTTPS functions can specify a higher timeout.
* A value of null restores the default of 60s
* The minimum timeout for a gen 2 function is 1s. The maximum timeout for a
* function depends on the type of function: Event handling functions have a
* maximum timeout of 540s (9 minutes). HTTPS and callable functions have a
* maximum timeout of 36,00s (1 hour). Task queue functions have a maximum
* timeout of 1,800s (30 minutes)
*/
timeoutSeconds?: number | null;
/**
* Min number of actual instances to be running at a given time.
* Instances will be billed for memory allocation and 10% of CPU allocation
* while idle.
* A value of null restores the default min instances.
*/
minInstances?: number | null;
/**
* Max number of instances to be running in parallel.
* A value of null restores the default max instances.
*/
maxInstances?: number | null;
/**
* Number of requests a function can serve at once.
* Can only be applied to functions running on Cloud Functions v2.
* A value of null restores the default concurrency (80 when CPU >= 1, 1 otherwise).
* Concurrency cannot be set to any value other than 1 if `cpu` is less than 1.
* The maximum value for concurrency is 1,000.
*/
concurrency?: number | null;
/**
* Fractional number of CPUs to allocate to a function.
* Defaults to 1 for functions with <= 2GB RAM and increases for larger memory sizes.
* This is different from the defaults when using the gcloud utility and is different from
* the fixed amount assigned in Google Cloud Functions generation 1.
* To revert to the CPU amounts used in gcloud or in Cloud Functions generation 1, set this
* to the value "gcf_gen1"
*/
cpu?: number | 'gcf_gen1';
/**
* Connect cloud function to specified VPC connector.
* A value of null removes the VPC connector
*/
vpcConnector?: string | null;
/**
* Egress settings for VPC connector.
* A value of null turns off VPC connector egress settings
*/
vpcConnectorEgressSettings?: options.VpcEgressSetting | null;
/**
* Specific service account for the function to run as.
* A value of null restores the default service account.
*/
serviceAccount?: string | null;
/**
* Ingress settings which control where this function can be called from.
* A value of null turns off ingress settings.
*/
ingressSettings?: options.IngressSetting | null;
/**
* User labels to set on the function.
*/
labels?: Record<string, string>;
/*
* Secrets to bind to a function.
*/
secrets?: string[];
}
/**
* Handles an event that is triggered before a user is created.
* @param handler - Event handler which is run every time before a user is created
*/
export function beforeUserCreated(
handler: (
event: AuthBlockingEvent
) =>
| BeforeCreateResponse
| Promise<BeforeCreateResponse>
| void
| Promise<void>
): BlockingFunction;
/**
* Handles an event that is triggered before a user is created.
* @param opts - Object containing function options
* @param handler - Event handler which is run every time before a user is created
*/
export function beforeUserCreated(
opts: BlockingOptions,
handler: (
event: AuthBlockingEvent
) =>
| BeforeCreateResponse
| Promise<BeforeCreateResponse>
| void
| Promise<void>
): BlockingFunction;
/**
* Handles an event that is triggered before a user is created
* @param optsOrHandler - Either an object containing function options, or an event handler (run before user creation)
* @param handler? - If defined, an event handler which is run every time before a user is created
*/
export function beforeUserCreated(
optsOrHandler:
| BlockingOptions
| ((
event: AuthBlockingEvent
) =>
| BeforeCreateResponse
| Promise<BeforeCreateResponse>
| void
| Promise<void>),
handler?: (
event: AuthBlockingEvent
) =>
| BeforeCreateResponse
| Promise<BeforeCreateResponse>
| void
| Promise<void>
): BlockingFunction {
return beforeOperation('beforeCreate', optsOrHandler, handler);
}
/**
* Handles an event that is triggered before a user is signed in.
* @param handler - Event handler which is run every time before a user is signed in
*/
export function beforeUserSignedIn(
handler: (
event: AuthBlockingEvent
) =>
| BeforeSignInResponse
| Promise<BeforeSignInResponse>
| void
| Promise<void>
): BlockingFunction;
/**
* Handles an event that is triggered before a user is signed in.
* @param opts - Object containing function options
* @param handler - Event handler which is run every time before a user is signed in
*/
export function beforeUserSignedIn(
opts: BlockingOptions,
handler: (
event: AuthBlockingEvent
) =>
| BeforeSignInResponse
| Promise<BeforeSignInResponse>
| void
| Promise<void>
): BlockingFunction;
/**
* Handles an event that is triggered before a user is signed in.
* @param optsOrHandler - Either an object containing function options, or an event handler (run before user signin)
* @param handler - Event handler which is run every time before a user is signed in
*/
export function beforeUserSignedIn(
optsOrHandler:
| BlockingOptions
| ((
event: AuthBlockingEvent
) =>
| BeforeSignInResponse
| Promise<BeforeSignInResponse>
| void
| Promise<void>),
handler?: (
event: AuthBlockingEvent
) =>
| BeforeSignInResponse
| Promise<BeforeSignInResponse>
| void
| Promise<void>
): BlockingFunction {
return beforeOperation('beforeSignIn', optsOrHandler, handler);
}
/** @hidden */
export function beforeOperation(
eventType: AuthBlockingEventType,
optsOrHandler:
| BlockingOptions
| ((
event: AuthBlockingEvent
) =>
| BeforeCreateResponse
| BeforeSignInResponse
| void
| Promise<BeforeCreateResponse>
| Promise<BeforeSignInResponse>
| Promise<void>),
handler: (
event: AuthBlockingEvent
) =>
| BeforeCreateResponse
| BeforeSignInResponse
| void
| Promise<BeforeCreateResponse>
| Promise<BeforeSignInResponse>
| Promise<void>
): BlockingFunction {
if (!handler || typeof optsOrHandler === 'function') {
handler = optsOrHandler as (
event: AuthBlockingEvent
) =>
| BeforeCreateResponse
| BeforeSignInResponse
| void
| Promise<BeforeCreateResponse>
| Promise<BeforeSignInResponse>
| Promise<void>;
optsOrHandler = {};
}
const { opts, accessToken, idToken, refreshToken } = getOpts(
optsOrHandler as BlockingOptions
);
// Create our own function that just calls the provided function so we know for sure that
// handler takes one argument. This is something common/providers/identity depends on.
const wrappedHandler = (event: AuthBlockingEvent) => handler(event);
const func: any = wrapHandler(eventType, wrappedHandler);
const legacyEventType = `providers/cloud.auth/eventTypes/user.${eventType}`;
/** Endpoint */
const baseOptsEndpoint = options.optionsToEndpoint(
options.getGlobalOptions()
);
const specificOptsEndpoint = options.optionsToEndpoint(opts);
func.__endpoint = {
platform: 'gcfv2',
...baseOptsEndpoint,
...specificOptsEndpoint,
labels: {
...baseOptsEndpoint?.labels,
...specificOptsEndpoint?.labels,
},
blockingTrigger: {
eventType: legacyEventType,
options: {
accessToken,
idToken,
refreshToken,
},
},
};
func.__requiredAPIs = [
{
api: 'identitytoolkit.googleapis.com',
reason: 'Needed for auth blocking functions',
},
];
func.run = handler;
return func;
}
/** @hidden */
export function getOpts(blockingOptions: BlockingOptions): InternalOptions {
const accessToken = blockingOptions.accessToken || false;
const idToken = blockingOptions.idToken || false;
const refreshToken = blockingOptions.refreshToken || false;
const opts = { ...blockingOptions };
delete (opts as any).accessToken;
delete (opts as any).idToken;
delete (opts as any).refreshToken;
return {
opts,
accessToken,
idToken,
refreshToken,
};
}