From a82fdc33d3e9aa14372a451a50cbc9b318befb52 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sun, 1 Oct 2023 22:09:49 +0200 Subject: [PATCH 1/6] Added var for when to refresh token --- src/GitHub/data/Auth.psd1 | 1 + 1 file changed, 1 insertion(+) 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 } From b7ed54a47695fdb5142743cbe19fd32feabb742a Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sun, 1 Oct 2023 22:10:02 +0200 Subject: [PATCH 2/6] Added test accesstoken function --- .../Test-GitHubAccessTokenRefreshRequired.ps1 | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/GitHub/private/Auth/DeviceFlow/Test-GitHubAccessTokenRefreshRequired.ps1 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 +} From 2ba26c22ab6446a9574b3d10d4bce8b05cd56375 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sun, 1 Oct 2023 22:10:44 +0200 Subject: [PATCH 3/6] Added step in Inv-GHAPI to check token --- src/GitHub/public/API/Invoke-GitHubAPI.ps1 | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/GitHub/public/API/Invoke-GitHubAPI.ps1 b/src/GitHub/public/API/Invoke-GitHubAPI.ps1 index 374cf2868..4faf94c65 100644 --- a/src/GitHub/public/API/Invoke-GitHubAPI.ps1 +++ b/src/GitHub/public/API/Invoke-GitHubAPI.ps1 @@ -63,9 +63,12 @@ # 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 + } + $functionName = $MyInvocation.MyCommand.Name $headers = @{ From 1bed3252a9a54ba4fcd57b108a7ada108088973e Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sun, 1 Oct 2023 22:11:05 +0200 Subject: [PATCH 4/6] Added silent switch to Connect-GitHubAccount --- .../public/Auth/Connect-GitHubAccount.ps1 | 37 ++++++++++++------- 1 file changed, 24 insertions(+), 13 deletions(-) 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 From ed1992f438c8ee957999c77082df1dc5933e3ceb Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sun, 1 Oct 2023 22:19:53 +0200 Subject: [PATCH 5/6] Need to reload the access token when its reset. --- src/GitHub/public/API/Invoke-GitHubAPI.ps1 | 1 + 1 file changed, 1 insertion(+) diff --git a/src/GitHub/public/API/Invoke-GitHubAPI.ps1 b/src/GitHub/public/API/Invoke-GitHubAPI.ps1 index 4faf94c65..3ae771b87 100644 --- a/src/GitHub/public/API/Invoke-GitHubAPI.ps1 +++ b/src/GitHub/public/API/Invoke-GitHubAPI.ps1 @@ -67,6 +67,7 @@ if (Test-GitHubAccessTokenRefreshRequired) { Connect-GitHubAccount -Silent + $AccessToken = (Get-GitHubConfig -Name AccessToken) } $functionName = $MyInvocation.MyCommand.Name From ddd44a88c0e5d5ee8e683d8cd8526a7e182d8f80 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Sun, 1 Oct 2023 22:29:54 +0200 Subject: [PATCH 6/6] fix sort --- src/GitHub/public/Config/Get-GitHubConfig.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 } } }