Skip to content

fix: added 'enable_granular_consent' parameter #2582

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [2.15.4](https://github.com/googleapis/google-api-php-client/compare/v2.15.3...v2.15.4) (2024-03-22)

### Features

* Add parameter `enable_granular_consent`

Comment on lines +3 to +8
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changelog gets updated automatically through our release-please bot. We can revert this change.

Suggested change
## [2.15.4](https://github.com/googleapis/google-api-php-client/compare/v2.15.3...v2.15.4) (2024-03-22)
### Features
* Add parameter `enable_granular_consent`

## [2.15.3](https://github.com/googleapis/google-api-php-client/compare/v2.15.2...v2.15.3) (2024-01-04)


Expand Down
20 changes: 20 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ class Client
* @type string $prompt
* @type string $openid
* @type bool $include_granted_scopes
* @type bool $enable_granular_consent
* @type string $login_hint
* @type string $request_visible_actions
* @type string $access_type
Expand Down Expand Up @@ -187,6 +188,7 @@ public function __construct(array $config = [])
'prompt' => '',
'openid.realm' => '',
'include_granted_scopes' => null,
'enable_granular_consent' => null,
'login_hint' => '',
'request_visible_actions' => '',
'access_type' => 'online',
Expand Down Expand Up @@ -404,11 +406,17 @@ public function createAuthUrl($scope = null, array $queryParams = [])
? null
: var_export($this->config['include_granted_scopes'], true);


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

$enableGranularConsent = $this->config['enable_granular_consent'] === null
? null
: var_export($this->config['enable_granular_consent'], true);

$params = array_filter([
'access_type' => $this->config['access_type'],
'approval_prompt' => $approvalPrompt,
'hd' => $this->config['hd'],
'include_granted_scopes' => $includeGrantedScopes,
'enable_granular_consent' => $enableGranularConsent,
'login_hint' => $this->config['login_hint'],
'openid.realm' => $this->config['openid.realm'],
'prompt' => $this->config['prompt'],
Expand Down Expand Up @@ -785,6 +793,18 @@ public function setIncludeGrantedScopes($include)
$this->config['include_granted_scopes'] = $include;
}

/**
* If set to false, more granular Google Account permissions
* will be disabled for OAuth client IDs created before 2019.
* No effect for newer OAuth client IDs, since more granular
* permissions is always enabled for them
* @param bool $consent
*/
public function setEnableGranularConsent($consent)
{
$this->config['enable_granular_consent'] = $consent;
}

/**
* sets function to be called when an access token is fetched
* @param callable $tokenCallback - function ($cacheKey, $accessToken)
Expand Down
2 changes: 2 additions & 0 deletions tests/Google/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ public function testCreateAuthUrl()
$client->setOpenIdRealm('example.com');
$client->setPrompt('select_account');
$client->setIncludeGrantedScopes(true);
$client->setEnableGranularConsent(false);
$authUrl = $client->createAuthUrl("http://googleapis.com/scope/foo");
$expected = "https://accounts.google.com/o/oauth2/v2/auth"
. "?response_type=code"
Expand All @@ -166,6 +167,7 @@ public function testCreateAuthUrl()
. "&scope=http%3A%2F%2Fgoogleapis.com%2Fscope%2Ffoo"
. "&hd=example.com"
. "&include_granted_scopes=true"
. "&enable_granular_consent=false"
. "&openid.realm=example.com"
. "&prompt=select_account";

Expand Down