-
Notifications
You must be signed in to change notification settings - Fork 8
/
Token.ps1
75 lines (62 loc) · 2.2 KB
/
Token.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
#
# Copyright 2021, Alexis La Goutte <alexis.lagoutte at gmail dot com>
#
# SPDX-License-Identifier: Apache-2.0
#
function Update-ArubaCLRefreshToken {
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'low')]
Param(
[Parameter(Mandatory = $false)]
[psobject]$connection = $DefaultArubaCLConnection
)
$url = "https://" + $connection.server + "/oauth2/token"
$url += "?client_id=" + $connection.token.client_id + "&client_secret=" + $connection.token.client_secret + "&grant_type=refresh_token&refresh_token=" + $connection.token.refresh_token
$headers = @{ Accept = "application/json"; "Content-type" = "application/json" }
Write-Verbose $url
if ($PSCmdlet.ShouldProcess($connection.token.client_id, 'Update Refresh Token')) {
try {
$response = Invoke-RestMethod $url -Method POST -WebSession $connection.session -headers $headers
}
catch {
Show-ArubaCLException $_
throw "Unable to get token"
}
}
Write-Verbose $response
#Update Headers
$connection.headers.Authorization = "Bearer " + $response.access_token
#Update token..
$connection.token.access_token = $response.access_token
$connection.token.refresh_token = $response.refresh_token
#Get when new token will be expire
$connection.token.expire = [int]((Get-Date -UFormat %s) -split ",")[0] + $response.expires_in
}
function Get-ArubaCLTokenStatus {
<#
.SYNOPSIS
Get status of token
.DESCRIPTION
Get status of token so we don't need to keep reauthenticating
.EXAMPLE
Get-ArubaCLTokenStatus
Get token status (expired or not)
.EXAMPLE
Get-ArubaCLTokenStatus -timeout 900
Get token status (expired or not) on less of 900 seconds (15 minutes)
#>
Param(
[Parameter(Mandatory = $false)]
[int]$timeout = 0,
[Parameter(Mandatory = $false)]
[psobject]$connection = $DefaultArubaCLConnection
)
$expire = $connection.token.expire
$now = [int]((Get-Date -UFormat %s) -split ",")[0]
if (($expire - $now) -le $timeout) {
# If token is expired, it should fail boolean
return $false
}
else {
return $true
}
}