chubbyphp/chubbyphp-cors

A minimal CORS middleware for PSR 15.

1.0-beta1 2019-07-14 13:13 UTC

README

Build Status Scrutinizer Code Quality Code Coverage Total Downloads Monthly Downloads Latest Stable Version Latest Unstable Version

Description

A minimal CORS middleware for PSR 15.

Requirements

Installation

Through Composer as chubbyphp/chubbyphp-cors.

Usage

chubbyphp-framework

<?php

declare(strict_types=1);

namespace App;

use Chubbyphp\Cors\CorsMiddleware;
use Chubbyphp\Cors\Negotiation\HeadersNegotiator;
use Chubbyphp\Cors\Negotiation\MethodNegotiator;
use Chubbyphp\Cors\Negotiation\Origin\AllowOriginExact;
use Chubbyphp\Cors\Negotiation\Origin\AllowOriginRegex;
use Chubbyphp\Cors\Negotiation\Origin\OriginNegotiator;
use Chubbyphp\Framework\Application;
use Chubbyphp\Framework\ExceptionHandler;
use Chubbyphp\Framework\Middleware\MiddlewareDispatcher;
use Chubbyphp\Framework\Router\FastRouteRouter;
use Zend\Diactoros\ResponseFactory;

$responseFactory = new ResponseFactory();

$app = new Application(
    new FastRouteRouter([]),
    new MiddlewareDispatcher(),
    new ExceptionHandler($responseFactory, true),
    [
        new CorsMiddleware(
            $responseFactory,
            new OriginNegotiator([
                new AllowOriginExact('https://myproject.com'),
                new AllowOriginRegex('^https://myproject\.'),
            ]), // allow-origin
            new MethodNegotiator(['GET', 'POST']), // allow-method
            new HeadersNegotiator(['X-Custom-Request']), // allow-headers
            ['X-Custom-Response'], // expose-headers
            true, // allow-credentials
            7200 // max age
        )
    ]
);

slim

<?php

declare(strict_types=1);

namespace App;

use Chubbyphp\Cors\CorsMiddleware;
use Chubbyphp\Cors\Negotiation\HeadersNegotiator;
use Chubbyphp\Cors\Negotiation\MethodNegotiator;
use Chubbyphp\Cors\Negotiation\Origin\AllowOriginExact;
use Chubbyphp\Cors\Negotiation\Origin\AllowOriginRegex;
use Chubbyphp\Cors\Negotiation\Origin\OriginNegotiator;
use Chubbyphp\SlimPsr15\MiddlewareAdapter;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Slim\App;
use Slim\Http\Response;

$responseFactory = new class() implements ResponseFactoryInterface
{
    /**
     * @param int    $code
        * @param string $reasonPhrase
        *
        * @return ResponseInterface
        */
    public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface
    {
        $response = new Response($code);
        if ('' !== $reasonPhrase) {
            $response = $response->withStatus($code, $reasonPhrase);
        }

        return $response;
    }
};

$app = new App();

$app->add(new MiddlewareAdapter(
    new CorsMiddleware(
        $responseFactory,
        new OriginNegotiator([
            new AllowOriginExact('https://myproject.com'),
            new AllowOriginRegex('^https://myproject\.'),
        ]), // allow-origin
        new MethodNegotiator(['GET', 'POST']), // allow-method
        new HeadersNegotiator(['X-Custom-Request']), // allow-headers
        ['X-Custom-Response'], // expose-headers
        true, // allow-credentials
        7200 // max age
    )
));

Copyright

Dominik Zogg 2019