<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://aznot.com/index.php?action=history&amp;feed=atom&amp;title=GitHub%2FJWT</id>
	<title>GitHub/JWT - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://aznot.com/index.php?action=history&amp;feed=atom&amp;title=GitHub%2FJWT"/>
	<link rel="alternate" type="text/html" href="https://aznot.com/index.php?title=GitHub/JWT&amp;action=history"/>
	<updated>2026-04-17T04:30:47Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://aznot.com/index.php?title=GitHub/JWT&amp;diff=6316&amp;oldid=prev</id>
		<title>Kenneth at 01:15, 6 July 2023</title>
		<link rel="alternate" type="text/html" href="https://aznot.com/index.php?title=GitHub/JWT&amp;diff=6316&amp;oldid=prev"/>
		<updated>2023-07-06T01:15:55Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== JSON Web Tokens (JWTs) ==&lt;br /&gt;
&lt;br /&gt;
&amp;quot;In order to authenticate as an app or generate an installation access token, you must generate a JSON Web Token (JWT). If a REST API endpoint requires a JWT, the documentation for that endpoint will indicate that you must use a JWT to access the endpoint.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Usage:&lt;br /&gt;
 curl --request GET \&lt;br /&gt;
  --url &amp;quot;https://api.github.com/app&amp;quot; \&lt;br /&gt;
  --header &amp;quot;Accept: application/vnd.github+json&amp;quot; \&lt;br /&gt;
  --header &amp;quot;Authorization: Bearer [YOUR_JWT]&amp;quot; \&lt;br /&gt;
  --header &amp;quot;X-GitHub-Api-Version: 2022-11-28&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Install python jwt:&lt;br /&gt;
 pip install jwt&lt;br /&gt;
&lt;br /&gt;
make_jwt.py:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/env python3&lt;br /&gt;
import jwt&lt;br /&gt;
import time&lt;br /&gt;
import sys&lt;br /&gt;
&lt;br /&gt;
# Get PEM file path&lt;br /&gt;
if len(sys.argv) &amp;gt; 1:&lt;br /&gt;
    pem = sys.argv[1]&lt;br /&gt;
