Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 60 additions & 31 deletions Sources/OneDrive.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,6 @@ function Get-ODAuthentication
} else
{
write-debug("Authentication mode: " +$Type)
[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | out-null
[Reflection.Assembly]::LoadWithPartialName("System.Drawing") | out-null
[Reflection.Assembly]::LoadWithPartialName("System.Web") | out-null
if ($Logout)
{
$URIGetAccessToken="https://login.live.com/logout.srf"
Expand All @@ -90,41 +87,70 @@ function Get-ODAuthentication
$URIGetAccessToken="https://login.live.com/oauth20_authorize.srf?client_id="+$ClientId+"&scope="+$Scope+"&response_type="+$Type+"&redirect_URI="+$RedirectURI
}
}
$form = New-Object Windows.Forms.Form
$form.text = "Authenticate to OneDrive"
$form.size = New-Object Drawing.size @(700,600)
$form.Width = 675
$form.Height = 750
$web=New-object System.Windows.Forms.WebBrowser
$web.IsWebBrowserContextMenuEnabled = $true
$web.Width = 600
$web.Height = 700
$web.Location = "25, 25"
$web.navigate($URIGetAccessToken)
$DocComplete = {
if ($web.Url.AbsoluteUri -match "access_token=|error|code=|logout") {$form.Close() }
if ($web.DocumentText -like '*ucaccept*') {
if ($AutoAccept) {$web.Document.GetElementById("idBtn_Accept").InvokeMember("click")}

$ReturnURI = ""

if($IsWindows) {

[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | out-null
[Reflection.Assembly]::LoadWithPartialName("System.Drawing") | out-null
[Reflection.Assembly]::LoadWithPartialName("System.Web") | out-null

$form = New-Object Windows.Forms.Form
$form.text = "Authenticate to OneDrive"
$form.size = New-Object Drawing.size @(700,600)
$form.Width = 675
$form.Height = 750
$web=New-object System.Windows.Forms.WebBrowser
$web.IsWebBrowserContextMenuEnabled = $true
$web.Width = 600
$web.Height = 700
$web.Location = "25, 25"
$web.navigate($URIGetAccessToken)
$DocComplete = {
if ($web.Url.AbsoluteUri -match "access_token=|error|code=|logout") {$form.Close() }
if ($web.DocumentText -like '*ucaccept*') {
if ($AutoAccept) {$web.Document.GetElementById("idBtn_Accept").InvokeMember("click")}
}
}
$web.Add_DocumentCompleted($DocComplete)
$form.Controls.Add($web)
if ($DontShowLoginScreen)
{
write-debug("Logon screen suppressed by flag -DontShowLoginScreen")
$form.Opacity = 0.0;
}
$form.showdialog() | out-null

# Build object from last URI (which should contains the token)
$ReturnURI=($web.Url).ToString().Replace("#","&")
}
$web.Add_DocumentCompleted($DocComplete)
$form.Controls.Add($web)
if ($DontShowLoginScreen)
{
write-debug("Logon screen suppressed by flag -DontShowLoginScreen")
$form.Opacity = 0.0;
else { # $IsLinux, $IsMacOS

Write-Host "You have to authorize this app on your browser."
Write-Host "After you accept, the browser will be redirected to a new (invalid) URL."
Write-Host "You will need to copy-paste that last URL in this shell."
Write-Host ""
Write-Host "Press ENTER to continue, then type here the last URL..."
Read-Host

Start-Process ${URIGetAccessToken}

$webURI=Read-Host "Copy-paste here the last URL from your browser"

$ReturnURI=$webURI.ToString().Replace("#","&")
}
$form.showdialog() | out-null
# Build object from last URI (which should contains the token)
$ReturnURI=($web.Url).ToString().Replace("#","&")

if ($LogOut) {return "Logout"}
if ($Type -eq "code")
{
write-debug("Getting code to redeem token")
$Authentication = New-Object PSObject
ForEach ($element in $ReturnURI.Split("?")[1].Split("&"))
{
$Authentication | add-member Noteproperty $element.split("=")[0] $element.split("=")[1]
if($element) {
$Authentication | add-member Noteproperty $element.split("=")[0] $element.split("=")[1]
}
}
if ($Authentication.code)
{
Expand Down Expand Up @@ -905,8 +931,11 @@ function Add-ODItemLarge {
# The (default) length of an upload session is about 15 minutes!

# Set the headers for the actual file chunk's PUT request (by means of the above preset variables)
$actHeaders=@{"Content-Length"="$uploadLength"; "Content-Range"="bytes $startingIndex-$endingIndex/$totalLength"};

# Note: specifying Content-Length gives duplicate values and raises false error regarding -ContentType
# https://github.com/PowerShell/PowerShell/issues/12500
$actHeaders=@{"Content-Range"="bytes $startingIndex-$endingIndex/$totalLength"};
#$actHeaders=@{"Content-Length"="$uploadLength"; "Content-Range"="bytes $startingIndex-$endingIndex/$totalLength"};

# Execute the PUT request (upload file chunk)
write-debug("Uploading chunk of bytes. Progress: "+$endingIndex/$totalLength*100+" %")
$uploadResponse=Invoke-WebRequest -Method PUT -Uri $uURL -Headers $actHeaders -Body $buf -UseBasicParsing -ErrorAction SilentlyContinue
Expand Down Expand Up @@ -996,4 +1025,4 @@ function Move-ODItem
return Get-ODWebContent -AccessToken $AccessToken -ResourceId $ResourceId -Method PATCH -rURI $rURI -Body $body
}
}
}
}