Skip to main content

Authentication

Paperbox provisions both an API Key (which can be limited to specific IPs) and a Private RSA Key (which is used to generate JSON Web Tokens). Every API consumer is responsible for securing these credentials and can be rotated upon request.

JWT generation examples

pip install PyJWT
from enum import Enum
import time
import jwt


class Environment(str, Enum):
ACCEPTANCE = "acc"
PRODUCTION = "prd"


def generate_jwt(
private_key_path="path/to/private-key.pem",
tenant_id: str = "tenant-id",
env: Environment = Environment.PRODUCTION,
expiry_length=3600,
) -> str:
"""Generates a signed JSON Web Token using a private key.

Args:
private_key_path (str): Path to the private key file
tenant_id (str): The tenant ID
env (Environment): The environment
expiry_length (int): The length of time in seconds before the token expires

Returns:
str: The signed JSON Web Token
"""
with open(private_key_path, "r") as file:
private_key = file.read()

now = int(time.time())

email = f"{tenant_id}@paperbox-{env}.iam.gserviceaccount.com"

# Build payload
payload = {
"iat": now,
"exp": now + expiry_length,
"iss": email,
"aud": f"https://integration.{env}.paperbox.ai",
"sub": email,
"email": email,
}

return jwt.encode(payload, private_key, algorithm="RS256")


if __name__ == "__main__":
print(generate_jwt("private_key.pem", "tenant_id", Environment.PRODUCTION))