1
0
Fork 0

Fix #10366: Improve messaging when GitHub tokens need SSO authorization (#10432)

pull/10449/head
Dane Powell 2022-01-08 03:03:54 -08:00 committed by GitHub
parent 24b62a1002
commit 93d4c8e531
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 0 deletions

View File

@ -92,6 +92,23 @@ class AuthHelper
$message = "\n";
$rateLimited = $gitHubUtil->isRateLimited($headers);
$requiresSso = $gitHubUtil->requiresSso($headers);
if ($requiresSso) {
$ssoUrl = $gitHubUtil->getSsoUrl($headers);
$message = sprintf(
'GitHub API token requires SSO authorization. Authorize this token at ' . $ssoUrl,
$ssoUrl
) . "\n";
$this->io->writeError($message);
if (!$this->io->isInteractive()) {
throw new TransportException('Could not authenticate against ' . $origin, 403);
}
$this->io->ask('After authorizing your token, confirm that you would like to retry the request');
return array('retry' => true, 'storeAuth' => $storeAuth);
}
if ($rateLimited) {
$rateLimit = $gitHubUtil->getRateLimit($headers);
if ($this->io->hasAuthentication($origin)) {

View File

@ -171,6 +171,28 @@ class GitHub
return $rateLimit;
}
/**
* Extract SSO URL from response.
*
* @param string[] $headers Headers from Composer\Downloader\TransportException.
*
* @return string|null
*/
public function getSsoUrl(array $headers)
{
foreach ($headers as $header) {
$header = trim($header);
if (false === stripos($header, 'x-github-sso: required')) {
continue;
}
if (Preg::isMatch('{\burl=(?P<url>[^\s;]+)}', $header, $match)) {
return $match['url'];
}
}
return null;
}
/**
* Finds whether a request failed due to rate limiting
*
@ -188,4 +210,24 @@ class GitHub
return false;
}
/**
* Finds whether a request failed due to lacking SSO authorization
*
* @see https://docs.github.com/en/rest/overview/other-authentication-methods#authenticating-for-saml-sso
*
* @param string[] $headers Headers from Composer\Downloader\TransportException.
*
* @return bool
*/
public function requiresSso(array $headers)
{
foreach ($headers as $header) {
if (Preg::isMatch('{^X-GitHub-SSO: required}i', trim($header))) {
return true;
}
}
return false;
}
}