Skip to content

Commit ded7732

Browse files
author
adil.slassi
committed
WIP feat captchEtat V2
1 parent 3b44bd7 commit ded7732

File tree

6 files changed

+55
-17
lines changed

6 files changed

+55
-17
lines changed

components/CaptchEtatBundle/src/bundle/Controller/CaptchEtatController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function apiSimpleCaptchaEndpointAction(Request $request): Response
5656
public function getCaptcha(): Response
5757
{
5858
$challenge = ($this->challengeGenerator)();
59-
$response = new Response($challenge->captchaHtml);
59+
$response = new Response($challenge);
6060
$response->setPrivate();
6161

6262
return $response;

components/CaptchEtatBundle/src/bundle/Resources/public/js/captchetat-widget.js

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,42 +38,60 @@ export const addLinksToHead = container => {
3838
}
3939
};
4040

41-
const captchaEtat = (function () {
41+
// Define captchaEtat object
42+
export const captchaEtat = (function () {
4243
function _init(container = document) {
4344
const widgets = container.querySelectorAll('.js-captcha-widget');
4445

4546
for (const widget of widgets) {
4647
const htmlContainer = widget.querySelector('.captcha-html-container');
47-
const idInput = widget.querySelector('.captcha-input [name*="[captcha_id]"]')
48-
if(htmlContainer.querySelector('.captcha-html')) {
49-
return
48+
const idInput = widget.querySelector('.captcha-input [name*="[captcha_id]"]');
49+
if (htmlContainer.querySelector('.captcha-html')) {
50+
return;
5051
}
5152

5253
fetch('/api/simple-captcha').then((response) => {
54+
console.log('response ::', response);
5355
return response.text();
5456
}).then((html) => {
57+
58+
//__________________ add img tag
59+
const parsedData = JSON.parse(html);
60+
const imageBase64 = parsedData.imageb64;
61+
const imageId = parsedData.uuid;
62+
const img = document.createElement('img');
63+
img.src = imageBase64;
64+
img.id = imageId;
65+
img.classList = "captch-etat-v2";
66+
console.log('imageBase64', imageBase64);
67+
console.log('img', img);
68+
document.getElementById('form-fre-FR-438825-1795842_fields_captcha').appendChild(img);
69+
console.log('okkk ::');
70+
//__________________ add img tag
71+
72+
5573
const tmp = document.createElement('div');
5674
tmp.innerHTML = html;
5775
addLinksToHead(tmp);
5876
addScriptsToHead(tmp);
59-
const originalIdInput = tmp.querySelector('[name^="BDC_VCID"]')
77+
const originalIdInput = tmp.querySelector('[name^="BDC_VCID"]');
6078
idInput.value = originalIdInput.value;
6179

6280
const tmpRoot = tmp.children.item(0);
63-
tmpRoot.removeAttribute('id')
81+
tmpRoot.removeAttribute('id');
6482
tmpRoot.classList.add('captcha-html');
6583

6684
htmlContainer.prepend(tmpRoot);
6785

6886
const soundLink = widget.querySelector('.BDC_SoundLink');
6987
const answerInput = widget.querySelector('.captcha-input input[type="text"]');
7088
soundLink.addEventListener('click', function (e) {
71-
if(answerInput) {
89+
if (answerInput) {
7290
answer.removeAttribute('disabled');
73-
answerInput.focus()
91+
answerInput.focus();
7492
}
75-
})
76-
})
93+
});
94+
});
7795
}
7896
}
7997

components/CaptchEtatBundle/src/lib/Api/Gateway.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function getSimpleCaptchaEndpoint(
3737
string $captchaType = 'html',
3838
?string $mode = null,
3939
?string $tech = null,
40-
string $type = 'numerique6_7CaptchaFR'
40+
string $type = 'alphanumerique4to6LightCaptchaFR'
4141
): string {
4242
$token = $this->oauthGateway->getOauth20Token();
4343
$available = [
@@ -64,11 +64,12 @@ public function getSimpleCaptchaEndpoint(
6464
}
6565

6666
$service = '/piste/captcha/simple-captcha-endpoint';
67+
$service = '/piste/captchetat/v2/simple-captcha-endpoint';
6768
$method = 'GET';
6869

6970
$queryParams = [
7071
'get' => $captchaType,
71-
'c' => $type,
72+
'c' => 'alphanumerique4to6LightCaptchaFR',
7273
];
7374
if ($mode) {
7475
$queryParams['mode'] = $mode;
@@ -79,6 +80,7 @@ public function getSimpleCaptchaEndpoint(
7980

8081
$url = $this->url.$service.'?'.http_build_query($queryParams);
8182

83+
//dd($url);
8284
$option = [
8385
'headers' => [
8486
'Authorization' => 'Bearer '.$token,
@@ -101,9 +103,11 @@ public function getSimpleCaptchaEndpoint(
101103
return $response->getContent();
102104
} catch (ClientException|ServerException $httpException) {
103105
$this->logger->logHttpException($httpException, $requestLog);
106+
dd($httpException, $requestLog);
104107
throw $httpException;
105108
} catch (TransportException $transportException) {
106109
$this->logger->logTransportException($transportException, $requestLog);
110+
dd($transportException, $requestLog);
107111
throw $transportException;
108112
}
109113
}
@@ -114,6 +118,7 @@ public function validateChallenge(
114118
): bool {
115119
$token = $this->oauthGateway->getOauth20Token();
116120
$service = '/piste/captcha/valider-captcha';
121+
$service = '/piste/captchetat/v2/valider-captcha';
117122
$method = 'POST';
118123

119124
$url = $this->url.$service;
@@ -142,12 +147,16 @@ public function validateChallenge(
142147
$response = $this->client->request($method, $url, $option);
143148
$content = $response->getContent();
144149

150+
dump($content);die;
151+
145152
return 'true' === $content;
146153
} catch (ClientException|ServerException $httpException) {
147154
$this->logger->logHttpException($httpException, $requestLog);
155+
dd($httpException, $requestLog);
148156
throw $httpException;
149157
} catch (TransportException $transportException) {
150158
$this->logger->logTransportException($transportException, $requestLog);
159+
dd($transportException, $requestLog);
151160
throw $transportException;
152161
}
153162
}

components/CaptchEtatBundle/src/lib/Api/OauthGateway.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function getOauth20Token(): string
5252
'grant_type' => 'client_credentials',
5353
'client_id' => $this->clientId,
5454
'client_secret' => $this->clientSecret,
55-
'scope' => ['resource.READ', 'piste.captchetat'],
55+
'scope' => 'WRITE',
5656
];
5757

5858
$url = $this->url.$service;

components/CaptchEtatBundle/src/lib/Challenge/ChallengeGenerator.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,18 @@ public function __construct(
3636
$this->configResolver = $configResolver;
3737
}
3838

39-
public function __invoke(): CaptchEtatChallenge
39+
public function __invoke()
4040
{
4141
$lang = $this->getShortLanguage();
42+
$type = $this->getType($lang);
43+
$image = $this->gateway->getSimpleCaptchaEndpoint(
44+
'image',
45+
'frontal',
46+
null,
47+
$type
48+
);
49+
50+
return $image;
4251

4352
return CaptchEtatChallenge::createLazyGhost(function (CaptchEtatChallenge $instance) use ($lang) {
4453
try {
@@ -59,10 +68,10 @@ protected function getCaptchaHtml(string $lang): string
5968
{
6069
$type = $this->getType($lang);
6170
$html = $this->gateway->getSimpleCaptchaEndpoint(
62-
'html',
71+
'image',
6372
'frontal',
6473
null,
65-
$type
74+
"alphanumerique4to6LightCaptchaFR"
6675
);
6776
$hidden = 'style="visibility: hidden !important"';
6877
$html = str_replace($hidden, '', $html);

components/CaptchEtatBundle/src/lib/Validator/CaptchEtatChallengeValidator.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public function validate($value, Constraint $constraint)
2929
if (!$constraint instanceof CaptchEtatValidChallenge) {
3030
throw new UnexpectedTypeException($constraint, CaptchEtatValidChallenge::class);
3131
}
32+
dump($value);
33+
die('222');
3234

3335
if (!isset($value['captcha_id']) && !isset($value['answer'])) {
3436
throw new UnexpectedValueException($value, 'array');

0 commit comments

Comments
 (0)