diff --git a/content/apps/creating-github-apps/authenticating-with-a-github-app/generating-a-json-web-token-jwt-for-a-github-app.md b/content/apps/creating-github-apps/authenticating-with-a-github-app/generating-a-json-web-token-jwt-for-a-github-app.md index 487eb7fa4832..7d98b195bfde 100644 --- a/content/apps/creating-github-apps/authenticating-with-a-github-app/generating-a-json-web-token-jwt-for-a-github-app.md +++ b/content/apps/creating-github-apps/authenticating-with-a-github-app/generating-a-json-web-token-jwt-for-a-github-app.md @@ -175,3 +175,32 @@ signature=$( JWT="${header_payload}"."${signature}" printf '%s\n' "JWT: $JWT" ``` + +### Example: Using PowerShell to generate a JWT + +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. + +```powershell copy +#!/usr/bin/env pwsh + +$app_id = YOUR_APP_ID +$private_key_path = "YOUR_PATH_TO_PEM" + +$header = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes((ConvertTo-Json -InputObject @{ + alg = "RS256" + typ = "JWT" +}))).TrimEnd('=').Replace('+', '-').Replace('/', '_'); + +$payload = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes((ConvertTo-Json -InputObject @{ + iat = [System.DateTimeOffset]::UtcNow.AddSeconds(-10).ToUnixTimeSeconds() + exp = [System.DateTimeOffset]::UtcNow.AddMinutes(10).ToUnixTimeSeconds() + iss = $app_id +}))).TrimEnd('=').Replace('+', '-').Replace('/', '_'); + +$rsa = [System.Security.Cryptography.RSA]::Create() +$rsa.ImportFromPem((Get-Content $private_key_path -Raw)) + +$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('/', '_') +$jwt = "$header.$payload.$signature" +Write-Host $jwt +```