Skip to content

Commit

Permalink
feat: CaptchEtat bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian ALEXANDRE committed Jun 27, 2024
1 parent a15adb6 commit 50e4084
Show file tree
Hide file tree
Showing 28 changed files with 1,132 additions and 0 deletions.
19 changes: 19 additions & 0 deletions components/CaptchEtatBundle/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2020 Novactive, https://github.com/Novactive/AlmaviaCXCaptchEtatBundle <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
79 changes: 79 additions & 0 deletions components/CaptchEtatBundle/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# AlmaviaCX CaptchEtat Bundle

----

This repository is what we call a "subtree split": a read-only copy of one directory of the main repository.
It is used by Composer to allow developers to depend on specific bundles.

If you want to report or contribute, you should instead open your issue on the main repository: https://github.com/Novactive/Nova-eZPlatform-Bundles

Documentation is available in this repository via `.md` files but also packaged here: https://novactive.github.io/Nova-eZPlatform-Bundles/master/2FABundle/README.md.html

----

This bundle provide a form type to use CaptchEtat (https://api.gouv.fr/les-api/api-captchetat) on your website

## Installation

### Requirements

* Ibexa 4
* PHP 7.4 || 8.0

### Use Composer

Add the lib to your composer.json, run `composer require almaviacx/captchetatbundle` to refresh dependencies.

### Register the bundle

Then inject the bundle in the `config\bundles.php` of your application.

```php
return [
// ...
AlmaviaCX\Bundle\CaptchEtatBundle\AlmaviaCXCaptchEtatBundle::class => [ 'all'=> true ],
];
```

### Add routes

Make sure you add this route to your routing:

```yaml
# config/routes.yaml

captchetat_routes:
resource: '@AlmaviaCXCaptchEtatBundle/Resources/config/routes.yaml'
```
### Accessibility
For accessibility, you might want to add the following script to your JS
```javascript
import CaptchaEtat from '../public/bundles/almaviacxcaptchetat/js/captchetat-widget'
CaptchaEtat.init()
```

## Configuration

Configuration can be done throught the following environment variables

```
CAPTCHETAT_API_URL="https://sandbox-api.piste.gouv.fr"
CAPTCHETAT_OAUTH_URL="https://sandbox-oauth.piste.gouv.fr"
CAPTCHETAT_OAUTH_CLIENT_ID=~
CAPTCHETAT_OAUTH_CLIENT_SECRET=~
CAPTCHETAT_TIMEOUT="2.5"
```

Dependeding if you use "sandbox" (default) or "production" environment, you might want to change the urls to :
```
CAPTCHETAT_API_URL="https://api.piste.gouv.fr"
CAPTCHETAT_OAUTH_URL="https://oauth.piste.gouv.fr"
```





4 changes: 4 additions & 0 deletions components/CaptchEtatBundle/ci-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
install: true
test: false
repo: Novactive/AlmaviaCXCaptchEtatBundle

32 changes: 32 additions & 0 deletions components/CaptchEtatBundle/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "almaviacx/captchetatbundle",
"description": "Add CaptchEtat to forms",
"keywords": [
"ibexa",
"bundle"
],
"homepage": "https://github.com/Novactive/AlmaviaCXCaptchEtatBundle",
"type": "ibexa-bundle",
"authors": [
{
"name": "AlmaviaCX",
"homepage": "https://almaviacx.com/expertises/web-mobile/",
"email": "[email protected]"
}
],
"license": [
"MIT"
],
"require": {
"php": "^7.4 || ^8.0",
"symfony/css-selector": "5.4.*",
"symfony/dom-crawler": "5.4.*"
},
"autoload": {
"psr-4": {
"AlmaviaCX\\Bundle\\CaptchEtatBundle\\": "src/bundle",
"AlmaviaCX\\Bundle\\CaptchEtat\\": "src/lib"
}
},
"extra": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace AlmaviaCX\Bundle\CaptchEtatBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class AlmaviaCXCaptchEtatBundle extends Bundle
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace AlmaviaCX\Bundle\CaptchEtatBundle\Controller;

use AlmaviaCX\Bundle\CaptchEtat\Api\Gateway;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;

class CaptchEtatController
{
protected Gateway $gateway;

public function __construct(
Gateway $gateway
) {
$this->gateway = $gateway;
}

/**
* Permet au captcha (programme js/html) d'appeler l'api en passant par le serveur.
*/
public function apiSimpleCaptchaEndpointAction(Request $request): Response
{
$get = $request->get('get');
$tech = $request->get('t');
$type = $request->get('c');
$content = $this->gateway->getSimpleCaptchaEndpoint($get, null, $tech, $type);
$response = new Response($content);
if ('script-include' === $get) {
$response->headers->set('Content-Type', 'text/javascript');
} elseif ('image' === $get) {
$response->headers->set('Content-Disposition', $response->headers->makeDisposition(
ResponseHeaderBag::DISPOSITION_INLINE,
'captcha.png'
));
$response->headers->set('Content-Type', 'image/png');
} elseif ('sound' === $get) {
$response->headers->set('Content-Disposition', $response->headers->makeDisposition(
ResponseHeaderBag::DISPOSITION_INLINE,
'captcha-sound.wave'
));
$response->headers->set('Content-Type', 'audio/wave');
}
$response->setPrivate();

return $response;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace AlmaviaCX\Bundle\CaptchEtatBundle\DependencyInjection;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;

class AlmaviaCXCaptchEtatExtension extends Extension implements PrependExtensionInterface
{
public function load(array $configs, ContainerBuilder $container)
{
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('default_settings.yaml');
$loader->load('services.yaml');
}

public function prepend(ContainerBuilder $container)
{
$container->prependExtensionConfig('monolog', [
'channels' => ['captcha_etat'],
]);
$container->prependExtensionConfig('twig', [
'form_themes' => ['captchetat-fields.html.twig'],
'paths' => [__DIR__.'/../Resources/templates'],
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
parameters:
#env(CAPTCHETAT_OAUTH_URL): "https://oauth.piste.gouv.fr"
env(CAPTCHETAT_OAUTH_URL): "https://sandbox-oauth.piste.gouv.fr"
env(CAPTCHETAT_OAUTH_CLIENT_ID): ~
env(CAPTCHETAT_OAUTH_CLIENT_SECRET): ~
#env(CAPTCHETAT_API_URL): "https://api.piste.gouv.fr"
env(CAPTCHETAT_API_URL): "https://sandbox-api.piste.gouv.fr"
env(CAPTCHETAT_TIMEOUT): "2.5"

captchetat_oauth_url: '%env(string:CAPTCHETAT_OAUTH_URL)%'
captchetat_oauth_client_id: '%env(string:CAPTCHETAT_OAUTH_CLIENT_ID)%'
captchetat_oauth_client_secret: '%env(string:CAPTCHETAT_OAUTH_CLIENT_SECRET)%'
captchetat_api_url: '%env(string:CAPTCHETAT_API_URL)%'
captchetat_timeout: '%env(float:CAPTCHETAT_TIMEOUT)%'
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
captchetat.endpoint:
path: /api/simple-captcha-endpoint
defaults:
_controller: 'AlmaviaCX\Bundle\CaptchEtatBundle\Controller\CaptchEtatController::apiSimpleCaptchaEndpointAction'
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
services:
AlmaviaCX\Bundle\CaptchEtat\Logger\CaptchEtatLogger:
arguments:
$innerLogger: '@Psr\Log\LoggerInterface'

AlmaviaCX\Bundle\CaptchEtat\Api\OauthGateway:
arguments:
$client: '@Symfony\Contracts\HttpClient\HttpClientInterface'
$logger: '@AlmaviaCX\Bundle\CaptchEtat\Logger\CaptchEtatLogger'
$url: '%captchetat_oauth_url%'
$clientId: '%captchetat_oauth_client_id%'
$clientSecret: '%captchetat_oauth_client_secret%'
$timeout: '%captchetat_timeout%'

AlmaviaCX\Bundle\CaptchEtat\Api\Gateway:
arguments:
$client: '@Symfony\Contracts\HttpClient\HttpClientInterface'
$logger: '@AlmaviaCX\Bundle\CaptchEtat\Logger\CaptchEtatLogger'
$oauthGateway: '@AlmaviaCX\Bundle\CaptchEtat\Api\OauthGateway'
$url: '%captchetat_api_url%'
$timeout: '%captchetat_timeout%'

AlmaviaCX\Bundle\CaptchEtat\Challenge\ChallengeGenerator:
arguments:
$configResolver: '@Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface'
$localeConverter: '@Ibexa\Core\MVC\Symfony\Locale\LocaleConverterInterface'
$gateway: '@AlmaviaCX\Bundle\CaptchEtat\Api\Gateway'
$translator: '@Symfony\Contracts\Translation\TranslatorInterface'
$logger: '@AlmaviaCX\Bundle\CaptchEtat\Logger\CaptchEtatLogger'

AlmaviaCX\Bundle\CaptchEtat\Challenge\ChallengeValidator:
arguments:
$gateway: '@AlmaviaCX\Bundle\CaptchEtat\Api\Gateway'

AlmaviaCX\Bundle\CaptchEtat\Validator\CaptchEtatChallengeValidator:
arguments:
$challengeValidator: '@AlmaviaCX\Bundle\CaptchEtat\Challenge\ChallengeValidator'
$translator: '@Symfony\Contracts\Translation\TranslatorInterface'
tags:
- validator.constraint_validator

AlmaviaCX\Bundle\CaptchEtat\Form\Type\CaptchEtatType:
tags:
- {name: 'form.type', alias: captchetat}

AlmaviaCX\Bundle\CaptchEtatBundle\Controller\CaptchEtatController:
arguments:
$gateway: '@AlmaviaCX\Bundle\CaptchEtat\Api\Gateway'
tags:
- controller.service_arguments
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const captchaEtat = (function () {
function _init(container = document) {
const widgets = container.querySelectorAll('.js-captcha-widget');

for (const widget of widgets) {
const soundLink = widget.querySelector('.BDC_SoundLink');
const answerInput = widget.querySelector('.captcha-input input[type="text"]');
soundLink.addEventListener('click', function (e) {
if(answerInput) {
answer.removeAttribute('disabled');
answerInput.focus()
}
})
}
}

return { init: _init };
})();

export default captchaEtat;
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{% block captchetat_row -%}
{% if display_captcha|default %}
{{ block('form_row') }}
{% endif %}
{%- endblock captchetat_row %}

{% block captchetat_label %}
{% if display_captcha|default %}
{{- block('form_label') }}
{% endif %}
{% endblock captchetat_label %}

{% block captchetat_widget %}
{% apply spaceless %}
{% if display_captcha|default %}
{%- set attr = attr|merge({
'class': 'widget-container row align-items-center js-captcha-widget'
}) -%}
<div {{ block('widget_container_attributes') }}>
<div class="captcha col-lg-4 col-md-6 d-flex align-items-center">
{{ captcha_html|raw }}
<div class="clearfix"></div>
</div>
<div class="captcha-input">
{{- form_widget(form.answer, { 'id': 'captchaFormulaireExtInput' } ) -}}
{{- form_widget(form.captcha_id ) -}}
</div>
</div>
{% endif %}
{% endapply %}
{% endblock captchetat_widget %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
form.answer.required: 'Answer is required'
form.answer.wrongAnswer: 'Wrong answer'
image_title: Captcha image
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
form.answer.required: 'Une réponse est requise'
form.answer.wrongAnswer: 'Mauvaise réponse'
image_title: Image du captcha
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
customform.show.captcha: 'Captcha'
form.captcha.input_answer: 'Captcha answer'
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
customform.show.captcha: 'Captcha'
form.captcha.input_answer: 'Réponse du captcha'
Loading

0 comments on commit 50e4084

Please sign in to comment.