Skip to content

Commit

Permalink
Diagnose command: Add GitHub OAuth token expiration date information (#…
Browse files Browse the repository at this point in the history
…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`).
  • Loading branch information
Ayesh committed Feb 7, 2024
1 parent 18cd8a0 commit e0807d3
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/Composer/Command/DiagnoseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ private function checkHttpProxy()
}

/**
* @return string|true|\Exception
* @return string|\Exception
*/
private function checkGithubOauth(string $domain, string $token)
{
Expand All @@ -339,11 +339,17 @@ private function checkGithubOauth(string $domain, string $token)
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 '<info>OK</> <comment>does not expire</>';
}

return '<info>OK</> <comment>expires on '. $expiration .'</>';
} catch (\Exception $e) {
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>';
Expand Down

0 comments on commit e0807d3

Please sign in to comment.