Skip to content

Commit

Permalink
feat(install): Allow downloading from private repositories (#4254)
Browse files Browse the repository at this point in the history
* added ability to use private github repositories

To do this, define a gh_api_token in the sccop config, if the repository is private it will use the apis to get and use the asset id

* corrected inherited linting error

* updated url to pull filename

* inlined get_headers and get-members functions and updated changelog

* simplified host headers and switched to checkver_token from: astelmachonak-nydig

* added ability to use private github repositories

To do this, define a gh_api_token in the sccop config, if the repository is private it will use the apis to get and use the asset id

* updated url to pull filename

* inlined get_headers and get-members functions and updated changelog

* simplified host headers and switched to checkver_token from: astelmachonak-nydig

* Update lib/core.ps1

Co-authored-by: Rashil Gandhi <46838874+rashil2000@users.noreply.github.com>

* Update lib/core.ps1

Co-authored-by: Rashil Gandhi <46838874+rashil2000@users.noreply.github.com>

* Fix RegEx

* Some tweaks

* optimize

Co-authored-by: Rashil Gandhi <46838874+rashil2000@users.noreply.github.com>
Co-authored-by: Hsiao-nan Cheung <niheaven@gmail.com>
Co-authored-by: Rashil Gandhi <rashil2000@gmail.com>
  • Loading branch information
4 people authored Mar 21, 2022
1 parent 5e4e6e9 commit 45db5fb
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## [Unreleased](https://github.com/ScoopInstaller/Scoop/compare/master...develop)

### Features

- **install:** Allow downloading from private repositories ([$4254](https://github.com/ScoopInstaller/Scoop/issues/4243))

### Bug Fixes

- **shim:** Manipulating shims with UTF8 encoding ([#4791](https://github.com/ScoopInstaller/Scoop/issues/4791), [#4813](https://github.com/ScoopInstaller/Scoop/issues/4813))
Expand Down
12 changes: 12 additions & 0 deletions lib/core.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,18 @@ function handle_special_urls($url)
# Reshapes the URL to avoid redirections
$url = "https://downloads.sourceforge.net/project/$($matches['project'])/$($matches['file'])"
}

# Github.com
if ($url -match 'github.com/(?<owner>[^/]+)/(?<repo>[^/]+)/releases/download/(?<tag>[^/]+)/(?<file>[^/#]+)(?<filename>.*)' -and ($token = get_config 'checkver_token')) {
$headers = @{ "Authorization" = "token $token" }
$privateUrl = "https://api.github.com/repos/$($Matches.owner)/$($Matches.repo)"
$assetUrl = "https://api.github.com/repos/$($Matches.owner)/$($Matches.repo)/releases/tags/$($Matches.tag)"

if ((Invoke-RestMethod -Uri $privateUrl -Headers $headers).Private) {
$url = ((Invoke-RestMethod -Uri $assetUrl -Headers $headers).Assets | Where-Object -Property Name -EQ -Value $Matches.file).Url, $Matches.filename -join ''
}
}

return $url
}

Expand Down
26 changes: 18 additions & 8 deletions lib/install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -387,15 +387,25 @@ function dl_with_cache_aria2($app, $version, $manifest, $architecture, $dir, $co

# download with filesize and progress indicator
function dl($url, $to, $cookies, $progress) {
$reqUrl = ($url -split "#")[0]
$wreq = [net.webrequest]::create($reqUrl)
if($wreq -is [net.httpwebrequest]) {
$wreq.useragent = Get-UserAgent
if (-not ($url -imatch "sourceforge\.net" -or $url -imatch "portableapps\.com")) {
$wreq.referer = strip_filename $url
$reqUrl = ($url -split '#')[0]
$wreq = [Net.WebRequest]::Create($reqUrl)
if ($wreq -is [Net.HttpWebRequest]) {
$wreq.UserAgent = Get-UserAgent
if (-not ($url -match 'sourceforge\.net' -or $url -match 'portableapps\.com')) {
$wreq.Referer = strip_filename $url
}
if($cookies) {
$wreq.headers.add('Cookie', (cookie_header $cookies))
if ($url -match 'api\.github\.com/repos') {
$wreq.Accept = 'application/octet-stream'
$wreq.Headers['Authorization'] = "token $(get_config 'checkver_token')"
}
if ($cookies) {
$wreq.Headers.Add('Cookie', (cookie_header $cookies))
}

get_config 'private_hosts' | Where-Object { $url -match $_.match } | ForEach-Object {
(ConvertFrom-StringData -StringData $_.Headers).GetEnumerator() | ForEach-Object {
$wreq.Headers[$_.Key] = $_.Value
}
}
}

Expand Down
9 changes: 7 additions & 2 deletions libexec/scoop-config.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@
#
# checkver_token:
# GitHub API token used to make authenticated requests.
# This is essential for checkver and similar functions
# to run without incurring rate limits.
# This is essential for checkver and similar functions to run without
# incurring rate limits and download from private repositories.
#
# virustotal_api_key:
# API key used for uploading/scanning files using virustotal.
Expand All @@ -96,6 +96,11 @@
# any target app process is running. Procedure here refers to reset/uninstall/update.
# When set to $true, Scoop only displays a warning message and continues procedure.
#
# private_hosts:
# Array of private hosts that need additional authentication.
# For example, if you want to access a private GitHub repository,
# you need to add the host to this list with 'match' and 'headers' strings.
#
# ARIA2 configuration
# -------------------
#
Expand Down

0 comments on commit 45db5fb

Please sign in to comment.