release: WRNexusJS 0.5.10
This commit is contained in:
@@ -38,6 +38,13 @@ const DEFAULT_TOKEN_TTL_MS = 300_000;
|
||||
const DEFAULT_MAX_ATTEMPTS = 3;
|
||||
const DEFAULT_MIN_COMPLETION_MS = 800;
|
||||
const DEFAULT_RESPONSE_FIELD = "wrn-captcha-response";
|
||||
const RANDOM_CHALLENGE_TYPES: CaptchaChallengeType[] = [
|
||||
"number",
|
||||
"alpha",
|
||||
"alphanumeric",
|
||||
"calculation",
|
||||
"image",
|
||||
];
|
||||
|
||||
function failure(
|
||||
action: string,
|
||||
@@ -150,13 +157,22 @@ export class DefaultCaptchaEngine implements CaptchaEngine {
|
||||
this.randomBytes = options.randomBytes ?? defaultRandomBytes;
|
||||
for (const generator of options.generators ?? defaultCaptchaGenerators())
|
||||
this.generators.set(generator.type, generator);
|
||||
if (!this.generators.has(this.defaultType))
|
||||
if (this.defaultType !== "random" && !this.generators.has(this.defaultType))
|
||||
throw new Error(`No CAPTCHA generator registered for ${this.defaultType}`);
|
||||
}
|
||||
|
||||
async create(options: CreateCaptchaOptions): Promise<CaptchaChallenge> {
|
||||
const action = assertAction(options.action);
|
||||
const requestedType = options.type ?? this.defaultType;
|
||||
const configuredType = options.type ?? this.defaultType;
|
||||
const randomInt = (min: number, max: number): number =>
|
||||
randomInteger(this.randomBytes, min, max);
|
||||
const randomTypes = RANDOM_CHALLENGE_TYPES.filter((type) => this.generators.has(type));
|
||||
if (configuredType === "random" && randomTypes.length === 0)
|
||||
throw new Error("No interactive CAPTCHA generators are registered");
|
||||
const requestedType =
|
||||
configuredType === "random"
|
||||
? randomTypes[randomInt(0, randomTypes.length - 1)]
|
||||
: configuredType;
|
||||
const requestedPresentation =
|
||||
options.presentation ??
|
||||
(requestedType === "honeypot" || requestedType === "timing" || requestedType === "not-robot"
|
||||
@@ -169,8 +185,6 @@ export class DefaultCaptchaEngine implements CaptchaEngine {
|
||||
const now = this.now();
|
||||
const difficulty = options.difficulty ?? this.defaultDifficulty;
|
||||
const disturbance = normalizeDisturbance(options.disturbance, difficulty);
|
||||
const randomInt = (min: number, max: number): number =>
|
||||
randomInteger(this.randomBytes, min, max);
|
||||
const imageStyle = resolveCaptchaImageStyle({
|
||||
imageStyle: options.imageStyle,
|
||||
allowedStyles: options.allowedStyles,
|
||||
|
||||
@@ -4,6 +4,7 @@ export type CaptchaProviderName =
|
||||
"self-hosted" | "wrnexus-managed" | "turnstile" | "recaptcha" | "hcaptcha" | (string & {});
|
||||
|
||||
export type CaptchaChallengeType =
|
||||
| "random"
|
||||
| "number"
|
||||
| "alpha"
|
||||
| "alphanumeric"
|
||||
|
||||
Reference in New Issue
Block a user