Skip to content

Commit 1071e93

Browse files
author
Tama Pugsley
committed
Adding 'login_hint' parameter to OAuth2 connections
1 parent cfb1eea commit 1071e93

File tree

4 files changed

+44
-6
lines changed

4 files changed

+44
-6
lines changed

src/Google/Auth/OAuth2.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ public function createAuthUrl($scope)
149149
'approval_prompt' => $this->client->getClassConfig($this, 'approval_prompt'),
150150
);
151151

152+
$login_hint = $this->client->getClassConfig($this, 'login_hint');
153+
if ($login_hint != '') {
154+
$params['login_hint'] = $login_hint;
155+
}
156+
152157
// If the list of scopes contains plus.login, add request_visible_actions
153158
// to auth URL.
154159
$rva = $this->client->getClassConfig($this, 'request_visible_actions');

src/Google/Client.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ function_exists('date_default_timezone_set')) {
100100
$config->setClassConfig('Google_Http_Request', 'disable_gzip', true);
101101
}
102102
}
103-
103+
104104
if ($config->getIoClass() == Google_Config::USE_AUTO_IO_SELECTION) {
105105
if (function_exists('curl_version') && function_exists('curl_exec')) {
106106
$config->setIoClass("Google_IO_Curl");
@@ -292,6 +292,15 @@ public function setApprovalPrompt($approvalPrompt)
292292
$this->config->setApprovalPrompt($approvalPrompt);
293293
}
294294

295+
/**
296+
* Set the login hint, email address or sub id.
297+
* @param string $loginHint
298+
*/
299+
public function setLoginHint($loginHint)
300+
{
301+
$this->config->setLoginHint($loginHint);
302+
}
303+
295304
/**
296305
* Set the application name, this is included in the User-Agent HTTP header.
297306
* @param string $applicationName

src/Google/Config.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ public function __construct($ini_file_location = null)
8080
// Other parameters.
8181
'access_type' => 'online',
8282
'approval_prompt' => 'auto',
83+
'login_hint' => '',
8384
'request_visible_actions' => '',
8485
'federated_signon_certs_url' =>
8586
'https://www.googleapis.com/oauth2/v1/certs',
@@ -282,6 +283,15 @@ public function setApprovalPrompt($approval)
282283
$this->setAuthConfig('approval_prompt', $approval);
283284
}
284285

286+
/**
287+
* Set the login hint (email address or sub identifier)
288+
* @param $hint string
289+
*/
290+
public function setLoginHint($hint)
291+
{
292+
$this->setAuthConfig('login_hint', $hint);
293+
}
294+
285295
/**
286296
* Set the developer key for the auth class. Note that this is separate value
287297
* from the client ID - if it looks like a URL, its a client ID!

tests/general/ApiOAuth2Test.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
class ApiOAuth2Test extends BaseTest {
2626

27-
public function testSign()
27+
public function testSign()
2828
{
2929
$client = $this->getClient();
3030
$oauth = new Google_Auth_OAuth2($client);
@@ -54,7 +54,7 @@ public function testSign()
5454
$this->assertEquals('Bearer ACCESS_TOKEN', $auth);
5555
}
5656

57-
public function testRevokeAccess()
57+
public function testRevokeAccess()
5858
{
5959
$accessToken = "ACCESS_TOKEN";
6060
$refreshToken = "REFRESH_TOKEN";
@@ -103,7 +103,7 @@ public function testRevokeAccess()
103103
$this->assertEquals($accessToken2, $token);
104104
}
105105

106-
public function testCreateAuthUrl()
106+
public function testCreateAuthUrl()
107107
{
108108
$client = $this->getClient();
109109
$oauth = new Google_Auth_OAuth2($client);
@@ -115,7 +115,21 @@ public function testCreateAuthUrl()
115115
$client->setAccessType('offline');
116116
$client->setApprovalPrompt('force');
117117
$client->setRequestVisibleActions(array('http://foo'));
118+
$client->setLoginHint("[email protected]");
118119

120+
$authUrl = $oauth->createAuthUrl("http://googleapis.com/scope/foo");
121+
$expected = "https://accounts.google.com/o/oauth2/auth"
122+
. "?response_type=code"
123+
. "&redirect_uri=http%3A%2F%2Flocalhost"
124+
. "&client_id=clientId1"
125+
. "&scope=http%3A%2F%2Fgoogleapis.com%2Fscope%2Ffoo"
126+
. "&access_type=offline"
127+
. "&approval_prompt=force"
128+
. "&login_hint=bob%40example.org";
129+
$this->assertEquals($expected, $authUrl);
130+
131+
// Again with a blank login hint (should remove all traces from authUrl)
132+
$client->setLoginHint("");
119133
$authUrl = $oauth->createAuthUrl("http://googleapis.com/scope/foo");
120134
$expected = "https://accounts.google.com/o/oauth2/auth"
121135
. "?response_type=code"
@@ -128,11 +142,11 @@ public function testCreateAuthUrl()
128142
}
129143

130144
/**
131-
* Most of the logic for ID token validation is in AuthTest -
145+
* Most of the logic for ID token validation is in AuthTest -
132146
* this is just a general check to ensure we verify a valid
133147
* id token if one exists.
134148
*/
135-
public function testValidateIdToken()
149+
public function testValidateIdToken()
136150
{
137151
if (!$this->checkToken()) {
138152
return;

0 commit comments

Comments
 (0)