|
| 1 | +const apiurl = import.meta.env.VITE_FAKE_BACKEND_APIURL; |
| 2 | +const flowid = import.meta.env.VITE_FAKE_BACKEND_FLOWID; |
| 3 | +const apikey = import.meta.env.VITE_FAKE_BACKEND_APIKEY; |
| 4 | + |
| 5 | +const defaultHeader = { |
| 6 | + "Content-Type": "application/json", |
| 7 | + "x-api-key": apikey, |
| 8 | + "api-version": "1.0", |
| 9 | +}; |
| 10 | + |
| 11 | +// Call Incode's `omni/start` API to create an Incode session which will include a |
| 12 | +// token in the response. |
| 13 | +const fakeBackendStart = async function () { |
| 14 | + const url = `${apiurl}/omni/start`; |
| 15 | + const params = { |
| 16 | + configurationId: flowid, |
| 17 | + // language: "en-US", |
| 18 | + // redirectionUrl: "https://example.com?custom_parameter=some+value", |
| 19 | + // externalCustomerId: "the id of the customer in your system", |
| 20 | + }; |
| 21 | + |
| 22 | + let response; |
| 23 | + try { |
| 24 | + response = await fetch(url, { method: "POST", body: JSON.stringify(params), headers: defaultHeader }); |
| 25 | + if (!response.ok) { |
| 26 | + throw new Error("Request failed with code " + response.status); |
| 27 | + } |
| 28 | + } catch (e) { |
| 29 | + throw new Error("HTTP Post Error: " + e.message); |
| 30 | + } |
| 31 | + |
| 32 | + // The session response has many values, but you should only pass the token to the frontend. |
| 33 | + const responseData = await response.json(); |
| 34 | + const { token, interviewId } = responseData; |
| 35 | + |
| 36 | + // Store session in local DB, session will be created as used: false. |
| 37 | + await addSession(interviewId, token); |
| 38 | + |
| 39 | + return { token, interviewId }; |
| 40 | +}; |
| 41 | + |
| 42 | +// Finishes the session started at /start |
| 43 | +const fakeBackendFinish = async function (token) { |
| 44 | + const url = `${apiurl}/omni/finish-status`; |
| 45 | + |
| 46 | + let sessionHeaders = { ...defaultHeader }; |
| 47 | + sessionHeaders["X-Incode-Hardware-Id"] = token; |
| 48 | + |
| 49 | + let response; |
| 50 | + try { |
| 51 | + response = await fetch(url, { method: "GET", headers: sessionHeaders }); |
| 52 | + if (!response.ok) { |
| 53 | + throw new Error("Request failed with code " + response.status); |
| 54 | + } |
| 55 | + } catch (e) { |
| 56 | + throw new Error("HTTP Post Error: " + e.message); |
| 57 | + } |
| 58 | + const { redirectionUrl, action } = await response.json(); |
| 59 | + return { redirectionUrl, action }; |
| 60 | +}; |
| 61 | + |
| 62 | +const fakeBackendValidateAuthentication = async function (interviewId, token, candidateId) { |
| 63 | + const session = await getSession(interviewId); |
| 64 | + |
| 65 | + if (!session) { |
| 66 | + return { |
| 67 | + message: "No session found for interviewId " + interviewId, |
| 68 | + valid: false, |
| 69 | + }; |
| 70 | + } |
| 71 | + // Prevents reuse of the same session. |
| 72 | + if (session.used) { |
| 73 | + return { |
| 74 | + message: "Session already used for interviewId " + interviewId, |
| 75 | + valid: false, |
| 76 | + }; |
| 77 | + } |
| 78 | + |
| 79 | + // Prevents usage of token from another interviewId. |
| 80 | + if (session.token !== token) { |
| 81 | + return { |
| 82 | + message: "Token mismatch for interviewId " + interviewId, |
| 83 | + valid: false, |
| 84 | + }; |
| 85 | + } |
| 86 | + |
| 87 | + let identityId, scoreStatus; |
| 88 | + try { |
| 89 | + // At this point we already verified that the token matches, but |
| 90 | + // to be clear about our intentions, we use the token stored in the |
| 91 | + // database to get the identityId and compare it with the candidateId. |
| 92 | + const scoreResponse = await fakeBackendGetScore(session.token); |
| 93 | + identityId = scoreResponse.authentication.identityId; |
| 94 | + scoreStatus = scoreResponse.overall.status; |
| 95 | + } catch (e) { |
| 96 | + // If there is an error communicating with API, we consider validation failed. |
| 97 | + return { |
| 98 | + message: "Error validating authentication for interviewId " + interviewId + ": " + e.message, |
| 99 | + valid: false, |
| 100 | + }; |
| 101 | + } |
| 102 | + |
| 103 | + // renderFaceAuth returns candidateId, which should match identityId from score, |
| 104 | + // this prevents tampering of the identityId in the frontend. |
| 105 | + if (identityId !== candidateId) { |
| 106 | + return { |
| 107 | + message: "Session data doesn't match for interviewId " + interviewId, |
| 108 | + valid: false, |
| 109 | + }; |
| 110 | + } |
| 111 | + |
| 112 | + // If backend score overall status is not OK, validation fails. |
| 113 | + if (scoreStatus !== "OK") { |
| 114 | + return { |
| 115 | + message: "Face Validation failed for interviewId " + interviewId, |
| 116 | + valid: false, |
| 117 | + }; |
| 118 | + } |
| 119 | + |
| 120 | + // Mark session as used so it can't be used again |
| 121 | + await markSessionAsUsed(interviewId); |
| 122 | + |
| 123 | + // Only valid if all checks passed, we return the identityId that was validated. |
| 124 | + return { |
| 125 | + message: "Face Validation succeeded for interviewId " + interviewId, |
| 126 | + valid: true, |
| 127 | + identityId: identityId, |
| 128 | + }; |
| 129 | +}; |
| 130 | + |
| 131 | +// Finishes the session started at /start |
| 132 | +const fakeBackendGetScore = async function (token) { |
| 133 | + const url = `${apiurl}/omni/get/score`; |
| 134 | + |
| 135 | + let sessionHeaders = { ...defaultHeader }; |
| 136 | + sessionHeaders["X-Incode-Hardware-Id"] = token; |
| 137 | + |
| 138 | + let response; |
| 139 | + try { |
| 140 | + response = await fetch(url, { method: "GET", headers: sessionHeaders }); |
| 141 | + if (!response.ok) { |
| 142 | + throw new Error("Request failed with code " + response.status); |
| 143 | + } |
| 144 | + } catch (e) { |
| 145 | + throw new Error("HTTP Post Error: " + e.message); |
| 146 | + } |
| 147 | + const score = await response.json(); |
| 148 | + /* Example score |
| 149 | + { |
| 150 | + "authentication": { |
| 151 | + "overall": { |
| 152 | + "value": "89.2", |
| 153 | + "status": "OK" |
| 154 | + }, |
| 155 | + "identityId": "68c851bedd5176f7d8bf4758" |
| 156 | + }, |
| 157 | + "liveness": { |
| 158 | + "physicalAttack": { |
| 159 | + "value": "100.0", |
| 160 | + "status": "OK" |
| 161 | + }, |
| 162 | + "spoofDetectionMethod": "SF", |
| 163 | + "overall": { |
| 164 | + "value": "100.0", |
| 165 | + "status": "OK" |
| 166 | + }, |
| 167 | + "livenessScore": { |
| 168 | + "value": "100.0", |
| 169 | + "status": "OK" |
| 170 | + } |
| 171 | + }, |
| 172 | + "deviceRisk": { |
| 173 | + "overall": { |
| 174 | + "status": "UNKNOWN" |
| 175 | + } |
| 176 | + }, |
| 177 | + "behavioralRisk": { |
| 178 | + "overall": { |
| 179 | + "status": "UNKNOWN" |
| 180 | + } |
| 181 | + }, |
| 182 | + "retryInfo": {}, |
| 183 | + "documentOnEdgeInfo": {}, |
| 184 | + "sessionRecording": { |
| 185 | + "mergedRecordingQualityChecks": {} |
| 186 | + }, |
| 187 | + "reasonMsg": "This session passed because it passed all of Incode's tests: Liveness Detection", |
| 188 | + "overall": { |
| 189 | + "value": "94.6", |
| 190 | + "status": "OK" |
| 191 | + } |
| 192 | + } |
| 193 | + */ |
| 194 | + return score; |
| 195 | +}; |
| 196 | + |
| 197 | +/** Helper functions for sessions saving and retrieval from IndexedDB, |
| 198 | + * in production this should be handled with your backend or secure storage */ |
| 199 | + |
| 200 | +// Local database helper functions using IndexedDB |
| 201 | +const DB_NAME = "AuthenticationDB"; |
| 202 | +const DB_VERSION = 1; |
| 203 | +const STORE_NAME = "sessions"; |
| 204 | + |
| 205 | +// Initialize IndexedDB |
| 206 | +function initDB() { |
| 207 | + return new Promise((resolve, reject) => { |
| 208 | + const request = indexedDB.open(DB_NAME, DB_VERSION); |
| 209 | + |
| 210 | + request.onerror = () => reject(request.error); |
| 211 | + request.onsuccess = () => resolve(request.result); |
| 212 | + |
| 213 | + request.onupgradeneeded = (event) => { |
| 214 | + const db = event.target.result; |
| 215 | + if (!db.objectStoreNames.contains(STORE_NAME)) { |
| 216 | + const objectStore = db.createObjectStore(STORE_NAME, { keyPath: "interviewId" }); |
| 217 | + objectStore.createIndex("interviewId", "interviewId", { unique: true }); |
| 218 | + } |
| 219 | + }; |
| 220 | + }); |
| 221 | +} |
| 222 | + |
| 223 | +// Read a specific session from IndexedDB by interviewId |
| 224 | +async function getSession(interviewId) { |
| 225 | + const db = await initDB(); |
| 226 | + return new Promise((resolve, reject) => { |
| 227 | + const transaction = db.transaction([STORE_NAME], "readonly"); |
| 228 | + const objectStore = transaction.objectStore(STORE_NAME); |
| 229 | + const request = objectStore.get(interviewId); |
| 230 | + |
| 231 | + request.onsuccess = () => resolve(request.result); |
| 232 | + request.onerror = () => reject(request.error); |
| 233 | + }); |
| 234 | +} |
| 235 | + |
| 236 | +// Add a new session to the database |
| 237 | +async function addSession(interviewId, token) { |
| 238 | + const db = await initDB(); |
| 239 | + return new Promise((resolve, reject) => { |
| 240 | + const transaction = db.transaction([STORE_NAME], "readwrite"); |
| 241 | + const objectStore = transaction.objectStore(STORE_NAME); |
| 242 | + const session = { |
| 243 | + interviewId, |
| 244 | + token, |
| 245 | + used: false, |
| 246 | + timestamp: new Date().toISOString(), |
| 247 | + }; |
| 248 | + const request = objectStore.add(session); |
| 249 | + |
| 250 | + request.onsuccess = () => resolve(session); |
| 251 | + request.onerror = () => reject(request.error); |
| 252 | + }); |
| 253 | +} |
| 254 | + |
| 255 | +// Update validation status for a session |
| 256 | +async function markSessionAsUsed(interviewId) { |
| 257 | + const db = await initDB(); |
| 258 | + return new Promise((resolve, reject) => { |
| 259 | + const transaction = db.transaction([STORE_NAME], "readwrite"); |
| 260 | + const objectStore = transaction.objectStore(STORE_NAME); |
| 261 | + const getRequest = objectStore.get(interviewId); |
| 262 | + |
| 263 | + getRequest.onsuccess = () => { |
| 264 | + const session = getRequest.result; |
| 265 | + if (session) { |
| 266 | + session.used = true; |
| 267 | + const updateRequest = objectStore.put(session); |
| 268 | + updateRequest.onsuccess = () => resolve(session); |
| 269 | + updateRequest.onerror = () => reject(updateRequest.error); |
| 270 | + } else { |
| 271 | + resolve(null); |
| 272 | + } |
| 273 | + }; |
| 274 | + getRequest.onerror = () => reject(getRequest.error); |
| 275 | + }); |
| 276 | +} |
| 277 | + |
| 278 | +export { fakeBackendStart, fakeBackendFinish, fakeBackendGetScore, fakeBackendValidateAuthentication }; |
0 commit comments