Skip to content

Commit

Permalink
feat: add pkce support and upgrade examples (#2438)
Browse files Browse the repository at this point in the history
  • Loading branch information
bshaffer committed May 18, 2023
1 parent c765b37 commit bded223
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -7,7 +7,7 @@
"license": "Apache-2.0",
"require": {
"php": "^7.4|^8.0",
"google/auth": "^1.26",
"google/auth": "^1.28",
"google/apiclient-services": "~0.200",
"firebase/php-jwt": "~6.0",
"monolog/monolog": "^2.9||^3.0",
Expand Down
3 changes: 2 additions & 1 deletion examples/idtoken.php
Expand Up @@ -57,7 +57,7 @@
* bundle in the session, and redirect to ourself.
************************************************/
if (isset($_GET['code'])) {
$token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
$token = $client->fetchAccessTokenWithAuthCode($_GET['code'], $_SESSION['code_verifier']);

// store in the session also
$_SESSION['id_token_token'] = $token;
Expand All @@ -77,6 +77,7 @@
) {
$client->setAccessToken($_SESSION['id_token_token']);
} else {
$_SESSION['code_verifier'] = $client->getOAuth2Service()->generateCodeVerifier();
$authUrl = $client->createAuthUrl();
}

Expand Down
3 changes: 2 additions & 1 deletion examples/large-file-download.php
Expand Up @@ -48,7 +48,7 @@
* bundle in the session, and redirect to ourself.
************************************************/
if (isset($_GET['code'])) {
$token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
$token = $client->fetchAccessTokenWithAuthCode($_GET['code'], $_SESSION['code_verifier']);
$client->setAccessToken($token);

// store in the session also
Expand All @@ -65,6 +65,7 @@
unset($_SESSION['upload_token']);
}
} else {
$_SESSION['code_verifier'] = $client->getOAuth2Service()->generateCodeVerifier();
$authUrl = $client->createAuthUrl();
}

Expand Down
3 changes: 2 additions & 1 deletion examples/large-file-upload.php
Expand Up @@ -53,7 +53,7 @@
* bundle in the session, and redirect to ourself.
************************************************/
if (isset($_GET['code'])) {
$token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
$token = $client->fetchAccessTokenWithAuthCode($_GET['code'], $_SESSION['code_verifier']);
$client->setAccessToken($token);

// store in the session also
Expand All @@ -70,6 +70,7 @@
unset($_SESSION['upload_token']);
}
} else {
$_SESSION['code_verifier'] = $client->getOAuth2Service()->generateCodeVerifier();
$authUrl = $client->createAuthUrl();
}

Expand Down
3 changes: 2 additions & 1 deletion examples/multi-api.php
Expand Up @@ -54,7 +54,7 @@
* bundle in the session, and redirect to ourself.
************************************************/
if (isset($_GET['code'])) {
$token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
$token = $client->fetchAccessTokenWithAuthCode($_GET['code'], $_SESSION['code_verifier']);
$client->setAccessToken($token);

// store in the session also
Expand All @@ -71,6 +71,7 @@
unset($_SESSION['multi-api-token']);
}
} else {
$_SESSION['code_verifier'] = $client->getOAuth2Service()->generateCodeVerifier();
$authUrl = $client->createAuthUrl();
}

Expand Down
3 changes: 2 additions & 1 deletion examples/simple-file-upload.php
Expand Up @@ -53,7 +53,7 @@
* bundle in the session, and redirect to ourself.
************************************************/
if (isset($_GET['code'])) {
$token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
$token = $client->fetchAccessTokenWithAuthCode($_GET['code'], $_SESSION['code_verifier']);
$client->setAccessToken($token);

// store in the session also
Expand All @@ -70,6 +70,7 @@
unset($_SESSION['upload_token']);
}
} else {
$_SESSION['code_verifier'] = $client->getOAuth2Service()->generateCodeVerifier();
$authUrl = $client->createAuthUrl();
}

Expand Down
6 changes: 5 additions & 1 deletion src/Client.php
Expand Up @@ -240,9 +240,10 @@ public function authenticate($code)
* Helper wrapped around the OAuth 2.0 implementation.
*
* @param string $code code from accounts.google.com
* @param string $codeVerifier the code verifier used for PKCE (if applicable)
* @return array access token
*/
public function fetchAccessTokenWithAuthCode($code)
public function fetchAccessTokenWithAuthCode($code, $codeVerifier = null)
{
if (strlen($code) == 0) {
throw new InvalidArgumentException("Invalid code");
Expand All @@ -251,6 +252,9 @@ public function fetchAccessTokenWithAuthCode($code)
$auth = $this->getOAuth2Service();
$auth->setCode($code);
$auth->setRedirectUri($this->getRedirectUri());
if ($codeVerifier) {
$auth->setCodeVerifier($codeVerifier);
}

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

0 comments on commit bded223

Please sign in to comment.