From 6e6c8505d5a1a82a3b1a1d54be5f37b8a8947651 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Tue, 5 Nov 2024 23:36:34 +0100 Subject: [PATCH 1/4] Update login prompts to use dynamic hostnames for GitHub login processes --- .../private/Auth/DeviceFlow/Invoke-GitHubDeviceFlowLogin.ps1 | 2 +- src/functions/public/Auth/Connect-GitHubAccount.ps1 | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/functions/private/Auth/DeviceFlow/Invoke-GitHubDeviceFlowLogin.ps1 b/src/functions/private/Auth/DeviceFlow/Invoke-GitHubDeviceFlowLogin.ps1 index 19b3c1bc7..e4b349d3b 100644 --- a/src/functions/private/Auth/DeviceFlow/Invoke-GitHubDeviceFlowLogin.ps1 +++ b/src/functions/private/Auth/DeviceFlow/Invoke-GitHubDeviceFlowLogin.ps1 @@ -55,7 +55,7 @@ Write-Host '! ' -ForegroundColor DarkYellow -NoNewline Write-Host "We added the code to your clipboard: [$userCode]" $userCode | Set-Clipboard - Read-Host 'Press Enter to open github.com in your browser...' + Read-Host "Press Enter to open $HostName in your browser..." Start-Process $verificationUri $tokenResponse = Wait-GitHubAccessToken -DeviceCode $deviceCode -ClientID $ClientID -Interval $interval -HostName $HostName diff --git a/src/functions/public/Auth/Connect-GitHubAccount.ps1 b/src/functions/public/Auth/Connect-GitHubAccount.ps1 index 38f183d33..a1c88acfc 100644 --- a/src/functions/public/Auth/Connect-GitHubAccount.ps1 +++ b/src/functions/public/Auth/Connect-GitHubAccount.ps1 @@ -211,7 +211,7 @@ Write-Verbose 'Logging in using personal access token...' Reset-GitHubConfig -Scope 'Auth' Write-Host '! ' -ForegroundColor DarkYellow -NoNewline - Start-Process 'https://github.com/settings/tokens' + Start-Process "https://$HostName/settings/tokens" $accessTokenValue = Read-Host -Prompt 'Enter your personal access token' -AsSecureString $accessTokenType = (ConvertFrom-SecureString $accessTokenValue -AsPlainText) -replace '_.*$', '_*' if ($accessTokenType -notmatch '^ghp_|^github_pat_') { @@ -229,7 +229,7 @@ break } 'App' { - Write-Verbose 'Logging in using a GitHub App...' + Write-Verbose 'Logging in as a GitHub App...' Reset-GitHubConfig -Scope 'Auth' $jwt = Get-GitHubAppJWT -ClientID $ClientID -PrivateKey $PrivateKey $settings = @{ From e74176729e98da7335d25ada0360eeb5aa943c6c Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Wed, 6 Nov 2024 09:54:15 +0100 Subject: [PATCH 2/4] Add normalization of $HostName --- src/functions/public/Auth/Connect-GitHubAccount.ps1 | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/functions/public/Auth/Connect-GitHubAccount.ps1 b/src/functions/public/Auth/Connect-GitHubAccount.ps1 index a1c88acfc..015b19980 100644 --- a/src/functions/public/Auth/Connect-GitHubAccount.ps1 +++ b/src/functions/public/Auth/Connect-GitHubAccount.ps1 @@ -109,9 +109,11 @@ [Parameter()] [string] $ApiVersion = '2022-11-28', - # The host to connect to. + # The host to connect to. Can use $env:GITHUB_SERVER_URL to set the host, as the protocol is removed automatically. + # Example: github.com, github.enterprise.com, msx.ghe.com [Parameter()] [Alias('Host')] + [Alias('Server')] [uri] $HostName = 'github.com', # Suppresses the output of the function. @@ -122,6 +124,7 @@ [switch] $Silent ) + $HostName = $HostName.Host $ApiBaseUri = "https://api.$HostName" $envVars = Get-ChildItem -Path 'Env:' From c8811bd77da26ba43067c701bf38278f815d761b Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Wed, 6 Nov 2024 09:54:53 +0100 Subject: [PATCH 3/4] Send $env:GITHUB_SERVER_URL as server when signing in on GHA --- .../private/Commands/Initialize-RunnerEnvironment.ps1 | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/functions/private/Commands/Initialize-RunnerEnvironment.ps1 b/src/functions/private/Commands/Initialize-RunnerEnvironment.ps1 index 303d20ca9..d8c13cf71 100644 --- a/src/functions/private/Commands/Initialize-RunnerEnvironment.ps1 +++ b/src/functions/private/Commands/Initialize-RunnerEnvironment.ps1 @@ -23,7 +23,6 @@ $tokenVar = Get-ChildItem -Path 'Env:' | Where-Object Name -In 'GH_TOKEN', 'GITHUB_TOKEN' | Select-Object -First 1 -ExpandProperty Value $tokenVarPresent = $tokenVar.count -gt 0 -and -not [string]::IsNullOrEmpty($tokenVar) if ($tokenVarPresent) { - $HostName = $env:GITHUB_SERVER_URL -replace '^https?://' - Connect-GitHubAccount -Repo $env:GITHUB_REPOSITORY_NAME -Owner $env:GITHUB_REPOSITORY_OWNER -Host $HostName + Connect-GitHubAccount -Repo $env:GITHUB_REPOSITORY_NAME -Owner $env:GITHUB_REPOSITORY_OWNER -Server $env:GITHUB_SERVER_URL } } From 25328bda9fadcf5fe0810db6af2f7668424f46d5 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Wed, 6 Nov 2024 11:13:53 +0100 Subject: [PATCH 4/4] Fix hostname fix --- src/functions/public/Auth/Connect-GitHubAccount.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/functions/public/Auth/Connect-GitHubAccount.ps1 b/src/functions/public/Auth/Connect-GitHubAccount.ps1 index 015b19980..58f885f37 100644 --- a/src/functions/public/Auth/Connect-GitHubAccount.ps1 +++ b/src/functions/public/Auth/Connect-GitHubAccount.ps1 @@ -114,7 +114,7 @@ [Parameter()] [Alias('Host')] [Alias('Server')] - [uri] $HostName = 'github.com', + [string] $HostName = 'github.com', # Suppresses the output of the function. [Parameter()] @@ -124,7 +124,7 @@ [switch] $Silent ) - $HostName = $HostName.Host + $HostName = $HostName -replace '^https?://' $ApiBaseUri = "https://api.$HostName" $envVars = Get-ChildItem -Path 'Env:'