function AuthHelper::addAuthenticationHeader
Parameters
string[] $headers:
Return value
string[] updated headers array
File
-
vendor/
composer/ composer/ src/ Composer/ Util/ AuthHelper.php, line 234
Class
- AuthHelper
- @author Jordi Boggiano <j.boggiano@seld.be>
Namespace
Composer\UtilCode
public function addAuthenticationHeader(array $headers, string $origin, string $url) : array {
if ($this->io
->hasAuthentication($origin)) {
$authenticationDisplayMessage = null;
$auth = $this->io
->getAuthentication($origin);
if ($auth['password'] === 'bearer') {
$headers[] = 'Authorization: Bearer ' . $auth['username'];
}
elseif ('github.com' === $origin && 'x-oauth-basic' === $auth['password']) {
// only add the access_token if it is actually a github API URL
if (Preg::isMatch('{^https?://api\\.github\\.com/}', $url)) {
$headers[] = 'Authorization: token ' . $auth['username'];
$authenticationDisplayMessage = 'Using GitHub token authentication';
}
}
elseif (in_array($origin, $this->config
->get('gitlab-domains'), true) && in_array($auth['password'], [
'oauth2',
'private-token',
'gitlab-ci-token',
], true)) {
if ($auth['password'] === 'oauth2') {
$headers[] = 'Authorization: Bearer ' . $auth['username'];
$authenticationDisplayMessage = 'Using GitLab OAuth token authentication';
}
else {
$headers[] = 'PRIVATE-TOKEN: ' . $auth['username'];
$authenticationDisplayMessage = 'Using GitLab private token authentication';
}
}
elseif ('bitbucket.org' === $origin && $url !== Bitbucket::OAUTH2_ACCESS_TOKEN_URL && 'x-token-auth' === $auth['username']) {
if (!$this->isPublicBitBucketDownload($url)) {
$headers[] = 'Authorization: Bearer ' . $auth['password'];
$authenticationDisplayMessage = 'Using Bitbucket OAuth token authentication';
}
}
else {
$authStr = base64_encode($auth['username'] . ':' . $auth['password']);
$headers[] = 'Authorization: Basic ' . $authStr;
$authenticationDisplayMessage = 'Using HTTP basic authentication with username "' . $auth['username'] . '"';
}
if ($authenticationDisplayMessage && (!isset($this->displayedOriginAuthentications[$origin]) || $this->displayedOriginAuthentications[$origin] !== $authenticationDisplayMessage)) {
$this->io
->writeError($authenticationDisplayMessage, true, IOInterface::DEBUG);
$this->displayedOriginAuthentications[$origin] = $authenticationDisplayMessage;
}
}
elseif (in_array($origin, [
'api.bitbucket.org',
'api.github.com',
], true)) {
return $this->addAuthenticationHeader($headers, str_replace('api.', '', $origin), $url);
}
return $headers;
}