Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow to use a custom requestInstance in solveCaptcha (SCR-612) #1012

Merged
merged 2 commits into from
May 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 34 additions & 22 deletions packages/cozy-konnector-libs/src/libs/solveCaptcha.js
Original file line number Diff line number Diff line change
@@ -50,16 +50,17 @@ const DEFAULT_TIMEOUT = connectorStartTime + 3 * m // 3 minutes by default to le
* ```
* @alias module:solveCaptcha
*/
const solveCaptcha = async (params = {}) => {
const solveCaptcha = async (userParams = {}) => {
const defaultParams = {
type: 'recaptcha',
timeout: DEFAULT_TIMEOUT,
withFullSolution: false
withFullSolution: false,
requestInstance: request
}
let solution
let resultAttribute = 'gRecaptchaResponse'

params = { ...defaultParams, ...params }
const params = { ...defaultParams, ...userParams }

const secrets = JSON.parse(process.env.COZY_PARAMETERS || '{}').secret

@@ -69,7 +70,8 @@ const solveCaptcha = async (params = {}) => {
solution = await solveWithAntiCaptcha(
{ websiteKey, websiteURL, type: 'NoCaptchaTaskProxyless' },
params.timeout,
secrets
secrets,
params
)
} else if (params.type === 'recaptchav3') {
checkMandatoryParams(params, [
@@ -88,7 +90,8 @@ const solveCaptcha = async (params = {}) => {
type: 'RecaptchaV3TaskProxyless'
},
params.timeout,
secrets
secrets,
params
)
} else if (params.type === 'hcaptcha') {
checkMandatoryParams(params, ['websiteKey', 'websiteURL'])
@@ -100,7 +103,8 @@ const solveCaptcha = async (params = {}) => {
type: 'HCaptchaTaskProxyless'
},
params.timeout,
secrets
secrets,
params
)
} else if (params.type === 'image') {
checkMandatoryParams(params, ['body'])
@@ -109,7 +113,8 @@ const solveCaptcha = async (params = {}) => {
solution = await solveWithAntiCaptcha(
{ body: params.body, type: 'ImageToTextTask' },
params.timeout,
secrets
secrets,
params
)
}
if (params.withFullSolution) {
@@ -132,7 +137,8 @@ function checkMandatoryParams(params = {}, mandatoryParams = []) {
async function solveWithAntiCaptcha(
taskParams,
timeout = DEFAULT_TIMEOUT,
secrets
secrets,
params
) {
const antiCaptchaApiUrl = 'https://api.anti-captcha.com'
let gRecaptchaResponse = null
@@ -142,23 +148,29 @@ async function solveWithAntiCaptcha(
const clientKey = secrets.antiCaptchaClientKey
if (clientKey) {
log('debug', ' Creating captcha resolution task...')
const task = await request.post(`${antiCaptchaApiUrl}/createTask`, {
body: {
clientKey,
task: taskParams
},
json: true
})
const task = await params.requestInstance.post(
`${antiCaptchaApiUrl}/createTask`,
{
body: {
clientKey,
task: taskParams
},
json: true
}
)
if (task && task.taskId) {
log('debug', ` Task id : ${task.taskId}`)
while (!gRecaptchaResponse) {
const resp = await request.post(`${antiCaptchaApiUrl}/getTaskResult`, {
body: {
clientKey,
taskId: task.taskId
},
json: true
})
const resp = await params.requestInstance.post(
`${antiCaptchaApiUrl}/getTaskResult`,
{
body: {
clientKey,
taskId: task.taskId
},
json: true
}
)
if (resp.status === 'ready') {
if (resp.errorId) {
log('error', `Anticaptcha error: ${JSON.stringify(resp)}`)