Skip to content

Commit 74a7d7b

Browse files
authored
feat: user-supplied query params for auth url (#2432)
1 parent 53c3168 commit 74a7d7b

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,28 @@ $client->setHttpClient($httpClient);
422422

423423
Other Guzzle features such as [Handlers and Middleware](http://docs.guzzlephp.org/en/stable/handlers-and-middleware.html) offer even more control.
424424

425+
### Partial Consent and Granted Scopes
426+
427+
When using OAuth2 3LO (e.g. you're a client requesting credentials from a 3rd
428+
party, such as in the [simple file upload example](examples/simple-file-upload.php)),
429+
you may want to take advantage of Partial Consent.
430+
431+
To allow clients to only grant certain scopes in the OAuth2 screen, pass the
432+
querystring parameter for `enable_serial_consent` when generating the
433+
authorization URL:
434+
435+
```php
436+
$authUrl = $client->createAuthUrl($scope, ['enable_serial_consent' => 'true']);
437+
```
438+
439+
Once the flow is completed, you can see which scopes were granted by calling
440+
`getGrantedScope` on the OAuth2 object:
441+
442+
```php
443+
// Space-separated string of granted scopes if it exists, otherwise null.
444+
echo $client->getOAuth2Service()->getGrantedScope();
445+
```
446+
425447
### Service Specific Examples ###
426448

427449
YouTube: https://github.com/youtube/api-samples/tree/master/php

src/Client.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,9 +357,10 @@ public function fetchAccessTokenWithRefreshToken($refreshToken = null)
357357
* The authorization endpoint allows the user to first
358358
* authenticate, and then grant/deny the access request.
359359
* @param string|array $scope The scope is expressed as an array or list of space-delimited strings.
360+
* @param array $queryParams Querystring params to add to the authorization URL.
360361
* @return string
361362
*/
362-
public function createAuthUrl($scope = null)
363+
public function createAuthUrl($scope = null, array $queryParams = [])
363364
{
364365
if (empty($scope)) {
365366
$scope = $this->prepareScopes();
@@ -390,7 +391,7 @@ public function createAuthUrl($scope = null)
390391
'response_type' => 'code',
391392
'scope' => $scope,
392393
'state' => $this->config['state'],
393-
]);
394+
]) + $queryParams;
394395

395396
// If the list of scopes contains plus.login, add request_visible_actions
396397
// to auth URL.

tests/Google/ClientTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,4 +1025,14 @@ public function testSetNewRedirectUri()
10251025
$authUrl2 = $client->createAuthUrl();
10261026
$this->assertStringContainsString(urlencode($redirectUri2), $authUrl2);
10271027
}
1028+
1029+
public function testQueryParamsForAuthUrl()
1030+
{
1031+
$client = new Client();
1032+
$client->setRedirectUri('https://example.com');
1033+
$authUrl1 = $client->createAuthUrl(null, [
1034+
'enable_serial_consent' => 'true'
1035+
]);
1036+
$this->assertStringContainsString('&enable_serial_consent=true', $authUrl1);
1037+
}
10281038
}

0 commit comments

Comments
 (0)