From 9e0a39561876b6acbf07ba1a5d0ee015d451b7d4 Mon Sep 17 00:00:00 2001 From: Timothy Gruber Date: Fri, 15 Nov 2024 16:14:46 -0800 Subject: [PATCH] fix: token expiration calculation to be locale and region agnostic --- Private/Invoke-AzureStorageBlobUpload.ps1 | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/Private/Invoke-AzureStorageBlobUpload.ps1 b/Private/Invoke-AzureStorageBlobUpload.ps1 index 66b1c95..460699d 100644 --- a/Private/Invoke-AzureStorageBlobUpload.ps1 +++ b/Private/Invoke-AzureStorageBlobUpload.ps1 @@ -53,11 +53,21 @@ function Invoke-AzureStorageBlobUpload { for ($Chunk = 0; $Chunk -lt $ChunkCount; $Chunk++) { Write-Verbose -Message "SAS Uri renewal timer has elapsed for: $($SASRenewalTimer.Elapsed.Minutes) minute $($SASRenewalTimer.Elapsed.Seconds) seconds" - # Refresh access token if about to expire - $UTCDateTime = (Get-Date).ToUniversalTime() + # Convert ExpiresOn to DateTimeOffset in UTC + $ExpiresOnUTC = [DateTimeOffset]::Parse( + $Global:AccessToken.ExpiresOn.ToString(), + [System.Globalization.CultureInfo]::InvariantCulture, + [System.Globalization.DateTimeStyles]::AssumeUniversal + ).ToUniversalTime() - # Determine the token expiration count as minutes - $TokenExpireMinutes = [System.Math]::Round(([datetime]$Global:AccessToken.ExpiresOn.ToUniversalTime() - $UTCDateTime).TotalMinutes) + # Get the current UTC time as DateTimeOffset + $UTCDateTime = [DateTimeOffset]::UtcNow + + # Calculate the TimeSpan between expiration and current time + $TimeSpan = $ExpiresOnUTC - $UTCDateTime + + # Calculate the token expiration time in minutes + $TokenExpireMinutes = [System.Math]::Round($TimeSpan.TotalMinutes) # Determine if refresh of access token is required when expiration count is less than or equal to minimum age if ($TokenExpireMinutes -le 10) {