diff --git a/src/GitHub/data/Auth.psd1 b/src/GitHub/data/Auth.psd1 index c1701ac68..9a306d5c6 100644 --- a/src/GitHub/data/Auth.psd1 +++ b/src/GitHub/data/Auth.psd1 @@ -5,4 +5,5 @@ OAuthApp = @{ ClientID = '7204ae9b0580f2cb8288' # $script:Auth.OAuthApp.ClientID } + AccessTokenGracePeriodInHours = 4 } diff --git a/src/GitHub/private/Auth/DeviceFlow/Test-GitHubAccessTokenRefreshRequired.ps1 b/src/GitHub/private/Auth/DeviceFlow/Test-GitHubAccessTokenRefreshRequired.ps1 new file mode 100644 index 000000000..5d16cf56e --- /dev/null +++ b/src/GitHub/private/Auth/DeviceFlow/Test-GitHubAccessTokenRefreshRequired.ps1 @@ -0,0 +1,28 @@ +function Test-GitHubAccessTokenRefreshRequired { + <# + .SYNOPSIS + Test if the GitHub access token should be refreshed. + + .DESCRIPTION + Test if the GitHub access token should be refreshed. + + .EXAMPLE + Test-GitHubAccessTokenRefreshRequired + + This will test if the GitHub access token should be refreshed. + #> + [CmdletBinding()] + param() + + $tokenExpirationDate = Get-GitHubConfig -Name 'AccessTokenExpirationDate' -ErrorAction SilentlyContinue + if (-not $tokenExpirationDate) { + return $false + } + $currentDateTime = Get-Date + + # Calulate the remaining time in hours + $remainindDuration = [datetime]$tokenExpirationDate - $currentDateTime + + # If the remaining time is less that $script:Auth.AccessTokenGracePeriodInHours then the token should be refreshed + $remainindDuration.TotalHours -lt $script:Auth.AccessTokenGracePeriodInHours +} diff --git a/src/GitHub/public/API/Invoke-GitHubAPI.ps1 b/src/GitHub/public/API/Invoke-GitHubAPI.ps1 index 374cf2868..3ae771b87 100644 --- a/src/GitHub/public/API/Invoke-GitHubAPI.ps1 +++ b/src/GitHub/public/API/Invoke-GitHubAPI.ps1 @@ -63,9 +63,13 @@ # The GitHub API version to be used. By default, it pulls from a configuration script variable. [Parameter()] [string] $Version = (Get-GitHubConfig -Name ApiVersion) - ) + if (Test-GitHubAccessTokenRefreshRequired) { + Connect-GitHubAccount -Silent + $AccessToken = (Get-GitHubConfig -Name AccessToken) + } + $functionName = $MyInvocation.MyCommand.Name $headers = @{ diff --git a/src/GitHub/public/Auth/Connect-GitHubAccount.ps1 b/src/GitHub/public/Auth/Connect-GitHubAccount.ps1 index cb935b8e8..441952710 100644 --- a/src/GitHub/public/Auth/Connect-GitHubAccount.ps1 +++ b/src/GitHub/public/Auth/Connect-GitHubAccount.ps1 @@ -70,7 +70,11 @@ # Set the default repository to use in commands. [Parameter()] - [string] $Repo + [string] $Repo, + + # Suppresses the output of the function. + [Parameter()] + [switch] $Silent ) $envVars = Get-ChildItem -Path 'Env:' @@ -93,21 +97,27 @@ $seconds = $accessTokenValidity.Seconds.ToString().PadLeft(2, '0') $accessTokenValidityText = "$hours`:$minutes`:$seconds" if ($accessTokenIsValid) { - if ($accessTokenValidity.TotalHours -gt 4) { - Write-Host '✓ ' -ForegroundColor Green -NoNewline - Write-Host "Access token is still valid for $accessTokenValidityText ..." + if ($accessTokenValidity.TotalHours -gt $script:Auth.AccessTokenGracePeriodInHours) { + if (-not $Silent) { + Write-Host '✓ ' -ForegroundColor Green -NoNewline + Write-Host "Access token is still valid for $accessTokenValidityText ..." + } break } else { - Write-Host '⚠ ' -ForegroundColor Yellow -NoNewline - Write-Host "Access token remaining validity $accessTokenValidityText. Refreshing access token..." + if (-not $Silent) { + Write-Host '⚠ ' -ForegroundColor Yellow -NoNewline + Write-Host "Access token remaining validity $accessTokenValidityText. Refreshing access token..." + } $tokenResponse = Invoke-GitHubDeviceFlowLogin -ClientID $clientID -RefreshToken (Get-GitHubConfig -Name RefreshToken) } } else { $refreshTokenValidity = [datetime](Get-GitHubConfig -Name 'RefreshTokenExpirationDate') - (Get-Date) $refreshTokenIsValid = $refreshTokenValidity.Seconds -gt 0 if ($refreshTokenIsValid) { - Write-Host '⚠ ' -ForegroundColor Yellow -NoNewline - Write-Verbose 'Access token expired. Refreshing access token...' + if (-not $Silent) { + Write-Host '⚠ ' -ForegroundColor Yellow -NoNewline + Write-Host 'Access token expired. Refreshing access token...' + } $tokenResponse = Invoke-GitHubDeviceFlowLogin -ClientID $clientID -RefreshToken (Get-GitHubConfig -Name RefreshToken) } else { Write-Verbose "Using $Mode authentication..." @@ -154,8 +164,8 @@ $accessTokenValue = Read-Host -Prompt 'Enter your personal access token' -AsSecureString $accessTokenType = (ConvertFrom-SecureString $accessTokenValue -AsPlainText) -replace '_.*$', '_*' if ($accessTokenType -notmatch '^ghp_|^github_pat_') { - Write-Host '⚠ ' -ForegroundColor Yellow -NoNewline - Write-Host "Unexpected access token format: $accessTokenType" + Write-Warning '⚠ ' -ForegroundColor Yellow -NoNewline + Write-Warning "Unexpected access token format: $accessTokenType" } $settings = @{ AccessToken = $accessTokenValue @@ -190,9 +200,10 @@ $username = 'system' } - Write-Host '✓ ' -ForegroundColor Green -NoNewline - Write-Host "Logged in as $username!" - + if (-not $Silent) { + Write-Host '✓ ' -ForegroundColor Green -NoNewline + Write-Host "Logged in as $username!" + } $systemRepo = $envVars | Where-Object Name -EQ 'GITHUB_REPOSITORY' $systemRepoPresent = $systemRepo.count -gt 0 diff --git a/src/GitHub/public/Config/Get-GitHubConfig.ps1 b/src/GitHub/public/Config/Get-GitHubConfig.ps1 index ba9960d52..76c0b5afe 100644 --- a/src/GitHub/public/Config/Get-GitHubConfig.ps1 +++ b/src/GitHub/public/Config/Get-GitHubConfig.ps1 @@ -53,7 +53,7 @@ if ($Name) { $metadata.$Name } else { - $metadata | Sort-Object -Property Name + $metadata.GetEnumerator() | Sort-Object -Property Name } } }