Skip to content

Commit 840fde5

Browse files
authored
Merge pull request #30 from fabiang/audience-support
Added support for `audience` parameter, thanks @fabiang!
2 parents 18fb67c + 068a3ee commit 840fde5

File tree

2 files changed

+89
-4
lines changed

2 files changed

+89
-4
lines changed

src/GrantType/ClientCredentials.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ protected function getPostBody()
8686
$data['scope'] = $this->config['scope'];
8787
}
8888

89+
if (!empty($this->config['audience'])) {
90+
$data['audience'] = $this->config['audience'];
91+
}
92+
8993
return \GuzzleHttp\Psr7\stream_for(http_build_query($data, '', '&'));
9094
}
9195

@@ -98,6 +102,10 @@ protected function getPostBody()
98102
$postBody->setField('scope', $this->config['scope']);
99103
}
100104

105+
if (!empty($this->config['audience'])) {
106+
$postBody->setField('audience', $this->config['audience']);
107+
}
108+
101109
return $postBody;
102110
}
103111
}

tests/GrantType/ClientCredentialsTest.php

Lines changed: 81 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,79 @@ public function testGetRawData()
4242
}
4343
}
4444

45+
public function testAudienceParameterIsPassedGuzzle6()
46+
{
47+
if (Helper::guzzleIs('<', 6)) {
48+
$this->markTestSkipped('This test is for Guzzle >= 6');
49+
return;
50+
}
51+
52+
$response_data = [];
53+
54+
$responder = new MockHandler([
55+
new Psr7Response(200, [], json_encode($response_data)),
56+
]);
57+
58+
$container = [];
59+
$history = Middleware::history($container);
60+
61+
$handler = HandlerStack::create($responder);
62+
$handler->push($history);
63+
64+
$client = new Client([
65+
'handler' => $handler,
66+
'base_uri' => 'http://localhost:10000/oauth_token',
67+
]);
68+
69+
$grant = new ClientCredentials($client, [
70+
'client_id' => 'foo',
71+
'client_secret' => 'bar',
72+
'audience' => 'http://localhost:20000',
73+
]);
74+
75+
$signer = new \kamermans\OAuth2\Signer\ClientCredentials\BasicAuth();
76+
77+
$data = $grant->getRawData($signer);
78+
79+
$this->assertNotEmpty($container);
80+
$request_body = $container[0]['request']->getBody();
81+
82+
parse_str($request_body, $form_data);
83+
84+
$this->assertSame('http://localhost:20000', $form_data['audience']);
85+
}
86+
87+
public function testAudienceParameterIsPassedGuzzleLegacy()
88+
{
89+
if (Helper::guzzleIs('>=', 6)) {
90+
$this->markTestSkipped('This test is for Guzzle < 6');
91+
return;
92+
}
93+
94+
$response_data = [];
95+
$response = new Response(200, [], Stream::factory(json_encode($response_data)));
96+
97+
$responder = new MockResponder([$response]);
98+
$history = new History();
99+
100+
$client = new Client();
101+
$client->getEmitter()->attach($responder);
102+
$client->getEmitter()->attach($history);
103+
104+
$grant = new ClientCredentials($client, [
105+
'client_id' => 'foo',
106+
'client_secret' => 'bar',
107+
'audience' => 'http://localhost:20000',
108+
]);
109+
110+
$signer = new \kamermans\OAuth2\Signer\ClientCredentials\BasicAuth();
111+
112+
$data = $grant->getRawData($signer);
113+
$request_body = $history->getLastRequest()->getBody();
114+
115+
$this->assertSame('http://localhost:20000', $request_body->getField('audience'));
116+
}
117+
45118
protected function doGetRawData6Plus()
46119
{
47120
$response_data = [
@@ -65,9 +138,10 @@ protected function doGetRawData6Plus()
65138
]);
66139

67140
$grant = new ClientCredentials($client, [
68-
'client_id' => 'foo',
141+
'client_id' => 'foo',
69142
'client_secret' => 'bar',
70-
'scope' => 'foo,bar',
143+
'scope' => 'foo,bar',
144+
'audience' => '', // empty: should not be added to request
71145
]);
72146

73147
$signer = new \kamermans\OAuth2\Signer\ClientCredentials\BasicAuth();
@@ -82,6 +156,7 @@ protected function doGetRawData6Plus()
82156
$this->assertEquals($response_data, $data);
83157
$this->assertEquals('foo,bar', $form_data['scope']);
84158
$this->assertEquals('client_credentials', $form_data['grant_type']);
159+
$this->assertArrayNotHasKey('audience', $form_data);
85160
}
86161

87162
protected function doGetRawDataLegacy()
@@ -100,9 +175,10 @@ protected function doGetRawDataLegacy()
100175
$client->getEmitter()->attach($history);
101176

102177
$grant = new ClientCredentials($client, [
103-
'client_id' => 'foo',
178+
'client_id' => 'foo',
104179
'client_secret' => 'bar',
105-
'scope' => 'foo,bar',
180+
'scope' => 'foo,bar',
181+
'audience' => '', // empty: should not be added to request
106182
]);
107183

108184
$signer = new \kamermans\OAuth2\Signer\ClientCredentials\BasicAuth();
@@ -113,5 +189,6 @@ protected function doGetRawDataLegacy()
113189
$this->assertEquals($response_data, $data);
114190
$this->assertEquals('foo,bar', $request_body->getField('scope'));
115191
$this->assertEquals('client_credentials', $request_body->getField('grant_type'));
192+
$this->assertNull($request_body->getField('audience'));
116193
}
117194
}

0 commit comments

Comments
 (0)