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
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 Oct 25, 2023
1 parent c827c93 commit 9571e9a
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/Composer/Command/DiagnoseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,20 @@ 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,
]);

$expiration = $response->getHeader('github-authentication-token-expiration');

if ($expiration === null) {
return '<info>OK</> <comment>no expiration</>';
}

if (\DateTime::createFromFormat('Y-m-d h:i:s O', $expiration)) {

Check failure on line 340 in src/Composer/Command/DiagnoseCommand.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, false)

Only booleans are allowed in an if condition, DateTime|false given.

Check failure on line 340 in src/Composer/Command/DiagnoseCommand.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, true)

Only booleans are allowed in an if condition, DateTime|false given.
return '<info>OK</> <comment>expires on '. $expiration .'</>';
}

return true;
} catch (\Exception $e) {
if ($e instanceof TransportException && $e->getCode() === 401) {
Expand Down

0 comments on commit 9571e9a

Please sign in to comment.