Skip to content

Commit

Permalink
Extra headers support
Browse files Browse the repository at this point in the history
Fix #72
  • Loading branch information
Florent Morselli committed Dec 29, 2014
1 parent 23e7653 commit 52d3001
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/OAuth2.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ class OAuth2
const CONFIG_WWW_REALM = 'realm';
const CONFIG_ENFORCE_INPUT_REDIRECT = 'enforce_redirect'; // Set to true to enforce redirect_uri on input for both authorize and token steps.
const CONFIG_ENFORCE_STATE = 'enforce_state'; // Set to true to enforce state to be passed in authorization (see http://tools.ietf.org/html/draft-ietf-oauth-v2-21#section-10.12)
const CONFIG_RESPONSE_EXTRA_HEADERS = 'response_extra_headers'; // Add extra headers to the response

/**
* Regex to filter out the client identifier (described in Section 2 of IETF draft).
Expand Down Expand Up @@ -413,6 +414,7 @@ protected function setDefaultOptions()
self::CONFIG_ENFORCE_STATE => false,
self::CONFIG_SUPPORTED_SCOPES => null,
// This is expected to be passed in on construction. Scopes can be an aribitrary string.
self::CONFIG_RESPONSE_EXTRA_HEADERS => array(),
);
}

Expand Down Expand Up @@ -1466,11 +1468,13 @@ protected function getAuthorizationHeader(Request $request)
*/
private function getJsonHeaders()
{
return array(
$headers = $this->getVariable(self::CONFIG_RESPONSE_EXTRA_HEADERS, array());
$headers += array(
'Content-Type' => 'application/json',
'Cache-Control' => 'no-store',
'Pragma' => 'no-cache',
);
return $headers;
}

/**
Expand Down
38 changes: 38 additions & 0 deletions tests/ExtraHeadersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

use OAuth2\OAuth2;
use OAuth2\Model\OAuth2Client;
use Symfony\Component\HttpFoundation\Request;
use OAuth2\Tests\Fixtures\OAuth2GrantUserStub;

/**
* Extra Headers test case.
*/
class ExtraHeadersTest extends PHPUnit_Framework_TestCase
{
public function testErrorResponseContainsExtraHeaders()
{
$config = array(
OAuth2::CONFIG_RESPONSE_EXTRA_HEADERS => array(
"Access-Control-Allow-Origin" => "http://www.foo.com",
"X-Extra-Header-1" => "Foo-Bar",
),
);
$stub = new OAuth2GrantUserStub();
$stub->addClient(new OAuth2Client('cid', 'cpass'));
$stub->addUser('foo', 'bar');
$stub->setAllowedGrantTypes(array('authorization_code', 'password'));

$oauth2 = new OAuth2($stub, $config);

$response = $oauth2->grantAccessToken(new Request(array(
'grant_type' => 'password',
'client_id' => 'cid',
'client_secret' => 'cpass',
'username' => 'foo',
'password' => 'bar',
)));
$this->assertSame("http://www.foo.com", $response->headers->get("Access-Control-Allow-Origin"));
$this->assertSame("Foo-Bar", $response->headers->get("X-Extra-Header-1"));
}
}

0 comments on commit 52d3001

Please sign in to comment.