Skip to content

Commit 8cc07ca

Browse files
MikaelaMcGrathCopilotmchammer01
authored
Add conditional for client ID to GHES 3.18 (#58606)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: mc <42146119+mchammer01@users.noreply.github.com>
1 parent 2a37557 commit 8cc07ca

File tree

2 files changed

+36
-15
lines changed

2 files changed

+36
-15
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: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Your JWT must be signed using the `RS256` algorithm and must contain the followi
2020
|---|---|---|
2121
|`iat`| Issued At | The time that the JWT was created. To protect against clock drift, we recommend that you set this 60 seconds in the past and ensure that your server's date and time is set accurately (for example, by using the Network Time Protocol). |
2222
|`exp`| Expires At | The expiration time of the JWT, after which it can't be used to request an installation token. The time must be no more than 10 minutes into the future. |
23-
|`iss`| Issuer | The client ID or application ID of your {% data variables.product.prodname_github_app %}. This value is used to find the right public key to verify the signature of the JWT. You can find your app's IDs on the settings page for your {% data variables.product.prodname_github_app %}. Use of the client ID is recommended. For more information about navigating to the settings page for your {% data variables.product.prodname_github_app %}, see [AUTOTITLE](/apps/maintaining-github-apps/modifying-a-github-app-registration#navigating-to-your-github-app-settings).|
23+
|`iss`| Issuer | The {% ifversion client-id-for-app %}client ID or {% endif %}application ID of your {% data variables.product.prodname_github_app %}. This value is used to find the right public key to verify the signature of the JWT. You can find your app's ID{% ifversion client-id-for-app %}s{% endif %} on the settings page for your {% data variables.product.prodname_github_app %}.{% ifversion client-id-for-app %} Use of the client ID is recommended.{% endif %} For more information about navigating to the settings page for your {% data variables.product.prodname_github_app %}, see [AUTOTITLE](/apps/maintaining-github-apps/modifying-a-github-app-registration#navigating-to-your-github-app-settings).|
2424
|`alg`| Message authentication code algorithm | This should be `RS256` since your JWT must be signed using the `RS256` algorithm. |
2525

2626
To use a JWT, pass it in the `Authorization` header of an API request. For example:
@@ -47,7 +47,7 @@ Most programming languages have a package that can generate a JWT. In all cases,
4747
> [!NOTE]
4848
> You must run `gem install jwt` to install the `jwt` package in order to use this script.
4949
50-
In the following example, replace `YOUR_PATH_TO_PEM` with the file path where your private key is stored. Replace `YOUR_CLIENT_ID` with the ID of your app. Make sure to enclose the values for `YOUR_PATH_TO_PEM` and `YOUR_CLIENT_ID` in double quotes.
50+
In the following example, replace `YOUR_PATH_TO_PEM` with the file path where your private key is stored. Replace {% ifversion client-id-for-app %}`YOUR_CLIENT_ID`{% else %}`YOUR_APP_ID`{% endif %} with the ID of your app. Make sure to enclose the values for `YOUR_PATH_TO_PEM` and {% ifversion client-id-for-app %}`YOUR_CLIENT_ID`{% else %}`YOUR_APP_ID`{% endif %} in double quotes.
5151

5252
```ruby
5353
require 'openssl'
@@ -63,10 +63,11 @@ payload = {
6363
iat: Time.now.to_i - 60,
6464
# JWT expiration time (10 minute maximum)
6565
exp: Time.now.to_i + (10 * 60),
66-
66+
{% ifversion client-id-for-app %}
6767
# {% data variables.product.prodname_github_app %}'s client ID
68-
iss: "YOUR_CLIENT_ID"
69-
68+
iss: "YOUR_CLIENT_ID"{% else %}
69+
# {% data variables.product.prodname_github_app %}'s app ID
70+
iss: "YOUR_APP_ID"{% endif %}
7071
}
7172

7273
jwt = JWT.encode(payload, private_key, "RS256")
@@ -92,12 +93,19 @@ if len(sys.argv) > 1:
9293
else:
9394
pem = input("Enter path of private PEM file: ")
9495

96+
{% ifversion client-id-for-app %}
9597
# Get the Client ID
9698
if len(sys.argv) > 2:
9799
client_id = sys.argv[2]
98100
else:
99101
client_id = input("Enter your Client ID: ")
100-
102+
{% else %}
103+
# Get the App ID
104+
if len(sys.argv) > 2:
105+
app_id = sys.argv[2]
106+
else:
107+
app_id = input("Enter your APP ID: ")
108+
{% endif %}
101109

102110
# Open PEM
103111
with open(pem, 'rb') as pem_file:
@@ -108,9 +116,11 @@ payload = {
108116
'iat': int(time.time()),
109117
# JWT expiration time (10 minutes maximum)
110118
'exp': int(time.time()) + 600,
111-
119+
{% ifversion client-id-for-app %}
112120
# {% data variables.product.prodname_github_app %}'s client ID
113-
'iss': client_id
121+
'iss': client_id{% else %}
122+
# {% data variables.product.prodname_github_app %}'s app ID
123+
'iss': app_id{% endif %}
114124

115125
}
116126

@@ -125,14 +135,16 @@ This script will prompt you for the file path where your private key is stored a
125135
### Example: Using Bash to generate a JWT
126136

127137
> [!NOTE]
128-
> You must pass your Client ID and the file path where your private key is stored as arguments when running this script.
138+
> You must pass your {% ifversion client-id-for-app %}Client ID{% else %}App ID{% endif %} and the file path where your private key is stored as arguments when running this script.
129139

130140
```bash copy
131141
#!/usr/bin/env bash
132142

133-
set -o pipefail
143+
{% ifversion client-id-for-app %}
134144
client_id=$1 # Client ID as first argument
135-
145+
{% else %}
146+
app_id=$1 # App ID as first argument
147+
{% endif %}
136148
pem=$( cat $2 ) # file path of the private key as second argument
137149

138150
now=$(date +%s)
@@ -151,7 +163,7 @@ header=$( echo -n "${header_json}" | b64enc )
151163
payload_json="{
152164
\"iat\":${iat},
153165
\"exp\":${exp},
154-
\"iss\":\"${client_id}\"
166+
{% ifversion client-id-for-app %}\"iss\":\"${client_id}\"{% else %}\"iss\":\"${app_id}\"{% endif %}
155167
}"
156168
# Payload encode
157169
payload=$( echo -n "${payload_json}" | b64enc )
@@ -170,13 +182,16 @@ printf '%s\n' "JWT: $JWT"
170182

