1- PyJWT [ ![ Build Status] ( https://travis-ci.org/progrium/pyjwt.png?branch=master )] ( https://travis-ci.org/progrium/pyjwt )
2- =====
1+ # PyJWT [ ![ Build Status] ( https://travis-ci.org/progrium/pyjwt.png?branch=master )] ( https://travis-ci.org/progrium/pyjwt )
2+
33A Python implementation of [ JSON Web Token draft 01] ( http://self-issued.info/docs/draft-jones-json-web-token-01.html ) .
44
5- Installing
6- ----------
5+ ## Installing
76
8- sudo easy_install PyJWT
7+ ```
8+ $ pip install PyJWT
9+ ```
910
1011** Note** : The RSASSA-PKCS1-v1_5 algorithms depend on PyCrypto. If you plan on
1112using any of those algorithms you'll need to install it as well.
1213
13- sudo easy_install PyCrypto
14+ ```
15+ $ pip install PyCrypto
16+ ```
1417
15- Usage
16- -----
18+ ## Usage
1719
18- import jwt
19- jwt.encode({"some": "payload"}, "secret")
20+ ``` python
21+ import jwt
22+ jwt.encode({' some' : ' payload' }, ' secret' )
23+ ```
2024
2125Additional headers may also be specified.
2226
23- jwt.encode({"some": "payload"}, "secret", headers={"kid": "230498151c214b788dd97f22b85410a5"})
27+ ``` python
28+ jwt.encode({' some' : ' payload' }, ' secret' , headers = {' kid' : ' 230498151c214b788dd97f22b85410a5' })
29+ ```
2430
2531Note the resulting JWT will not be encrypted, but verifiable with a secret key.
2632
27- jwt.decode("someJWTstring", "secret")
33+ ``` python
34+ jwt.decode(' someJWTstring' , ' secret' )
35+ ```
2836
2937If the secret is wrong, it will raise a ` jwt.DecodeError ` telling you as such.
3038You can still get the payload by setting the ` verify ` argument to ` False ` .
3139
32- jwt.decode("someJWTstring", verify=False)
40+ ``` python
41+ jwt.decode(' someJWTstring' , verify = False )
42+ ```
3343
34- Algorithms
35- ----------
44+ ## Algorithms
3645
3746The JWT spec supports several algorithms for cryptographic signing. This library
3847currently supports:
@@ -46,29 +55,30 @@ currently supports:
4655
4756Change the algorithm with by setting it in encode:
4857
49- jwt.encode({"some": "payload"}, "secret", "HS512")
58+ ``` python
59+ jwt.encode({' some' : ' payload' }, ' secret' , ' HS512' )
60+ ```
5061
5162When using the RSASSA-PKCS1-v1_5 algorithms, the ` key ` argument in both
5263` jwt.encode() ` and ` jwt.decode() ` (` "secret" ` in the examples) is expected to
5364be an RSA private key as imported with ` Crypto.PublicKey.RSA.importKey() ` .
5465
55- Tests
56- -----
66+ ## Tests
5767
5868You can run tests from the project root after cloning with:
5969
60- python tests/test_jwt.py
70+ ```
71+ $ python tests/test_jwt.py
72+ ```
6173
62- Support of reserved claim names
63- -------------------------------
74+ ## Support of reserved claim names
6475
6576JSON Web Token defines some reserved claim names and defines how they should be
6677used. PyJWT supports these reserved claim names:
6778
6879 - "exp" (Expiration Time) Claim
6980
70- Expiration Time Claim
71- =====================
81+ ### Expiration Time Claim
7282
7383From [ draft 01 of the JWT spec] ( http://self-issued.info/docs/draft-jones-json-web-token-01.html#ReservedClaimName ) :
7484
@@ -83,18 +93,23 @@ From [draft 01 of the JWT spec](http://self-issued.info/docs/draft-jones-json-we
8393You can pass the expiration time as a UTC UNIX timestamp (an int) or as a
8494datetime, which will be converted into an int. For example:
8595
86- jwt.encode({"exp": 1371720939}, "secret")
96+ ``` python
97+ jwt.encode({' exp' : 1371720939 }, ' secret' )
8798
88- jwt.encode({"exp": datetime.utcnow()}, "secret")
99+ jwt.encode({' exp' : datetime.utcnow()}, ' secret' )
100+ ```
89101
90102Expiration time is automatically verified in ` jwt.decode() ` and raises
91103` jwt.ExpiredSignature ` if the expiration time is in the past:
92104
93- import jwt
94- try:
95- jwt.decode('JWT_STRING', "secret")
96- except jwt.ExpiredSignature:
97- # Signature has expired
105+ ``` python
106+ import jwt
107+
108+ try :
109+ jwt.decode(' JWT_STRING' , ' secret' )
110+ except jwt.ExpiredSignature:
111+ # Signature has expired
112+ ```
98113
99114Expiration time will be compared to the current UTC time (as given by
100115` timegm(datetime.utcnow().utctimetuple()) ` ), so be sure to use a UTC timestamp
@@ -108,15 +123,21 @@ For example, if you have a JWT payload with a expiration time set to 30 seconds
108123after creation but you know that sometimes you will process it after 30 seconds,
109124you can set a leeway of 10 seconds in order to have some margin:
110125
111- import jwt, time
112- jwt_payload = jwt.encode({'exp': datetime.utcnow() + datetime.timedelta(seconds=30)}, 'secret')
113- time.sleep(32)
114- # Jwt payload is now expired
115- # But with some leeway, it will still validate
116- jwt.decode(jwt_payload, 'secret', leeway=10)
126+ ``` python
127+ import time
128+ import jwt
129+
130+ jwt_payload = jwt.encode({
131+ ' exp' : datetime.utcnow() + datetime.timedelta(seconds = 30 )
132+ }, ' secret' )
133+
134+ time.sleep(32 )
117135
136+ # JWT payload is now expired
137+ # But with some leeway, it will still validate
138+ jwt.decode(jwt_payload, ' secret' , leeway = 10 )
139+ ```
118140
119- License
120- -------
141+ ## License
121142
122143MIT
0 commit comments