liveBook · Manning

Public key

As explained in Secure APIs

A public key is the openly distributable half of an asymmetric cryptographic key pair used to verify signatures or perform public-key operations.

A public key is the openly distributable half of an asymmetric cryptographic key pair. The private key signs data, while the public key validates signatures without revealing the signing secret. section 8.2 section 8.3

Use public keys to keep verification separate from signing:

  • Generate a paired RSA private key and public key for asymmetric JWT signing. section 8.2
  • Keep the private key for token issuance and provide the public key to validators. section 8.2 section 8.3
  • Restrict accepted algorithms explicitly so attackers cannot turn public-key validation into an algorithm-confusion attack. section 8.3

Validating a PS256 JWT

Load public_key.pem with load_pem_public_key(), then pass the resulting key to PyJWT’s decode() with algorithms=["PS256"], the expected audience, and the expected issuer. Successful validation returns the payload; failure raises an exception. section 8.3

Listing 8.8 Validating tokens with a PS256 signature
# file: ch08/jwt_validator_pyjwt_ps256.py
from pathlib import Path
import jwt
from cryptography.hazmat.primitives.serialization \
    import load_pem_public_key

public_key = load_pem_public_key(     #1
    Path("public_key.pem").read_text().encode()
)


def validate_token(token):
    return jwt.decode(
        jwt=token,
        key=public_key,     #2
        algorithms=["PS256"],     #3
        audience="https://apithreats.com/api",
        issuer="https://auth.apithreats.com",
    )


token = <token>
print(validate_token(token))

The focused listing shows how a validator loads a PEM-encoded public key and uses it with an explicit PS256 algorithm. section 8.3

How Secure APIs covers it

How deep it goes
Explains key-pair roles and demonstrates PEM generation, loading, and PS256 validation, but defers broader key-management operations.
Examples
Demonstrates OpenSSL RSA generation, public_key.pem, and PyJWT validation with a PS256-signed token.
What it assumes
Assumes basic JWT use and Python familiarity while teaching asymmetric key roles and PEM key loading directly.
What you can do after
Operate PyJWT validation with a PEM public key, fixed algorithm, audience, and issuer.
How it connects
Connects asymmetric cryptography, digital signatures, private keys, JWT, PS256, OpenSSL, and PyJWT.

Also explained in

Compare how 8 books explain public key →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Secure APIs. The section links point to the parts of the book it draws on.