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
parent
18cd8a01a4
commit
e0807d381e
|
@ -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 '<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>';
|
||||
|
|
Loading…
Reference in New Issue