else:&lt;br /&gt;
    pem = input(&amp;quot;Enter path of private PEM file: &amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Get the App ID&lt;br /&gt;
if len(sys.argv) &amp;gt; 2:&lt;br /&gt;
    app_id = sys.argv[2]&lt;br /&gt;
else:&lt;br /&gt;
    app_id = input(&amp;quot;Enter your APP ID: &amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Open PEM&lt;br /&gt;
with open(pem, &amp;#039;rb&amp;#039;) as pem_file:&lt;br /&gt;
    signing_key = jwt.jwk_from_pem(pem_file.read())&lt;br /&gt;
&lt;br /&gt;
payload = {&lt;br /&gt;
    # Issued at time&lt;br /&gt;
    &amp;#039;iat&amp;#039;: int(time.time()),&lt;br /&gt;
    # JWT expiration time (10 minutes maximum)&lt;br /&gt;
    &amp;#039;exp&amp;#039;: int(time.time()) + 600,&lt;br /&gt;
    # GitHub App&amp;#039;s identifier&lt;br /&gt;
    &amp;#039;iss&amp;#039;: app_id&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
# Create JWT&lt;br /&gt;
jwt_instance = jwt.JWT()&lt;br /&gt;
encoded_jwt = jwt_instance.encode(payload, signing_key, alg=&amp;#039;RS256&amp;#039;)&lt;br /&gt;
&lt;br /&gt;
print(f&amp;quot;JWT:  {encoded_jwt}&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
ref: [https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/generating-a-json-web-token-jwt-for-a-github-app]&lt;br /&gt;
&lt;br /&gt;
== Authenticating App ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Notes ==&lt;br /&gt;
&lt;br /&gt;
ref: [https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/authenticating-as-a-github-app-installation#generating-an-installation-access-token]&lt;br /&gt;
&lt;br /&gt;
 Generating a user access token for a GitHub App - GitHub Docs&lt;br /&gt;
 https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/generating-a-user-access-token-for-a-github-app&lt;br /&gt;
&lt;br /&gt;
# GitHub CLI api&lt;br /&gt;
# https://cli.github.com/manual/gh_api&lt;br /&gt;
&lt;br /&gt;
 gh api \&lt;br /&gt;
  --method POST \&lt;br /&gt;
  -H &amp;quot;Accept: application/vnd.github+json&amp;quot; \&lt;br /&gt;
  -H &amp;quot;X-GitHub-Api-Version: 2022-11-28&amp;quot; \&lt;br /&gt;
  /orgs/ORG/actions/runners/registration-token&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 Permissions required for GitHub Apps - GitHub Docs&lt;br /&gt;
 https://docs.github.com/en/rest/overview/permissions-required-for-github-apps?apiVersion=2022-11-28#organization-self-hosted-runners&lt;br /&gt;
&lt;br /&gt;
 fpe-devops/github-app-token: A private copy of tibdex/github-app-token&lt;br /&gt;
 https://github.com/fpe-devops/github-app-token&lt;br /&gt;
&lt;br /&gt;
 tibdex/github-app-token: Impersonate a GitHub App in a GitHub Action&lt;br /&gt;
 https://github.com/tibdex/github-app-token&lt;br /&gt;
&lt;br /&gt;
 Differences between GitHub Apps and OAuth apps - GitHub Docs - Machine vs. bot accounts&lt;br /&gt;
 https://docs.github.com/en/apps/oauth-apps/building-oauth-apps/differences-between-github-apps-and-oauth-apps#machine-vs-bot-accounts&lt;br /&gt;
&lt;br /&gt;
Installation ID:&lt;br /&gt;
 Where can we find GitHub Apps Installation ID? - Stack Overflow&lt;br /&gt;
 https://stackoverflow.com/questions/74462420/where-can-we-find-github-apps-installation-id&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
For GitHub Apps created under Organizations:&lt;br /&gt;
&lt;br /&gt;
Go to the Organization settings&lt;br /&gt;
Click on &amp;#039;GitHub Apps&amp;#039; under &amp;#039;Third-party Access&amp;#039;&lt;br /&gt;
If there are multiple GitHub apps, choose your App and click on &amp;#039;Configure&amp;#039;&lt;br /&gt;
Once your GitHub App is selected check the URL for obtaining &amp;#039;GitHub App Installation ID&amp;#039;&lt;br /&gt;
&lt;br /&gt;
The URL looks like this:&lt;br /&gt;
&lt;br /&gt;
https://github.com/organizations/&amp;lt;Organization-name&amp;gt;/settings/installations/&amp;lt;ID&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Pick the &amp;lt;ID&amp;gt; part and that&amp;#039;s your GitHub App Installation ID.&lt;br /&gt;
&lt;br /&gt;
For GitHub Apps created under Repository, you can find this under repository settings.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
- name: Get Installation Token&lt;br /&gt;
        uses: tibdex/github-app-token@v1&lt;br /&gt;
        id: get_installation_token&lt;br /&gt;
        with: &lt;br /&gt;
          app_id: 335706&lt;br /&gt;
          # installation_id not needed IF the app is installed on this current repo&lt;br /&gt;
          installation_id: 37655626&lt;br /&gt;
          private_key: ${{ secrets.PRIVATE_KEY }}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
make-jwt.py Python:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/env python3&lt;br /&gt;
import jwt&lt;br /&gt;
import time&lt;br /&gt;
import sys&lt;br /&gt;
&lt;br /&gt;
# Get PEM file path&lt;br /&gt;
if len(sys.argv) &amp;gt; 1:&lt;br /&gt;
    pem = sys.argv[1]&lt;br /&gt;
else:&lt;br /&gt;
    pem = input(&amp;quot;Enter path of private PEM file: &amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Get the App ID&lt;br /&gt;
if len(sys.argv) &amp;gt; 2:&lt;br /&gt;
    app_id = sys.argv[2]&lt;br /&gt;
else:&lt;br /&gt;
    app_id = input(&amp;quot;Enter your APP ID: &amp;quot;)&lt;br /&gt;
&lt;br /&gt;
# Open PEM&lt;br /&gt;
with open(pem, &amp;#039;rb&amp;#039;) as pem_file:&lt;br /&gt;
    signing_key = jwt.jwk_from_pem(pem_file.read())&lt;br /&gt;
&lt;br /&gt;
payload = {&lt;br /&gt;
    # Issued at time&lt;br /&gt;
    &amp;#039;iat&amp;#039;: int(time.time()),&lt;br /&gt;
    # JWT expiration time (10 minutes maximum)&lt;br /&gt;
    &amp;#039;exp&amp;#039;: int(time.time()) + 600,&lt;br /&gt;
    # GitHub App&amp;#039;s identifier&lt;br /&gt;
    &amp;#039;iss&amp;#039;: app_id&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
# Create JWT&lt;br /&gt;
jwt_instance = jwt.JWT()&lt;br /&gt;
encoded_jwt = jwt_instance.encode(payload, signing_key, alg=&amp;#039;RS256&amp;#039;)&lt;br /&gt;
&lt;br /&gt;
#print(f&amp;quot;JWT:  {encoded_jwt}&amp;quot;)&lt;br /&gt;
print(f&amp;quot;{encoded_jwt}&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Generate GitHub App Installation Token:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#! /bin/tcsh&lt;br /&gt;
&lt;br /&gt;
set PRIVATE_KEY=&amp;#039;create-repos.2023-05-26.private-key.pem&amp;#039;&lt;br /&gt;
set APP_ID=&amp;#039;339112&amp;#039;&lt;br /&gt;
set INSTALLATION_ID=&amp;#039;37930554&amp;#039; # In the URL when you configure the app&lt;br /&gt;
set JWT=`./make-jwt.py $PRIVATE_KEY $APP_ID`&lt;br /&gt;
set INSTALLATION_TOKEN=`curl -s --request POST --url &amp;quot;https://api.github.com/app/installations/$INSTALLATION_ID/access_tokens&amp;quot; --header &amp;quot;Accept: application/vnd.github+json&amp;quot; --header &amp;quot;Authorization: Bearer $JWT&amp;quot; --header &amp;quot;X-GitHub-Api-Version: 2022-11-28&amp;quot; | python -c &amp;quot;import sys, json; print(json.load(sys.stdin)[&amp;#039;token&amp;#039;])&amp;quot;`&lt;br /&gt;
setenv GH_TOKEN $INSTALLATION_TOKEN&lt;br /&gt;
echo GH_TOKEN: $GH_TOKEN&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
---&lt;br /&gt;
&lt;br /&gt;
 JSON Web Tokens - jwt.io&lt;br /&gt;
 https://jwt.io/&lt;br /&gt;
 JSON Web Tokens are an open, industry standard RFC 7519 method for representing claims securely between two parties.&lt;br /&gt;
&lt;br /&gt;
---&lt;br /&gt;
&lt;br /&gt;
 Generating a JSON Web Token (JWT) for a GitHub App - GitHub Docs&lt;br /&gt;
 https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/generating-a-json-web-token-jwt-for-a-github-app#about-json-web-tokens-jwts&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
curl --request GET \&lt;br /&gt;
--url &amp;quot;https://api.github.com/app&amp;quot; \&lt;br /&gt;
--header &amp;quot;Accept: application/vnd.github+json&amp;quot; \&lt;br /&gt;
--header &amp;quot;Authorization: Bearer YOUR_JWT&amp;quot; \&lt;br /&gt;
--header &amp;quot;X-GitHub-Api-Version: 2022-11-28&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
---&lt;br /&gt;
&lt;br /&gt;
[6/2 3:15 PM] Khaled El Hussein&lt;br /&gt;
&lt;br /&gt;
 GitHub App documentation is here: https://docs.github.com/en/apps/overview&lt;br /&gt;
 &lt;br /&gt;
 Creating an app (it requires Org Owner which I can help with): https://docs.github.com/en/apps/creating-github-apps/setting-up-a-github-app/creating-a-github-app&lt;br /&gt;
 &lt;br /&gt;
 Permissions: https://docs.github.com/en/apps/creating-github-apps/setting-up-a-github-app/choosing-permissions-for-a-github-app&lt;br /&gt;
 &lt;br /&gt;
 Authenticate your system/Jenkins/Tool to this github app, couple of options:     &lt;br /&gt;
&lt;br /&gt;
 Using SDK (in javascript) https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/authenticating-as-a-github-app-installation#using-octokitjs-to-authenticate-with-an-installation-id&lt;br /&gt;
 &lt;br /&gt;
 JWT Token for Rest APIs: https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/authenticating-as-a-github-app-installation#using-an-installation-access-token-to-authenticate-as-an-app-installation&lt;br /&gt;
 &lt;br /&gt;
---&lt;br /&gt;
&lt;br /&gt;
 Self-hosted runners - GitHub Docs - Create a registration token for an organization&lt;br /&gt;
 https://docs.github.com/en/rest/actions/self-hosted-runners?apiVersion=2022-11-28#create-a-registration-token-for-an-organization&lt;/div&gt;</summary>
		<author><name>Kenneth</name></author>
	</entry>
</feed>