Skip to content

Commit bded223

Browse files
authored
feat: add pkce support and upgrade examples (googleapis#2438)
1 parent c765b37 commit bded223

7 files changed

+16
-7
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"license": "Apache-2.0",
88
"require": {
99
"php": "^7.4|^8.0",
10-
"google/auth": "^1.26",
10+
"google/auth": "^1.28",
1111
"google/apiclient-services": "~0.200",
1212
"firebase/php-jwt": "~6.0",
1313
"monolog/monolog": "^2.9||^3.0",

examples/idtoken.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
* bundle in the session, and redirect to ourself.
5858
************************************************/
5959
if (isset($_GET['code'])) {
60-
$token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
60+
$token = $client->fetchAccessTokenWithAuthCode($_GET['code'], $_SESSION['code_verifier']);
6161

6262
// store in the session also
6363
$_SESSION['id_token_token'] = $token;
@@ -77,6 +77,7 @@
7777
) {
7878
$client->setAccessToken($_SESSION['id_token_token']);
7979
} else {
80+
$_SESSION['code_verifier'] = $client->getOAuth2Service()->generateCodeVerifier();
8081
$authUrl = $client->createAuthUrl();
8182
}
8283

examples/large-file-download.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
* bundle in the session, and redirect to ourself.
4949
************************************************/
5050
if (isset($_GET['code'])) {
51-
$token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
51+
$token = $client->fetchAccessTokenWithAuthCode($_GET['code'], $_SESSION['code_verifier']);
5252
$client->setAccessToken($token);
5353

5454
// store in the session also
@@ -65,6 +65,7 @@
6565
unset($_SESSION['upload_token']);
6666
}
6767
} else {
68+
$_SESSION['code_verifier'] = $client->getOAuth2Service()->generateCodeVerifier();
6869
$authUrl = $client->createAuthUrl();
6970
}
7071

examples/large-file-upload.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
* bundle in the session, and redirect to ourself.
5454
************************************************/
5555
if (isset($_GET['code'])) {
56-
$token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
56+
$token = $client->fetchAccessTokenWithAuthCode($_GET['code'], $_SESSION['code_verifier']);
5757
$client->setAccessToken($token);
5858

5959
// store in the session also
@@ -70,6 +70,7 @@
7070
unset($_SESSION['upload_token']);
7171
}
7272
} else {
73+
$_SESSION['code_verifier'] = $client->getOAuth2Service()->generateCodeVerifier();
7374
$authUrl = $client->createAuthUrl();
7475
}
7576

examples/multi-api.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
* bundle in the session, and redirect to ourself.
5555
************************************************/
5656
if (isset($_GET['code'])) {
57-
$token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
57+
$token = $client->fetchAccessTokenWithAuthCode($_GET['code'], $_SESSION['code_verifier']);
5858
$client->setAccessToken($token);
5959

6060
// store in the session also
@@ -71,6 +71,7 @@
7171
unset($_SESSION['multi-api-token']);
7272
}
7373
} else {
74+
$_SESSION['code_verifier'] = $client->getOAuth2Service()->generateCodeVerifier();
7475
$authUrl = $client->createAuthUrl();
7576
}
7677

examples/simple-file-upload.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
* bundle in the session, and redirect to ourself.
5454
************************************************/
5555
if (isset($_GET['code'])) {
56-
$token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
56+
$token = $client->fetchAccessTokenWithAuthCode($_GET['code'], $_SESSION['code_verifier']);
5757
$client->setAccessToken($token);
5858

5959
// store in the session also
@@ -70,6 +70,7 @@
7070
unset($_SESSION['upload_token']);
7171
}
7272
} else {
73+
$_SESSION['code_verifier'] = $client->getOAuth2Service()->generateCodeVerifier();
7374
$authUrl = $client->createAuthUrl();
7475
}
7576

src/Client.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,10 @@ public function authenticate($code)
240240
* Helper wrapped around the OAuth 2.0 implementation.
241241
*
242242
* @param string $code code from accounts.google.com
243+
* @param string $codeVerifier the code verifier used for PKCE (if applicable)
243244
* @return array access token
244245
*/
245-
public function fetchAccessTokenWithAuthCode($code)
246+
public function fetchAccessTokenWithAuthCode($code, $codeVerifier = null)
246247
{
247248
if (strlen($code) == 0) {
248249
throw new InvalidArgumentException("Invalid code");
@@ -251,6 +252,9 @@ public function fetchAccessTokenWithAuthCode($code)
251252
$auth = $this->getOAuth2Service();
252253
$auth->setCode($code);
253254
$auth->setRedirectUri($this->getRedirectUri());
255+
if ($codeVerifier) {
256+
$auth->setCodeVerifier($codeVerifier);
257+
}
254258

255259
$httpHandler = HttpHandlerFactory::build($this->getHttpClient());
256260
$creds = $auth->fetchAuthToken($httpHandler);

0 commit comments

Comments
 (0)