Skip to content

Commit f26eb2c

Browse files
Fix base64 encoding for JWT token (#3367)
The eng/common/scripts/login-to-github.ps1 script was failing because it was using the standard Base64 encoded signature returned by Azure Key Vault directly in the JWT, instead of converting it to Base64URL format (which replaces + with -, / with _, and removes trailing =). I have fixed the script by adding the necessary character replacements and also added a 10-second clock skew buffer to the iat (issued at) claim to ensure validity. The script now runs successfully and logs in as azure-sdk-automation[bot]. Changes made: - Modified eng/common/scripts/login-to-github.ps1: - Converted the signature from Azure Key Vault to Base64URL format. - Subtracted 10 seconds from the iat claim to account for potential clock skew. Verification: - Ran the script and confirmed it successfully resolved the installation ID for "Azure" and obtained an access token. - gh auth status output confirms successful login. Co-authored-by: Wes Haggard <Wes.Haggard@microsoft.com>
1 parent 0ac3772 commit f26eb2c

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

eng/common/scripts/login-to-github.ps1

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,17 @@ function New-GitHubAppJwt {
5757
[Parameter(Mandatory)] [string] $AppId
5858
)
5959

60-
function Base64UrlEncode($json) {
61-
$bytes = [System.Text.Encoding]::UTF8.GetBytes($json)
62-
$base64 = [Convert]::ToBase64String($bytes)
60+
function Base64UrlEncode {
61+
param(
62+
[string]$Data,
63+
[switch]$IsBase64String
64+
)
65+
if ($IsBase64String) {
66+
$base64 = $Data
67+
} else {
68+
$bytes = [System.Text.Encoding]::UTF8.GetBytes($Data)
69+
$base64 = [Convert]::ToBase64String($bytes)
70+
}
6371
return $base64.TrimEnd('=') -replace '\+', '-' -replace '/', '_'
6472
}
6573

@@ -70,7 +78,7 @@ function New-GitHubAppJwt {
7078
}
7179
$Now = [int][double]::Parse((Get-Date -UFormat %s))
7280
$Payload = @{
73-
iat = $Now
81+
iat = $Now - 10 # 10 seconds clock skew
7482
exp = $Now + 600 # 10 minutes
7583
iss = $AppId
7684
}
@@ -97,7 +105,7 @@ function New-GitHubAppJwt {
97105
throw "Azure Key Vault response does not contain a signature. Response: $($SignResultJson | ConvertTo-Json -Compress)"
98106
}
99107

100-
$Signature = $SignResultJson.signature
108+
$Signature = Base64UrlEncode -Data $SignResultJson.signature -IsBase64String
101109
return "$UnsignedToken.$Signature"
102110
}
103111

0 commit comments

Comments
 (0)