api de gestion de ticket, basé sur php-crud-api. Le but est de décorrélé les outils de gestion des données, afin
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CorsMiddleware.php 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace Tqdev\PhpCrudApi\Middleware;
  3. use Psr\Http\Message\ResponseInterface;
  4. use Psr\Http\Message\ServerRequestInterface;
  5. use Psr\Http\Server\RequestHandlerInterface;
  6. use Tqdev\PhpCrudApi\Controller\Responder;
  7. use Tqdev\PhpCrudApi\Middleware\Base\Middleware;
  8. use Tqdev\PhpCrudApi\Record\ErrorCode;
  9. use Tqdev\PhpCrudApi\ResponseFactory;
  10. class CorsMiddleware extends Middleware
  11. {
  12. private function isOriginAllowed(string $origin, string $allowedOrigins): bool
  13. {
  14. $found = false;
  15. foreach (explode(',', $allowedOrigins) as $allowedOrigin) {
  16. $hostname = preg_quote(strtolower(trim($allowedOrigin)));
  17. $regex = '/^' . str_replace('\*', '.*', $hostname) . '$/';
  18. if (preg_match($regex, $origin)) {
  19. $found = true;
  20. break;
  21. }
  22. }
  23. return $found;
  24. }
  25. public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
  26. {
  27. $method = $request->getMethod();
  28. $origin = count($request->getHeader('Origin')) ? $request->getHeader('Origin')[0] : '';
  29. $allowedOrigins = $this->getProperty('allowedOrigins', '*');
  30. if ($origin && !$this->isOriginAllowed($origin, $allowedOrigins)) {
  31. $response = $this->responder->error(ErrorCode::ORIGIN_FORBIDDEN, $origin);
  32. } elseif ($method == 'OPTIONS') {
  33. $response = ResponseFactory::fromStatus(ResponseFactory::OK);
  34. $allowHeaders = $this->getProperty('allowHeaders', 'Content-Type, X-XSRF-TOKEN, X-Authorization');
  35. if ($allowHeaders) {
  36. $response = $response->withHeader('Access-Control-Allow-Headers', $allowHeaders);
  37. }
  38. $allowMethods = $this->getProperty('allowMethods', 'OPTIONS, GET, PUT, POST, DELETE, PATCH');
  39. if ($allowMethods) {
  40. $response = $response->withHeader('Access-Control-Allow-Methods', $allowMethods);
  41. }
  42. $allowCredentials = $this->getProperty('allowCredentials', 'true');
  43. if ($allowCredentials) {
  44. $response = $response->withHeader('Access-Control-Allow-Credentials', $allowCredentials);
  45. }
  46. $maxAge = $this->getProperty('maxAge', '1728000');
  47. if ($maxAge) {
  48. $response = $response->withHeader('Access-Control-Max-Age', $maxAge);
  49. }
  50. $exposeHeaders = $this->getProperty('exposeHeaders', '');
  51. if ($exposeHeaders) {
  52. $response = $response->withHeader('Access-Control-Expose-Headers', $exposeHeaders);
  53. }
  54. } else {
  55. $response = $next->handle($request);
  56. }
  57. if ($origin) {
  58. $allowCredentials = $this->getProperty('allowCredentials', 'true');
  59. if ($allowCredentials) {
  60. $response = $response->withHeader('Access-Control-Allow-Credentials', $allowCredentials);
  61. }
  62. $response = $response->withHeader('Access-Control-Allow-Origin', $origin);
  63. }
  64. return $response;
  65. }
  66. }