Browse Source

add idCardRequest route

olinox14 1 year ago
parent
commit
f48fa0b8cd
4 changed files with 147 additions and 1 deletions
  1. 5 1
      .env
  2. 3 0
      config/services.yaml
  3. 67 0
      src/ApiResource/IdCardRequest.php
  4. 72 0
      src/State/Provider/IdCardRequestProvider.php

+ 5 - 1
.env

@@ -20,4 +20,8 @@ CONTACT_EMAIL=olinox14@tuta.io
 
 ###> altcha-org/altcha ###
 HMAC_KEY=eJd1VZeA9JVivlLdy6RrwUjUcs8kVRsMbILWudLC0kF3NiAl
-###< altcha-org/altcha ###
+###< altcha-org/altcha ###
+
+ID_AGE=35
+ID_PLACE="50290 BREHAL, France"
+ID_STATUS="Civil partnership, 3 children"

+ 3 - 0
config/services.yaml

@@ -15,6 +15,9 @@ services:
             $fromEmail: '%env(FROM_EMAIL)%'
             $contactEmail: '%env(CONTACT_EMAIL)%'
             $hmacKey: '%env(HMAC_KEY)%'
+            $idAge: '%env(ID_AGE)%'
+            $idPlace: '%env(ID_PLACE)%'
+            $idStatus: '%env(ID_STATUS)%'
 
     # makes classes in src/ available to be used as services
     # this creates a service per class whose id is the fully-qualified class name

+ 67 - 0
src/ApiResource/IdCardRequest.php

@@ -0,0 +1,67 @@
+<?php
+declare(strict_types=1);
+
+namespace App\ApiResource;
+
+use ApiPlatform\Metadata\ApiResource;
+use ApiPlatform\Metadata\Get;
+use App\State\Provider\IdCardRequestProvider;
+
+#[ApiResource(
+    operations: [
+        new Get(
+            uriTemplate: '/download-cv',
+        )
+    ],
+    provider: IdCardRequestProvider::class,
+)]
+class IdCardRequest
+{
+    protected string $base64Photo;
+
+    protected string $age;
+
+    protected string $place;
+
+    protected string $status;
+
+    public function getBase64Photo(): string
+    {
+        return $this->base64Photo;
+    }
+
+    public function setBase64Photo(string $base64Photo): void
+    {
+        $this->base64Photo = $base64Photo;
+    }
+
+    public function getAge(): string
+    {
+        return $this->age;
+    }
+
+    public function setAge(string $age): void
+    {
+        $this->age = $age;
+    }
+
+    public function getPlace(): string
+    {
+        return $this->place;
+    }
+
+    public function setPlace(string $place): void
+    {
+        $this->place = $place;
+    }
+
+    public function getStatus(): string
+    {
+        return $this->status;
+    }
+
+    public function setStatus(string $status): void
+    {
+        $this->status = $status;
+    }
+}

+ 72 - 0
src/State/Provider/IdCardRequestProvider.php

@@ -0,0 +1,72 @@
+<?php
+declare(strict_types=1);
+
+namespace App\State\Provider;
+
+use AltchaOrg\Altcha\Altcha;
+use ApiPlatform\Metadata\Get;
+use ApiPlatform\Metadata\Operation;
+use ApiPlatform\State\ProviderInterface;
+use App\ApiResource\CvPdfRequest;
+use App\ApiResource\IdCardRequest;
+use Path\Exception\FileNotFoundException;
+use Path\Exception\IOException;
+use Symfony\Component\HttpFoundation\HeaderUtils;
+use Symfony\Component\HttpFoundation\RedirectResponse;
+use Symfony\Component\HttpFoundation\Response;
+use Path\Path;
+
+class IdCardRequestProvider implements ProviderInterface
+{
+    public function __construct(
+        private readonly string $projectDir,
+        private readonly string $hmacKey,
+        private readonly string $idAge,
+        private readonly string $idPlace,
+        private readonly string $idStatus,
+    )
+    {}
+
+    /**
+     * @throws IOException
+     * @throws FileNotFoundException
+     */
+    public function provide(Operation $operation, array $uriVariables = [], array $context = []): IdCardRequest
+    {
+        if (!$operation instanceof Get) {
+            throw new \RuntimeException('not supported', Response::HTTP_METHOD_NOT_ALLOWED);
+        }
+
+        $filters = $context['filters'] ?? [];
+        $altchaPayload = $filters['payload'] ?? null;
+        if ($altchaPayload === null) {
+            throw new \RuntimeException('missing parameter: payload', Response::HTTP_BAD_REQUEST);
+        }
+
+        try {
+            $valid = Altcha::verifySolution(
+                $altchaPayload,
+                $this->hmacKey,
+                true
+            );
+        } catch (\Throwable) {
+            $valid = false;
+        }
+
+        if (!$valid) {
+            throw new \RuntimeException('Invalid payload');
+        }
+
+        $photo = (new Path($this->projectDir))
+            ->append('static', 'profile.min.png')
+            ->getContent();
+
+        $idCardRequest = new IdCardRequest();
+        $idCardRequest->setBase64Photo(base64_encode($photo));
+        $idCardRequest->setPlace($this->idPlace);
+        $idCardRequest->setAge($this->idAge);
+        $idCardRequest->setStatus($this->idStatus);
+
+        return $idCardRequest;
+    }
+}