171183
### Example: Using PowerShell to generate a JWT
172184

173-
In the following example, replace `YOUR_PATH_TO_PEM` with the file path where your private key is stored. Replace `YOUR_CLIENT_ID` with the ID of your app. Make sure to enclose the values for `YOUR_PATH_TO_PEM` in double quotes.
185+
In the following example, replace `YOUR_PATH_TO_PEM` with the file path where your private key is stored. Replace {% ifversion client-id-for-app %}`YOUR_CLIENT_ID`{% else %}`YOUR_APP_ID`{% endif %} with the ID of your app. Make sure to enclose the values for `YOUR_PATH_TO_PEM` in double quotes.
174186

175187
```powershell copy
176188
#!/usr/bin/env pwsh
177189

190+
{% ifversion client-id-for-app %}
178191
$client_id = YOUR_CLIENT_ID
179-
192+
{% else %}
193+
$app_id = YOUR_APP_ID
194+
{% endif %}
180195
$private_key_path = "YOUR_PATH_TO_PEM"
181196

182197
$header = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes((ConvertTo-Json -InputObject @{
@@ -187,7 +202,7 @@ $header = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes((Conve
187202
$payload = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes((ConvertTo-Json -InputObject @{
188203
iat = [System.DateTimeOffset]::UtcNow.AddSeconds(-10).ToUnixTimeSeconds()
189204
exp = [System.DateTimeOffset]::UtcNow.AddMinutes(10).ToUnixTimeSeconds()
190-
iss = $client_id
205+
{% ifversion client-id-for-app %}iss = $client_id{% else %}iss = $app_id{% endif %}
191206
}))).TrimEnd('=').Replace('+', '-').Replace('/', '_');
192207

193208
$rsa = [System.Security.Cryptography.RSA]::Create()
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Reference: #14091
2+
3+
versions:
4+
fpt: '*'
5+
ghec: '*'
6+
ghes: '>=3.18'

0 commit comments

Comments
 (0)