lib/Classes/Public/TMSession.ps1


class TMSession {
    ## A Name parameter to identify the session in other -TMSessions functions
    [String]$Name

    # TM Server hostname
    [String]$TMServer

    # TMVersion drives the selection of compatible APIs to use
    [String]$TMVersion

    # Logged in TM User's Context (indicates loggedin-ness)
    [PSCustomObject]$UserContext

    # TMWebSession Variable. Maintained by the Invoke-WebRequest function's capability
    $TMWebSession

    # TMRestSession Variable. Maintained by the Invoke-RestMethod function's capability
    $TMRestSession

    ## Tracks non-changing items to reduce HTTP lookups, and increase speed of scripts.
    ## DataCache is expected to be a k/v pair, where the V could be another k/v pair,
    ## However, it's implementation will be more of the nature to hold the list of object calls from the API
    ## like 'credentials' = @(@{...},@{...}); 'actions' = @(@{...},@{...})
    ## Get-TM* functions will cache unless a -NoCache switch is provided
    [Hashtable]$DataCache

    ## Should PowerShell ignore the SSL Cert on the TM Server?
    [Bool]$AllowInsecureSSL
    
    ## Hods the TM Server's Public Key
    [String]$PublicKey

    TMSession() {
        $this.Name = 'Default'
        $this.AllowInsecureSSL = $false
        $this.DataCache = @{}
        $this.UserContext = $null
        $this.TMWebSession = New-Object -Type Microsoft.PowerShell.Commands.WebRequestSession
        $this.TMRestSession = New-Object -Type Microsoft.PowerShell.Commands.WebRequestSession
    }
    
    TMSession([String]$_name = 'Default') {
        $this.Name = $_name
        $this.AllowInsecureSSL = $false
        $this.DataCache = @{}
        $this.UserContext = $null
        $this.TMWebSession = New-Object -Type Microsoft.PowerShell.Commands.WebRequestSession
        $this.TMRestSession = New-Object -Type Microsoft.PowerShell.Commands.WebRequestSession
    }
    
    TMSession([String]$_name = 'Default', [String]$_server, [Bool]$_allowInsecureSSL = $false) {
        $this.Name = $_name
        $this.AllowInsecureSSL = $_allowInsecureSSL
        $this.TMServer = $_server
        $this.DataCache = @{}
        $this.UserContext = $null
        $this.TMWebSession = New-Object -Type Microsoft.PowerShell.Commands.WebRequestSession
        $this.TMRestSession = New-Object -Type Microsoft.PowerShell.Commands.WebRequestSession
    }
    
    ## Constructor used by Import-TMCActionRequest
    TMSession([PSObject]$_actionrequest) {
        $server = $_actionrequest.tmUserSession.url -replace 'https://', ''
        
        ## Assign Default Values
        $this.Name = 'Default'
        $this.AllowInsecureSSL = $False
        $this.TMServer = $server
        $this.TMVersion = $_actionrequest.tmUserSession.tmVersion
        $this.DataCache = @{}
        $this.UserContext = $_actionrequest.tmUserSession.userContext

        ## Build a Rest and Web Session Object
        $this.TMWebSession = New-Object -Type Microsoft.PowerShell.Commands.WebRequestSession
        $this.TMRestSession = New-Object -Type Microsoft.PowerShell.Commands.WebRequestSession

        ## Add the JSESSIONID Cookie
        $JSessionCookie = New-Object System.Net.Cookie 
        $JSessionCookie.Name = "JSESSIONID"
        $JSessionCookie.Value = $_actionrequest.tmUserSession.jsessionid
        $JSessionCookie.Domain = $server
        $this.TMWebSession.Cookies.Add($JSessionCookie)
        $this.TMRestSession.Cookies.Add($JSessionCookie)

        ## Add the CSRF header
        if($_actionrequest.tmUserSession.csrf){
            $this.TMWebSession.Headers.Add($_actionrequest.tmUserSession.csrf.tokenHeaderName, $_actionrequest.tmUserSession.csrf.token)
            $this.TMRestSession.Headers.Add($_actionrequest.tmUserSession.csrf.tokenHeaderName, $_actionrequest.tmUserSession.csrf.token)
        }
    }
}