@prosopo/provider 3.13.6 → 3.13.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -341,8 +341,8 @@ class FrictionlessManager extends captchaManager.CaptchaManager {
341
341
  decryptionFailed
342
342
  };
343
343
  }
344
- async getClientEntropy(siteKey) {
345
- return this.db.getClientEntropy(siteKey);
344
+ async getClientContextEntropy(siteKey, contextType) {
345
+ return this.db.getClientContextEntropy(siteKey, contextType);
346
346
  }
347
347
  }
348
348
  exports.DEFAULT_ENTROPY = DEFAULT_ENTROPY;
@@ -1,9 +1,10 @@
1
1
  import { createPrivateKey } from "node:crypto";
2
2
  import { ProsopoApiError } from "@prosopo/common";
3
3
  import { CaptchaDatabase, ClientDatabase } from "@prosopo/database";
4
- import { ScheduledTaskNames, ScheduledTaskStatus } from "@prosopo/types";
4
+ import { ScheduledTaskNames, ScheduledTaskStatus, Tier } from "@prosopo/types";
5
5
  import { majorityAverage, parseUrl } from "@prosopo/util";
6
6
  import { validateSiteKey } from "../../api/validateAddress.js";
7
+ const SAMPLE_SIZE = 100;
7
8
  const isValidPrivateKey = (privateKeyString) => {
8
9
  const privateKey = Buffer.from(privateKeyString, "base64").toString("ascii");
9
10
  try {
@@ -210,14 +211,39 @@ class ClientTaskManager {
210
211
  ScheduledTaskStatus.Running
211
212
  );
212
213
  try {
213
- const clients = await this.providerDB.getAllClientRecords();
214
+ let clients = await this.providerDB.getAllClientRecords();
215
+ clients = clients.filter((client) => client.tier !== Tier.Free);
216
+ this.logger.info(() => ({
217
+ msg: `Calculating entropies for ${clients.length} clients`
218
+ }));
214
219
  for (const client of clients) {
215
- const sampleEntropies = await this.providerDB.sampleEntropy(
216
- 100,
217
- client.account
218
- );
219
- const avgEntropy = majorityAverage(sampleEntropies);
220
- await this.providerDB.setClientEntropy(client.account, avgEntropy);
220
+ if (client.settings?.contextAware?.enabled) {
221
+ const contextTypes = Object.keys(
222
+ client.settings.contextAware.contexts ?? {}
223
+ );
224
+ for (const contextType of contextTypes) {
225
+ const contextSamples = await this.providerDB.sampleContextEntropy(
226
+ SAMPLE_SIZE,
227
+ client.account,
228
+ contextType
229
+ );
230
+ if (contextSamples.length < SAMPLE_SIZE) {
231
+ this.logger.info(() => ({
232
+ msg: `Skipping ${contextType} entropy calculation for client ${client.account} due to insufficient samples (${contextSamples.length}/${SAMPLE_SIZE})`
233
+ }));
234
+ continue;
235
+ }
236
+ const contextAvgEntropy = majorityAverage(contextSamples);
237
+ this.logger.info(() => ({
238
+ msg: `Calculated ${contextType} entropy for client ${client.account}: ${contextAvgEntropy}`
239
+ }));
240
+ await this.providerDB.setClientContextEntropy(
241
+ client.account,
242
+ contextType,
243
+ contextAvgEntropy
244
+ );
245
+ }
246
+ }
221
247
  }
222
248
  await this.providerDB.updateScheduledTaskStatus(
223
249
  taskID,