@@ -4,26 +4,28 @@ Usage Examples
44Encoding & Decoding Tokens with HS256
55-------------------------------------
66
7- .. code-block :: python
8-
9- >> import jwt
10- >> key = ' secret'
11- >> encoded = jwt.encode({' some' : ' payload' }, key, algorithm = ' HS256' )
12- ' eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzb21lIjoicGF5bG9hZCJ9.4twFt5NiznN84AWoo1d7KO1T_yoc0Z6XOpOVswacPZg'
13- >> decoded = jwt.decode(encoded, key, algorithms = ' HS256' )
7+ .. code-block :: pycon
8+
9+ >>> import jwt
10+ >>> key = "secret"
11+ >>> encoded = jwt.encode({"some": "payload"}, key, algorithm="HS256")
12+ >>> print(encoded)
13+ eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzb21lIjoicGF5bG9hZCJ9.4twFt5NiznN84AWoo1d7KO1T_yoc0Z6XOpOVswacPZg
14+ >>> jwt.decode(encoded, key, algorithms="HS256")
1415 {'some': 'payload'}
1516
1617 Encoding & Decoding Tokens with RS256 (RSA)
1718-------------------------------------------
1819
19- .. code-block :: python
20+ .. code-block :: pycon
2021
21- >> import jwt
22- >> private_key = b ' -----BEGIN PRIVATE KEY-----\n MIGEAgEAMBAGByqGSM49AgEGBS...'
23- >> public_key = b ' -----BEGIN PUBLIC KEY-----\n MHYwEAYHKoZIzj0CAQYFK4EEAC...'
24- >> encoded = jwt.encode({' some' : ' payload' }, private_key, algorithm = ' RS256' )
25- ' eyJhbGciOiJIU...'
26- >> decoded = jwt.decode(encoded, public_key, algorithms = [' RS256' ])
22+ >>> import jwt
23+ >>> private_key = b"-----BEGIN PRIVATE KEY-----\nMIGEAgEAMBAGByqGSM49AgEGBS..."
24+ >>> public_key = b"-----BEGIN PUBLIC KEY-----\nMHYwEAYHKoZIzj0CAQYFK4EEAC..."
25+ >>> encoded = jwt.encode({"some": "payload"}, private_key, algorithm="RS256")
26+ >>> print(encoded)
27+ eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzb21lIjoicGF5bG9hZCJ9.4twFt5NiznN84AWoo1d7KO1T_yoc0Z6XOpOVswacPZg
28+ >>> decoded = jwt.decode(encoded, public_key, algorithms=["RS256"])
2729 {'some': 'payload'}
2830
2931 If your private key needs a passphrase, you need to pass in a ``PrivateKey `` object from ``cryptography ``.
@@ -33,20 +35,21 @@ If your private key needs a passphrase, you need to pass in a ``PrivateKey`` obj
3335 from cryptography.hazmat.primitives import serialization
3436 from cryptography.hazmat.backends import default_backend
3537
36- pem_bytes = b ' -----BEGIN PRIVATE KEY-----\n MIGEAgEAMBAGByqGSM49AgEGBS...'
37- passphrase = b ' your password'
38+ pem_bytes = b " -----BEGIN PRIVATE KEY-----\n MIGEAgEAMBAGByqGSM49AgEGBS..."
39+ passphrase = b " your password"
3840
3941 private_key = serialization.load_pem_private_key(
40- pem_bytes, password = passphrase, backend = default_backend())
41- encoded = jwt.encode({' some' : ' payload' }, private_key, algorithm = ' RS256' )
42+ pem_bytes, password = passphrase, backend = default_backend()
43+ )
44+ encoded = jwt.encode({" some" : " payload" }, private_key, algorithm = " RS256" )
4245
4346
4447 Specifying Additional Headers
4548-----------------------------
4649
47- .. code-block :: python
50+ .. code-block :: pycon
4851
49- >> jwt.encode({' some' : ' payload' }, ' secret' , algorithm = ' HS256' , headers = {' kid' : ' 230498151c214b788dd97f22b85410a5' })
52+ >>> jwt.encode({" some": " payload" }, " secret" , algorithm=" HS256" , headers={" kid": " 230498151c214b788dd97f22b85410a5" })
5053 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IjIzMDQ5ODE1MWMyMTRiNzg4ZGQ5N2YyMmI4NTQxMGE1In0.eyJzb21lIjoicGF5bG9hZCJ9.DogbDGmMHgA_bU05TAB-R6geQ2nMU2BRM-LnYEtefwg'
5154
5255
@@ -61,10 +64,10 @@ Note: It is generally ill-advised to use this functionality unless you
6164clearly understand what you are doing. Without digital signature information,
6265the integrity or authenticity of the claimset cannot be trusted.
6366
64- .. code-block :: python
67+ .. code-block :: pycon
6568
66- >> jwt.decode(encoded, options = {" verify_signature" : False })
67- {u ' some' : u ' payload' }
69+ >>> jwt.decode(encoded, options={"verify_signature": False})
70+ {'some': 'payload'}
6871
6972 Reading Headers without Validation
7073----------------------------------
@@ -75,10 +78,10 @@ way of knowing in advance which one of the issuer's public keys or shared
7578secrets to use for validation, the issuer may include an identifier for the
7679key in the header.
7780
78- .. code-block :: python
81+ .. code-block :: pycon
7982
80- >> jwt.get_unverified_header(encoded)
81- {u ' alg' : u ' RS256' , u ' typ' : u ' JWT' , u ' kid' : u ' key-id-12345...' }
83+ >>> jwt.get_unverified_header(encoded)
84+ {'alg': 'RS256', 'typ': 'JWT', 'kid': 'key-id-12345...'}
8285
8386 Registered Claim Names
8487----------------------
@@ -108,18 +111,19 @@ datetime, which will be converted into an int. For example:
108111
109112.. code-block :: python
110113
111- jwt.encode({' exp' : 1371720939 }, ' secret' )
112- jwt.encode({' exp' : datetime.utcnow()}, ' secret' )
114+ jwt.encode({" exp" : 1371720939 }, " secret" )
115+ jwt.encode({" exp" : datetime.utcnow()}, " secret" )
113116
114117 Expiration time is automatically verified in `jwt.decode() ` and raises
115118`jwt.ExpiredSignatureError ` if the expiration time is in the past:
116119
117120.. code-block :: python
118121
119122 try :
120- jwt.decode(' JWT_STRING' , ' secret' , algorithms = [' HS256' ])
123+ jwt.decode(" JWT_STRING" , " secret" , algorithms = [" HS256" ])
121124 except jwt.ExpiredSignatureError:
122125 # Signature has expired
126+ ...
123127
124128 Expiration time will be compared to the current UTC time (as given by
125129`timegm(datetime.utcnow().utctimetuple()) `), so be sure to use a UTC timestamp
@@ -135,22 +139,24 @@ you can set a leeway of 10 seconds in order to have some margin:
135139
136140.. code-block :: python
137141
138- jwt_payload = jwt.encode({
139- ' exp' : datetime.datetime.utcnow() + datetime.timedelta(seconds = 30 )
140- }, ' secret ' )
142+ jwt_payload = jwt.encode(
143+ { " exp" : datetime.datetime.utcnow() + datetime.timedelta(seconds = 30 )}, " secret "
144+ )
141145
142146 time.sleep(32 )
143147
144148 # JWT payload is now expired
145149 # But with some leeway, it will still validate
146- jwt.decode(jwt_payload, ' secret' , leeway = 10 , algorithms = [' HS256' ])
150+ jwt.decode(jwt_payload, " secret" , leeway = 10 , algorithms = [" HS256" ])
147151
148152 Instead of specifying the leeway as a number of seconds, a `datetime.timedelta `
149153instance can be used. The last line in the example above is equivalent to:
150154
151155.. code-block :: python
152156
153- jwt.decode(jwt_payload, ' secret' , leeway = datetime.timedelta(seconds = 10 ), algorithms = [' HS256' ])
157+ jwt.decode(
158+ jwt_payload, " secret" , leeway = datetime.timedelta(seconds = 10 ), algorithms = [" HS256" ]
159+ )
154160
155161 Not Before Time Claim (nbf)
156162~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -167,8 +173,8 @@ The `nbf` claim works similarly to the `exp` claim above.
167173
168174.. code-block :: python
169175
170- jwt.encode({' nbf' : 1371720939 }, ' secret' )
171- jwt.encode({' nbf' : datetime.utcnow()}, ' secret' )
176+ jwt.encode({" nbf" : 1371720939 }, " secret" )
177+ jwt.encode({" nbf" : datetime.utcnow()}, " secret" )
172178
173179 Issuer Claim (iss)
174180~~~~~~~~~~~~~~~~~~
@@ -180,13 +186,10 @@ Issuer Claim (iss)
180186
181187.. code-block :: python
182188
183- payload = {
184- ' some' : ' payload' ,
185- ' iss' : ' urn:foo'
186- }
189+ payload = {" some" : " payload" , " iss" : " urn:foo" }
187190
188- token = jwt.encode(payload, ' secret' )
189- decoded = jwt.decode(token, ' secret' , issuer = ' urn:foo' , algorithms = [' HS256' ])
191+ token = jwt.encode(payload, " secret" )
192+ decoded = jwt.decode(token, " secret" , issuer = " urn:foo" , algorithms = [" HS256" ])
190193
191194 If the issuer claim is incorrect, `jwt.InvalidIssuerError ` will be raised.
192195
@@ -205,39 +208,32 @@ sensitive strings, each containing a StringOrURI value.
205208
206209.. code-block :: python
207210
208- payload = {
209- ' some' : ' payload' ,
210- ' aud' : [' urn:foo' , ' urn:bar' ]
211- }
211+ payload = {" some" : " payload" , " aud" : [" urn:foo" , " urn:bar" ]}
212212
213- token = jwt.encode(payload, ' secret' )
214- decoded = jwt.decode(token, ' secret' , audience = ' urn:foo' , algorithms = [' HS256' ])
213+ token = jwt.encode(payload, " secret" )
214+ decoded = jwt.decode(token, " secret" , audience = " urn:foo" , algorithms = [" HS256" ])
215215
216216 In the special case when the JWT has one audience, the "aud" value MAY be
217217a single case-sensitive string containing a StringOrURI value.
218218
219219.. code-block :: python
220220
221- payload = {
222- ' some' : ' payload' ,
223- ' aud' : ' urn:foo'
224- }
221+ payload = {" some" : " payload" , " aud" : " urn:foo" }
225222
226- token = jwt.encode(payload, ' secret' )
227- decoded = jwt.decode(token, ' secret' , audience = ' urn:foo' , algorithms = [' HS256' ])
223+ token = jwt.encode(payload, " secret" )
224+ decoded = jwt.decode(token, " secret" , audience = " urn:foo" , algorithms = [" HS256" ])
228225
229226 If multiple audiences are accepted, the ``audience `` parameter for
230227``jwt.decode `` can also be an iterable
231228
232229.. code-block :: python
233230
234- payload = {
235- ' some' : ' payload' ,
236- ' aud' : ' urn:foo'
237- }
231+ payload = {" some" : " payload" , " aud" : " urn:foo" }
238232
239- token = jwt.encode(payload, ' secret' )
240- decoded = jwt.decode(token, ' secret' , audience = [' urn:foo' , ' urn:bar' ], algorithms = [' HS256' ])
233+ token = jwt.encode(payload, " secret" )
234+ decoded = jwt.decode(
235+ token, " secret" , audience = [" urn:foo" , " urn:bar" ], algorithms = [" HS256" ]
236+ )
241237
242238 The interpretation of audience values is generally application specific.
243239Use of this claim is OPTIONAL.
@@ -255,15 +251,15 @@ Issued At Claim (iat)
255251
256252.. code-block :: python
257253
258- jwt.encode({' iat' : 1371720939 }, ' secret' )
259- jwt.encode({' iat' : datetime.utcnow()}, ' secret' )
254+ jwt.encode({" iat" : 1371720939 }, " secret" )
255+ jwt.encode({" iat" : datetime.utcnow()}, " secret" )
260256
261257 Requiring Presence of Claims
262258----------------------------
263259
264260If you wish to require one or more claims to be present in the claimset, you can set the ``require `` paramenter to include these claims.
265261
266- .. code-block :: python
262+ .. code-block :: pycon
267263
268- >> jwt.decode(encoded, options = {' require' : [' exp' , ' iss' , ' sub' ]})
269- {u ' exp' : 1371720939 , u ' iss' : u ' urn:foo' , u ' sub' : u ' 25c37522-f148-4cbf-8ee6-c4a9718dd0af' }
264+ >>> jwt.decode(encoded, options={" require" : [" exp", " iss", " sub" ]})
265+ {'exp': 1371720939, 'iss': 'urn:foo', 'sub': '25c37522-f148-4cbf-8ee6-c4a9718dd0af'}
0 commit comments