Skip to content

Commit

Permalink
bug #842 Pass GitHub access token when accessing raw.githubuserconten…
Browse files Browse the repository at this point in the history
…t.com in case of private recipes (aivus)

This PR was merged into the 1.x branch.

Discussion
----------

Pass GitHub access token when accessing raw.githubusercontent.com in case of private recipes

I'm checking how to use private symfony recipes and found that `symfony/flex` is not using GH access token when accessing `raw.githubcontent.com`.

For a public repositories it's not a case, but for private repositories it's not possible to access the recipe template url from `raw.githubcontent.com` without using access token.

Adding access token for requests to `api.github.com` is [covered by Composer](https://github.com/composer/composer/blob/f5ffedfe60b5b0043c368b91e656288517aad0d9/src/Composer/Util/AuthHelper.php#L210-L215), but it doesn't cover downloading files from `raw.githubcontent.com`.

This PR introduces logic for adding token for requests to `raw.githubcontent.com` in the [similar way as composer does](https://github.com/composer/composer/blob/f5ffedfe60b5b0043c368b91e656288517aad0d9/src/Composer/Util/AuthHelper.php#L210-L215) for `api.github.com`.

Limitations:
* Current implementation adds the token (if it presents) to ALL requests to the `raw.githubusercontent.com`, but I don't think that it's a big issue actually.

Commits
-------

d7dd781 Pass GitHub access token when accessing raw.githubusercontent.com
  • Loading branch information
nicolas-grekas committed Feb 16, 2022
2 parents 787e2e3 + d7dd781 commit 988b39c
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Downloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,11 @@ private function get(array $urls, bool $isRecipe = false, int $try = 3): array

if (preg_match('{^https?://api\.github\.com/}', $url)) {
$headers[] = 'Accept: application/vnd.github.v3.raw';
} elseif (preg_match('{^https?://raw\.githubusercontent\.com/}', $url) && $this->io->hasAuthentication('github.com')) {
$auth = $this->io->getAuthentication('github.com');
if ('x-oauth-basic' === $auth['password']) {
$headers[] = 'Authorization: token '.$auth['username'];
}
} elseif ($this->legacyEndpoint) {
$headers[] = 'Package-Session: '.$this->sess;
}
Expand Down

0 comments on commit 988b39c

Please sign in to comment.