Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow CORS #2

Closed
barbosaigor opened this issue Nov 20, 2019 · 2 comments
Closed

Allow CORS #2

barbosaigor opened this issue Nov 20, 2019 · 2 comments
Labels
good first issue Good for newcomers help wanted Extra attention is needed question Further information is requested

Comments

@barbosaigor
Copy link

Hey there, could anyone tell me how to allow cors in my app? I am using the water pipe, but I have not found how to address it.

@na2axl
Copy link
Member

na2axl commented Nov 21, 2019

Hi @barbosaigor , and thank you for using WaterPipe.

There is many way for you to achieve this. But the best way for me is to use a middleware-route to fill CORS HTTP headers when executing your request.

To achieve this you have to:

  1. Update WP to the latest release (I've just published an upgrade today)
  2. In your pipes, locate the one you want to allow CORS, transform it in a Route like this:
// Our Route class handling access to CORS-enabled public ressources
// Note that this Route implements IRouteMiddleware
class ResourceRoute extends Route implements IRouteMiddleware
{
    public function __construct()
    {
        // Here we define the route URI
        parent::__construct("/public-data/:domain/:id");
    }

    // We implement this method from IRouteMiddleware interface
    public function beforeExecute()
    {
        // Here we fill all required HTTP headers to enable CORS on this route.
        // It's to you to define exactly what you want here, this is only for illustration.
        $header = $this->_response->getHeader();
        $header->setField("Access-Control-Allow-Origin", "http://{$this->_request->uri['domain']}");
        $header->setField("Access-Control-Allow-Credentials", "true");
        $header->setField("Access-Control-Max-Age", "86400");
        $this->_response->setHeader($header);
        // Note that here we send ONLY headers
        $this->_response->sendHeaders();
    }

    // We implement this method from IRouteMiddleware interface
    public function beforeSend()
    {
        // For this example, there is nothing to do here...
    }

    // We implement this method from Route class
    public function get()
    {
        // Just send a text as the response
        $this->_response->sendText("Resource " . $this->_request->uri['id']);
    }

    // We implement this method from Route class
    public function post()
    {
        $this->_response->sendJson([
            "success" => true
        ]);
    }

    // We implement this method from Route class
    public function options()
    {
        // Here you can send to the client required headers for a CORS preflighted request:
        $header = $this->_response->getHeader();
        $header->setField("Access-Control-Allow-Methods", "GET, POST, DELETE");
        $header->setField("Access-Control-Allow-Headers", "Content-Type, X-Resource-Signature, X-Access-Timestamp");
        $header->setContentType("text/plain");
        $header->setContentLength(0);
        $header->setContentEncoding("gzip");
        $this->_response->setHeader($header);
        // Again, just send headers here, OPTIONS HTTP method don't want a body
        $this->_response->sendHeaders();
    }
}

$wp = new \ElementaryFramework\WaterPipe\WaterPipe();

$wp->use(new ResourceRoute());

// You can define other pipes and sub-pipes

$wp->run();

With this example, access to an URI like http://yourdomain.com/public-data/domain1.net/resource-423 will only be allowed for requests with origin http://domain1.net. The same rule apply for an URI like http://yourdomain.com/public-data/another-domain.io/resource-423. Even if these two domains share the same resource (resource-423), domain1.net can't access this through http://yourdomain.com/public-data/another-domain.io/resource-423.

Feel free to close this issue if this comment solves your problem.

Please consider that WaterPipe is a low-level library, so high-level things like CORS, Oauth and more must be implemented by the user.

@na2axl na2axl added help wanted Extra attention is needed question Further information is requested good first issue Good for newcomers labels Nov 21, 2019
@barbosaigor
Copy link
Author

Solved, thank you !!

@na2axl na2axl pinned this issue Nov 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers help wanted Extra attention is needed question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants