-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXMCSession.ps1
53 lines (34 loc) · 1.41 KB
/
XMCSession.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
class XMCSession {
#base URI for API calls
[string]$Server
hidden [string]$Token
[datetime]$LastUsed
[int]$TimeToLive = 600
hidden [string]$ClientID
hidden [string]$ClientSecret
requestToken ($Server, $ClientID, $ClientSecret) {
#logic to retrieve key here
$Uri = 'https://{0}/oauth/token/access-token?grant_type=client_credentials' -f $Server
$this.Server = $Server
$this.ClientID = $ClientID
$this.ClientSecret = $ClientSecret
#Encoding of the client data
$IDSecret = $ClientID + ":" + $ClientSecret
$EncodedIDSecret = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($IDSecret))
$headers = @{
"Authorization" = "Basic $EncodedIDSecret"
}
# Maybe add some error handling
$response = Invoke-WebRequest -uri $Uri -headers $headers -Method Post -ContentType "application/x-www-form-urlencoded"
$this.LastUsed = [datetime]::Now
# return token as a string
$this.token = ($response.content | convertfrom-json).access_token
}
[string] getToken () {
if ( ([datetime]::Now - $this.LastUsed).TotalSeconds -gt $this.TimeToLive ) {
$this.requestToken($this.Server, $this.ClientID, $this.ClientSecret)
Write-Host "New session token requested"
}
return $this.token
}
}