USE SECURELY STORED API AUTHORIZATION STRING

It took much longer to write this follow-up to the API AUTHORIZATION STRING STORAGE post than I had anticipated. I was tasked with something specific to Avaya IP Office however, in classic Avaya fashion, the tools offered fall way short of the task. Alas, I have decided to use use the “IP Office 11.0 Management API” to demonstrate how to use the stored secure authorization string generated in the previous post, as that is just about all this API is good for – simple examples. 

This article assumes you have a working version of IP Office 11.

As the script itself is outlined with basic comments I offer it without further ado.

#Bypass SSL certificate warning
add-type @"
    using System.Net;
    using System.Security.Cryptography.X509Certificates;
    public class TrustAllCertsPolicy : ICertificatePolicy {
        public bool CheckValidationResult(
            ServicePoint srvPoint, X509Certificate certificate,
            WebRequest request, int certificateProblem) {
            return true;
        }
    }
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

#Read secured credential
$credentialHash = get-content "C:\zScripts\AvayaAPICreds1.txt"
[Byte[]] $hashKey = (1..16)
#Convert to hashed credential to use with API
$authInfo = (New-Object System.Net.NetworkCredential("", (ConvertTo-SecureString -k ($hashKey) $credentialHash))).Password

#Create cookie
$avayaSession = new-object system.net.cookie

#Build authorization string to pass with session header
$authString = @{Authorization=("Basic {0}" -f $authInfo)}

#Update with Avaya IP Office address
$authURI = "https://10.0.0.12:7070/WebManagement/ws/sdk/security/authenticate"

#Initiate API conversation
$authRequest = Invoke-WebRequest -ContentType "application/json" -Method Get -Uri $authURI -SessionVariable avayaSession | select Headers

#Manipulate headers as per Avaya documentation
$authRequest.Headers.Add("X-User-Client", "Avaya-WebAdmin")
$authRequest.Headers.Add("X-User-Agent", "Avaya-SDKUser")
$authRequest.Headers.Add("Authorization", "Basic {0}" -f $authInfo)
$authRequest.Headers.Remove("Transfer-Encoding")
#Set session headers for authentication
$headers = $authRequest.Headers

#Send authenticated session information and set cookie
$token = Invoke-WebRequest -Uri $authURI -Method Get -ContentType "application/json" -WebSession $avayaSession -Headers $headers
#Extract and manipulate cookie data
$token = $token.Headers.'Set-Cookie'.Split(";")[0].split("=")
#Add cookie data to session headers
$headers.Add($token[0],$token[1])
#Set cookie with full headers when a session to $usersURI is executed
$avayaSession.Cookies.SetCookies($usersURI, $headers)

#Update with Avaya IP Office address
$usersURI = "https://10.0.0.12:7070/WebManagement/ws/sdk/admin/v1/users"
#Execute session to $usersURI
$users = Invoke-RestMethod -Uri $usersURI -Method Get -ContentType "application/xml" -WebSession $avayaSession
#Export user data
$users.InnerXml | out-file C:\TEMP\users.xml -Force utf8

#Set cookie with full headers when a session to $extensionsURI is executed
$avayaSession.Cookies.SetCookies($extensionsURI, $headers)
#Update with Avaya IP Office address
$extensionsURI = "https://10.0.0.12:7070/WebManagement/ws/sdk/admin/v1/users"
#Execute session to $extensionsURI
$extensions = Invoke-RestMethod -Uri $extensionsURI -Method Get -ContentType "application/xml" -WebSession $avayaSession
#Export extension data
$extensions.InnerXml | out-file C:\TEMP\extensions.xml -Force utf8

#Sanitize memory
$headers = $null
$authRequest = $null
$avayaSession = $null 
David Goldstein on Linkedin
David Goldstein
David has been innovating enterprise-class solutions for over two decades. His exposure to a variety of industries and regulatory regimes has given him the breadth of knowledge and hands-on experience to successfully navigate the current technology landscape and provide dependable solutions no matter how seemingly mundane or complex. Technologically agnostic with an operational bent, David's philosophy is firmly rooted in the concept of the "Data Custodian" and as such architects solution's with safety, accessibility, redundancy, and integrity of the data always at the top of mind.

As a holistic information fiduciary David leverages proven industry standards and best practices that will optimize your processes and systems to maximize performance and minimize complexity. David has managed projects in varying phases of development, be it greenfield builds or forklift moves, backup design and architecture, infrastructure monitoring and alerting, scripting and automation, communications and network infrastructure, policy review and auditing, rack and stacks and virtualization.

He doesn't just get IT, he groks IT.

Leave a Reply

Your email address will not be published. Required fields are marked *