lib/Classes/Public/TMSession.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

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 -split '-')[0]
        $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)

        ## Add the Bearer Token header
        $this.TMRestSession.Headers.Add('Authorization', "Bearer $($_actionrequest.options.callback.token)")

        ## 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)
        }

        ## Add TMConsole's Public Session Key to the TMSession
        $this.PublicKey = $_actionrequest.publicRSAKey
    }
}