Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/GitHub/data/Auth.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
OAuthApp = @{
ClientID = '7204ae9b0580f2cb8288' # $script:Auth.OAuthApp.ClientID
}
AccessTokenGracePeriodInHours = 4
}
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 5 additions & 1 deletion src/GitHub/public/API/Invoke-GitHubAPI.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 = @{
Expand Down
37 changes: 24 additions & 13 deletions src/GitHub/public/Auth/Connect-GitHubAccount.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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:'
Expand All @@ -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..."
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/GitHub/public/Config/Get-GitHubConfig.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
if ($Name) {
$metadata.$Name
} else {
$metadata | Sort-Object -Property Name
$metadata.GetEnumerator() | Sort-Object -Property Name
}
}
}
Expand Down