@@ -12,6 +12,7 @@ import {
1212 beforeUserCreated ,
1313 beforeUserSignedIn ,
1414} from "firebase-functions/v2/identity" ;
15+ import { z } from "zod" ;
1516import { privilegedServiceAccount } from "./helpers.js" ;
1617import { Env } from "../env.js" ;
1718import { Flags } from "../flags.js" ;
@@ -37,9 +38,7 @@ export const beforeUserCreatedFunction = beforeUserCreated(
3738 const userService = factory . user ( ) ;
3839 const credential = event . credential ;
3940
40- logger . info (
41- `${ userId } : About to check existence of credential ${ JSON . stringify ( credential ) } .` ,
42- ) ;
41+ logger . info ( `${ userId } : About to check existence of credential.` ) ;
4342
4443 // Escape hatch for users using invitation code to enroll
4544 if ( ! credential ) {
@@ -49,12 +48,31 @@ export const beforeUserCreatedFunction = beforeUserCreated(
4948 return { customClaims : { } } ;
5049 }
5150
52- logger . info (
53- `${ userId } : About to check email address: ${ JSON . stringify ( event . data ) } .` ,
54- ) ;
51+ let emailAddress : string | undefined = undefined ;
52+ try {
53+ const credentialClaims = event . credential ?. claims ?? { } ;
54+ emailAddress = z
55+ . string ( )
56+ . optional ( )
57+ . parse (
58+ event . data . email ??
59+ credentialClaims [ "upn" ] ??
60+ credentialClaims [ "unique_name" ] ,
61+ ) ;
62+ } catch ( error : unknown ) {
63+ logger . error (
64+ `${ userId } : Email address validation failed ${ String ( error ) } .` ,
65+ ) ;
66+ throw new https . HttpsError (
67+ "invalid-argument" ,
68+ "Email address validation failed." ,
69+ ) ;
70+ }
5571
56- if ( event . data . email === undefined ) {
57- logger . error ( `Email address not set.` ) ;
72+ logger . info ( `${ userId } : About to check email address: ${ emailAddress } .` ) ;
73+
74+ if ( emailAddress === undefined ) {
75+ logger . error ( `${ userId } : Email address not set.` ) ;
5876 throw new https . HttpsError (
5977 "invalid-argument" ,
6078 "Email address is required for user." ,
@@ -79,14 +97,12 @@ export const beforeUserCreatedFunction = beforeUserCreated(
7997 ) ;
8098 }
8199
82- logger . info (
83- `${ userId } : About to get invitation by code: ${ event . data . email } .` ,
84- ) ;
100+ logger . info ( `${ userId } : About to get invitation by code: ${ emailAddress } .` ) ;
85101
86- const invitation = await userService . getInvitationByCode ( event . data . email ) ;
102+ const invitation = await userService . getInvitationByCode ( emailAddress ) ;
87103 if ( invitation ?. content === undefined ) {
88104 logger . error (
89- `${ userId } : No valid invitation code found for user with email ${ event . data . email } .` ,
105+ `${ userId } : No valid invitation code found for user with email ${ emailAddress } .` ,
90106 ) ;
91107 throw new https . HttpsError (
92108 "not-found" ,
0 commit comments