Skip to content

Commit 168bb42

Browse files
[Enhancement] Auth - Auto refresh GitHub App access token (#40)
- Fixes #40 - Added function to check if GitHub App access token has reached its grace period. If so, run Connect-GitHubAccount silently and get the new access token.
1 parent 5eec22c commit 168bb42

File tree

5 files changed

+59
-15
lines changed

5 files changed

+59
-15
lines changed

src/GitHub/data/Auth.psd1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
OAuthApp = @{
66
ClientID = '7204ae9b0580f2cb8288' # $script:Auth.OAuthApp.ClientID
77
}
8+
AccessTokenGracePeriodInHours = 4
89
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
function Test-GitHubAccessTokenRefreshRequired {
2+
<#
3+
.SYNOPSIS
4+
Test if the GitHub access token should be refreshed.
5+
6+
.DESCRIPTION
7+
Test if the GitHub access token should be refreshed.
8+
9+
.EXAMPLE
10+
Test-GitHubAccessTokenRefreshRequired
11+
12+
This will test if the GitHub access token should be refreshed.
13+
#>
14+
[CmdletBinding()]
15+
param()
16+
17+
$tokenExpirationDate = Get-GitHubConfig -Name 'AccessTokenExpirationDate' -ErrorAction SilentlyContinue
18+
if (-not $tokenExpirationDate) {
19+
return $false
20+
}
21+
$currentDateTime = Get-Date
22+
23+
# Calulate the remaining time in hours
24+
$remainindDuration = [datetime]$tokenExpirationDate - $currentDateTime
25+
26+
# If the remaining time is less that $script:Auth.AccessTokenGracePeriodInHours then the token should be refreshed
27+
$remainindDuration.TotalHours -lt $script:Auth.AccessTokenGracePeriodInHours
28+
}

src/GitHub/public/API/Invoke-GitHubAPI.ps1

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,13 @@
6363
# The GitHub API version to be used. By default, it pulls from a configuration script variable.
6464
[Parameter()]
6565
[string] $Version = (Get-GitHubConfig -Name ApiVersion)
66-
6766
)
6867

68+
if (Test-GitHubAccessTokenRefreshRequired) {
69+
Connect-GitHubAccount -Silent
70+
$AccessToken = (Get-GitHubConfig -Name AccessToken)
71+
}
72+
6973
$functionName = $MyInvocation.MyCommand.Name
7074

7175
$headers = @{

src/GitHub/public/Auth/Connect-GitHubAccount.ps1

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@
7070

7171
# Set the default repository to use in commands.
7272
[Parameter()]
73-
[string] $Repo
73+
[string] $Repo,
74+
75+
# Suppresses the output of the function.
76+
[Parameter()]
77+
[switch] $Silent
7478
)
7579

7680
$envVars = Get-ChildItem -Path 'Env:'
@@ -93,21 +97,27 @@
9397
$seconds = $accessTokenValidity.Seconds.ToString().PadLeft(2, '0')
9498
$accessTokenValidityText = "$hours`:$minutes`:$seconds"
9599
if ($accessTokenIsValid) {
96-
if ($accessTokenValidity.TotalHours -gt 4) {
97-
Write-Host '' -ForegroundColor Green -NoNewline
98-
Write-Host "Access token is still valid for $accessTokenValidityText ..."
100+
if ($accessTokenValidity.TotalHours -gt $script:Auth.AccessTokenGracePeriodInHours) {
101+
if (-not $Silent) {
102+
Write-Host '' -ForegroundColor Green -NoNewline
103+
Write-Host "Access token is still valid for $accessTokenValidityText ..."
104+
}
99105
break
100106
} else {
101-
Write-Host '' -ForegroundColor Yellow -NoNewline
102-
Write-Host "Access token remaining validity $accessTokenValidityText. Refreshing access token..."
107+
if (-not $Silent) {
108+
Write-Host '' -ForegroundColor Yellow -NoNewline
109+
Write-Host "Access token remaining validity $accessTokenValidityText. Refreshing access token..."
110+
}
103111
$tokenResponse = Invoke-GitHubDeviceFlowLogin -ClientID $clientID -RefreshToken (Get-GitHubConfig -Name RefreshToken)
104112
}
105113
} else {
106114
$refreshTokenValidity = [datetime](Get-GitHubConfig -Name 'RefreshTokenExpirationDate') - (Get-Date)
107115
$refreshTokenIsValid = $refreshTokenValidity.Seconds -gt 0
108116
if ($refreshTokenIsValid) {
109-
Write-Host '' -ForegroundColor Yellow -NoNewline
110-
Write-Verbose 'Access token expired. Refreshing access token...'
117+
if (-not $Silent) {
118+
Write-Host '' -ForegroundColor Yellow -NoNewline
119+
Write-Host 'Access token expired. Refreshing access token...'
120+
}
111121
$tokenResponse = Invoke-GitHubDeviceFlowLogin -ClientID $clientID -RefreshToken (Get-GitHubConfig -Name RefreshToken)
112122
} else {
113123
Write-Verbose "Using $Mode authentication..."
@@ -154,8 +164,8 @@
154164
$accessTokenValue = Read-Host -Prompt 'Enter your personal access token' -AsSecureString
155165
$accessTokenType = (ConvertFrom-SecureString $accessTokenValue -AsPlainText) -replace '_.*$', '_*'
156166
if ($accessTokenType -notmatch '^ghp_|^github_pat_') {
157-
Write-Host '' -ForegroundColor Yellow -NoNewline
158-
Write-Host "Unexpected access token format: $accessTokenType"
167+
Write-Warning '' -ForegroundColor Yellow -NoNewline
168+
Write-Warning "Unexpected access token format: $accessTokenType"
159169
}
160170
$settings = @{
161171
AccessToken = $accessTokenValue
@@ -190,9 +200,10 @@
190200
$username = 'system'
191201
}
192202

193-
Write-Host '' -ForegroundColor Green -NoNewline
194-
Write-Host "Logged in as $username!"
195-
203+
if (-not $Silent) {
204+
Write-Host '' -ForegroundColor Green -NoNewline
205+
Write-Host "Logged in as $username!"
206+
}
196207

197208
$systemRepo = $envVars | Where-Object Name -EQ 'GITHUB_REPOSITORY'
198209
$systemRepoPresent = $systemRepo.count -gt 0

src/GitHub/public/Config/Get-GitHubConfig.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
if ($Name) {
5454
$metadata.$Name
5555
} else {
56-
$metadata | Sort-Object -Property Name
56+
$metadata.GetEnumerator() | Sort-Object -Property Name
5757
}
5858
}
5959
}

0 commit comments

Comments
 (0)