- Added a new CSRF token verification Middleware.
- Added a new ActionLog middleware to catch every modifying request and save the changes made and the user who did. - Added a new RequestLog to catch every request made whenever required.
This commit is contained in:
1 parent
9571c6a462
commit
4b86ac9e82
42 files changed
+1331
-148
No files matched your search
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace Lean\Api\Controller\Security;
|
||||
|
||||
use DI\Attribute\Inject;
|
||||
use CSSLSJ\TransportScolaire\Api\{Entity, Form};
|
||||
use Notes\Route\Attribute\Method\Route;
|
||||
use Notes\Security\Attribute\Security;
|
||||
use Psr\Http\Message\{ResponseInterface, ServerRequestInterface};
|
||||
use Ulmus\User\Entity\UserInterface;
|
||||
|
||||
#[Security(locked: true)]
|
||||
##[Taxus(Entity\UserTypeEnum::Developer)]
|
||||
#[\Notes\Route\Attribute\Object\Route(base: "/lean/security/csrf")]
|
||||
class CSRF {
|
||||
use \Lean\Api\Lib\ControllerTrait;
|
||||
|
||||
#[Inject]
|
||||
protected UserInterface $user;
|
||||
|
||||
#[Route("/documentation", name: "lean.api:security-csrf-doc", method: "GET", description: "Route de documentation pour cette section")]
|
||||
public function documentation(ServerRequestInterface $request, array $arguments) : ResponseInterface
|
||||
{
|
||||
return $this->renderMarkdown(LEAN_API_PROJECT_PATH . "/meta/docs/security/csrf.md", [ ], [ ]);
|
||||
}
|
||||
|
||||
#[Route("/", name: "lean.api:security-csrf-generate", method: [ "GET" ], description: "Generate a CSRF token")]
|
||||
public function generate(ServerRequestInterface $request, array $arguments): ResponseInterface
|
||||
{
|
||||
$context = $this->leanApiFormFactory->securityCsrfGenerateContext($request->withMethod('PUT'));
|
||||
|
||||
return $this->output([
|
||||
'token' => $this->form($this->leanApiFormFactory->securityCsrfGenerate($this->user), $context)->executionStatus
|
||||
], 1);
|
||||
}
|
||||
|
||||
#[Route("/{token}", name: "lean.api:security-csrf-test-token", method: [ "POST", "PUT", "DELETE", "PATCH" ], description: "Verify a CSRF token")]
|
||||
public function test(ServerRequestInterface $request, array $arguments) : ResponseInterface
|
||||
{
|
||||
try {
|
||||
$valid = $this->validate($request->withHeader('X-CSRF-Token', $arguments['token']), $arguments);
|
||||
}
|
||||
catch (\Throwable $ex) {
|
||||
$valid = false;
|
||||
}
|
||||
|
||||
return $this->output([
|
||||
'valid' => $valid
|
||||
], 1);
|
||||
}
|
||||
|
||||
public function validate(ServerRequestInterface $request, array $arguments) : bool
|
||||
{
|
||||
$context = $this->leanApiFormFactory->securityCsrfVerifyTokenContext($request);
|
||||
|
||||
# This form call WILL throw an exception if anything is wrong (missing, invalid, outdated) with the CSRF token.
|
||||
return (bool) $this->form($this->leanApiFormFactory->securityCsrfVerifyToken($this->user, $arguments['token']), $context)->executionStatus;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user