jdesrosiers / silex-cors-provider

A silex service provider that adds CORS services to silex
MIT License
78 stars 25 forks source link

Cors: make isCorsRequest() and isPreflightRequest() methods public #22

Closed MacDada closed 8 years ago

MacDada commented 8 years ago

Hi,

I need to use isCorsRequest() and isPreflightRequest() in my code, outside of the Cors class. I could just copy–paste them, but it would be nice to just reuse existing implementation.

BTW, we could make them static, as they don't contain any object state logic.

jdesrosiers commented 8 years ago

Do you mind sharing what it is you need access to these functions for?

MacDada commented 8 years ago

Do you mind sharing what it is you need access to these functions for?

@jdesrosiers I have a filter that validates the right app that is "talking" to my REST(y) API. It must ignore preflight requests, as browsers don't send my custom headers:

    $app->before(function (Request $request) use ($app) {
        $apiClient = $request->headers->get('X-Api-Client');

        if (!in_array($apiClient, $app['valid_api_clients'])
            && !(isset($app['cors']) && $app['cors']->isPreflightRequest($request))
        ) {
            return new JsonResponse(['api_client' => 'access_denied'], 403);
        }
    }, App::EARLY_EVENT);
MacDada commented 8 years ago

So, to be precise, I only need isPreflightRequest(), but I thought it would be useful to use isCorsRequest() as well (for some people, for me in the future? for logging?).

jdesrosiers commented 8 years ago

Closing issue because we are taking another approach. See #24