Skip to Content
Python Microservices Development
book

Python Microservices Development

by Tarek Ziadé
July 2017
Beginner to intermediate
340 pages
7h 43m
English
Packt Publishing
Content preview from Python Microservices Development

PyJWT

In Python, the PyJWT (https://pyjwt.readthedocs.io/) library provides all the tools you need to generate and read back JWT tokens.

Once you've pip-installed pyjwt (and cryptography), you can use the encode() function and the decode() functions to create tokens.

In the following example, we're creating a JWT token using HMAC-SHA256 and reading it back. The signature is verified when the token is read, by providing the secret:

>>> import jwt 
 
>>> def create_token(alg='HS256', secret='secret', **data): 
...     return jwt.encode(data, secret, algorithm=alg) 
... 
>>> def read_token(token, secret='secret', algs=['HS256']): 
...     return jwt.decode(token, secret) 
... 
>>> token = create_token(some='data', inthe='token') 
>>> print(token) b'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpbnRoZSI6InRva2VuIiwic29tZSI6ImRhdGEifQ.oKmFaNV-C2wHb_WaMAfIGDqBPnOCyOzVf-JWvh-6bRQ' ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python Microservices Development - Second Edition

Python Microservices Development - Second Edition

Simon Fraser, Tarek Ziadé
Python Web Development with Sanic

Python Web Development with Sanic

Stephen Sadowski, Adam Hopkins

Publisher Resources

ISBN: 9781785881114Supplemental Content