@@ -4,9 +4,12 @@ import { setAPIKey, setEmbedHost } from "@toruslabs/http-helpers";
44import { config } from "./config" ;
55import {
66 bigintToHex ,
7+ buildAuditPayload ,
78 bytesToHex ,
89 callAllowApi ,
10+ callAuditApi ,
911 CitadelAllowParams ,
12+ CitadelAuthFlowAuditParams ,
1013 Curve ,
1114 encodeEd25519Point ,
1215 generateAddressFromPubKey ,
@@ -150,17 +153,24 @@ class Torus {
150153 extraParams . session_token_exp_second = Torus . sessionTime ;
151154 }
152155
156+ const recordId = params . recordId || generateRecordId ( ) ;
157+
153158 const allowParams = {
154159 buildEnv : this . buildEnv ,
155160 verifier,
156161 verifierId : verifierParams . verifier_id ,
157162 network : this . network ,
158163 clientId : this . clientId ,
159164 source : this . source ,
160- recordId : generateRecordId ( ) ,
165+ recordId,
161166 } ;
162167
163168 let result : TorusKey ;
169+
170+ // report oauth completed
171+ // if recordId isn't provided in the params, we will also report oauth initiated
172+ this . reportUserAuthFlowAudit ( { ...params , recordId } , { oauthCompleted : true , ...( params . recordId ? { } : { oauthInitiated : true } ) } ) ;
173+
164174 try {
165175 result = await retrieveOrImportShare ( {
166176 recordId : allowParams . recordId ,
@@ -185,8 +195,13 @@ class Torus {
185195 checkCommitment,
186196 source : this . source ,
187197 } ) ;
198+
199+ // report oauth verified
200+ this . reportUserAuthFlowAudit ( { ...params , recordId } , { oauthVerified : true , ...( params . recordId ? { } : { oauthInitiated : true } ) } ) ;
188201 } catch ( error ) {
189202 this . reportSignerAllow ( { ...allowParams , torusLoginFailed : true } ) ;
203+ // report oauth verification failed
204+ this . reportUserAuthFlowAudit ( { ...params , recordId } , { oauthVerificationFailed : true } ) ;
190205 throw error ;
191206 }
192207
@@ -202,6 +217,21 @@ class Torus {
202217 }
203218 }
204219
220+ /**
221+ * Report user auth flow audit to the citadel server.
222+ * @param recordId - The record id to be used for the analytics tracking.
223+ * @param params - The parameters for the retrieve shares operation.
224+ * @param authStepStatus - The status of the authentication steps.
225+ */
226+ async reportUserAuthFlowAudit ( params : RetrieveSharesParams , authFlowAuditParams : CitadelAuthFlowAuditParams ) : Promise < void > {
227+ try {
228+ const auditParams = buildAuditPayload ( this . network , this . clientId , params , authFlowAuditParams ) ;
229+ await callAuditApi ( this . buildEnv , auditParams ) ;
230+ } catch ( error ) {
231+ log . error ( "Failed to log user auth flow audit" , error ) ;
232+ }
233+ }
234+
205235 async getPublicAddress (
206236 endpoints : string [ ] ,
207237 torusNodePubs : INodePub [ ] ,
@@ -262,8 +292,10 @@ class Torus {
262292 }
263293 }
264294
295+ const recordId = params . recordId || generateRecordId ( ) ;
296+
265297 return retrieveOrImportShare ( {
266- recordId : generateRecordId ( ) ,
298+ recordId,
267299 legacyMetadataHost : this . legacyMetadataHost ,
268300 serverTimeOffset : this . serverTimeOffset ,
269301 enableOneKey : this . enableOneKey ,
0 commit comments