Skip to content

Commit b0e57e1

Browse files
committed
Merge pull request #75 from Spomky/ExtraHeaders
Extra headers support
2 parents ce444e6 + 52d3001 commit b0e57e1

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

lib/OAuth2.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ class OAuth2
130130
const CONFIG_WWW_REALM = 'realm';
131131
const CONFIG_ENFORCE_INPUT_REDIRECT = 'enforce_redirect'; // Set to true to enforce redirect_uri on input for both authorize and token steps.
132132
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)
133+
const CONFIG_RESPONSE_EXTRA_HEADERS = 'response_extra_headers'; // Add extra headers to the response
133134

134135
/**
135136
* Regex to filter out the client identifier (described in Section 2 of IETF draft).
@@ -422,6 +423,7 @@ protected function setDefaultOptions()
422423
self::CONFIG_ENFORCE_STATE => false,
423424
self::CONFIG_SUPPORTED_SCOPES => null,
424425
// This is expected to be passed in on construction. Scopes can be an aribitrary string.
426+
self::CONFIG_RESPONSE_EXTRA_HEADERS => array(),
425427
);
426428
}
427429

@@ -1475,11 +1477,13 @@ protected function getAuthorizationHeader(Request $request)
14751477
*/
14761478
private function getJsonHeaders()
14771479
{
1478-
return array(
1480+
$headers = $this->getVariable(self::CONFIG_RESPONSE_EXTRA_HEADERS, array());
1481+
$headers += array(
14791482
'Content-Type' => 'application/json',
14801483
'Cache-Control' => 'no-store',
14811484
'Pragma' => 'no-cache',
14821485
);
1486+
return $headers;
14831487
}
14841488

14851489
/**

tests/ExtraHeadersTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
use OAuth2\OAuth2;
4+
use OAuth2\Model\OAuth2Client;
5+
use Symfony\Component\HttpFoundation\Request;
6+
use OAuth2\Tests\Fixtures\OAuth2GrantUserStub;
7+
8+
/**
9+
* Extra Headers test case.
10+
*/
11+
class ExtraHeadersTest extends PHPUnit_Framework_TestCase
12+
{
13+
public function testErrorResponseContainsExtraHeaders()
14+
{
15+
$config = array(
16+
OAuth2::CONFIG_RESPONSE_EXTRA_HEADERS => array(
17+
"Access-Control-Allow-Origin" => "http://www.foo.com",
18+
"X-Extra-Header-1" => "Foo-Bar",
19+
),
20+
);
21+
$stub = new OAuth2GrantUserStub();
22+
$stub->addClient(new OAuth2Client('cid', 'cpass'));
23+
$stub->addUser('foo', 'bar');
24+
$stub->setAllowedGrantTypes(array('authorization_code', 'password'));
25+
26+
$oauth2 = new OAuth2($stub, $config);
27+
28+
$response = $oauth2->grantAccessToken(new Request(array(
29+
'grant_type' => 'password',
30+
'client_id' => 'cid',
31+
'client_secret' => 'cpass',
32+
'username' => 'foo',
33+
'password' => 'bar',
34+
)));
35+
$this->assertSame("http://www.foo.com", $response->headers->get("Access-Control-Allow-Origin"));
36+
$this->assertSame("Foo-Bar", $response->headers->get("X-Extra-Header-1"));
37+
}
38+
}

0 commit comments

Comments
 (0)