1
0
Fork 0

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`).
pull/11833/head
Ayesh Karunaratne 2024-02-08 03:30:24 +07:00 committed by GitHub
parent 18cd8a01a4
commit e0807d381e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 9 additions and 3 deletions

View File

@ -326,7 +326,7 @@ EOT
} }
/** /**
* @return string|true|\Exception * @return string|\Exception
*/ */
private function checkGithubOauth(string $domain, string $token) private function checkGithubOauth(string $domain, string $token)
{ {
@ -339,11 +339,17 @@ EOT
try { try {
$url = $domain === 'github.com' ? 'https://api.'.$domain.'/' : 'https://'.$domain.'/api/v3/'; $url = $domain === 'github.com' ? 'https://api.'.$domain.'/' : 'https://'.$domain.'/api/v3/';
$this->httpDownloader->get($url, [ $response = $this->httpDownloader->get($url, [
'retry-auth-failure' => false, 'retry-auth-failure' => false,
]); ]);
return true; $expiration = $response->getHeader('github-authentication-token-expiration');
if ($expiration === null) {
return '<info>OK</> <comment>does not expire</>';
}
return '<info>OK</> <comment>expires on '. $expiration .'</>';
} catch (\Exception $e) { } catch (\Exception $e) {
if ($e instanceof TransportException && $e->getCode() === 401) { if ($e instanceof TransportException && $e->getCode() === 401) {
return '<comment>The oauth token for '.$domain.' seems invalid, run "composer config --global --unset github-oauth.'.$domain.'" to remove it</comment>'; return '<comment>The oauth token for '.$domain.' seems invalid, run "composer config --global --unset github-oauth.'.$domain.'" to remove it</comment>';