@atproto/oauth-provider 0.26.0 → 0.27.1
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.
- package/CHANGELOG.md +78 -0
- package/dist/account/account-manager.d.ts +5 -11
- package/dist/account/account-manager.d.ts.map +1 -1
- package/dist/account/account-manager.js +17 -59
- package/dist/account/account-manager.js.map +1 -1
- package/dist/account/account-store.d.ts +2 -8
- package/dist/account/account-store.d.ts.map +1 -1
- package/dist/account/account-store.js.map +1 -1
- package/dist/errors/captcha-rejected-error.d.ts +12 -0
- package/dist/errors/captcha-rejected-error.d.ts.map +1 -0
- package/dist/errors/captcha-rejected-error.js +18 -0
- package/dist/errors/captcha-rejected-error.js.map +1 -0
- package/dist/errors/captcha-required-error.d.ts +9 -0
- package/dist/errors/captcha-required-error.d.ts.map +1 -0
- package/dist/errors/captcha-required-error.js +11 -0
- package/dist/errors/captcha-required-error.js.map +1 -0
- package/dist/errors/oauth-error.d.ts +1 -0
- package/dist/errors/oauth-error.d.ts.map +1 -1
- package/dist/errors/oauth-error.js +3 -0
- package/dist/errors/oauth-error.js.map +1 -1
- package/dist/errors/rate-limit-exceeded-error.d.ts +12 -0
- package/dist/errors/rate-limit-exceeded-error.d.ts.map +1 -0
- package/dist/errors/rate-limit-exceeded-error.js +14 -0
- package/dist/errors/rate-limit-exceeded-error.js.map +1 -0
- package/dist/errors/www-authenticate-error.d.ts +1 -0
- package/dist/errors/www-authenticate-error.d.ts.map +1 -1
- package/dist/errors/www-authenticate-error.js +4 -0
- package/dist/errors/www-authenticate-error.js.map +1 -1
- package/dist/hcaptcha/hcaptcha-client-tokens.d.ts +7 -0
- package/dist/hcaptcha/hcaptcha-client-tokens.d.ts.map +1 -0
- package/dist/hcaptcha/hcaptcha-client-tokens.js +2 -0
- package/dist/hcaptcha/hcaptcha-client-tokens.js.map +1 -0
- package/dist/hcaptcha/hcaptcha-manager.d.ts +23 -0
- package/dist/hcaptcha/hcaptcha-manager.d.ts.map +1 -0
- package/dist/hcaptcha/hcaptcha-manager.js +83 -0
- package/dist/hcaptcha/hcaptcha-manager.js.map +1 -0
- package/dist/hcaptcha/hcaptcha-verify-data.d.ts +15 -0
- package/dist/hcaptcha/hcaptcha-verify-data.d.ts.map +1 -0
- package/dist/hcaptcha/hcaptcha-verify-data.js +2 -0
- package/dist/hcaptcha/hcaptcha-verify-data.js.map +1 -0
- package/dist/lib/hcaptcha.d.ts +5 -11
- package/dist/lib/hcaptcha.d.ts.map +1 -1
- package/dist/lib/hcaptcha.js +12 -20
- package/dist/lib/hcaptcha.js.map +1 -1
- package/dist/lib/http/response.d.ts +1 -1
- package/dist/lib/http/response.d.ts.map +1 -1
- package/dist/lib/http/response.js +15 -17
- package/dist/lib/http/response.js.map +1 -1
- package/dist/lib/identifier.d.ts +3 -0
- package/dist/lib/identifier.d.ts.map +1 -0
- package/dist/lib/identifier.js +4 -0
- package/dist/lib/identifier.js.map +1 -0
- package/dist/oauth-errors.d.ts +3 -0
- package/dist/oauth-errors.d.ts.map +1 -1
- package/dist/oauth-errors.js +3 -0
- package/dist/oauth-errors.js.map +1 -1
- package/dist/oauth-hooks.d.ts +16 -14
- package/dist/oauth-hooks.d.ts.map +1 -1
- package/dist/oauth-hooks.js.map +1 -1
- package/dist/oauth-provider.d.ts +27 -8
- package/dist/oauth-provider.d.ts.map +1 -1
- package/dist/oauth-provider.js +28 -48
- package/dist/oauth-provider.js.map +1 -1
- package/dist/rate-limit/rate-limit-manager.d.ts +39 -0
- package/dist/rate-limit/rate-limit-manager.d.ts.map +1 -0
- package/dist/rate-limit/rate-limit-manager.js +99 -0
- package/dist/rate-limit/rate-limit-manager.js.map +1 -0
- package/dist/rate-limit/rate-limit-policies.d.ts +108 -0
- package/dist/rate-limit/rate-limit-policies.d.ts.map +1 -0
- package/dist/rate-limit/rate-limit-policies.js +90 -0
- package/dist/rate-limit/rate-limit-policies.js.map +1 -0
- package/dist/rate-limit/rate-limiter.d.ts +36 -0
- package/dist/rate-limit/rate-limiter.d.ts.map +1 -0
- package/dist/rate-limit/rate-limiter.js +2 -0
- package/dist/rate-limit/rate-limiter.js.map +1 -0
- package/dist/result/{authorization-result-authorize-page.d.ts → authorization-result-authorize.d.ts} +2 -2
- package/dist/result/authorization-result-authorize.d.ts.map +1 -0
- package/dist/result/authorization-result-authorize.js +2 -0
- package/dist/result/authorization-result-authorize.js.map +1 -0
- package/dist/router/assets/send-authorization-page.d.ts +2 -2
- package/dist/router/assets/send-authorization-page.d.ts.map +1 -1
- package/dist/router/assets/send-authorization-page.js.map +1 -1
- package/dist/router/assets/send-error-page.d.ts.map +1 -1
- package/dist/router/assets/send-error-page.js +3 -2
- package/dist/router/assets/send-error-page.js.map +1 -1
- package/dist/router/create-api-middleware.d.ts.map +1 -1
- package/dist/router/create-api-middleware.js +72 -15
- package/dist/router/create-api-middleware.js.map +1 -1
- package/dist/router/create-authorization-page-middleware.d.ts.map +1 -1
- package/dist/router/create-authorization-page-middleware.js +18 -17
- package/dist/router/create-authorization-page-middleware.js.map +1 -1
- package/dist/router/create-oauth-middleware.d.ts.map +1 -1
- package/dist/router/create-oauth-middleware.js +9 -8
- package/dist/router/create-oauth-middleware.js.map +1 -1
- package/dist/types/email.d.ts +2 -1
- package/dist/types/email.d.ts.map +1 -1
- package/dist/types/email.js.map +1 -1
- package/dist/types/indentifier.d.ts +2 -3
- package/dist/types/indentifier.d.ts.map +1 -1
- package/dist/types/indentifier.js.map +1 -1
- package/package.json +16 -16
- package/dist/account/sign-in-data.d.ts +0 -22
- package/dist/account/sign-in-data.d.ts.map +0 -1
- package/dist/account/sign-in-data.js +0 -15
- package/dist/account/sign-in-data.js.map +0 -1
- package/dist/account/sign-up-input.d.ts +0 -28
- package/dist/account/sign-up-input.d.ts.map +0 -1
- package/dist/account/sign-up-input.js +0 -19
- package/dist/account/sign-up-input.js.map +0 -1
- package/dist/result/authorization-result-authorize-page.d.ts.map +0 -1
- package/dist/result/authorization-result-authorize-page.js +0 -2
- package/dist/result/authorization-result-authorize-page.js.map +0 -1
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { OAuthError } from './oauth-error.js';
|
|
2
|
+
/**
|
|
3
|
+
* The request's hCaptcha token failed verification (e.g. it expired, its score
|
|
4
|
+
* was too high, or hCaptcha could not be reached): the request must be retried
|
|
5
|
+
* with a new token.
|
|
6
|
+
*/
|
|
7
|
+
export declare class CaptchaRejectedError extends OAuthError {
|
|
8
|
+
name: string;
|
|
9
|
+
constructor(error_description?: string, cause?: unknown);
|
|
10
|
+
static from(err: unknown, message?: string): OAuthError;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=captcha-rejected-error.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"captcha-rejected-error.d.ts","sourceRoot":"","sources":["../../src/errors/captcha-rejected-error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAE7C;;;;GAIG;AACH,qBAAa,oBAAqB,SAAQ,UAAU;IAClD,IAAI,SAAyB;IAE7B,YACE,iBAAiB,SAAiC,EAClD,KAAK,CAAC,EAAE,OAAO,EAGhB;IAED,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,UAAU,CAGtD;CACF"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { OAuthError } from './oauth-error.js';
|
|
2
|
+
/**
|
|
3
|
+
* The request's hCaptcha token failed verification (e.g. it expired, its score
|
|
4
|
+
* was too high, or hCaptcha could not be reached): the request must be retried
|
|
5
|
+
* with a new token.
|
|
6
|
+
*/
|
|
7
|
+
export class CaptchaRejectedError extends OAuthError {
|
|
8
|
+
constructor(error_description = 'hCaptcha verification failed', cause) {
|
|
9
|
+
super('captcha_rejected', error_description, 400, cause);
|
|
10
|
+
this.name = 'CaptchaRejectedError';
|
|
11
|
+
}
|
|
12
|
+
static from(err, message) {
|
|
13
|
+
if (err instanceof OAuthError)
|
|
14
|
+
return err;
|
|
15
|
+
return new CaptchaRejectedError(message, err);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=captcha-rejected-error.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"captcha-rejected-error.js","sourceRoot":"","sources":["../../src/errors/captcha-rejected-error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAE7C;;;;GAIG;AACH,MAAM,OAAO,oBAAqB,SAAQ,UAAU;IAGlD,YACE,iBAAiB,GAAG,8BAA8B,EAClD,KAAe;QAEf,KAAK,CAAC,kBAAkB,EAAE,iBAAiB,EAAE,GAAG,EAAE,KAAK,CAAC,CAAA;QAN1D,SAAI,GAAG,sBAAsB,CAAA;IAO7B,CAAC;IAED,MAAM,CAAC,IAAI,CAAC,GAAY,EAAE,OAAgB;QACxC,IAAI,GAAG,YAAY,UAAU;YAAE,OAAO,GAAG,CAAA;QACzC,OAAO,IAAI,oBAAoB,CAAC,OAAO,EAAE,GAAG,CAAC,CAAA;IAC/C,CAAC;CACF","sourcesContent":["import { OAuthError } from './oauth-error.js'\n\n/**\n * The request's hCaptcha token failed verification (e.g. it expired, its score\n * was too high, or hCaptcha could not be reached): the request must be retried\n * with a new token.\n */\nexport class CaptchaRejectedError extends OAuthError {\n name = 'CaptchaRejectedError'\n\n constructor(\n error_description = 'hCaptcha verification failed',\n cause?: unknown,\n ) {\n super('captcha_rejected', error_description, 400, cause)\n }\n\n static from(err: unknown, message?: string): OAuthError {\n if (err instanceof OAuthError) return err\n return new CaptchaRejectedError(message, err)\n }\n}\n"]}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { OAuthError } from './oauth-error.js';
|
|
2
|
+
/**
|
|
3
|
+
* The request carried no hCaptcha token: it must be retried with one.
|
|
4
|
+
*/
|
|
5
|
+
export declare class CaptchaRequiredError extends OAuthError {
|
|
6
|
+
name: string;
|
|
7
|
+
constructor(error_description?: string, cause?: unknown);
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=captcha-required-error.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"captcha-required-error.d.ts","sourceRoot":"","sources":["../../src/errors/captcha-required-error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAE7C;;GAEG;AACH,qBAAa,oBAAqB,SAAQ,UAAU;IAClD,IAAI,SAAyB;IAE7B,YACE,iBAAiB,SAAkC,EACnD,KAAK,CAAC,EAAE,OAAO,EAGhB;CACF"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { OAuthError } from './oauth-error.js';
|
|
2
|
+
/**
|
|
3
|
+
* The request carried no hCaptcha token: it must be retried with one.
|
|
4
|
+
*/
|
|
5
|
+
export class CaptchaRequiredError extends OAuthError {
|
|
6
|
+
constructor(error_description = 'Captcha verification required', cause) {
|
|
7
|
+
super('captcha_required', error_description, 400, cause);
|
|
8
|
+
this.name = 'CaptchaRequiredError';
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=captcha-required-error.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"captcha-required-error.js","sourceRoot":"","sources":["../../src/errors/captcha-required-error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAE7C;;GAEG;AACH,MAAM,OAAO,oBAAqB,SAAQ,UAAU;IAGlD,YACE,iBAAiB,GAAG,+BAA+B,EACnD,KAAe;QAEf,KAAK,CAAC,kBAAkB,EAAE,iBAAiB,EAAE,GAAG,EAAE,KAAK,CAAC,CAAA;QAN1D,SAAI,GAAG,sBAAsB,CAAA;IAO7B,CAAC;CACF","sourcesContent":["import { OAuthError } from './oauth-error.js'\n\n/**\n * The request carried no hCaptcha token: it must be retried with one.\n */\nexport class CaptchaRequiredError extends OAuthError {\n name = 'CaptchaRequiredError'\n\n constructor(\n error_description = 'Captcha verification required',\n cause?: unknown,\n ) {\n super('captcha_required', error_description, 400, cause)\n }\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"oauth-error.d.ts","sourceRoot":"","sources":["../../src/errors/oauth-error.ts"],"names":[],"mappings":"AAAA,qBAAa,UAAW,SAAQ,KAAK;aAIjB,KAAK,EAAE,MAAM;aACb,iBAAiB,EAAE,MAAM;aACzB,MAAM;IALjB,MAAM,EAAE,OAAO,CAAA;IAEtB,YACkB,KAAK,EAAE,MAAM,EACb,iBAAiB,EAAE,MAAM,EACzB,MAAM,SAAM,EAC5B,KAAK,CAAC,EAAE,OAAO,EAQhB;IAED,IAAI,UAAU,WAEb;IAED,MAAM;QAEF,KAAK;QACL,iBAAiB;MAEpB;CACF"}
|
|
1
|
+
{"version":3,"file":"oauth-error.d.ts","sourceRoot":"","sources":["../../src/errors/oauth-error.ts"],"names":[],"mappings":"AAAA,qBAAa,UAAW,SAAQ,KAAK;aAIjB,KAAK,EAAE,MAAM;aACb,iBAAiB,EAAE,MAAM;aACzB,MAAM;IALjB,MAAM,EAAE,OAAO,CAAA;IAEtB,YACkB,KAAK,EAAE,MAAM,EACb,iBAAiB,EAAE,MAAM,EACzB,MAAM,SAAM,EAC5B,KAAK,CAAC,EAAE,OAAO,EAQhB;IAED,IAAI,UAAU,WAEb;IAED,MAAM;QAEF,KAAK;QACL,iBAAiB;MAEpB;IAEA,kBAAkB,IAAI,SAAS,CAAC,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,CAAC,EAAE,IAAI,EAAE,OAAO,CAAC,CAE3E;CACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"oauth-error.js","sourceRoot":"","sources":["../../src/errors/oauth-error.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,UAAW,SAAQ,KAAK;IAGnC,YACkB,KAAa,EACb,iBAAyB,EACzB,MAAM,GAAG,GAAG,EAC5B,KAAe;QAEf,KAAK,CAAC,iBAAiB,EAAE,EAAE,KAAK,EAAE,CAAC,CAAA;qBALnB,KAAK;iCACL,iBAAiB;sBACjB,MAAM;QAKtB,KAAK,CAAC,iBAAiB,EAAE,CAAC,IAAI,EAAE,IAAI,CAAC,WAAW,CAAC,CAAA;QAEjD,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,WAAW,CAAC,IAAI,CAAA;QACjC,IAAI,CAAC,MAAM,GAAG,MAAM,GAAG,GAAG,CAAA;IAC5B,CAAC;IAED,IAAI,UAAU;QACZ,OAAO,IAAI,CAAC,MAAM,CAAA;IACpB,CAAC;IAED,MAAM;QACJ,OAAO;YACL,KAAK,EAAE,IAAI,CAAC,KAAK;YACjB,iBAAiB,EAAE,IAAI,CAAC,iBAAiB;SAC1C,CAAA;IACH,CAAC;CACF","sourcesContent":["export class OAuthError extends Error {\n public expose: boolean\n\n constructor(\n public readonly error: string,\n public readonly error_description: string,\n public readonly status = 400,\n cause?: unknown,\n ) {\n super(error_description, { cause })\n\n Error.captureStackTrace?.(this, this.constructor)\n\n this.name = this.constructor.name\n this.expose = status < 500\n }\n\n get statusCode() {\n return this.status\n }\n\n toJSON() {\n return {\n error: this.error,\n error_description: this.error_description,\n }\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"oauth-error.js","sourceRoot":"","sources":["../../src/errors/oauth-error.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,UAAW,SAAQ,KAAK;IAGnC,YACkB,KAAa,EACb,iBAAyB,EACzB,MAAM,GAAG,GAAG,EAC5B,KAAe;QAEf,KAAK,CAAC,iBAAiB,EAAE,EAAE,KAAK,EAAE,CAAC,CAAA;qBALnB,KAAK;iCACL,iBAAiB;sBACjB,MAAM;QAKtB,KAAK,CAAC,iBAAiB,EAAE,CAAC,IAAI,EAAE,IAAI,CAAC,WAAW,CAAC,CAAA;QAEjD,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,WAAW,CAAC,IAAI,CAAA;QACjC,IAAI,CAAC,MAAM,GAAG,MAAM,GAAG,GAAG,CAAA;IAC5B,CAAC;IAED,IAAI,UAAU;QACZ,OAAO,IAAI,CAAC,MAAM,CAAA;IACpB,CAAC;IAED,MAAM;QACJ,OAAO;YACL,KAAK,EAAE,IAAI,CAAC,KAAK;YACjB,iBAAiB,EAAE,IAAI,CAAC,iBAAiB;SAC1C,CAAA;IACH,CAAC;IAED,CAAC,kBAAkB;QACjB,EAAE;IACJ,CAAC;CACF","sourcesContent":["export class OAuthError extends Error {\n public expose: boolean\n\n constructor(\n public readonly error: string,\n public readonly error_description: string,\n public readonly status = 400,\n cause?: unknown,\n ) {\n super(error_description, { cause })\n\n Error.captureStackTrace?.(this, this.constructor)\n\n this.name = this.constructor.name\n this.expose = status < 500\n }\n\n get statusCode() {\n return this.status\n }\n\n toJSON() {\n return {\n error: this.error,\n error_description: this.error_description,\n }\n }\n\n *getResponseHeaders(): Generator<[name: string, val: string], void, unknown> {\n //\n }\n}\n"]}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { OAuthError } from './oauth-error.js';
|
|
2
|
+
export declare class RateLimitExceededError extends OAuthError {
|
|
3
|
+
name: string;
|
|
4
|
+
/**
|
|
5
|
+
* Number of seconds after which the request may be retried, suitable for use
|
|
6
|
+
* as `Retry-After` header value.
|
|
7
|
+
*/
|
|
8
|
+
readonly retryAfter: number;
|
|
9
|
+
constructor(msBeforeNext: number, error_description?: string, cause?: unknown);
|
|
10
|
+
getResponseHeaders(): Generator<[name: string, val: string], void, unknown>;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=rate-limit-exceeded-error.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rate-limit-exceeded-error.d.ts","sourceRoot":"","sources":["../../src/errors/rate-limit-exceeded-error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAE7C,qBAAa,sBAAuB,SAAQ,UAAU;IACpD,IAAI,SAA2B;IAE/B;;;OAGG;IACH,SAAgB,UAAU,EAAE,MAAM,CAAA;IAElC,YACE,YAAY,EAAE,MAAM,EACpB,iBAAiB,SAA8C,EAC/D,KAAK,CAAC,EAAE,OAAO,EAIhB;IAEA,kBAAkB,IAAI,SAAS,CAAC,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,CAAC,EAAE,IAAI,EAAE,OAAO,CAAC,CAK3E;CACF"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { OAuthError } from './oauth-error.js';
|
|
2
|
+
export class RateLimitExceededError extends OAuthError {
|
|
3
|
+
constructor(msBeforeNext, error_description = 'Too many requests, please try again later', cause) {
|
|
4
|
+
super('rate_limit_exceeded', error_description, 429, cause);
|
|
5
|
+
this.name = 'RateLimitExceededError';
|
|
6
|
+
this.retryAfter = Math.max(0, Math.ceil(msBeforeNext / 1e3));
|
|
7
|
+
}
|
|
8
|
+
*getResponseHeaders() {
|
|
9
|
+
yield* super.getResponseHeaders();
|
|
10
|
+
// https://datatracker.ietf.org/doc/html/rfc9126#section-2.3
|
|
11
|
+
yield ['Retry-After', String(this.retryAfter)];
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=rate-limit-exceeded-error.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rate-limit-exceeded-error.js","sourceRoot":"","sources":["../../src/errors/rate-limit-exceeded-error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAE7C,MAAM,OAAO,sBAAuB,SAAQ,UAAU;IASpD,YACE,YAAoB,EACpB,iBAAiB,GAAG,2CAA2C,EAC/D,KAAe;QAEf,KAAK,CAAC,qBAAqB,EAAE,iBAAiB,EAAE,GAAG,EAAE,KAAK,CAAC,CAAA;QAb7D,SAAI,GAAG,wBAAwB,CAAA;QAc7B,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,IAAI,CAAC,YAAY,GAAG,GAAG,CAAC,CAAC,CAAA;IAC9D,CAAC;IAED,CAAC,kBAAkB;QACjB,KAAK,CAAC,CAAC,KAAK,CAAC,kBAAkB,EAAE,CAAA;QAEjC,4DAA4D;QAC5D,MAAM,CAAC,aAAa,EAAE,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,CAAA;IAChD,CAAC;CACF","sourcesContent":["import { OAuthError } from './oauth-error.js'\n\nexport class RateLimitExceededError extends OAuthError {\n name = 'RateLimitExceededError'\n\n /**\n * Number of seconds after which the request may be retried, suitable for use\n * as `Retry-After` header value.\n */\n public readonly retryAfter: number\n\n constructor(\n msBeforeNext: number,\n error_description = 'Too many requests, please try again later',\n cause?: unknown,\n ) {\n super('rate_limit_exceeded', error_description, 429, cause)\n this.retryAfter = Math.max(0, Math.ceil(msBeforeNext / 1e3))\n }\n\n *getResponseHeaders(): Generator<[name: string, val: string], void, unknown> {\n yield* super.getResponseHeaders()\n\n // https://datatracker.ietf.org/doc/html/rfc9126#section-2.3\n yield ['Retry-After', String(this.retryAfter)]\n }\n}\n"]}
|
|
@@ -5,5 +5,6 @@ export declare class WWWAuthenticateError extends OAuthError {
|
|
|
5
5
|
readonly wwwAuthenticate: WWWAuthenticate;
|
|
6
6
|
constructor(error: string, error_description: string, wwwAuthenticate: WWWAuthenticate, cause?: unknown);
|
|
7
7
|
get wwwAuthenticateHeader(): string;
|
|
8
|
+
getResponseHeaders(): Generator<[string, string], void, unknown>;
|
|
8
9
|
}
|
|
9
10
|
//# sourceMappingURL=www-authenticate-error.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"www-authenticate-error.d.ts","sourceRoot":"","sources":["../../src/errors/www-authenticate-error.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAE7C,MAAM,MAAM,qBAAqB,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAA;AACtE,MAAM,MAAM,eAAe,GAAG,MAAM,CAAC,MAAM,EAAE,SAAS,GAAG,qBAAqB,CAAC,CAAA;AAE/E,qBAAa,oBAAqB,SAAQ,UAAU;IAClD,SAAgB,eAAe,EAAE,eAAe,CAAA;IAEhD,YACE,KAAK,EAAE,MAAM,EACb,iBAAiB,EAAE,MAAM,EACzB,eAAe,EAAE,eAAe,EAChC,KAAK,CAAC,EAAE,OAAO,EAWhB;IAED,IAAI,qBAAqB,WAExB;CACF"}
|
|
1
|
+
{"version":3,"file":"www-authenticate-error.d.ts","sourceRoot":"","sources":["../../src/errors/www-authenticate-error.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAE7C,MAAM,MAAM,qBAAqB,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAA;AACtE,MAAM,MAAM,eAAe,GAAG,MAAM,CAAC,MAAM,EAAE,SAAS,GAAG,qBAAqB,CAAC,CAAA;AAE/E,qBAAa,oBAAqB,SAAQ,UAAU;IAClD,SAAgB,eAAe,EAAE,eAAe,CAAA;IAEhD,YACE,KAAK,EAAE,MAAM,EACb,iBAAiB,EAAE,MAAM,EACzB,eAAe,EAAE,eAAe,EAChC,KAAK,CAAC,EAAE,OAAO,EAWhB;IAED,IAAI,qBAAqB,WAExB;IAEA,kBAAkB,IAAI,SAAS,CAAC,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,IAAI,EAAE,OAAO,CAAC,CAGhE;CACF"}
|
|
@@ -14,6 +14,10 @@ export class WWWAuthenticateError extends OAuthError {
|
|
|
14
14
|
get wwwAuthenticateHeader() {
|
|
15
15
|
return formatWWWAuthenticateHeader(this.wwwAuthenticate);
|
|
16
16
|
}
|
|
17
|
+
*getResponseHeaders() {
|
|
18
|
+
yield* super.getResponseHeaders();
|
|
19
|
+
yield ['WWW-Authenticate', this.wwwAuthenticateHeader];
|
|
20
|
+
}
|
|
17
21
|
}
|
|
18
22
|
function formatWWWAuthenticateHeader(wwwAuthenticate) {
|
|
19
23
|
return Object.entries(wwwAuthenticate)
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"www-authenticate-error.js","sourceRoot":"","sources":["../../src/errors/www-authenticate-error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAA;AACpD,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAK7C,MAAM,OAAO,oBAAqB,SAAQ,UAAU;IAGlD,YACE,KAAa,EACb,iBAAyB,EACzB,eAAgC,EAChC,KAAe;QAEf,KAAK,CAAC,KAAK,EAAE,iBAAiB,EAAE,GAAG,EAAE,KAAK,CAAC,CAAA;QAE3C,IAAI,CAAC,eAAe;YAClB,eAAe,CAAC,MAAM,CAAC,IAAI,IAAI;gBAC7B,CAAC,CAAC;oBACE,GAAG,eAAe;oBAClB,IAAI,EAAE,EAAE,IAAI,EAAE,YAAY,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,GAAG,eAAe,CAAC,MAAM,CAAC,EAAE;iBACnE;gBACH,CAAC,CAAC,eAAe,CAAA;IACvB,CAAC;IAED,IAAI,qBAAqB;QACvB,OAAO,2BAA2B,CAAC,IAAI,CAAC,eAAe,CAAC,CAAA;IAC1D,CAAC;CACF;AAED,SAAS,2BAA2B,CAAC,eAAgC;IACnE,OAAO,MAAM,CAAC,OAAO,CAAC,eAAe,CAAC;SACnC,MAAM,CAAC,sBAAsB,CAAC;SAC9B,GAAG,CAAC,4BAA4B,CAAC;SACjC,IAAI,CAAC,IAAI,CAAC,CAAA;AACf,CAAC;AAGD,SAAS,sBAAsB,CAC7B,KAAwB;IAExB,MAAM,CAAC,EAAE,KAAK,CAAC,GAAG,KAAK,CAAA;IACvB,OAAO,KAAK,IAAI,IAAI,IAAI,OAAO,KAAK,KAAK,QAAQ,CAAA;AACnD,CAAC;AAED,SAAS,4BAA4B,CAAC,CAAC,IAAI,EAAE,MAAM,CAAuB;IACxE,MAAM,SAAS,GAAG,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC;SACrC,MAAM,CAAC,YAAY,CAAC;SACpB,GAAG,CAAC,kBAAkB,CAAC,CAAA;IAE1B,OAAO,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,IAAI,IAAI,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAA;AACpE,CAAC;AAID,SAAS,YAAY,CAAC,KAAwB;IAC5C,MAAM,CAAC,EAAE,KAAK,CAAC,GAAG,KAAK,CAAA;IACvB,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAA;AAC1E,CAAC;AAED,SAAS,kBAAkB,CAAC,CAAC,IAAI,EAAE,KAAK,CAAa;IACnD,OAAO,GAAG,IAAI,KAAK,KAAK,GAAG,CAAA;AAC7B,CAAC","sourcesContent":["import { VERIFY_ALGOS } from '../lib/util/crypto.js'\nimport { OAuthError } from './oauth-error.js'\n\nexport type WWWAuthenticateParams = Record<string, string | undefined>\nexport type WWWAuthenticate = Record<string, undefined | WWWAuthenticateParams>\n\nexport class WWWAuthenticateError extends OAuthError {\n public readonly wwwAuthenticate: WWWAuthenticate\n\n constructor(\n error: string,\n error_description: string,\n wwwAuthenticate: WWWAuthenticate,\n cause?: unknown,\n ) {\n super(error, error_description, 401, cause)\n\n this.wwwAuthenticate =\n wwwAuthenticate['DPoP'] != null\n ? {\n ...wwwAuthenticate,\n DPoP: { algs: VERIFY_ALGOS.join(' '), ...wwwAuthenticate['DPoP'] },\n }\n : wwwAuthenticate\n }\n\n get wwwAuthenticateHeader() {\n return formatWWWAuthenticateHeader(this.wwwAuthenticate)\n }\n}\n\nfunction formatWWWAuthenticateHeader(wwwAuthenticate: WWWAuthenticate): string {\n return Object.entries(wwwAuthenticate)\n .filter(isWWWAuthenticateEntry)\n .map(wwwAuthenticateEntryToString)\n .join(', ')\n}\n\ntype WWWAuthenticateEntry = [type: string, params: WWWAuthenticateParams]\nfunction isWWWAuthenticateEntry(\n entry: [string, unknown],\n): entry is WWWAuthenticateEntry {\n const [, value] = entry\n return value != null && typeof value === 'object'\n}\n\nfunction wwwAuthenticateEntryToString([type, params]: WWWAuthenticateEntry) {\n const paramsEnc = Object.entries(params)\n .filter(isParamEntry)\n .map(paramEntryToString)\n\n return paramsEnc.length ? `${type} ${paramsEnc.join(', ')}` : type\n}\n\ntype ParamEntry = [name: string, value: string]\n\nfunction isParamEntry(entry: [string, unknown]): entry is ParamEntry {\n const [, value] = entry\n return typeof value === 'string' && value !== '' && !value.includes('\"')\n}\n\nfunction paramEntryToString([name, value]: ParamEntry): string {\n return `${name}=\"${value}\"`\n}\n"]}
|
|
1
|
+
{"version":3,"file":"www-authenticate-error.js","sourceRoot":"","sources":["../../src/errors/www-authenticate-error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAA;AACpD,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAK7C,MAAM,OAAO,oBAAqB,SAAQ,UAAU;IAGlD,YACE,KAAa,EACb,iBAAyB,EACzB,eAAgC,EAChC,KAAe;QAEf,KAAK,CAAC,KAAK,EAAE,iBAAiB,EAAE,GAAG,EAAE,KAAK,CAAC,CAAA;QAE3C,IAAI,CAAC,eAAe;YAClB,eAAe,CAAC,MAAM,CAAC,IAAI,IAAI;gBAC7B,CAAC,CAAC;oBACE,GAAG,eAAe;oBAClB,IAAI,EAAE,EAAE,IAAI,EAAE,YAAY,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,GAAG,eAAe,CAAC,MAAM,CAAC,EAAE;iBACnE;gBACH,CAAC,CAAC,eAAe,CAAA;IACvB,CAAC;IAED,IAAI,qBAAqB;QACvB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G,CAAA;AAC7B,CAAC","sourcesContent":["import { VERIFY_ALGOS } from '../lib/util/crypto.js'\nimport { OAuthError } from './oauth-error.js'\n\nexport type WWWAuthenticateParams = Record<string, string | undefined>\nexport type WWWAuthenticate = Record<string, undefined | WWWAuthenticateParams>\n\nexport class WWWAuthenticateError extends OAuthError {\n public readonly wwwAuthenticate: WWWAuthenticate\n\n constructor(\n error: string,\n error_description: string,\n wwwAuthenticate: WWWAuthenticate,\n cause?: unknown,\n ) {\n super(error, error_description, 401, cause)\n\n this.wwwAuthenticate =\n wwwAuthenticate['DPoP'] != null\n ? {\n ...wwwAuthenticate,\n DPoP: { algs: VERIFY_ALGOS.join(' '), ...wwwAuthenticate['DPoP'] },\n }\n : wwwAuthenticate\n }\n\n get wwwAuthenticateHeader() {\n return formatWWWAuthenticateHeader(this.wwwAuthenticate)\n }\n\n *getResponseHeaders(): Generator<[string, string], void, unknown> {\n yield* super.getResponseHeaders()\n yield ['WWW-Authenticate', this.wwwAuthenticateHeader]\n }\n}\n\nfunction formatWWWAuthenticateHeader(wwwAuthenticate: WWWAuthenticate): string {\n return Object.entries(wwwAuthenticate)\n .filter(isWWWAuthenticateEntry)\n .map(wwwAuthenticateEntryToString)\n .join(', ')\n}\n\ntype WWWAuthenticateEntry = [type: string, params: WWWAuthenticateParams]\nfunction isWWWAuthenticateEntry(\n entry: [string, unknown],\n): entry is WWWAuthenticateEntry {\n const [, value] = entry\n return value != null && typeof value === 'object'\n}\n\nfunction wwwAuthenticateEntryToString([type, params]: WWWAuthenticateEntry) {\n const paramsEnc = Object.entries(params)\n .filter(isParamEntry)\n .map(paramEntryToString)\n\n return paramsEnc.length ? `${type} ${paramsEnc.join(', ')}` : type\n}\n\ntype ParamEntry = [name: string, value: string]\n\nfunction isParamEntry(entry: [string, unknown]): entry is ParamEntry {\n const [, value] = entry\n return typeof value === 'string' && value !== '' && !value.includes('\"')\n}\n\nfunction paramEntryToString([name, value]: ParamEntry): string {\n return `${name}=\"${value}\"`\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hcaptcha-client-tokens.d.ts","sourceRoot":"","sources":["../../src/hcaptcha/hcaptcha-client-tokens.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,oBAAoB,GAAG;IACjC,QAAQ,EAAE,MAAM,CAAA;IAChB,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,gBAAgB,CAAC,EAAE,MAAM,CAAA;IACzB,eAAe,CAAC,EAAE,MAAM,CAAA;CACzB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hcaptcha-client-tokens.js","sourceRoot":"","sources":["../../src/hcaptcha/hcaptcha-client-tokens.ts"],"names":[],"mappings":"","sourcesContent":["export type HcaptchaClientTokens = {\n hashedIp: string\n hashedEmail?: string\n hashedIdentifier?: string\n hashedUserAgent?: string\n}\n"]}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { OAuthIssuerIdentifier } from '@atproto/oauth-types';
|
|
2
|
+
import type { DeviceId } from '../device/device-id.js';
|
|
3
|
+
import { HCaptchaClient, type HcaptchaConfig, type HcaptchaVerifyResult } from '../lib/hcaptcha.js';
|
|
4
|
+
import type { RequestMetadata } from '../lib/http/request.js';
|
|
5
|
+
import type { OAuthHooks } from '../oauth-hooks.js';
|
|
6
|
+
import type { HcaptchaClientTokens } from './hcaptcha-client-tokens.js';
|
|
7
|
+
import type { HcaptchaVerifyData } from './hcaptcha-verify-data.js';
|
|
8
|
+
export type { HcaptchaClientTokens, HcaptchaVerifyData };
|
|
9
|
+
export declare class HCaptchaManager {
|
|
10
|
+
protected readonly hooks: OAuthHooks;
|
|
11
|
+
protected readonly client?: HCaptchaClient;
|
|
12
|
+
constructor(issuer: OAuthIssuerIdentifier, hooks: OAuthHooks, config?: HcaptchaConfig);
|
|
13
|
+
get enabled(): boolean;
|
|
14
|
+
/**
|
|
15
|
+
* @returns `undefined` when hCaptcha is not configured
|
|
16
|
+
* @throws {CaptchaRequiredError} when no token was provided
|
|
17
|
+
* @throws {CaptchaRejectedError} when the token could not be verified,
|
|
18
|
+
* including when hCaptcha could not be reached
|
|
19
|
+
*/
|
|
20
|
+
verify(deviceId: DeviceId, deviceMetadata: RequestMetadata, data: HcaptchaVerifyData): Promise<HcaptchaVerifyResult | undefined>;
|
|
21
|
+
protected buildClientTokens(deviceId: DeviceId, deviceMetadata: RequestMetadata, data: HcaptchaVerifyData): Promise<HcaptchaClientTokens>;
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=hcaptcha-manager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hcaptcha-manager.d.ts","sourceRoot":"","sources":["../../src/hcaptcha/hcaptcha-manager.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,sBAAsB,CAAA;AACjE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,wBAAwB,CAAA;AAGtD,OAAO,EACL,cAAc,EACd,KAAK,cAAc,EACnB,KAAK,oBAAoB,EAC1B,MAAM,oBAAoB,CAAA;AAC3B,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAA;AAE7D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAA;AACnD,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAA;AACvE,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAA;AAEnE,YAAY,EAAE,oBAAoB,EAAE,kBAAkB,EAAE,CAAA;AAExD,qBAAa,eAAe;IAKxB,SAAS,CAAC,QAAQ,CAAC,KAAK,EAAE,UAAU;IAJtC,SAAS,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,cAAc,CAAA;IAE1C,YACE,MAAM,EAAE,qBAAqB,EACV,KAAK,EAAE,UAAU,EACpC,MAAM,CAAC,EAAE,cAAc,EAKxB;IAED,IAAI,OAAO,IAAI,OAAO,CAErB;IAED;;;;;OAKG;IACU,MAAM,CACjB,QAAQ,EAAE,QAAQ,EAClB,cAAc,EAAE,eAAe,EAC/B,IAAI,EAAE,kBAAkB,GACvB,OAAO,CAAC,oBAAoB,GAAG,SAAS,CAAC,CAoC3C;IAED,UAAgB,iBAAiB,CAC/B,QAAQ,EAAE,QAAQ,EAClB,cAAc,EAAE,eAAe,EAC/B,IAAI,EAAE,kBAAkB,GACvB,OAAO,CAAC,oBAAoB,CAAC,CAmC/B;CACF"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { CaptchaRejectedError } from '../errors/captcha-rejected-error.js';
|
|
2
|
+
import { CaptchaRequiredError } from '../errors/captcha-required-error.js';
|
|
3
|
+
import { HCaptchaClient, } from '../lib/hcaptcha.js';
|
|
4
|
+
import { normalizeIdentifierString } from '../lib/identifier.js';
|
|
5
|
+
export class HCaptchaManager {
|
|
6
|
+
constructor(issuer, hooks, config) {
|
|
7
|
+
this.hooks = hooks;
|
|
8
|
+
this.client = config
|
|
9
|
+
? new HCaptchaClient(new URL(issuer).hostname, config)
|
|
10
|
+
: undefined;
|
|
11
|
+
}
|
|
12
|
+
get enabled() {
|
|
13
|
+
return this.client != null;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* @returns `undefined` when hCaptcha is not configured
|
|
17
|
+
* @throws {CaptchaRequiredError} when no token was provided
|
|
18
|
+
* @throws {CaptchaRejectedError} when the token could not be verified,
|
|
19
|
+
* including when hCaptcha could not be reached
|
|
20
|
+
*/
|
|
21
|
+
async verify(deviceId, deviceMetadata, data) {
|
|
22
|
+
const { client } = this;
|
|
23
|
+
if (!client)
|
|
24
|
+
return undefined;
|
|
25
|
+
if (!data.input.hcaptchaToken) {
|
|
26
|
+
throw new CaptchaRequiredError();
|
|
27
|
+
}
|
|
28
|
+
const tokens = await this.buildClientTokens(deviceId, deviceMetadata, data);
|
|
29
|
+
try {
|
|
30
|
+
const result = await client.verify(data.input.hcaptchaToken, deviceMetadata.ipAddress, data.action === 'sign-up'
|
|
31
|
+
? 'signup'
|
|
32
|
+
: data.action === 'sign-in'
|
|
33
|
+
? 'login'
|
|
34
|
+
: undefined, // no equivalent behavior type for other actions
|
|
35
|
+
tokens);
|
|
36
|
+
await this.hooks.onHcaptchaResult?.call(null, {
|
|
37
|
+
...data,
|
|
38
|
+
deviceId,
|
|
39
|
+
deviceMetadata,
|
|
40
|
+
tokens,
|
|
41
|
+
result,
|
|
42
|
+
});
|
|
43
|
+
client.checkVerifyResult(result, tokens);
|
|
44
|
+
return result;
|
|
45
|
+
}
|
|
46
|
+
catch (err) {
|
|
47
|
+
throw CaptchaRejectedError.from(err);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
async buildClientTokens(deviceId, deviceMetadata, data) {
|
|
51
|
+
const { client } = this;
|
|
52
|
+
if (!client)
|
|
53
|
+
throw new Error('HCaptcha client not configured');
|
|
54
|
+
const inputEmail = 'email' in data.input
|
|
55
|
+
? normalizeIdentifierString(data.input.email)
|
|
56
|
+
: undefined;
|
|
57
|
+
const inputIdentifier = data.action === 'sign-up'
|
|
58
|
+
? normalizeIdentifierString(data.input.handle)
|
|
59
|
+
: 'identifier' in data.input
|
|
60
|
+
? normalizeIdentifierString(data.input.identifier)
|
|
61
|
+
: undefined;
|
|
62
|
+
const identifierEmail = inputIdentifier?.includes('@', 1)
|
|
63
|
+
? inputIdentifier
|
|
64
|
+
: undefined;
|
|
65
|
+
const email = inputEmail || identifierEmail;
|
|
66
|
+
const identifier = inputIdentifier === email ? undefined : inputIdentifier;
|
|
67
|
+
// @NOTE We are **not** using Promise.all here because libuv's thread pool
|
|
68
|
+
// might get saturated, leading to performance degradation. Instead, we
|
|
69
|
+
// await each hash sequentially, ensuring that every HTTP request only uses
|
|
70
|
+
// one thread at a time.
|
|
71
|
+
return {
|
|
72
|
+
hashedIp: await client.hashToken(deviceMetadata.ipAddress),
|
|
73
|
+
hashedEmail: email ? await client.hashToken(email) : undefined,
|
|
74
|
+
hashedIdentifier: identifier
|
|
75
|
+
? await client.hashToken(identifier)
|
|
76
|
+
: undefined,
|
|
77
|
+
hashedUserAgent: deviceMetadata.userAgent
|
|
78
|
+
? await client.hashToken(deviceMetadata.userAgent)
|
|
79
|
+
: undefined,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
//# sourceMappingURL=hcaptcha-manager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hcaptcha-manager.js","sourceRoot":"","sources":["../../src/hcaptcha/hcaptcha-manager.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,oBAAoB,EAAE,MAAM,qCAAqC,CAAA;AAC1E,OAAO,EAAE,oBAAoB,EAAE,MAAM,qCAAqC,CAAA;AAC1E,OAAO,EACL,cAAc,GAGf,MAAM,oBAAoB,CAAA;AAE3B,OAAO,EAAE,yBAAyB,EAAE,MAAM,sBAAsB,CAAA;AAOhE,MAAM,OAAO,eAAe;IAG1B,YACE,MAA6B,EACV,KAAiB,EACpC,MAAuB;qBADJ,KAAK;QAGxB,IAAI,CAAC,MAAM,GAAG,MAAM;YAClB,CAAC,CAAC,IAAI,cAAc,CAAC,IAAI,GAAG,CAAC,MAAM,CAAC,CAAC,QAAQ,EAAE,MAAM,CAAC;YACtD,CAAC,CAAC,SAAS,CAAA;IACf,CAAC;IAED,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,MAAM,IAAI,IAAI,CAAA;IAC5B,CAAC;IAED;;;;;OAKG;IACI,KAAK,CAAC,MAAM,CACjB,QAAkB,EAClB,cAA+B,EAC/B,IAAwB;QAExB,MAAM,EAAE,MAAM,EAAE,GAAG,IAAI,CAAA;QACvB,IAAI,CAAC,MAAM;YAAE,OAAO,SAAS,CAAA;QAE7B,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,aAAa,EAAE,CAAC;YAC9B,MAAM,IAAI,oBAAoB,EAAE,CAAA;QAClC,CAAC;QAED,MAAM,MAAM,GAAG,MAAM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,CAAC,CAAC,YAAY,IAAI,IAAI,CAAC,KAAK;gBAC1B,CAAC,CAAC,yBAAyB,CAAC,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC;gBAClD,CAAC,CAAC,SAAS,CAAA;QACjB,MAAM,eAAe,GAAG,eAAe,EAAE,QAAQ,CAAC,GAAG,EAAE,CAAC,CAAC;YACvD,CAAC,CAAC,eAAe;YACjB,CAAC,CAAC,SAAS,CAAA;QAEb,MAAM,KAAK,GAAG,UAAU,IAAI,eAAe,CAAA;QAC3C,MAAM,UAAU,GAAG,eAAe,KAAK,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,eAAe,CAAA;QAE1E,0EAA0E;QAC1E,uEAAuE;QACvE,2EAA2E;QAC3E,wBAAwB;QACxB,OAAO;YACL,QAAQ,EAAE,MAAM,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,SAAS,CAAC;YAC1D,WAAW,EAAE,KAAK,CAAC,CAAC,CAAC,MAAM,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,SAAS;YAC9D,gBAAgB,EAAE,UAAU;gBAC1B,CAAC,CAAC,MAAM,MAAM,CAAC,SAAS,CAAC,UAAU,CAAC;gBACpC,CAAC,CAAC,SAAS;YACb,eAAe,EAAE,cAAc,CAAC,SAAS;gBACvC,CAAC,CAAC,MAAM,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,SAAS,CAAC;gBAClD,CAAC,CAAC,SAAS;SACd,CAAA;IACH,CAAC;CACF","sourcesContent":["import type { OAuthIssuerIdentifier } from '@atproto/oauth-types'\nimport type { DeviceId } from '../device/device-id.js'\nimport { CaptchaRejectedError } from '../errors/captcha-rejected-error.js'\nimport { CaptchaRequiredError } from '../errors/captcha-required-error.js'\nimport {\n HCaptchaClient,\n type HcaptchaConfig,\n type HcaptchaVerifyResult,\n} from '../lib/hcaptcha.js'\nimport type { RequestMetadata } from '../lib/http/request.js'\nimport { normalizeIdentifierString } from '../lib/identifier.js'\nimport type { OAuthHooks } from '../oauth-hooks.js'\nimport type { HcaptchaClientTokens } from './hcaptcha-client-tokens.js'\nimport type { HcaptchaVerifyData } from './hcaptcha-verify-data.js'\n\nexport type { HcaptchaClientTokens, HcaptchaVerifyData }\n\nexport class HCaptchaManager {\n protected readonly client?: HCaptchaClient\n\n constructor(\n issuer: OAuthIssuerIdentifier,\n protected readonly hooks: OAuthHooks,\n config?: HcaptchaConfig,\n ) {\n this.client = config\n ? new HCaptchaClient(new URL(issuer).hostname, config)\n : undefined\n }\n\n get enabled(): boolean {\n return this.client != null\n }\n\n /**\n * @returns `undefined` when hCaptcha is not configured\n * @throws {CaptchaRequiredError} when no token was provided\n * @throws {CaptchaRejectedError} when the token could not be verified,\n * including when hCaptcha could not be reached\n */\n public async verify(\n deviceId: DeviceId,\n deviceMetadata: RequestMetadata,\n data: HcaptchaVerifyData,\n ): Promise<HcaptchaVerifyResult | undefined> {\n const { client } = this\n if (!client) return undefined\n\n if (!data.input.hcaptchaToken) {\n throw new CaptchaRequiredError()\n }\n\n const tokens = await this.buildClientTokens(deviceId, deviceMetadata, data)\n\n try {\n const result = await client.verify(\n data.input.hcaptchaToken,\n deviceMetadata.ipAddress,\n data.action === 'sign-up'\n ? 'signup'\n : data.action === 'sign-in'\n ? 'login'\n : undefined, // no equivalent behavior type for other actions\n tokens,\n )\n\n await this.hooks.onHcaptchaResult?.call(null, {\n ...data,\n deviceId,\n deviceMetadata,\n tokens,\n result,\n })\n\n client.checkVerifyResult(result, tokens)\n\n return result\n } catch (err) {\n throw CaptchaRejectedError.from(err)\n }\n }\n\n protected async buildClientTokens(\n deviceId: DeviceId,\n deviceMetadata: RequestMetadata,\n data: HcaptchaVerifyData,\n ): Promise<HcaptchaClientTokens> {\n const { client } = this\n if (!client) throw new Error('HCaptcha client not configured')\n\n const inputEmail =\n 'email' in data.input\n ? normalizeIdentifierString(data.input.email)\n : undefined\n const inputIdentifier =\n data.action === 'sign-up'\n ? normalizeIdentifierString(data.input.handle)\n : 'identifier' in data.input\n ? normalizeIdentifierString(data.input.identifier)\n : undefined\n const identifierEmail = inputIdentifier?.includes('@', 1)\n ? inputIdentifier\n : undefined\n\n const email = inputEmail || identifierEmail\n const identifier = inputIdentifier === email ? undefined : inputIdentifier\n\n // @NOTE We are **not** using Promise.all here because libuv's thread pool\n // might get saturated, leading to performance degradation. Instead, we\n // await each hash sequentially, ensuring that every HTTP request only uses\n // one thread at a time.\n return {\n hashedIp: await client.hashToken(deviceMetadata.ipAddress),\n hashedEmail: email ? await client.hashToken(email) : undefined,\n hashedIdentifier: identifier\n ? await client.hashToken(identifier)\n : undefined,\n hashedUserAgent: deviceMetadata.userAgent\n ? await client.hashToken(deviceMetadata.userAgent)\n : undefined,\n }\n }\n}\n"]}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { ConfirmResetPasswordInput, InitiatePasswordResetInput, SignInInput, SignUpInput } from '@atproto/oauth-provider-api';
|
|
2
|
+
export type HcaptchaVerifyData = {
|
|
3
|
+
action: 'sign-up';
|
|
4
|
+
input: SignUpInput;
|
|
5
|
+
} | {
|
|
6
|
+
action: 'sign-in';
|
|
7
|
+
input: SignInInput;
|
|
8
|
+
} | {
|
|
9
|
+
action: 'reset-password-request';
|
|
10
|
+
input: InitiatePasswordResetInput;
|
|
11
|
+
} | {
|
|
12
|
+
action: 'reset-password-confirm';
|
|
13
|
+
input: ConfirmResetPasswordInput;
|
|
14
|
+
};
|
|
15
|
+
//# sourceMappingURL=hcaptcha-verify-data.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hcaptcha-verify-data.d.ts","sourceRoot":"","sources":["../../src/hcaptcha/hcaptcha-verify-data.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,yBAAyB,EACzB,0BAA0B,EAC1B,WAAW,EACX,WAAW,EACZ,MAAM,6BAA6B,CAAA;AAEpC,MAAM,MAAM,kBAAkB,GAC1B;IACE,MAAM,EAAE,SAAS,CAAA;IACjB,KAAK,EAAE,WAAW,CAAA;CACnB,GACD;IACE,MAAM,EAAE,SAAS,CAAA;IACjB,KAAK,EAAE,WAAW,CAAA;CACnB,GACD;IACE,MAAM,EAAE,wBAAwB,CAAA;IAChC,KAAK,EAAE,0BAA0B,CAAA;CAClC,GACD;IACE,MAAM,EAAE,wBAAwB,CAAA;IAChC,KAAK,EAAE,yBAAyB,CAAA;CACjC,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hcaptcha-verify-data.js","sourceRoot":"","sources":["../../src/hcaptcha/hcaptcha-verify-data.ts"],"names":[],"mappings":"","sourcesContent":["import type {\n ConfirmResetPasswordInput,\n InitiatePasswordResetInput,\n SignInInput,\n SignUpInput,\n} from '@atproto/oauth-provider-api'\n\nexport type HcaptchaVerifyData =\n | {\n action: 'sign-up'\n input: SignUpInput\n }\n | {\n action: 'sign-in'\n input: SignInInput\n }\n | {\n action: 'reset-password-request'\n input: InitiatePasswordResetInput\n }\n | {\n action: 'reset-password-confirm'\n input: ConfirmResetPasswordInput\n }\n"]}
|
package/dist/lib/hcaptcha.d.ts
CHANGED
|
@@ -147,17 +147,12 @@ export declare const hcaptchaVerifyResultSchema: z.ZodObject<{
|
|
|
147
147
|
tags?: string[] | undefined;
|
|
148
148
|
}>;
|
|
149
149
|
export type HcaptchaVerifyResult = z.infer<typeof hcaptchaVerifyResultSchema>;
|
|
150
|
-
export type HcaptchaClientTokens = {
|
|
151
|
-
hashedIp: string;
|
|
152
|
-
hashedHandle: string;
|
|
153
|
-
hashedUserAgent?: string;
|
|
154
|
-
};
|
|
155
150
|
export declare class HCaptchaClient {
|
|
156
151
|
readonly hostname: string;
|
|
157
152
|
readonly config: HcaptchaConfig;
|
|
158
153
|
protected readonly fetch: FetchBound;
|
|
159
154
|
constructor(hostname: string, config: HcaptchaConfig, fetch?: Fetch);
|
|
160
|
-
verify(
|
|
155
|
+
verify(response: string, remoteip: string, behaviorType?: 'login' | 'signup', clientTokens?: Record<string, string>): Promise<{
|
|
161
156
|
success: boolean;
|
|
162
157
|
challenge_ts: string;
|
|
163
158
|
hostname: string | null;
|
|
@@ -176,13 +171,12 @@ export declare class HCaptchaClient {
|
|
|
176
171
|
sigs?: Record<string, unknown> | undefined;
|
|
177
172
|
tags?: string[] | undefined;
|
|
178
173
|
}>;
|
|
179
|
-
checkVerifyResult(result: HcaptchaVerifyResult, tokens
|
|
180
|
-
|
|
181
|
-
protected hashToken(value: string): Promise<string>;
|
|
174
|
+
checkVerifyResult(result: HcaptchaVerifyResult, tokens?: Record<string, string>): void;
|
|
175
|
+
hashToken(value: string): Promise<string>;
|
|
182
176
|
}
|
|
183
177
|
export declare class HCaptchaVerifyError extends Error {
|
|
184
178
|
readonly result: HcaptchaVerifyResult;
|
|
185
|
-
readonly tokens
|
|
186
|
-
constructor(result: HcaptchaVerifyResult, tokens
|
|
179
|
+
readonly tokens?: Record<string, string> | undefined;
|
|
180
|
+
constructor(result: HcaptchaVerifyResult, tokens?: Record<string, string> | undefined, message?: string);
|
|
187
181
|
}
|
|
188
182
|
//# sourceMappingURL=hcaptcha.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hcaptcha.d.ts","sourceRoot":"","sources":["../../src/lib/hcaptcha.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AACvB,OAAO,EACL,KAAK,KAAK,EACV,KAAK,UAAU,EAKhB,MAAM,0BAA0B,CAAA;AAGjC,eAAO,MAAM,mBAAmB,aAAoB,CAAA;AACpD,MAAM,MAAM,aAAa,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,mBAAmB,CAAC,CAAA;AAE/D,eAAO,MAAM,oBAAoB;IAC/B;;OAEG;;IAEH;;OAEG;;IAEH;;OAEG;;IAEH;;;;;;OAMG;;;;;;;;;;;;EAEH,CAAA;AACF,MAAM,MAAM,cAAc,GAAG,CAAC,CAAC,KAAK,CAAC,OA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109
|
}
|
|
110
|
-
async verify(
|
|
110
|
+
async verify(response, remoteip, behaviorType, clientTokens) {
|
|
111
|
+
const body = new URLSearchParams({
|
|
112
|
+
secret: this.config.secretKey,
|
|
113
|
+
sitekey: this.config.siteKey,
|
|
114
|
+
response,
|
|
115
|
+
remoteip,
|
|
116
|
+
});
|
|
117
|
+
if (behaviorType)
|
|
118
|
+
body.set('behavior_type', behaviorType);
|
|
119
|
+
if (clientTokens)
|
|
120
|
+
body.set('client_tokens', JSON.stringify(clientTokens));
|
|
111
121
|
return this.fetch('https://api.hcaptcha.com/siteverify', {
|
|
112
122
|
method: 'POST',
|
|
113
123
|
headers: {
|
|
114
124
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
115
125
|
},
|
|
116
|
-
body:
|
|
117
|
-
secret: this.config.secretKey,
|
|
118
|
-
sitekey: this.config.siteKey,
|
|
119
|
-
behavior_type: behaviorType,
|
|
120
|
-
response,
|
|
121
|
-
remoteip,
|
|
122
|
-
client_tokens: JSON.stringify(clientTokens),
|
|
123
|
-
}).toString(),
|
|
126
|
+
body: body.toString(),
|
|
124
127
|
}).then(fetchSuccessHandler);
|
|
125
128
|
}
|
|
126
129
|
checkVerifyResult(result, tokens) {
|
|
@@ -144,17 +147,6 @@ export class HCaptchaClient {
|
|
|
144
147
|
throw new HCaptchaVerifyError(result, tokens, `Score ${score} is above the threshold ${this.config.scoreThreshold}`);
|
|
145
148
|
}
|
|
146
149
|
}
|
|
147
|
-
async buildClientTokens(remoteip, handle, userAgent) {
|
|
148
|
-
// @NOTE We are **not** using Promise.all here because libuv's thread pool
|
|
149
|
-
// might get saturated, leading to performance degradation. Instead, we
|
|
150
|
-
// await each hash sequentially, ensuring that every HTTP request only uses
|
|
151
|
-
// one thread at a time.
|
|
152
|
-
return {
|
|
153
|
-
hashedIp: await this.hashToken(remoteip),
|
|
154
|
-
hashedHandle: await this.hashToken(handle),
|
|
155
|
-
hashedUserAgent: userAgent ? await this.hashToken(userAgent) : undefined,
|
|
156
|
-
};
|
|
157
|
-
}
|
|
158
150
|
async hashToken(value) {
|
|
159
151
|
return new Promise((resolve, reject) => {
|
|
160
152
|
// @NOTE This used to be implemented as sha256("<salt><value>"), which
|
package/dist/lib/hcaptcha.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hcaptcha.js","sourceRoot":"","sources":["../../src/lib/hcaptcha.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACpC,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AACvB,OAAO,EAGL,SAAS,EACT,kBAAkB,EAClB,qBAAqB,EACrB,gBAAgB,GACjB,MAAM,0BAA0B,CAAA;AACjC,OAAO,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAA;AAEzC,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAA;AAGpD,MAAM,CAAC,MAAM,oBAAoB,GAAG,CAAC,CAAC,MAAM,CAAC;IAC3C;;OAEG;IACH,OAAO,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;IAC1B;;OAEG;IACH,SAAS,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;IAC5B;;OAEG;IACH,SAAS,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;IAC5B;;;;;;OAMG;IACH,cAAc,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;CACtC,CAAC,CAAA;AAGF;;GAEG;AACH,MAAM,CAAC,MAAM,0BAA0B,GAAG,CAAC,CAAC,MAAM,CAAC;IACjD;;OAEG;IACH,OAAO,EAAE,CAAC,CAAC,OAAO,EAAE;IACpB;;OAEG;IACH,YAAY,EAAE,CAAC,CAAC,MAAM,EAAE;IACxB;;OAEG;IACH,QAAQ,EA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AA;QACH,CAAC,CAAC,CAAA;IACJ,CAAC;CACF;AAED,MAAM,OAAO,mBAAoB,SAAQ,KAAK;IAC5C,YACW,MAA4B,EAC5B,MAA4B,EACrC,OAAgB;QAEhB,KAAK,CAAC,OAAO,CAAC,CAAA;sBAJL,MAAM;sBACN,MAAM;IAIjB,CAAC;CACF","sourcesContent":["import { scrypt } from 'node:crypto'\nimport { z } from 'zod'\nimport {\n type Fetch,\n type FetchBound,\n bindFetch,\n fetchJsonProcessor,\n fetchJsonZodProcessor,\n fetchOkProcessor,\n} from '@atproto-labs/fetch-node'\nimport { pipe } from '@atproto-labs/pipe'\n\nexport const hcaptchaTokenSchema = z.string().min(1)\nexport type HcaptchaToken = z.infer<typeof hcaptchaTokenSchema>\n\nexport const hcaptchaConfigSchema = z.object({\n /**\n * The hCaptcha site key to use for the sign-up form.\n */\n siteKey: z.string().min(1),\n /**\n * The hCaptcha secret key to use for the sign-up form.\n */\n secretKey: z.string().min(1),\n /**\n * A salt to use when hashing client tokens.\n */\n tokenSalt: z.string().min(1),\n /**\n * The risk score above which the user is considered a threat and will be\n * denied access. This will be ignored if the enterprise features are not\n * available.\n *\n * Note: Score values ranges from 0.0 (no risk) to 1.0 (confirmed threat).\n */\n scoreThreshold: z.number().optional(),\n})\nexport type HcaptchaConfig = z.infer<typeof hcaptchaConfigSchema>\n\n/**\n * @see {@link https://docs.hcaptcha.com/#verify-the-user-response-server-side hCaptcha API}\n */\nexport const hcaptchaVerifyResultSchema = z.object({\n /**\n * is the passcode valid, and does it meet security criteria you specified, e.g. sitekey?\n */\n success: z.boolean(),\n /**\n * timestamp of the challenge (ISO format yyyy-MM-dd'T'HH:mm:ssZZ)\n */\n challenge_ts: z.string(),\n /**\n * the hostname of the site where the challenge was passed\n */\n hostname: z.string().nullable(),\n /**\n * optional: any error codes returned by the hCaptcha API.\n * @see {@link https://docs.hcaptcha.com/#siteverify-error-codes-table}\n */\n 'error-codes': z.array(z.string()).optional(),\n /**\n * ENTERPRISE feature: a score denoting malicious activity. Value ranges from\n * 0.0 (no risk) to 1.0 (confirmed threat).\n */\n score: z.number().optional(),\n /**\n * ENTERPRISE feature: reason(s) for score.\n */\n score_reason: z.array(z.string()).optional(),\n /**\n * sitekey of the request\n */\n sitekey: z.string().optional(),\n /**\n * obj of form: {'ip_device': 1, .. etc}\n */\n behavior_counts: z.record(z.unknown()).optional(),\n /**\n * how similar is this? (0.0 - 1.0, -1 on err)\n */\n similarity: z.number().optional(),\n /**\n * count of similar_tokens not processed\n */\n similarity_failures: z.number().optional(),\n /**\n * array of strings for any similarity errors\n */\n similarity_error_details: z.array(z.string()).optional(),\n /**\n * encoded clientID\n */\n scoped_uid_0: z.string().optional(),\n /**\n * encoded IP\n */\n scoped_uid_1: z.string().optional(),\n /**\n * encoded IP (APT)\n */\n scoped_uid_2: z.string().optional(),\n /**\n * Risk Insights (APT + RI)\n */\n risk_insights: z.record(z.unknown()).optional(),\n /**\n * Advanced Threat Signatures (APT)\n */\n sigs: z.record(z.unknown()).optional(),\n /**\n * tags added via Rules\n */\n tags: z.array(z.string()).optional(),\n})\n\nexport type HcaptchaVerifyResult = z.infer<typeof hcaptchaVerifyResultSchema>\n\nexport type HcaptchaClientTokens = {\n hashedIp: string\n hashedHandle: string\n hashedUserAgent?: string\n}\n\nconst fetchSuccessHandler = pipe(\n fetchOkProcessor(),\n fetchJsonProcessor(),\n fetchJsonZodProcessor(hcaptchaVerifyResultSchema),\n)\n\nexport class HCaptchaClient {\n protected readonly fetch: FetchBound\n constructor(\n readonly hostname: string,\n readonly config: HcaptchaConfig,\n fetch: Fetch = globalThis.fetch,\n ) {\n this.fetch = bindFetch(fetch)\n }\n\n public async verify(\n behaviorType: 'login' | 'signup',\n response: string,\n remoteip: string,\n clientTokens: HcaptchaClientTokens,\n ) {\n return this.fetch('https://api.hcaptcha.com/siteverify', {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/x-www-form-urlencoded',\n },\n body: new URLSearchParams({\n secret: this.config.secretKey,\n sitekey: this.config.siteKey,\n behavior_type: behaviorType,\n response,\n remoteip,\n client_tokens: JSON.stringify(clientTokens),\n }).toString(),\n }).then(fetchSuccessHandler)\n }\n\n public checkVerifyResult(\n result: HcaptchaVerifyResult,\n tokens: HcaptchaClientTokens,\n ): void {\n const { success, score } = result\n\n if (success !== true) {\n throw new HCaptchaVerifyError(\n result,\n tokens,\n 'Expected success to be true',\n )\n }\n\n // https://docs.hcaptcha.com/#verify-the-user-response-server-side\n\n // Please [...] note that the hostname field is derived from the user's\n // browser, and should not be used for authentication of any kind; it is\n // primarily useful as a statistical metric. Additionally, in the event that\n // your site experiences unusually high challenge traffic, the hostname\n // field may be returned as \"not-provided\" rather than the usual value; all\n // other fields will return their normal values.\n\n if (\n // Ignore if enterprise feature is not enabled\n score != null &&\n // Ignore if disabled through config\n this.config.scoreThreshold != null &&\n score >= this.config.scoreThreshold\n ) {\n throw new HCaptchaVerifyError(\n result,\n tokens,\n `Score ${score} is above the threshold ${this.config.scoreThreshold}`,\n )\n }\n }\n\n public async buildClientTokens(\n remoteip: string,\n handle: string,\n userAgent?: string,\n ): Promise<HcaptchaClientTokens> {\n // @NOTE We are **not** using Promise.all here because libuv's thread pool\n // might get saturated, leading to performance degradation. Instead, we\n // await each hash sequentially, ensuring that every HTTP request only uses\n // one thread at a time.\n return {\n hashedIp: await this.hashToken(remoteip),\n hashedHandle: await this.hashToken(handle),\n hashedUserAgent: userAgent ? await this.hashToken(userAgent) : undefined,\n }\n }\n\n protected async hashToken(value: string): Promise<string> {\n return new Promise<string>((resolve, reject) => {\n // @NOTE This used to be implemented as sha256(\"<salt><value>\"), which\n // resulted in a 32-byte derived key, hence the length parameter 32 in\n // scrypt.\n scrypt(\n value,\n this.config.tokenSalt,\n 32,\n {\n // @NOTE we use a small cost to reduce CPU usage during hashing. Since\n // we are not hashing passwords, a lower cost is acceptable.\n cost: 1024, // defaults to 16384 (must be power of 2)\n },\n (err, derivedKey) => {\n if (err) reject(err)\n else resolve(derivedKey.toString('base64'))\n },\n )\n })\n }\n}\n\nexport class HCaptchaVerifyError extends Error {\n constructor(\n readonly result: HcaptchaVerifyResult,\n readonly tokens: HcaptchaClientTokens,\n message?: string,\n ) {\n super(message)\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"hcaptcha.js","sourceRoot":"","sources":["../../src/lib/hcaptcha.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACpC,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AACvB,OAAO,EAGL,SAAS,EACT,kBAAkB,EAClB,qBAAqB,EACrB,gBAAgB,GACjB,MAAM,0BAA0B,CAAA;AACjC,OAAO,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAA;AAEzC,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAA;AAGpD,MAAM,CAAC,MAAM,oBAAoB,GAAG,CAAC,CAAC,MAAM,CAAC;IAC3C;;OAEG;IACH,OAAO,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;IAC1B;;OAEG;IACH,SAAS,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;IAC5B;;OAEG;IACH,SAAS,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;IAC5B;;;;;;OAMG;IACH,cAAc,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;CACtC,CAAC,CAAA;AAGF;;GAEG;AACH,MAAM,CAAC,MAAM,0BAA0B,GAAG,CAAC,CAAC,MAAM,CAAC;IACjD;;OAEG;IACH,OAAO,EAAE,CAAC,CAAC,OAAO,EAAE;IACpB;;OAEG;IACH,YAAY,EAAE,CAAC,CAAC,MAAM,EAAE;IACxB;;OAEG;IACH,QAAQ,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,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EACN,MAAM,EACN,6BAA6B,CAC9B,CAAA;QACH,CAAC;QAED,kEAAkE;QAElE,uEAAuE;QACvE,wEAAwE;QACxE,4EAA4E;QAC5E,uEAAuE;QACvE,2EAA2E;QAC3E,gDAAgD;QAEhD;QACE,8CAA8C;QAC9C,KAAK,IAAI,IAAI;YACb,oCAAoC;YACpC,IAAI,CAAC,MAAM,CAAC,cAAc,IAAI,IAAI;YAClC,KAAK,IAAI,IAAI,CAAC,MAAM,CAAC,cAAc,EACnC,CAAC;YACD,MAAM,IAAI,mBAAmB,CAC3B,MAAM,EACN,MAAM,EACN,SAAS,KAAK,2BAA2B,IAAI,CAAC,MAAM,CAAC,cAAc,EAAE,CACtE,CAAA;QACH,CAAC;IACH,CAAC;IAEM,KAAK,CAAC,SAAS,CAAC,KAAa;QAClC,OAAO,IAAI,OAAO,CAAS,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YAC7C,sEAAsE;YACtE,sEAAsE;YACtE,UAAU;YACV,MAAM,CACJ,KAAK,EACL,IAAI,CAAC,MAAM,CAAC,SAAS,EACrB,EAAE,EACF;gBACE,sEAAsE;gBACtE,4DAA4D;gBAC5D,IAAI,EAAE,IAAI,EAAE,yCAAyC;aACtD,EACD,CAAC,GAAG,EAAE,UAAU,EAAE,EAAE;gBAClB,IAAI,GAAG;oBAAE,MAAM,CAAC,GAAG,CAAC,CAAA;;oBACf,OAAO,CAAC,UAAU,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC,CAAA;YAC7C,CAAC,CACF,CAAA;QACH,CAAC,CAAC,CAAA;IACJ,CAAC;CACF;AAED,MAAM,OAAO,mBAAoB,SAAQ,KAAK;IAC5C,YACW,MAA4B,EAC5B,MAA+B,EACxC,OAAgB;QAEhB,KAAK,CAAC,OAAO,CAAC,CAAA;sBAJL,MAAM;sBACN,MAAM;IAIjB,CAAC;CACF","sourcesContent":["import { scrypt } from 'node:crypto'\nimport { z } from 'zod'\nimport {\n type Fetch,\n type FetchBound,\n bindFetch,\n fetchJsonProcessor,\n fetchJsonZodProcessor,\n fetchOkProcessor,\n} from '@atproto-labs/fetch-node'\nimport { pipe } from '@atproto-labs/pipe'\n\nexport const hcaptchaTokenSchema = z.string().min(1)\nexport type HcaptchaToken = z.infer<typeof hcaptchaTokenSchema>\n\nexport const hcaptchaConfigSchema = z.object({\n /**\n * The hCaptcha site key to use for the sign-up form.\n */\n siteKey: z.string().min(1),\n /**\n * The hCaptcha secret key to use for the sign-up form.\n */\n secretKey: z.string().min(1),\n /**\n * A salt to use when hashing client tokens.\n */\n tokenSalt: z.string().min(1),\n /**\n * The risk score above which the user is considered a threat and will be\n * denied access. This will be ignored if the enterprise features are not\n * available.\n *\n * Note: Score values ranges from 0.0 (no risk) to 1.0 (confirmed threat).\n */\n scoreThreshold: z.number().optional(),\n})\nexport type HcaptchaConfig = z.infer<typeof hcaptchaConfigSchema>\n\n/**\n * @see {@link https://docs.hcaptcha.com/#verify-the-user-response-server-side hCaptcha API}\n */\nexport const hcaptchaVerifyResultSchema = z.object({\n /**\n * is the passcode valid, and does it meet security criteria you specified, e.g. sitekey?\n */\n success: z.boolean(),\n /**\n * timestamp of the challenge (ISO format yyyy-MM-dd'T'HH:mm:ssZZ)\n */\n challenge_ts: z.string(),\n /**\n * the hostname of the site where the challenge was passed\n */\n hostname: z.string().nullable(),\n /**\n * optional: any error codes returned by the hCaptcha API.\n * @see {@link https://docs.hcaptcha.com/#siteverify-error-codes-table}\n */\n 'error-codes': z.array(z.string()).optional(),\n /**\n * ENTERPRISE feature: a score denoting malicious activity. Value ranges from\n * 0.0 (no risk) to 1.0 (confirmed threat).\n */\n score: z.number().optional(),\n /**\n * ENTERPRISE feature: reason(s) for score.\n */\n score_reason: z.array(z.string()).optional(),\n /**\n * sitekey of the request\n */\n sitekey: z.string().optional(),\n /**\n * obj of form: {'ip_device': 1, .. etc}\n */\n behavior_counts: z.record(z.unknown()).optional(),\n /**\n * how similar is this? (0.0 - 1.0, -1 on err)\n */\n similarity: z.number().optional(),\n /**\n * count of similar_tokens not processed\n */\n similarity_failures: z.number().optional(),\n /**\n * array of strings for any similarity errors\n */\n similarity_error_details: z.array(z.string()).optional(),\n /**\n * encoded clientID\n */\n scoped_uid_0: z.string().optional(),\n /**\n * encoded IP\n */\n scoped_uid_1: z.string().optional(),\n /**\n * encoded IP (APT)\n */\n scoped_uid_2: z.string().optional(),\n /**\n * Risk Insights (APT + RI)\n */\n risk_insights: z.record(z.unknown()).optional(),\n /**\n * Advanced Threat Signatures (APT)\n */\n sigs: z.record(z.unknown()).optional(),\n /**\n * tags added via Rules\n */\n tags: z.array(z.string()).optional(),\n})\n\nexport type HcaptchaVerifyResult = z.infer<typeof hcaptchaVerifyResultSchema>\n\nconst fetchSuccessHandler = pipe(\n fetchOkProcessor(),\n fetchJsonProcessor(),\n fetchJsonZodProcessor(hcaptchaVerifyResultSchema),\n)\n\nexport class HCaptchaClient {\n protected readonly fetch: FetchBound\n constructor(\n readonly hostname: string,\n readonly config: HcaptchaConfig,\n fetch: Fetch = globalThis.fetch,\n ) {\n this.fetch = bindFetch(fetch)\n }\n\n public async verify(\n response: string,\n remoteip: string,\n behaviorType?: 'login' | 'signup',\n clientTokens?: Record<string, string>,\n ) {\n const body = new URLSearchParams({\n secret: this.config.secretKey,\n sitekey: this.config.siteKey,\n response,\n remoteip,\n })\n if (behaviorType) body.set('behavior_type', behaviorType)\n if (clientTokens) body.set('client_tokens', JSON.stringify(clientTokens))\n\n return this.fetch('https://api.hcaptcha.com/siteverify', {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/x-www-form-urlencoded',\n },\n body: body.toString(),\n }).then(fetchSuccessHandler)\n }\n\n public checkVerifyResult(\n result: HcaptchaVerifyResult,\n tokens?: Record<string, string>,\n ): void {\n const { success, score } = result\n\n if (success !== true) {\n throw new HCaptchaVerifyError(\n result,\n tokens,\n 'Expected success to be true',\n )\n }\n\n // https://docs.hcaptcha.com/#verify-the-user-response-server-side\n\n // Please [...] note that the hostname field is derived from the user's\n // browser, and should not be used for authentication of any kind; it is\n // primarily useful as a statistical metric. Additionally, in the event that\n // your site experiences unusually high challenge traffic, the hostname\n // field may be returned as \"not-provided\" rather than the usual value; all\n // other fields will return their normal values.\n\n if (\n // Ignore if enterprise feature is not enabled\n score != null &&\n // Ignore if disabled through config\n this.config.scoreThreshold != null &&\n score >= this.config.scoreThreshold\n ) {\n throw new HCaptchaVerifyError(\n result,\n tokens,\n `Score ${score} is above the threshold ${this.config.scoreThreshold}`,\n )\n }\n }\n\n public async hashToken(value: string): Promise<string> {\n return new Promise<string>((resolve, reject) => {\n // @NOTE This used to be implemented as sha256(\"<salt><value>\"), which\n // resulted in a 32-byte derived key, hence the length parameter 32 in\n // scrypt.\n scrypt(\n value,\n this.config.tokenSalt,\n 32,\n {\n // @NOTE we use a small cost to reduce CPU usage during hashing. Since\n // we are not hashing passwords, a lower cost is acceptable.\n cost: 1024, // defaults to 16384 (must be power of 2)\n },\n (err, derivedKey) => {\n if (err) reject(err)\n else resolve(derivedKey.toString('base64'))\n },\n )\n })\n }\n}\n\nexport class HCaptchaVerifyError extends Error {\n constructor(\n readonly result: HcaptchaVerifyResult,\n readonly tokens?: Record<string, string>,\n message?: string,\n ) {\n super(message)\n }\n}\n"]}
|
|
@@ -17,5 +17,5 @@ export declare function cacheControlMiddleware(maxAge: number): Middleware<void>
|
|
|
17
17
|
export type JsonResponse<P = unknown> = WriteResponseOptions & {
|
|
18
18
|
json: P;
|
|
19
19
|
};
|
|
20
|
-
export declare function jsonHandler<T, Req extends IncomingMessage = IncomingMessage, Res extends ServerResponse = ServerResponse>(buildJson: (this: T, req: Req, res: Res) => Awaitable<JsonResponse
|
|
20
|
+
export declare function jsonHandler<T, Req extends IncomingMessage = IncomingMessage, Res extends ServerResponse = ServerResponse>(buildJson: (this: T, req: Req, res: Res) => Awaitable<JsonResponse>, supportedContentTypes?: string[]): Middleware<T, Req, Res>;
|
|
21
21
|
//# sourceMappingURL=response.d.ts.map
|
|
@@ -1 +1 @@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GAAG,EAAE,cAAc,EACnB,KAAK,EAAE,MAAM,GAAG,MAAM,EACtB,IAAI,EAAE,oBAAoB,GACzB,IAAI,CAIN;AAED,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAMnD;AAED,wBAAgB,SAAS,CACvB,GAAG,EAAE,cAAc,EACnB,OAAO,EAAE,OAAO,EAChB,EAAE,WAAgC,EAAE,GAAG,OAAO,EAAE,GAAE,oBAAyB,GAC1E,IAAI,CAGN;AAED,wBAAgB,oBAAoB,CAClC,KAAK,EAAE,OAAO,EACd,EAAE,WAAgC,EAAE,GAAG,OAAO,EAAE,GAAE,oBAAyB,GAC1E,OAAO,CAAC,OAAO,CAAC,CAMlB;AAED,wBAAgB,sBAAsB,CAAC,MAAM,EAAE,MAAM,GAAG,UAAU,CAAC,IAAI,CAAC,CAMvE;AAED,MAAM,MAAM,YAAY,CAAC,CAAC,GAAG,OAAO,IAAI,oBAAoB,GAAG;IAC7D,IAAI,EAAE,CAAC,CAAA;CACR,CAAA;AAED,wBAAgB,WAAW,CACzB,CAAC,EACD,GAAG,SAAS,eAAe,GAAG,eAAe,EAC7C,GAAG,SAAS,cAAc,GAAG,cAAc,EAE3C,SAAS,EAAE,CAAC,IAAI,EAAE,CAAC,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,KAAK,SAAS,CAAC,YAAY,CAAC,EACnE,qBAAqB,GAAE,MAAM,EAAyB,GACrD,UAAU,CAAC,CAAC,EAAE,GAAG,EAAE,GAAG,CAAC,CAoBzB"}
|
|
@@ -50,27 +50,25 @@ export function cacheControlMiddleware(maxAge) {
|
|
|
50
50
|
next();
|
|
51
51
|
};
|
|
52
52
|
}
|
|
53
|
-
export function jsonHandler(buildJson) {
|
|
53
|
+
export function jsonHandler(buildJson, supportedContentTypes = ['application/json']) {
|
|
54
54
|
return function (req, res, next) {
|
|
55
55
|
// Ensure we can agree on a content encoding & type before starting to
|
|
56
56
|
// build the JSON response.
|
|
57
|
-
if (negotiateResponseContent(req,
|
|
58
|
-
|
|
59
|
-
// promise and return it.
|
|
60
|
-
void (async () => {
|
|
61
|
-
try {
|
|
62
|
-
const jsonResponse = await buildJson.call(this, req, res);
|
|
63
|
-
const { json, status = 200, ...options } = jsonResponse;
|
|
64
|
-
writeJson(res, json, { ...options, status });
|
|
65
|
-
}
|
|
66
|
-
catch (err) {
|
|
67
|
-
next(asError(err, 'Failed to build JSON response'));
|
|
68
|
-
}
|
|
69
|
-
})();
|
|
70
|
-
}
|
|
71
|
-
else {
|
|
72
|
-
next(createHttpError(406, 'Unsupported media type'));
|
|
57
|
+
if (!negotiateResponseContent(req, supportedContentTypes)) {
|
|
58
|
+
return void next(createHttpError(406, 'Unsupported media type'));
|
|
73
59
|
}
|
|
60
|
+
// A middleware should not be async, so we wrap the async operation in a
|
|
61
|
+
// promise and return it.
|
|
62
|
+
void (async () => {
|
|
63
|
+
try {
|
|
64
|
+
const jsonResponse = await buildJson.call(this, req, res);
|
|
65
|
+
const { json, status = 200, ...options } = jsonResponse;
|
|
66
|
+
writeJson(res, json, { ...options, status });
|
|
67
|
+
}
|
|
68
|
+
catch (err) {
|
|
69
|
+
next(asError(err, 'Failed to build JSON response'));
|
|
70
|
+
}
|
|
71
|
+
})();
|
|
74
72
|
};
|
|
75
73
|
}
|
|
76
74
|
function asError(cause, message) {
|