Skip to content
Closed
Show file tree
Hide file tree
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
187 changes: 105 additions & 82 deletions ReST.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,67 @@
$global:fedAuthTicket = ''
$global:digestValue = ''

function Set-SPOAuthenticationTicket([string] $siteUrl)
Add-Type -Path "$($PSScriptRoot)\WebLogin.cs" -ReferencedAssemblies System.Windows.Forms

function Get-AuthCookieWebLogin([Uri] $Uri)
{
$username = "admin@contoso.microsoft.com"
Write-Host 'Enter password for user' $username 'on site' $siteUrl
$password = Read-Host -AsSecureString

# load the SP client runtime code
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
$onlineCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)
if ($onlineCredentials -ne $null)
{
$global:fedAuthTicket = $onlineCredentials.GetAuthenticationCookie($SiteUrl, $true).TrimStart('SPOIDCRL=')
}

if ([String]::IsNullOrEmpty($global:fedAuthTicket))
{
throw 'Could not obtain authentication ticket based on provided credentials for specified site'
}
$cookieStr = [WebLogin.WebLogin]::GetWebLoginCookie($Uri)
if ([System.String]::IsNullOrEmpty($cookieStr))
{
throw "Authentication failed"
}

return $cookieStr
}

function Set-SPOAuthenticationTicket([string] $siteUrl, $useWebLogin = $false)
{
if ($useWebLogin)
{
$global:fedAuthTicket = Get-AuthCookieWebLogin($siteUrl)
}
else
{
$username = "admin@contoso.onmicrosoft.com"
Write-Host 'Enter password for user' $username 'on site' $siteUrl
$password = Read-Host -AsSecureString

# load the SP client runtime code
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
$onlineCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)
if ($onlineCredentials -ne $null)
{
$global:fedAuthTicket = $onlineCredentials.GetAuthenticationCookie($siteUrl, $true);
}

if ([String]::IsNullOrEmpty($global:fedAuthTicket))
{
throw 'Could not obtain authentication ticket based on provided credentials for specified site'
}
}
}

function Build-ReSTRequest([string] $siteUrl, [string]$endpoint, [string]$method, [string]$body = $null)
{
$url = ([string]$siteUrl).TrimEnd("/") + "/_api/" + $endpoint
$req = [System.Net.WebRequest]::Create($url)
$req.Method = $method

[bool]$isReadOnly = (('GET','HEAD') -contains $req.Method)
[bool]$isDigestRequest = $endpoint -contains 'contextinfo'

if ([String]::IsNullOrEmpty($body))
{
$req.ContentLength = 0;
}
else
{
$req.ContentLength = $body.Length
$req.ContentType = "application/json"
}

$domain = (New-Object System.Uri($url)).Authority
$url = ([string]$siteUrl).TrimEnd("/") + "/_api/" + $endpoint
$req = [System.Net.WebRequest]::Create($url)
$req.Method = $method

[bool]$isReadOnly = (('GET','HEAD') -contains $req.Method)
[bool]$isDigestRequest = $endpoint -contains 'contextinfo'

if ([String]::IsNullOrEmpty($body))
{
$req.ContentLength = 0;
}
else
{
$req.ContentLength = $body.Length
$req.ContentType = "application/json"
}

$cookies = New-Object System.Net.CookieContainer
$fedCookie = New-Object System.Net.Cookie 'SPOIDCRL', $global:fedAuthTicket, "", $domain
$cookies.Add($fedCookie)
$cookies.SetCookies($siteUrl, $global:fedAuthTicket)

$req.CookieContainer = $cookies

Expand All @@ -58,59 +76,64 @@ function Build-ReSTRequest([string] $siteUrl, [string]$endpoint, [string]$method
if (-not $isReadOnly)
{
$req.Headers.Add("X-RequestDigest", $global:digestValue)

if ($method -eq "PATCH")
{
$req.Headers.Add("If-Match", "*");
}
}
}
if (-not [String]::IsNullOrEmpty($body))
{
$writer = New-Object System.IO.StreamWriter $req.GetRequestStream()
$writer.Write($body)
$writer.Close()

if (-not [String]::IsNullOrEmpty($body))
{
$writer = New-Object System.IO.StreamWriter $req.GetRequestStream()
$writer.Write($body)
$writer.Close()
$writer.Dispose()
}
return $req
}
return $req
}

function Set-DigestValue([string]$siteUrl)
{
$request = Build-ReSTRequest $siteUrl 'contextinfo' 'POST' $null
if ($request -eq $null)
{
throw 'Could not obtain a request digest value based on provided credentials for specified site'
}
try
{
$resp = $request.GetResponse()
$reader = [System.Xml.XmlReader]::Create($resp.GetResponseStream())
if ($reader.ReadToDescendant("d:FormDigestValue"))
{
$global:digestValue = $reader.ReadElementContentAsString()
}
else
{
throw 'Could not obtain a request digest value based on provided credentials for specified site'
}
}
finally
{
if ($reader -ne $null)
{
$reader.Close()
$reader.Dispose()
}
if ($resp -ne $null)
{
$resp.Close()
$resp.Dispose()
}
}
$request = Build-ReSTRequest $siteUrl 'contextinfo' 'POST' $null
if ($request -eq $null)
{
throw 'Could not obtain a request digest value based on provided credentials for specified site'
}
try
{
$resp = $request.GetResponse()
$reader = [System.Xml.XmlReader]::Create($resp.GetResponseStream())
if ($reader.ReadToDescendant("d:FormDigestValue"))
{
$global:digestValue = $reader.ReadElementContentAsString()
}
else
{
throw 'Could not obtain a request digest value based on provided credentials for specified site'
}
}
finally
{
if ($reader -ne $null)
{
$reader.Close()
$reader.Dispose()
}
if ($resp -ne $null)
{
$resp.Close()
$resp.Dispose()
}
}
}

function Post-ReSTRequest([string]$siteUrl, [string]$endpoint, [string]$body = $null)
{
$request = Build-ReSTRequest $siteUrl $endpoint 'POST' $body
$request = Build-ReSTRequest $siteUrl $endpoint 'POST' $body
$resp = $request.GetResponse()
if ($resp -ne $null)
{
Expand All @@ -122,7 +145,7 @@ function Post-ReSTRequest([string]$siteUrl, [string]$endpoint, [string]$body = $

function Patch-ReSTRequest([string]$siteUrl, [string]$endpoint, [string]$body)
{
$request = Build-ReSTRequest $siteUrl $endpoint 'PATCH' $body
$request = Build-ReSTRequest $siteUrl $endpoint 'PATCH' $body
$resp = $request.GetResponse()
if ($resp -ne $null)
{
Expand All @@ -134,12 +157,12 @@ function Patch-ReSTRequest([string]$siteUrl, [string]$endpoint, [string]$body)

function Get-ReSTRequest([string]$siteUrl, [string]$endpoint)
{
$request = Build-ReSTRequest $siteUrl $endpoint 'GET'
$request = Build-ReSTRequest $siteUrl $endpoint 'GET'
$resp = $request.GetResponse()
if ($resp -ne $null)
{
$reader = New-Object System.IO.StreamReader $resp.GetResponseStream()
$reader.ReadToEnd()
$reader.Dispose()
$reader.Dispose()
}
}
Loading