Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add credential option to the Install-ChocolateyPackage API and pass
the cred object down to the relevant web calls.
  • Loading branch information
Russell Mora committed Jan 23, 2017
1 parent 4e4ce52 commit 45bdad5
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ param(
[parameter(Mandatory=$false)][string] $checksumType = '',
[parameter(Mandatory=$false)][string] $checksum64 = '',
[parameter(Mandatory=$false)][string] $checksumType64 = $checksumType,
[parameter(Mandatory=$false)][Object] $credential = $null,
[parameter(Mandatory=$false)][hashtable] $options = @{Headers=@{}},
[parameter(Mandatory=$false)][switch] $getOriginalFileName,
[parameter(Mandatory=$false)][switch] $forceDownload,
Expand Down Expand Up @@ -261,7 +262,7 @@ param(
if ($url.StartsWith('http:')) {
try {
$httpsUrl = $url.Replace("http://", "https://")
Get-WebHeaders -Url $httpsUrl -ErrorAction "Stop" | Out-Null
Get-WebHeaders -Url $httpsUrl -ErrorAction "Stop" -Credential $credential | Out-Null
$url = $httpsUrl
Write-Warning "Url has SSL/TLS available, switching to HTTPS for download"
} catch {
Expand All @@ -274,7 +275,7 @@ param(
$fileFullPath = $fileFullPath -replace '\\chocolatey\\chocolatey\\', '\chocolatey\'
$fileDirectory = [System.IO.Path]::GetDirectoryName($fileFullPath)
$originalFileName = [System.IO.Path]::GetFileName($fileFullPath)
$fileFullPath = Get-WebFileName -Url $url -DefaultName $originalFileName
$fileFullPath = Get-WebFileName -Url $url -DefaultName $originalFileName -Credential $credential
$fileFullPath = Join-Path $fileDirectory $fileFullPath
$fileFullPath = [System.IO.Path]::GetFullPath($fileFullPath)
} catch {
Expand All @@ -295,15 +296,15 @@ param(
$headers = @{}
if ($url.StartsWith('http')) {
try {
$headers = Get-WebHeaders -Url $url -ErrorAction "Stop"
$headers = Get-WebHeaders -Url $url -ErrorAction "Stop" -Credential $credential
} catch {
if ($host.Version -lt (New-Object 'Version' 3,0)) {
Write-Debug "Converting Security Protocol to SSL3 only for Powershell v2"
# this should last for the entire duration
$originalProtocol = [System.Net.ServicePointManager]::SecurityProtocol
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Ssl3
try {
$headers = Get-WebHeaders -Url $url -ErrorAction "Stop"
$headers = Get-WebHeaders -Url $url -ErrorAction "Stop" -Credential $credential
} catch {
Write-Host "Attempt to get headers for $url failed.`n $($_.Exception.Message)"
[System.Net.ServicePointManager]::SecurityProtocol = $originalProtocol
Expand Down Expand Up @@ -334,7 +335,7 @@ param(
if ($needsDownload) {
Write-Host "Downloading $packageName $bitPackage
from `'$url`'"
Get-WebFile -Url $url -FileName $fileFullPath -Options $options
Get-WebFile -Url $url -FileName $fileFullPath -Credential $credential -Options $options
} else {
Write-Debug "$($packageName)'s requested file has already been downloaded. Using cached copy at
'$fileFullPath'."
Expand Down
5 changes: 4 additions & 1 deletion src/chocolatey.resources/helpers/functions/Get-WebFile.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ param(
[parameter(Mandatory=$false, Position=0)][string] $url = '', #(Read-Host "The URL to download"),
[parameter(Mandatory=$false, Position=1)][string] $fileName = $null,
[parameter(Mandatory=$false, Position=2)][string] $userAgent = 'chocolatey command line',
[parameter(Mandatory=$false)][Object] $credential = $null,
[parameter(Mandatory=$false)][switch] $Passthru,
[parameter(Mandatory=$false)][switch] $quiet,
[parameter(Mandatory=$false)][hashtable] $options = @{Headers=@{}},
Expand All @@ -109,7 +110,9 @@ param(

$req = [System.Net.HttpWebRequest]::Create($url);
$defaultCreds = [System.Net.CredentialCache]::DefaultCredentials
if ($defaultCreds -ne $null) {
if ($credential -ne $null) {
$req.Credentials = $credential
} elseif ($defaultCreds -ne $null) {
$req.Credentials = $defaultCreds
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ param(
[parameter(Mandatory=$false, Position=0)][string] $url = '',
[parameter(Mandatory=$true, Position=1)][string] $defaultName,
[parameter(Mandatory=$false)][string] $userAgent = 'chocolatey command line',
[parameter(Mandatory=$false)][Object] $credential = $null,
[parameter(ValueFromRemainingArguments = $true)][Object[]] $ignoredArguments
)

Expand Down Expand Up @@ -106,7 +107,9 @@ param(
}

$defaultCreds = [System.Net.CredentialCache]::DefaultCredentials
if ($defaultCreds -ne $null) {
if ($credential -ne $null) {
$req.Credentials = $credential
} elseif ($defaultCreds -ne $null) {
$request.Credentials = $defaultCreds
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Get-WebFile
param(
[parameter(Mandatory=$false, Position=0)][string] $url = '',
[parameter(Mandatory=$false, Position=1)][string] $userAgent = 'chocolatey command line',
[parameter(Mandatory=$false, Position=2)][Object] $credential = $null,
[parameter(ValueFromRemainingArguments = $true)][Object[]] $ignoredArguments
)

Expand All @@ -62,7 +63,10 @@ param(

$request = [System.Net.HttpWebRequest]::Create($url);
$defaultCreds = [System.Net.CredentialCache]::DefaultCredentials
if ($defaultCreds -ne $null) {
if ($credential -ne $null) {
Write-Host "Using credential."
$request.Credentials = $credential
} elseif ($defaultCreds -ne $null) {
$request.Credentials = $defaultCreds
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ param(
[parameter(Mandatory=$false)][string] $checksumType = '',
[parameter(Mandatory=$false)][string] $checksum64 = '',
[parameter(Mandatory=$false)][string] $checksumType64 = '',
[parameter(Mandatory=$false)][object] $credential = $null,
[parameter(Mandatory=$false)][hashtable] $options = @{Headers=@{}},
[parameter(Mandatory=$false)]
[alias("useOnlyPackageSilentArgs")][switch] $useOnlyPackageSilentArguments = $false,
Expand Down Expand Up @@ -307,6 +308,7 @@ param(
-ChecksumType $checksumType `
-Checksum64 $checksum64 `
-ChecksumType64 $checksumType64 `
-Credential $credential `
-Options $options `
-GetOriginalFileName
}
Expand Down

0 comments on commit 45bdad5

Please sign in to comment.