{"id":20511015,"url":"https://github.com/sandipb/using-python-jwt","last_synced_at":"2025-08-27T14:24:45.120Z","repository":{"id":221588696,"uuid":"754815457","full_name":"sandipb/using-python-jwt","owner":"sandipb","description":"Sample code for encoding/decoding JWTs in Python","archived":false,"fork":false,"pushed_at":"2024-02-08T20:29:47.000Z","size":16,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-06-27T22:53:10.899Z","etag":null,"topics":["jwt","pyjwt","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sandipb.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2024-02-08T20:19:34.000Z","updated_at":"2024-02-08T20:23:09.000Z","dependencies_parsed_at":"2024-02-08T22:37:01.244Z","dependency_job_id":null,"html_url":"https://github.com/sandipb/using-python-jwt","commit_stats":null,"previous_names":["sandipb/using-python-jwt"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/sandipb/using-python-jwt","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sandipb%2Fusing-python-jwt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sandipb%2Fusing-python-jwt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sandipb%2Fusing-python-jwt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sandipb%2Fusing-python-jwt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sandipb","download_url":"https://codeload.github.com/sandipb/using-python-jwt/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sandipb%2Fusing-python-jwt/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262811453,"owners_count":23368112,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["jwt","pyjwt","python"],"created_at":"2024-11-15T20:32:27.865Z","updated_at":"2025-06-30T16:39:06.021Z","avatar_url":"https://github.com/sandipb.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# How to use Python for encoding and decoding JWTs\n\nThis is a simple example of how to use Python to encode and decode JWTs. It uses the `PyJWT` library for encoding and decoding JWTs.\nThis demonstrates using both OpenSSH and OpenSSL keys for signing JWTs.\n\n## Pre-requisites\n\n```shell-session\npip install pyjwt[crypto]\n```\n\n## Generate some keys to be used for signing JWTs\n\nThis will generate some test keys for shared secret and RSA keys from OpenSSH and OpenSSL.\n\n```shell-session\n$ make genkeys\n\n*** Generating RSA keys using openssl\n\nrm -f rsa-private.pem rsa-public.pem\nopenssl genrsa -out rsa-private.pem 2048\nopenssl rsa -in rsa-private.pem -pubout -outform PEM  -out rsa-public.pem\nwriting RSA key\n\n*** Generating RSA keys using ssh-keygen\n\nrm -f id_rsa id_rsa.pub\nssh-keygen -t rsa -f id_rsa -N \"\"\nGenerating public/private rsa key pair.\nYour identification has been saved in id_rsa\nYour public key has been saved in id_rsa.pub\nThe key fingerprint is:\nSHA256:vJoObruvvbyNNxmzfN4Ug4Hrc3f4s3y8AycmMkFCR14 sbhattacharya@somehost\nThe key's randomart image is:\n+---[RSA 3072]----+\n|      ...o E     |\n|       .oo.      |\n|        +..      |\n|       . o o     |\n|        S o o    |\n|       .o+ . B . |\n|    .  .+=+ * =. |\n|   ..+ =*o.+ +..o|\n|   .=*@+.+. . +=o|\n+----[SHA256]-----+\n\n*** Generating shared secret\n\nrm -f shared-secret\nopenssl rand -hex 32 \u003e shared-secret\n```\n\n## Running the code\n\n```shell-session\n$ ./jwt-test.py \n*** Sample payload:\n{'iss': 'jwt-test.py', 'sub': 'sbhattacharya', 'name': 'Sandip Bhattacharya', 'nickname': 'sbhattacharya', 'aud': 'https://example.com', 'exp': 1707426737, 'iat': 1707423137}\n\n*** Token signed by shared secret:\neyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJqd3QtdGVzdC5weSIsInN1YiI6InNiaGF0dGFjaGFyeWEiLCJuYW1lIjoiU2FuZGlwIEJoYXR0YWNoYXJ5YSIsIm5pY2tuYW1lIjoic2JoYXR0YWNoYXJ5YSIsImF1ZCI6Imh0dHBzOi8vZXhhbXBsZS5jb20iLCJleHAiOjE3MDc0MjY3MzcsImlhdCI6MTcwNzQyMzEzN30.o-XdNSEE0FmViPdGi5mex6cZ9MfGEugdexONpppgAlc\n\n*** Token signed by RSA key(OPENSSH):\neyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJqd3QtdGVzdC5weSIsInN1YiI6InNiaGF0dGFjaGFyeWEiLCJuYW1lIjoiU2FuZGlwIEJoYXR0YWNoYXJ5YSIsIm5pY2tuYW1lIjoic2JoYXR0YWNoYXJ5YSIsImF1ZCI6Imh0dHBzOi8vZXhhbXBsZS5jb20iLCJleHAiOjE3MDc0MjY3MzcsImlhdCI6MTcwNzQyMzEzN30.sGtgRqv2dXfC2m6ehV_uXX-wYl2UYhYMXK5saV6Dmum0xiYbHPCdEoDDk5X8I_WTlHL0FPwnbq47t10ky0CXoSWFOyE4hetl7vPJf-KFn1T962DDI1MjbJZluIiYXqTQ3HhMQ8GokxtAxWf63dxZDIqOXpEg41Vj-gmpxe1BX0AZV0vVPSCJHVZbrrNzrYFBs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WEiLCJuYW1lIjoiU2FuZGlwIEJoYXR0YWNoYXJ5YSIsIm5pY2tuYW1lIjoic2JoYXR0YWNoYXJ5YSIsImF1ZCI6Imh0dHBzOi8vZXhhbXBsZS5jb20iLCJleHAiOjE3MDc0MjY3MzcsImlhdCI6MTcwNzQyMzEzN30.o-XdNSEE0FmViPdGi5mex6cZ9MfGEugdexONpppgAlc\n\n*** [INFO] Token signature algorithm: HS256\n*** [INFO] Token expiration: 2024-02-08 16:12:17-05:00 (in 0:59:52.511284)\n*** Verifying and decoding via shared secret:\n{'iss': 'jwt-test.py', 'sub': 'sbhattacharya', 'name': 'Sandip Bhattacharya', 'nickname': 'sbhattacharya', 'aud': 'https://example.com', 'exp': 1707426737, 'iat': 1707423137}\n\n*** Enter token to verify: eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJqd3QtdGVzdC5weSIsInN1YiI6InNiaGF0dGFjaGFyeWEiLCJuYW1lIjoiU2FuZGlwIEJoYXR0YWNoYXJ5YSIsIm5pY2tuYW1lIjoic2JoYXR0YWNoYXJ5YSIsImF1ZCI6Imh0dHBzOi8vZXhhbXBsZS5jb20iLCJleHAiOjE3MDc0MjY3MzcsImlhdCI6MTcwNzQyMzEzN30.lfCVsa6enUp5prsaycHpkZ30_quW-7XAS4jW02y6sajn4r7XBeZnnt50Krlbhf6DNLq9OX0b4e4v5nlTOR1M7vvNWUs3vvT70osgLcvV21JZf5azNr3AOUkGShKNTfs7L74qzGVircAsvZkNTs_tCdJ_2jKg0UxsDr5AUaDFV4-UYKwfWK8hpu8CSAMBRFZiBc-TRuMiY69MoAXHjfVUnSS0Cj6l3iGsI-WR3C-tUtUHFxLH_e_A-R0YtXGGSH29uWYlBHapYIvLxNq00BpqKZn2EvKXXrQY2-Fcbt8U2QibHbcQlj1-wg5tCNGpHk2Z8-VBFOSzTEMbG9kDO26-7A\n\n*** [INFO] Token signature algorithm: RS256\n*** [INFO] Token expiration: 2024-02-08 16:12:17-05:00 (in 0:59:39.321347)\n*** Verifying and decoding via ssh public key:\nInvalid signature\n\n*** Verifying and decoding via openssl public key:\n{'iss': 'jwt-test.py', 'sub': 'sbhattacharya', 'name': 'Sandip Bhattacharya', 'nickname': 'sbhattacharya', 'aud': 'https://example.com', 'exp': 1707426737, 'iat': 1707423137}\n\n*** Enter token to verify: \n```\n\n## References\n\n- \u003chttps://auth0.com/blog/how-to-handle-jwt-in-python/\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsandipb%2Fusing-python-jwt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsandipb%2Fusing-python-jwt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsandipb%2Fusing-python-jwt/lists"}