Skip to content

Commit

Permalink
Merge pull request #4 from bairwell/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
rbairwell committed Jan 5, 2016
2 parents fbd02d5 + 80b0b71 commit c0cb9d4
Show file tree
Hide file tree
Showing 23 changed files with 343 additions and 281 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
v0.2.0 - 5th Jan 2016
Renamed from Bairwell/Cors to Bairwell/MiddlewareCors (packagist name Bairwell\Middleware-Cors)
Remove Slim dependency from dev (moved to examples) (fixes https://github.com/bairwell/middleware-cors/issues/2 )
Added code of conduct and installation instructions (fixed https://github.com/bairwell/middleware-cors/issues/3 )
v0.1.1 - 31st Dec 2015
Made Preflight and ValidateSettings (formerly Validate) their own classes instead of being traits.
Migrated from Bitbucket to Github.
Expand Down
22 changes: 22 additions & 0 deletions CONDUCT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Contributor Code of Conduct

As contributors and maintainers of this project, and in the interest of fostering an open and welcoming community, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.

We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, religion, or nationality.

Examples of unacceptable behavior by participants include:

* The use of sexualized language or imagery
* Personal attacks
* Trolling or insulting/derogatory comments
* Public or private harassment
* Publishing other's private information, such as physical or electronic addresses, without explicit permission
* Other unethical or unprofessional conduct.

Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. By adopting this Code of Conduct, project maintainers commit themselves to fairly and consistently applying these principles to every aspect of managing this project. Project maintainers who do not follow or enforce the Code of Conduct may be permanently removed from the project team.

This code of conduct applies both within project spaces and in public spaces when an individual is representing the project or its community.

Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.

This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.2.0, available at [http://contributor-covenant.org/version/1/2/0/](http://contributor-covenant.org/version/1/2/0/)
6 changes: 3 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ All pull requests must adhere to the [PSR-2 standard](http://www.php-fig.org/psr

This can be checked via, you can run the following commands to check if everything is ready to submit:

cd cors
cd cors-middleware
vendor/bin/phpcs -np

Which should give you no output, indicating that there are no coding standard errors. And then:


## Unit Testing

All pull requests must be accompanied by passing unit tests and complete code coverage. The Bairwell\Cors library uses phpunit for testing.
All pull requests must be accompanied by passing unit tests and complete code coverage. The Bairwell\MiddlewareCors library uses phpunit for testing.

[Learn about PHPUnit](https://github.com/sebastianbergmann/phpunit/)

cd cors
cd cors-middleware
vendor/bin/phpunit

Which should give you no failures or errors. You can ignore any skipped tests as these are for external tools.
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2016 Bairwell Ltd.
Copyright (c) 2016 Bairwell Ltd/Richard Bairwell.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
63 changes: 50 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Bairwell\Cors
# Bairwell\Middleware-Cors

[![Latest Stable Version](https://poser.pugx.org/bairwell/cors/v/stable)](https://packagist.org/packages/bairwell/cors)
[![License](https://poser.pugx.org/bairwell/cors/license)](https://packagist.org/packages/bairwell/cors)
[![Latest Stable Version](https://poser.pugx.org/bairwell/middleware-cors/v/stable)](https://packagist.org/packages/bairwell/middleware-cors)
[![License](https://poser.pugx.org/bairwell/middleware-cors/license)](https://packagist.org/packages/bairwell/middleware-cors)
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/8aea9987-e478-4faa-a3fe-728e9423b4e7/mini.png)](https://insight.sensiolabs.com/projects/8aea9987-e478-4faa-a3fe-728e9423b4e7)
[![Coverage Status](https://coveralls.io/repos/bairwell/cors/badge.svg?branch=master&service=github)](https://coveralls.io/github/bairwell/cors?branch=master)
[![Build Status](https://travis-ci.org/bairwell/cors.svg?branch=master)](https://travis-ci.org/bairwell/cors)
[![Total Downloads](https://poser.pugx.org/bairwell/cors/downloads)](https://packagist.org/packages/bairwell/cors)
[![Coverage Status](https://coveralls.io/repos/bairwell/middleware-cors/badge.svg?branch=master&service=github)](https://coveralls.io/github/bairwell/middleware-cors?branch=master)
[![Build Status](https://travis-ci.org/bairwell/middleware-cors.svg?branch=master)](https://travis-ci.org/bairwell/middleware-cors)
[![Total Downloads](https://poser.pugx.org/bairwell/middleware-cors/downloads)](https://packagist.org/packages/bairwell/middleware-cors)

This is a PHP 7 [Composer](https://getcomposer.org/) compatible library for providing a [PSR-7]((http://www.php-fig.org/psr/psr-7/) compatible middleware layer for handling
"[CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS)" (Cross Origin Request Security/Cross-Origin Http Request/HTTP access control) headers and security.
Expand All @@ -24,15 +24,36 @@ This is a PHP 7 [Composer](https://getcomposer.org/) compatible library for prov
* Licensed under the [MIT License](https://opensource.org/licenses/MIT) allowing you to practically do whatever you want.
* Uses namespaces and is 100% object orientated.
* Blocks invalid settings.
* Minimal third party requirements (just the definition files "psr/http-message" and "psr/log" for main, and PHPUnit, PHPCodeSniffer, SlimFramework and Monolog for development/testing).
* Minimal third party requirements (just the definition files "[psr/http-message](https://github.com/php-fig/http-message)" and "[psr/log](https://github.com/php-fig/log)" as interface definitions, and [PHPUnit](https://phpunit.de/), [PHPCodeSniffer](http://www.squizlabs.com/php-codesniffer), and [Monolog](https://github.com/Seldaek/monolog) for development/testing).

# Installation
Install the latest version with Composer via:

```bash
$ composer require bairwell/middleware-cors
```

or by modifying your `composer.json` file:
````
{
"require": {
"bairwell/middleware-cors" : "@stable"
}
}
````

or from the Github repository (which is needed to be able to fork and contribute):
````
$ git clone git://github.com:bairwell/middleware-cors.git
````

# Usage

You can utilise this CORs library as simply as:

```php
$slim=new \Slim\App(); // use Slim3 as it supports PSR7 middleware
$slim->add(new Cors()); // add CORs
$slim->add(new MiddlewareCors()); // add CORs
// add routes
$slim->run(); // get Slim running
```
Expand All @@ -46,7 +67,7 @@ $slim=new \Slim\App(); // use Slim3 as it supports PSR7 middleware
$config=[
'origin'=>'*.example.com' // allow all hosts ending example.com
];
$slim->add(new Cors($config)); // add CORs
$slim->add(new MiddlewareCors($config)); // add CORs
// add routes
$slim->run(); // get Slim running
```
Expand All @@ -59,7 +80,7 @@ $config=[
'origin'=>['*.example.com','*.example.com.test','example.com','dev.*',
'allowCredentials'=>true
];
$slim->add(new Cors($config)); // add CORs
$slim->add(new MiddlewareCors($config)); // add CORs
// add routes
$slim->run(); // get Slim running
```
Expand All @@ -68,7 +89,7 @@ which will allow all Origins ending .example.com or *.example.com.test, the exac
any host starting with dev. It'll also allow credentials to be allowed.

For a more complicated integration which relies on the Slim router to feed back which methods are actually
allowed per route, see ``tests/Cors/FunctionalTests/SlimTest.php``
allowed per route, see ``tests/MiddlewareCors/FunctionalTests/SlimTest.php``

## Standards

Expand Down Expand Up @@ -101,7 +122,7 @@ For a list of the tests that have ran:
`vendor/bin/phpunit --tap`

To restrict the tests run:
`vendor/bin/phpunit --filter 'Cors\\Exceptions\\BadOrigin'`
`vendor/bin/phpunit --filter 'MiddlewareCors\\Exceptions\\BadOrigin'`

or just

Expand All @@ -110,5 +131,21 @@ or just
for all tests which have "Exception" in them and:
`vendor/bin/phpunit --filter '(ExceptionTest::testEverything|ExceptionTest::testStub)'`

to test the two testEverything and testStub methods in the ExceptionTest class.
to test the two testEverything and testStub methods in the ExceptionTest class (for example).

# Licence/License

Licenced under the MIT license. See LICENSE.md for full information.

Bairwell/MiddlewareCors is Copyright (c) Bairwell Ltd/Richard Bairwell 2016.

# Supporting development

You can help support development of this library via a variety of methods:

* "Sponsorship" via a monthly donation via [Patreon](https://www.patreon.com/rbairwell)
* [Reporting issues](https://github.com/bairwell/middleware-cors/issues)
* Making updates via [Github](https://github.com/bairwell/middleware-cors)
* Spreading the word.
* Just letting me know what you think of it via [Twitter](http://twitter.com/rbairwell) or via [Bairwell Ltd](http://www.bairwell.com)

7 changes: 3 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "bairwell/cors",
"name": "bairwell/middleware-cors",
"description": "A PSR-7 middleware layer for providing CORS (Cross Origin Request Security) headers and security provisions. Instead of just allowing invalid CORs requests to come through, this middleware actively blocks them after validating.",
"keywords": ["psr-7","middleware","cors"],
"homepage": "https://bitbucket.org/bairwell/cors",
"keywords": ["psr-7","middleware","cors","cross origin request security"],
"homepage": "https://github.com/bairwell/middleware-cors",
"license": "MIT",
"authors": [
{
Expand All @@ -20,7 +20,6 @@
"require-dev": {
"phpunit/phpunit": "^5.1",
"squizlabs/php_codesniffer": "^2.5",
"slim/slim": "^3.0",
"monolog/monolog": "^1.13"
},
"autoload": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

namespace Bairwell\Cors\FunctionalTests;
namespace Bairwell\MiddlewareCors\FunctionalTests;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use FastRoute\Dispatcher;
use Bairwell\Cors;
use Bairwell\MiddlewareCors;
use Interop\Container\ContainerInterface;
use Slim\Http\Headers;
use Slim\Http\Request;
Expand Down Expand Up @@ -57,9 +57,9 @@ public function getLoggerStrings() {
*
* @param ContainerInterface $container The Slim Container.
*
* @return Cors
* @return MiddlewareCors
*/
protected function getCors(ContainerInterface $container) : Cors {
protected function getCors(ContainerInterface $container) : MiddlewareCors {
// set our allowed methods callback to integrate with Slim
$corsAllowedMethods = function (ServerRequestInterface $request) use ($container) : array {
// if this closure is called, make sure it has the route available in the container.
Expand Down Expand Up @@ -95,7 +95,7 @@ protected function getCors(ContainerInterface $container) : Cors {
return $methods;
};
// setup CORs
$cors = new Cors(
$cors = new MiddlewareCors(
[
'origin' => $this->allowedHosts,
'exposeHeaders' => '',
Expand Down Expand Up @@ -253,7 +253,7 @@ public function testOptionsWithOrigin() {
$body=$result->getBody();
$body->rewind();
$contents=$body->getContents();
$this->assertEquals('Error Handler caught exception type Bairwell\Cors\Exceptions\NoMethod: No method provided',$contents);
$this->assertEquals('Error Handler caught exception type Bairwell\MiddlewareCors\Exceptions\NoMethod: No method provided',$contents);
$this->assertEquals(500,$result->getStatusCode());
$this->assertEquals('Internal Server Error',$result->getReasonPhrase());
$headers=$result->getHeaders();
Expand Down
16 changes: 8 additions & 8 deletions src/Cors.php → src/MiddlewareCors.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Class Cors.
* Class MiddlewareCors.
*
* A piece of PSR7 compliant middleware (in that it takes a PSR7 request and response
* fields, a "next" field and handles them appropriate) which adds appropriate CORS
Expand All @@ -18,15 +18,15 @@

namespace Bairwell;

use Bairwell\Cors\ValidateSettings;
use Bairwell\Cors\Preflight;
use Bairwell\MiddlewareCors\ValidateSettings;
use Bairwell\MiddlewareCors\Preflight;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Bairwell\Cors\Exceptions\BadOrigin;
use Bairwell\MiddlewareCors\Exceptions\BadOrigin;
use Psr\Log\LoggerInterface;

/**
* Class Cors.
* Class MiddlewareCors.
*
* A piece of PSR7 compliant middleware (in that it takes a PSR7 request and response
* fields, a "next" field and handles them appropriate) which adds appropriate CORS
Expand Down Expand Up @@ -58,10 +58,10 @@
* - Add Access-Control-Max-Age (optional)
* - Add Vary:Origin if Origin is not *
*/
class Cors
class MiddlewareCors
{

use Cors\Traits\Parse;
use \Bairwell\MiddlewareCors\Traits\Parse;

/**
* The settings configuration.
Expand Down Expand Up @@ -106,7 +106,7 @@ class Cors
protected $logger = null;

/**
* Cors constructor.
* MiddlewareCors constructor.
*
* @param array $settings Our list of CORs related settings.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
declare (strict_types = 1);

namespace Bairwell\Cors\Exceptions;
namespace Bairwell\MiddlewareCors\Exceptions;

/**
* CORS Exception for handling bad/invalid origins which are sent.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
declare (strict_types = 1);

namespace Bairwell\Cors\Exceptions;
namespace Bairwell\MiddlewareCors\Exceptions;

/**
* Class CorsException.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/
declare (strict_types = 1);

namespace Bairwell\Cors\Exceptions;
namespace Bairwell\MiddlewareCors\Exceptions;

/**
* CORS Exception for handling inbound requests which specify that a particular
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
declare (strict_types = 1);

namespace Bairwell\Cors\Exceptions;
namespace Bairwell\MiddlewareCors\Exceptions;

/**
* CORS Exception for handling bad/invalid methods which are sent.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/
declare (strict_types = 1);

namespace Bairwell\Cors\Exceptions;
namespace Bairwell\MiddlewareCors\Exceptions;

/**
* CORS Exception for handling inbound requests which specify headers will be sent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
declare (strict_types = 1);

namespace Bairwell\Cors\Exceptions;
namespace Bairwell\MiddlewareCors\Exceptions;

/**
* CORS Exception for handling inbound requests which do not have a method specified.
Expand Down
12 changes: 6 additions & 6 deletions src/Cors/Preflight.php → src/MiddlewareCors/Preflight.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*
* All the CORs orientated preflight code.
*
* Part of the Bairwell\Cors package.
* Part of the Bairwell\MiddlewareCors package.
*
* (c) Richard Bairwell <[email protected]>
*
Expand All @@ -13,14 +13,14 @@
*/
declare (strict_types = 1);

namespace Bairwell\Cors;
namespace Bairwell\MiddlewareCors;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Bairwell\Cors\Exceptions\NoMethod;
use Bairwell\Cors\Exceptions\MethodNotAllowed;
use Bairwell\Cors\Exceptions\NoHeadersAllowed;
use Bairwell\Cors\Exceptions\HeaderNotAllowed;
use Bairwell\MiddlewareCors\Exceptions\NoMethod;
use Bairwell\MiddlewareCors\Exceptions\MethodNotAllowed;
use Bairwell\MiddlewareCors\Exceptions\NoHeadersAllowed;
use Bairwell\MiddlewareCors\Exceptions\HeaderNotAllowed;

/**
* Preflight.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*
* All the CORs orientated parsing code.
*
* Part of the Bairwell\Cors package.
* Part of the Bairwell\MiddlewareCors package.
*
* (c) Richard Bairwell <[email protected]>
*
Expand All @@ -13,7 +13,7 @@
*/
declare (strict_types = 1);

namespace Bairwell\Cors\Traits;
namespace Bairwell\MiddlewareCors\Traits;

use Psr\Http\Message\ServerRequestInterface;

Expand Down
Loading

0 comments on commit c0cb9d4

Please sign in to comment.