Skip to content

Commit 8b9d506

Browse files
mac2000skedwards88
andauthored
github app: generate jwt with powershell (#30679)
Co-authored-by: Sarah Edwards <skedwards88@github.com>
1 parent ccff40c commit 8b9d506

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

content/apps/creating-github-apps/authenticating-with-a-github-app/generating-a-json-web-token-jwt-for-a-github-app.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,32 @@ signature=$(
175175
JWT="${header_payload}"."${signature}"
176176
printf '%s\n' "JWT: $JWT"
177177
```
178+
179+
### Example: Using PowerShell to generate a JWT
180+
181+
In the following example, replace `YOUR_PATH_TO_PEM` with the file path where your private key is stored. Replace `YOUR_APP_ID` with the ID of your app. Make sure to enclose the values for `YOUR_PATH_TO_PEM` in double quotes.
182+
183+
```powershell copy
184+
#!/usr/bin/env pwsh
185+
186+
$app_id = YOUR_APP_ID
187+
$private_key_path = "YOUR_PATH_TO_PEM"
188+
189+
$header = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes((ConvertTo-Json -InputObject @{
190+
alg = "RS256"
191+
typ = "JWT"
192+
}))).TrimEnd('=').Replace('+', '-').Replace('/', '_');
193+
194+
$payload = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes((ConvertTo-Json -InputObject @{
195+
iat = [System.DateTimeOffset]::UtcNow.AddSeconds(-10).ToUnixTimeSeconds()
196+
exp = [System.DateTimeOffset]::UtcNow.AddMinutes(10).ToUnixTimeSeconds()
197+
iss = $app_id
198+
}))).TrimEnd('=').Replace('+', '-').Replace('/', '_');
199+
200+
$rsa = [System.Security.Cryptography.RSA]::Create()
201+
$rsa.ImportFromPem((Get-Content $private_key_path -Raw))
202+
203+
$signature = [Convert]::ToBase64String($rsa.SignData([System.Text.Encoding]::UTF8.GetBytes("$header.$payload"), [System.Security.Cryptography.HashAlgorithmName]::SHA256, [System.Security.Cryptography.RSASignaturePadding]::Pkcs1)).TrimEnd('=').Replace('+', '-').Replace('/', '_')
204+
$jwt = "$header.$payload.$signature"
205+
Write-Host $jwt
206+
```

0 commit comments

Comments
 (0)