Skip to content

Commit a41a9dc

Browse files
committed
Merge pull request googleapis#337 from ianbarber/master
Update comment in config & add new auth params
2 parents f925fd3 + 1987ba4 commit a41a9dc

File tree

5 files changed

+130
-14
lines changed

5 files changed

+130
-14
lines changed

src/Google/Auth/OAuth2.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,14 @@ public function createAuthUrl($scope)
149149
'client_id' => $this->client->getClassConfig($this, 'client_id'),
150150
'scope' => $scope,
151151
'access_type' => $this->client->getClassConfig($this, 'access_type'),
152-
'approval_prompt' => $this->client->getClassConfig($this, 'approval_prompt'),
153152
);
154153

155-
$login_hint = $this->client->getClassConfig($this, 'login_hint');
156-
if ($login_hint != '') {
157-
$params['login_hint'] = $login_hint;
158-
}
154+
$params = $this->maybeAddParam($params, 'approval_prompt');
155+
$params = $this->maybeAddParam($params, 'login_hint');
156+
$params = $this->maybeAddParam($params, 'hd');
157+
$params = $this->maybeAddParam($params, 'openid.realm');
158+
$params = $this->maybeAddParam($params, 'prompt');
159+
$params = $this->maybeAddParam($params, 'include_granted_scopes');
159160

160161
// If the list of scopes contains plus.login, add request_visible_actions
161162
// to auth URL.
@@ -604,4 +605,16 @@ public function verifySignedJwtWithCerts(
604605
// All good.
605606
return new Google_Auth_LoginTicket($envelope, $payload);
606607
}
608+
609+
/**
610+
* Add a parameter to the auth params if not empty string.
611+
*/
612+
private function maybeAddParam($params, $name)
613+
{
614+
$param = $this->client->getClassConfig($this, $name);
615+
if ($param != '') {
616+
$params[$name] = $param;
617+
}
618+
return $params;
619+
}
607620
}

src/Google/Client.php

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ public function getAccessToken()
247247
// in the library.
248248
return (null == $token || 'null' == $token || '[]' == $token) ? null : $token;
249249
}
250-
250+
251251
/**
252252
* Get the OAuth 2.0 refresh token.
253253
* @return string $refreshToken refresh token or null if not available
@@ -367,6 +367,50 @@ public function setDeveloperKey($developerKey)
367367
$this->config->setDeveloperKey($developerKey);
368368
}
369369

370+
/**
371+
* Set the hd (hosted domain) parameter streamlines the login process for
372+
* Google Apps hosted accounts. By including the domain of the user, you
373+
* restrict sign-in to accounts at that domain.
374+
* @param $hd string - the domain to use.
375+
*/
376+
public function setHostedDomain($hd)
377+
{
378+
$this->config->setHostedDomain($hd);
379+
}
380+
381+
/**
382+
* Set the prompt hint. Valid values are none, consent and select_account.
383+
* If no value is specified and the user has not previously authorized
384+
* access, then the user is shown a consent screen.
385+
* @param $prompt string
386+
*/
387+
public function setPrompt($prompt)
388+
{
389+
$this->config->setPrompt($prompt);
390+
}
391+
392+
/**
393+
* openid.realm is a parameter from the OpenID 2.0 protocol, not from OAuth
394+
* 2.0. It is used in OpenID 2.0 requests to signify the URL-space for which
395+
* an authentication request is valid.
396+
* @param $realm string - the URL-space to use.
397+
*/
398+
public function setOpenidRealm($realm)
399+
{
400+
$this->config->setOpenidRealm($realm);
401+
}
402+
403+
/**
404+
* If this is provided with the value true, and the authorization request is
405+
* granted, the authorization will include any previous authorizations
406+
* granted to this user/application combination for other scopes.
407+
* @param $include boolean - the URL-space to use.
408+
*/
409+
public function setIncludeGrantedScopes($include)
410+
{
411+
$this->config->setIncludeGrantedScopes($include);
412+
}
413+
370414
/**
371415
* Fetches a fresh OAuth 2.0 access token with the given refresh token.
372416
* @param string $refreshToken

src/Google/Config.php

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Google_Config
3030
/**
3131
* Create a new Google_Config. Can accept an ini file location with the
3232
* local configuration. For example:
33-
* application_name: "My App";
33+
* application_name="My App"
3434
*
3535
* @param [$ini_file_location] - optional - The location of the ini file to load
3636
*/
@@ -78,10 +78,14 @@ public function __construct($ini_file_location = null)
7878
'developer_key' => '',
7979

