Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Diagnose command: Add GitHub OAuth token expiration date information #11688

Merged
merged 2 commits into from
Feb 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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