Private/RefreshMIASessionIfNeeded.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
function RefreshMIASessionIfNeeded
{
    $elapsed = New-TimeSpan -Start $Script:MIASession.CreatedAt
    Write-Verbose "MIA Session at $($elapsed.TotalSeconds) of $($Script:MIASession.ExpiresIn)"

    # If the key is within 30 seconds of expiring, let's go ahead and
    # refresh it.
    if ($elapsed.TotalSeconds -gt $Script:MIASession.ExpiresIn - 30) {

        Write-Verbose "Session expired, refreshing..."

        $params = @{
            Uri = "https://$($Script:MIASession.Hostname)/api/v1/token"
            Method = 'POST'
            ContentType = 'application/x-www-form-urlencoded'
            Body = "grant_type=refresh_token&refresh_token=$($Script:MIASession.RefreshToken)"
            Headers = @{Accept = "application/json"}
        }

        $response = Invoke-RestMethod @params
        Set-MIASession -Hostname $Script:MIASession.Hostname -Response $response
    }
}