-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathcaptcha.service.js
More file actions
138 lines (116 loc) · 5.16 KB
/
captcha.service.js
File metadata and controls
138 lines (116 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { getElement } from '../utils/utils.js';
import { removeUrlQueryParams } from '../utils/url.js';
import { ErrorResponse, PirError, SuccessResponse } from '../types';
import { getCaptchaProvider, getCaptchaSolveProvider } from './get-captcha-provider';
import { captchaFactory } from './providers/registry.js';
import { getCaptchaInfo as getCaptchaInfoDeprecated, solveCaptcha as solveCaptchaDeprecated } from '../actions/captcha-deprecated';
/**
*
* @param {Document | HTMLElement} root
* @param {import('../types.js').PirAction['selector']} [selector]
* @returns {HTMLElement | import('../types.js').PirError}
*/
const getCaptchaContainer = (root, selector) => {
if (!selector) {
return PirError.create('missing selector');
}
const captchaContainer = getElement(root, selector);
if (!captchaContainer) {
return PirError.create(`could not find captcha container with selector ${selector}`);
}
return captchaContainer;
};
/**
* Returns the supporting code to inject for the given captcha type
*
* @param {import('../types.js').PirAction} action
* @return {import('../types.js').SuccessResponse<{code?: string | null}> | import('../types.js').ErrorResponse}
*/
export function getSupportingCodeToInject(action) {
const { id: actionID, actionType, injectCaptchaHandler: captchaType } = action;
const createError = ErrorResponse.generateErrorResponseFunction({ actionID, context: 'getSupportingCodeToInject' });
if (!captchaType) {
// ensures backward compatibility with old actions
return SuccessResponse.create({ actionID, actionType, response: {} });
}
const captchaProvider = captchaFactory.getProviderByType(captchaType);
if (!captchaProvider) {
return createError(`could not find captchaProvider with type ${captchaType}`);
}
return SuccessResponse.create({ actionID, actionType, response: { code: captchaProvider.getSupportingCodeToInject() } });
}
/**
* Gets the captcha information to send to the backend
*
* @param {import('../types.js').PirAction} action
* @param {Document | HTMLElement} root
* @return {Promise<import('../types.js').ActionResponse>}
*/
export async function getCaptchaInfo(action, root = document) {
const { id: actionID, actionType, captchaType, selector } = action;
if (!captchaType) {
// ensures backward compatibility with old actions
return getCaptchaInfoDeprecated(action, root);
}
const createError = ErrorResponse.generateErrorResponseFunction({ actionID, context: `[getCaptchaInfo] captchaType: ${captchaType}` });
const captchaContainer = getCaptchaContainer(root, selector);
if (PirError.isError(captchaContainer)) {
return createError(captchaContainer.error.message);
}
const captchaProvider = getCaptchaProvider(root, captchaContainer, captchaType);
if (PirError.isError(captchaProvider)) {
return createError(captchaProvider.error.message);
}
const captchaIdentifier = await captchaProvider.getCaptchaIdentifier(captchaContainer);
if (!captchaIdentifier) {
return createError(`could not extract captcha identifier from the container with selector ${selector}`);
}
if (PirError.isError(captchaIdentifier)) {
return createError(captchaIdentifier.error.message);
}
const response = {
url: removeUrlQueryParams(window.location.href), // query params (which may include PII)
siteKey: captchaIdentifier,
type: captchaProvider.getType(),
};
return SuccessResponse.create({ actionID, actionType, response });
}
/**
* Takes the solved captcha token and injects it into the page to solve the captcha
*
* @param {import('../types.js').PirAction} action
* @param {string} token
* @param {Document} root
* @return {import('../types.js').ActionResponse}
*/
export function solveCaptcha(action, token, root = document) {
const { id: actionID, actionType, captchaType, selector } = action;
if (!captchaType) {
// ensures backward compatibility with old actions
return solveCaptchaDeprecated(action, token, root);
}
const createError = ErrorResponse.generateErrorResponseFunction({ actionID, context: `[solveCaptcha] captchaType: ${captchaType}` });
const captchaContainer = getCaptchaContainer(root, selector);
if (PirError.isError(captchaContainer)) {
return createError(captchaContainer.error.message);
}
const captchaSolveProvider = getCaptchaSolveProvider(captchaContainer, captchaType);
if (PirError.isError(captchaSolveProvider)) {
return createError(captchaSolveProvider.error.message);
}
if (!captchaSolveProvider.canSolve(captchaContainer)) {
return createError('cannot solve captcha');
}
const tokenResponse = captchaSolveProvider.injectToken(captchaContainer, token);
if (PirError.isError(tokenResponse)) {
return createError(tokenResponse.error.message);
}
if (!tokenResponse.response.injected) {
return createError('could not inject token');
}
return SuccessResponse.create({
actionID,
actionType,
response: { callback: { eval: captchaSolveProvider.getSolveCallback(captchaContainer, token) } },
});
}