8080
// Other parameters.
81-
'access_type' => 'online',
82-
'approval_prompt' => 'auto',
81+
'hd' => '',
82+
'prompt' => '',
83+
'openid.realm' => '',
84+
'include_granted_scopes' => '',
8385
'login_hint' => '',
8486
'request_visible_actions' => '',
87+
'access_type' => 'online',
88+
'approval_prompt' => 'auto',
8589
'federated_signon_certs_url' =>
8690
'https://www.googleapis.com/oauth2/v1/certs',
8791
),
@@ -297,6 +301,53 @@ public function setDeveloperKey($key)
297301
$this->setAuthConfig('developer_key', $key);
298302
}
299303

304+
/**
305+
* Set the hd (hosted domain) parameter streamlines the login process for
306+
* Google Apps hosted accounts. By including the domain of the user, you
307+
* restrict sign-in to accounts at that domain.
308+
* @param $hd string - the domain to use.
309+
*/
310+
public function setHostedDomain($hd)
311+
{
312+
$this->setAuthConfig('hd', $hd);
313+
}
314+
315+
/**
316+
* Set the prompt hint. Valid values are none, consent and select_account.
317+
* If no value is specified and the user has not previously authorized
318+
* access, then the user is shown a consent screen.
319+
* @param $prompt string
320+
*/
321+
public function setPrompt($prompt)
322+
{
323+
$this->setAuthConfig('prompt', $prompt);
324+
}
325+
326+
/**
327+
* openid.realm is a parameter from the OpenID 2.0 protocol, not from OAuth
328+
* 2.0. It is used in OpenID 2.0 requests to signify the URL-space for which
329+
* an authentication request is valid.
330+
* @param $realm string - the URL-space to use.
331+
*/
332+
public function setOpenidRealm($realm)
333+
{
334+
$this->setAuthConfig('openid.realm', $realm);
335+
}
336+
337+
/**
338+
* If this is provided with the value true, and the authorization request is
339+
* granted, the authorization will include any previous authorizations
340+
* granted to this user/application combination for other scopes.
341+
* @param $include boolean - the URL-space to use.
342+
*/
343+
public function setIncludeGrantedScopes($include)
344+
{
345+
$this->setAuthConfig(
346+
'include_granted_scopes',
347+
$include ? "true" : "false"
348+
);
349+
}
350+
300351
/**
301352
* @return string the base URL to use for API calls
302353
*/

src/Google/Http/MediaFileUpload.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,13 +288,13 @@ private function getResumeUri()
288288
return $location;
289289
}
290290
$message = $code;
291-
$body = @json_decode( $response->getResponseBody() );
292-
if ( ! empty( $body->error->errors ) ) {
291+
$body = @json_decode($response->getResponseBody());
292+
if (!empty( $body->error->errors ) ) {
293293
$message .= ': ';
294-
foreach( $body->error->errors as $error ) {
294+
foreach ($body->error->errors as $error) {
295295
$message .= "{$error->domain}, {$error->message};";
296296
}
297-
$message = rtrim( $message, ';' );
297+
$message = rtrim($message, ';');
298298
}
299299
throw new Google_Exception("Failed to start the resumable upload (HTTP {$message})");
300300
}

tests/general/ApiOAuth2Test.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,22 @@ public function testCreateAuthUrl()
130130

131131
// Again with a blank login hint (should remove all traces from authUrl)
132132
$client->setLoginHint("");
133+
$client->setHostedDomain("example.com");
134+
$client->setOpenidRealm("example.com");
135+
$client->setPrompt("select_account");
136+
$client->setIncludeGrantedScopes(true);
133137
$authUrl = $oauth->createAuthUrl("http://googleapis.com/scope/foo");
134138
$expected = "https://accounts.google.com/o/oauth2/auth"
135139
. "?response_type=code"
136140
. "&redirect_uri=http%3A%2F%2Flocalhost"
137141
. "&client_id=clientId1"
138142
. "&scope=http%3A%2F%2Fgoogleapis.com%2Fscope%2Ffoo"
139143
. "&access_type=offline"
140-
. "&approval_prompt=force";
144+
. "&approval_prompt=force"
145+
. "&hd=example.com"
146+
. "&openid.realm=example.com"
147+
. "&prompt=select_account"
148+
. "&include_granted_scopes=true";
141149
$this->assertEquals($expected, $authUrl);
142150
}
143151

0 commit comments

Comments
 (0)