Authorization via JWT

#!/usr/bin/env python3
"""
Option 1: Authorization via JWT.
  1. Log in via POST /user/login -> receive JWT
  2. Put JWT into the "token" header
  3. Send an image to POST /v1/kyc/process
"""
import requests
import json
import sys

# ===== CONFIGURATION =====
API_BASE = "https://api.enface.ai"
LOGIN_URL = f"{API_BASE}/v1/user/login"
PROCESS_URL = f"{API_BASE}/v1/kyc/process"

# Login credentials
LOGIN = "your_email@example.com"   # <-- insert your email
PASSWORD = "your_password"         # <-- insert your password

SCHEMA_ID = "your-schema-id"       # <-- insert schemaId from your dashboard
IMAGE_PATH = "path/to/your/image.jpg"  # <-- path to the image

TIMEOUT = (30, 120)

# --- Step 1: obtain JWT ---
print("Step 1: Logging in...")
resp = requests.post(
    LOGIN_URL,
    json={"email": LOGIN, "password": PASSWORD, "valid_days": 5},
    timeout=TIMEOUT,
)
print(f"  Login status: {resp.status_code}")

login_data = resp.json()
print(f"  Response: {json.dumps(login_data, ensure_ascii=False, indent=2)}")

if resp.status_code != 200:
    sys.exit(f"Login error: {resp.status_code}")

jwt = login_data.get("token") or login_data.get("jwt") or login_data.get("accessToken")
if not jwt:
    sys.exit(f"JWT not found in response. Keys: {list(login_data.keys())}")

print(f"  JWT received: {jwt[:20]}...")

# --- Step 2: send image with JWT in header ---
print(f"\nStep 2: Sending {IMAGE_PATH}...")
headers = {
    "Token": jwt,
}

data = {
    "schemaId": SCHEMA_ID,
    "mode": "sync",
}

with open(IMAGE_PATH, "rb") as f:
    files = [("images", (IMAGE_PATH.split("/")[-1], f, "image/jpeg"))]
    resp = requests.post(
        PROCESS_URL,
        headers=headers,
        data=data,
        files=files,
        timeout=TIMEOUT,
    )

print(f"Status: {resp.status_code}")
try:
    print(json.dumps(resp.json(), ensure_ascii=False, indent=2))
except Exception:
    print(resp.text)