Skip to content

Commit

Permalink
fix: token expiration calculation to be locale and
Browse files Browse the repository at this point in the history
 region agnostic
  • Loading branch information
tjgruber committed Nov 16, 2024
1 parent 002878c commit 9e0a395
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions Private/Invoke-AzureStorageBlobUpload.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 9e0a395

Please sign in to comment.