Skip to content

Commit

Permalink
🩹 [Patch]: Update GitHub authentication functions to use context sett…
Browse files Browse the repository at this point in the history
…ings for ClientID, Token, and RefreshToken
  • Loading branch information
MariusStorhaug committed Nov 24, 2024
1 parent f3e9fdb commit 2bbb323
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
3 changes: 1 addition & 2 deletions src/functions/public/API/Invoke-GitHubAPI.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@

switch ($tokenType) {
'ghu' {
if (Test-GitHubAccessTokenRefreshRequired) {
if (Test-GitHubAccessTokenRefreshRequired -Context $Context) {
Connect-GitHubAccount -Silent
$Token = (Get-GitHubContextSetting -Name 'Token' -Context $Context)
}
Expand All @@ -142,7 +142,6 @@
}
}


$headers = @{
Accept = $Accept
'X-GitHub-Api-Version' = $ApiVersion
Expand Down
16 changes: 8 additions & 8 deletions src/functions/public/Auth/Connect-GitHubAccount.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -175,18 +175,18 @@
if (-not [string]::IsNullOrEmpty($ClientID)) {
Write-Verbose "Using provided ClientID: [$ClientID]"
$authClientID = $ClientID
} elseif (-not [string]::IsNullOrEmpty($(Get-GitHubConfig -Name 'AuthClientID'))) {
Write-Verbose "Reusing previously stored ClientID: [$(Get-GitHubConfig -Name 'AuthClientID')]"
$authClientID = Get-GitHubConfig -Name 'AuthClientID'
} elseif (-not [string]::IsNullOrEmpty($(Get-GitHubContextSetting -Name 'AuthClientID'))) {
Write-Verbose "Reusing previously stored ClientID: [$(Get-GitHubContextSetting -Name 'AuthClientID')]"
$authClientID = Get-GitHubContextSetting -Name 'AuthClientID'
} else {
Write-Verbose "Using default ClientID: [$($script:Auth.$Mode.ClientID)]"
$authClientID = $script:Auth.$Mode.ClientID
}
if ($Mode -ne (Get-GitHubConfig -Name 'DeviceFlowType' -ErrorAction SilentlyContinue)) {
if ($Mode -ne (Get-GitHubContextSetting -Name 'DeviceFlowType' -ErrorAction SilentlyContinue)) {
Write-Verbose "Using $Mode authentication..."
$tokenResponse = Invoke-GitHubDeviceFlowLogin -ClientID $authClientID -Scope $Scope -HostName $HostName
} else {
$accessTokenValidity = [datetime](Get-GitHubConfig -Name 'TokenExpirationDate') - (Get-Date)
$accessTokenValidity = [datetime](Get-GitHubContextSetting -Name 'TokenExpirationDate') - (Get-Date)
$accessTokenIsValid = $accessTokenValidity.Seconds -gt 0
$hours = $accessTokenValidity.Hours.ToString().PadLeft(2, '0')
$minutes = $accessTokenValidity.Minutes.ToString().PadLeft(2, '0')
Expand All @@ -204,17 +204,17 @@
Write-Host 'âš  ' -ForegroundColor Yellow -NoNewline
Write-Host "Access token remaining validity $accessTokenValidityText. Refreshing access token..."
}
$tokenResponse = Invoke-GitHubDeviceFlowLogin -ClientID $authClientID -RefreshToken (Get-GitHubConfig -Name 'RefreshToken') -HostName $HostName
$tokenResponse = Invoke-GitHubDeviceFlowLogin -ClientID $authClientID -RefreshToken (Get-GitHubContextSetting -Name 'RefreshToken') -HostName $HostName
}
} else {
$refreshTokenValidity = [datetime](Get-GitHubConfig -Name 'RefreshTokenExpirationDate') - (Get-Date)
$refreshTokenValidity = [datetime](Get-GitHubContextSetting -Name 'RefreshTokenExpirationDate') - (Get-Date)
$refreshTokenIsValid = $refreshTokenValidity.Seconds -gt 0
if ($refreshTokenIsValid) {
if (-not $Silent) {
Write-Host 'âš  ' -ForegroundColor Yellow -NoNewline
Write-Host 'Access token expired. Refreshing access token...'
}
$tokenResponse = Invoke-GitHubDeviceFlowLogin -ClientID $authClientID -RefreshToken (Get-GitHubConfig -Name 'RefreshToken') -HostName $HostName
$tokenResponse = Invoke-GitHubDeviceFlowLogin -ClientID $authClientID -RefreshToken (Get-GitHubContextSetting -Name 'RefreshToken') -HostName $HostName
} else {
Write-Verbose "Using $Mode authentication..."
$tokenResponse = Invoke-GitHubDeviceFlowLogin -ClientID $authClientID -Scope $Scope -HostName $HostName
Expand Down

0 comments on commit 2bbb323

Please sign in to comment.