Authenticating the requests

The logic for authentication is encapsulated in the thoughts_backend/token_validation.py file. This contains both the generation and the validation of the header.

The following functions generate the Bearer token:

def encode_token(payload, private_key):    return jwt.encode(payload, private_key, algorithm='RS256')def generate_token_header(username, private_key):    '''    Generate a token header base on the username.     Sign using the private key.    '''    payload = {        'username': username,        'iat': datetime.utcnow(),        'exp': datetime.utcnow() + timedelta(days=2),    }    token = encode_token(payload, private_key)    token = token.decode('utf8')    return f'Bearer {token}'

This generates a JWT payload. It includes username to be used as a custom ...

Get Hands-On Docker for Microservices with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.