From e0807d381ebc90f1d1570e7751700374d3dcbfc7 Mon Sep 17 00:00:00 2001 From: Ayesh Karunaratne Date: Thu, 8 Feb 2024 03:30:24 +0700 Subject: [PATCH] Diagnose command: Add GitHub OAuth token expiration date information (#11688) GitHub's new fine-grained tokens have a cumpulsory expiration date, and their classic tokens also support an expiration date. https://github.blog/changelog/2021-07-26-expiration-options-for-personal-access-tokens/ This improves the `composer diagnose` command to display the expiration date and time if it is provided by the response headers (via `GitHub-Authentication-Token-Expiration`). --- src/Composer/Command/DiagnoseCommand.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Composer/Command/DiagnoseCommand.php b/src/Composer/Command/DiagnoseCommand.php index 18b1911d7..fc594f7d9 100644 --- a/src/Composer/Command/DiagnoseCommand.php +++ b/src/Composer/Command/DiagnoseCommand.php @@ -326,7 +326,7 @@ EOT } /** - * @return string|true|\Exception + * @return string|\Exception */ private function checkGithubOauth(string $domain, string $token) { @@ -339,11 +339,17 @@ EOT try { $url = $domain === 'github.com' ? 'https://api.'.$domain.'/' : 'https://'.$domain.'/api/v3/'; - $this->httpDownloader->get($url, [ + $response = $this->httpDownloader->get($url, [ 'retry-auth-failure' => false, ]); - return true; + $expiration = $response->getHeader('github-authentication-token-expiration'); + + if ($expiration === null) { + return 'OK does not expire'; + } + + return 'OK expires on '. $expiration .''; } catch (\Exception $e) { if ($e instanceof TransportException && $e->getCode() === 401) { return 'The oauth token for '.$domain.' seems invalid, run "composer config --global --unset github-oauth.'.$domain.'" to remove it';